Skip to content

Commit

Permalink
fix(Select): keyboard behavior on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
iapolya committed Jan 31, 2025
1 parent 4aec513 commit 73c056a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function

if (multiple) {
const activeItemIndex = listRef?.current?.getActiveItem();
filterRef.current?.focus();

if (!mobile) {
filterRef.current?.focus();
}

if (typeof activeItemIndex === 'number') {
// prevent item deactivation in case of multiple selection
Expand All @@ -178,7 +181,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function

handleSelection(option);
},
[handleSelection, multiple],
[handleSelection, mobile, multiple],
);

const handleControlKeyDown = React.useCallback(
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useFocusWithin/useFocusWithin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as React from 'react';

import {useMobile} from '../../components/mobile';

import {SyntheticFocusEvent} from './SyntheticFocusEvent';
import {useSyntheticBlurEvent} from './useSyntheticBlurEvent';

Expand Down Expand Up @@ -144,6 +146,8 @@ function useFocusEvents({
const capturedRef = React.useRef(false);
const targetRef = React.useRef<EventTarget | null>(null);

const mobile = useMobile();

React.useEffect(() => {
if (isDisabled) {
return undefined;
Expand Down Expand Up @@ -186,15 +190,15 @@ function useFocusEvents({
(event: React.FocusEvent) => {
if (
document.activeElement !== event.target &&
(event.relatedTarget === null ||
((!mobile && event.relatedTarget === null) ||
event.relatedTarget === document.body ||
event.relatedTarget === (document as EventTarget))
) {
onBlur(event);
targetRef.current = null;
}
},
[onBlur],
[isMobile, onBlur],
);

const onSyntheticFocus = useSyntheticBlurEvent(onBlur);
Expand Down

0 comments on commit 73c056a

Please sign in to comment.