Skip to content

Commit

Permalink
fix(Select): fix focus state on control click in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
korvin89 committed Nov 16, 2023
1 parent e76fd7c commit e804e29
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -85,6 +85,19 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
error: Boolean(error),
};

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 @@ -118,7 +131,7 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
{
onKeyDown,
onClear: clearValue,
onClick: toggleOpen,
onClick: handleControlClick,
renderClear: (arg) => renderClearIcon(arg),
ref,
open: Boolean(open),
Expand Down Expand Up @@ -147,7 +160,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
3 changes: 2 additions & 1 deletion src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type SelectRenderClearArgs = {

export type SelectRenderControlProps = {
onClear: () => void;
onClick: () => void;
// FIXME: change "e" property to required in the next major
onClick: (e?: React.MouseEvent<HTMLElement>) => void;
onKeyDown: (e: React.KeyboardEvent<HTMLElement>) => void;
renderClear?: (args: SelectRenderClearArgs) => React.ReactNode;
ref: React.Ref<HTMLElement>;
Expand Down

0 comments on commit e804e29

Please sign in to comment.