Skip to content

Commit f6cbf9a

Browse files
committed
chore: [tabSelectsValue]逻辑完善,补充测试用例
1 parent 43b3b63 commit f6cbf9a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/OptionList.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
233233

234234
// >>> Select (Tab / Enter)
235235
case KeyCode.TAB: {
236-
if (tabSelectsValue) selectOptionHotKeyLogic(event);
236+
if (tabSelectsValue) {
237+
selectOptionHotKeyLogic(event);
238+
} else {
239+
toggleOpen(false);
240+
}
237241
break;
238242
}
239243
case KeyCode.ENTER: {

tests/OptionList.test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('OptionList', () => {
6464
options,
6565
onActiveValue: () => {},
6666
onSelect: () => {},
67+
tabSelectsValue: true,
6768
rawValues: values || new Set(),
6869
virtual: true,
6970
...props,
@@ -244,6 +245,47 @@ describe('OptionList', () => {
244245
expect(toggleOpen).toHaveBeenCalledWith(false);
245246
});
246247

248+
it('should not select active option when tab key pressed with tabSelectsValue false', () => {
249+
const onActiveValue = jest.fn();
250+
const onSelect = jest.fn();
251+
const toggleOpen = jest.fn();
252+
const listRef = React.createRef<RefOptionListProps>();
253+
254+
render(
255+
generateList({
256+
options: [{ value: '1' }, { value: '2' }],
257+
onActiveValue,
258+
onSelect,
259+
toggleOpen,
260+
ref: listRef,
261+
tabSelectsValue: false, // 关闭 tab 选中功能
262+
}),
263+
);
264+
265+
act(() => {
266+
toggleOpen(true);
267+
});
268+
269+
act(() => {
270+
listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
271+
});
272+
273+
expect(onActiveValue).toHaveBeenCalledWith(
274+
'2',
275+
expect.anything(),
276+
expect.objectContaining({ source: 'keyboard' }),
277+
);
278+
279+
act(() => {
280+
listRef.current.onKeyDown({ which: KeyCode.TAB } as any);
281+
});
282+
283+
// 断言选择未被触发
284+
expect(onSelect).toHaveBeenCalledTimes(0);
285+
// 断言弹窗状态关闭
286+
expect(toggleOpen).toHaveBeenCalledWith(false);
287+
});
288+
247289
// mocked how we detect running platform in test environment
248290
it('special key operation on Mac', () => {
249291
const onActiveValue = jest.fn();

0 commit comments

Comments
 (0)