Skip to content

Commit 994083b

Browse files
committed
fix: scroll selected option into view when select + filter
1 parent 8568662 commit 994083b

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/select/__tests__/use-select.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,21 @@ describe('useSelect', () => {
449449
});
450450
});
451451

452-
test('select with filter should open and NOT navigate to selected option', () => {
452+
test('select with filter should open and navigate to selected option', () => {
453453
const hook = renderHook(useSelect, {
454-
initialProps: { ...initialProps, selectedOptions: [{ value: 'child1' }] },
454+
initialProps: { ...initialProps, selectedOptions: [{ value: 'child1' }], filteringValue: '' },
455455
});
456456
const { getTriggerProps } = hook.result.current;
457457
const triggerProps = getTriggerProps();
458458
act(() => triggerProps.onKeyDown && triggerProps.onKeyDown(createTestEvent(KeyCode.space)));
459459
expect(hook.result.current.isOpen).toBe(true);
460-
expect(hook.result.current.highlightedOption).toBeFalsy();
460+
expect(hook.result.current.highlightedOption).toEqual({
461+
type: 'child',
462+
option: {
463+
label: 'Child 1',
464+
value: 'child1',
465+
},
466+
});
461467
});
462468

463469
describe('calculates if the highlighted option is selected', () => {

src/select/utils/use-select.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ export function useSelect({
305305

306306
const prevOpen = usePrevious<boolean>(isOpen);
307307
useEffect(() => {
308-
// highlight the first selected option, when opening the Select component without filter input
308+
// highlight the first selected option and scroll into view
309309
// keep the focus in the filter input when opening, so that screenreader can recognize the combobox
310-
if (isOpen && !prevOpen && options.length > 0 && !hasFilter) {
310+
if (isOpen && !prevOpen && options.length > 0) {
311311
if (openedWithKeyboard) {
312312
if (__selectedOptions[0]) {
313313
highlightOptionWithKeyboard(__selectedOptions[0]);
@@ -334,7 +334,6 @@ export function useSelect({
334334
openedWithKeyboard,
335335
options,
336336
prevOpen,
337-
hasFilter,
338337
]);
339338

340339
useEffect(() => {

0 commit comments

Comments
 (0)