Skip to content

fix: tab to select an active option #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
break;
}

// >>> Select
// >>> Select (Tab / Enter)
case KeyCode.TAB:
case KeyCode.ENTER: {
// value
const item = memoFlattenOptions[activeIndex];
Expand Down
40 changes: 40 additions & 0 deletions tests/OptionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,46 @@ describe('OptionList', () => {
expect(onSelect).toHaveBeenCalledWith('1', expect.objectContaining({ selected: true }));
});

it('should tab key select an active option', () => {
const onActiveValue = jest.fn();
const onSelect = jest.fn();
const toggleOpen = jest.fn();
const listRef = React.createRef<RefOptionListProps>();

render(
generateList({
options: [{ value: '1' }, { value: '2' }],
onActiveValue,
onSelect,
toggleOpen,
ref: listRef,
}),
);

act(() => {
toggleOpen(true);
});

act(() => {
listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
});

expect(onActiveValue).toHaveBeenCalledWith(
'2',
expect.anything(),
expect.objectContaining({ source: 'keyboard' }),
);

act(() => {
listRef.current.onKeyDown({ which: KeyCode.TAB } as any);
});

expect(onSelect).toHaveBeenCalledTimes(1);
expect(onSelect).toHaveBeenCalledWith('2', expect.objectContaining({ selected: true }));

expect(toggleOpen).toHaveBeenCalledWith(false);
});

// mocked how we detect running platform in test environment
it('special key operation on Mac', () => {
const onActiveValue = jest.fn();
Expand Down
Loading