Skip to content

Commit 33a598f

Browse files
authored
fix: tab to select an active option (#1078)
1 parent ca86130 commit 33a598f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/OptionList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
215215
break;
216216
}
217217

218-
// >>> Select
218+
// >>> Select (Tab / Enter)
219+
case KeyCode.TAB:
219220
case KeyCode.ENTER: {
220221
// value
221222
const item = memoFlattenOptions[activeIndex];

tests/OptionList.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,46 @@ describe('OptionList', () => {
202202
expect(onSelect).toHaveBeenCalledWith('1', expect.objectContaining({ selected: true }));
203203
});
204204

205+
it('should tab key select an active option', () => {
206+
const onActiveValue = jest.fn();
207+
const onSelect = jest.fn();
208+
const toggleOpen = jest.fn();
209+
const listRef = React.createRef<RefOptionListProps>();
210+
211+
render(
212+
generateList({
213+
options: [{ value: '1' }, { value: '2' }],
214+
onActiveValue,
215+
onSelect,
216+
toggleOpen,
217+
ref: listRef,
218+
}),
219+
);
220+
221+
act(() => {
222+
toggleOpen(true);
223+
});
224+
225+
act(() => {
226+
listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
227+
});
228+
229+
expect(onActiveValue).toHaveBeenCalledWith(
230+
'2',
231+
expect.anything(),
232+
expect.objectContaining({ source: 'keyboard' }),
233+
);
234+
235+
act(() => {
236+
listRef.current.onKeyDown({ which: KeyCode.TAB } as any);
237+
});
238+
239+
expect(onSelect).toHaveBeenCalledTimes(1);
240+
expect(onSelect).toHaveBeenCalledWith('2', expect.objectContaining({ selected: true }));
241+
242+
expect(toggleOpen).toHaveBeenCalledWith(false);
243+
});
244+
205245
// mocked how we detect running platform in test environment
206246
it('special key operation on Mac', () => {
207247
const onActiveValue = jest.fn();

0 commit comments

Comments
 (0)