Skip to content

Commit 52ff2a4

Browse files
(Picklist): add Tab / Shift + Tab support
1 parent 610776d commit 52ff2a4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/scripts/Picklist.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,40 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
371371
setFocusedValue(prevValue);
372372
scrollFocusedElementIntoView(prevValue);
373373
}
374+
} else if (e.keyCode === 9) {
375+
// Tab or Shift+Tab
376+
if (opened) {
377+
e.preventDefault();
378+
e.stopPropagation();
379+
const optionValues = getOptionValues();
380+
const currentIndex = focusedValue
381+
? optionValues.indexOf(focusedValue)
382+
: -1;
383+
384+
if (e.shiftKey) {
385+
// Shift+Tab - Navigate to previous option or close if at first
386+
if (currentIndex <= 0) {
387+
// At first option or no focus, close the picklist
388+
setOpened(false);
389+
onComplete?.();
390+
} else {
391+
const prevValue = getPrevValue(focusedValue);
392+
setFocusedValue(prevValue);
393+
scrollFocusedElementIntoView(prevValue);
394+
}
395+
} else {
396+
// Tab - Navigate to next option or close if at last
397+
if (currentIndex >= optionValues.length - 1) {
398+
// At last option, close the picklist
399+
setOpened(false);
400+
onComplete?.();
401+
} else {
402+
const nextValue = getNextValue(focusedValue);
403+
setFocusedValue(nextValue);
404+
scrollFocusedElementIntoView(nextValue);
405+
}
406+
}
407+
}
374408
} else if (e.keyCode === 27) {
375409
// ESC
376410
e.preventDefault();

0 commit comments

Comments
 (0)