Skip to content

Commit

Permalink
fix(Select): fix focus state on control click in Safari (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
korvin89 authored May 7, 2024
1 parent c7b0888 commit 80730cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/components/Select/components/SelectControl/SelectControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
error: isErrorVisible,
};

const handleControlClick = React.useCallback(
(e?: React.MouseEvent<HTMLElement>) => {
// Fix for Safari
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#clicking_and_focus
if (e && e.currentTarget !== document.activeElement && 'focus' in e.currentTarget) {
(e.currentTarget as HTMLButtonElement).focus();
}

toggleOpen();
},
[toggleOpen],
);

const disableButtonAnimation = React.useCallback(() => {
setIsDisabledButtonAnimation(true);
}, []);
Expand Down Expand Up @@ -143,7 +156,7 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
{
onKeyDown,
onClear: clearValue,
onClick: toggleOpen,
onClick: handleControlClick,
renderClear: (arg) => renderClearIcon(arg),
renderCounter: renderCounterComponent,
ref,
Expand Down Expand Up @@ -173,7 +186,7 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
}
name={name}
disabled={disabled}
onClick={toggleOpen}
onClick={handleControlClick}
onKeyDown={onKeyDown}
type="button"
data-qa={qa}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type SelectRenderClearArgs = {

export type SelectRenderControlProps = {
onClear: () => void;
onClick: () => void;
onClick: (e: React.MouseEvent<HTMLElement>) => void;
onKeyDown?: (e: React.KeyboardEvent<HTMLElement>) => void;
renderClear?: (args: SelectRenderClearArgs) => React.ReactNode;
renderCounter?: () => React.ReactNode;
Expand Down

0 comments on commit 80730cc

Please sign in to comment.