Skip to content

Commit f7ed85e

Browse files
authored
Fix selecting an option with the keyboard while using fieldNames (#713)
* Fix selecting an option with the keyboard while using fieldNames * Missed one * Test cases for fieldNames and key operations
1 parent ad8214c commit f7ed85e

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

src/OptionList.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
3131
_,
3232
ref,
3333
) => {
34-
const { prefixCls, id, open, multiple, searchValue, toggleOpen, notFoundContent, onPopupScroll } =
35-
useBaseProps();
34+
const {
35+
prefixCls,
36+
id,
37+
open,
38+
multiple,
39+
searchValue,
40+
toggleOpen,
41+
notFoundContent,
42+
onPopupScroll,
43+
} = useBaseProps();
3644
const {
3745
flattenOptions,
3846
onActiveValue,
@@ -95,8 +103,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
95103
onActiveValue(null, -1, info);
96104
return;
97105
}
98-
99-
onActiveValue(flattenItem.data.value, index, info);
106+
onActiveValue(flattenItem.value, index, info);
100107
};
101108

102109
// Auto active first item when list length or searchValue changed
@@ -180,7 +187,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
180187
// value
181188
const item = memoFlattenOptions[activeIndex];
182189
if (item && !item.data.disabled) {
183-
onSelectValue(item.data.value);
190+
onSelectValue(item.value);
184191
} else {
185192
onSelectValue(undefined);
186193
}

tests/OptionList.test.tsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('OptionList', () => {
2424
});
2525

2626
function generateList({ options, values, ref, ...props }: any) {
27-
const fieldNames = fillFieldNames({}, false);
27+
const fieldNames = fillFieldNames(props.fieldNames || {}, false);
2828
const flattenedOptions = flattenOptions(options, {
2929
fieldNames,
3030
childrenAsData: false,
@@ -121,6 +121,56 @@ describe('OptionList', () => {
121121
);
122122
});
123123

124+
it('key operation with fieldNames', () => {
125+
const onActiveValue = jest.fn();
126+
const toggleOpen = jest.fn();
127+
const onSelect = jest.fn();
128+
const listRef = React.createRef<RefOptionListProps>();
129+
mount(
130+
generateList({
131+
fieldNames: { value: 'foo', label: 'bar' },
132+
options: [{ foo: '1' }, { foo: '2' }],
133+
onActiveValue,
134+
onSelect,
135+
toggleOpen,
136+
ref: listRef,
137+
}),
138+
);
139+
140+
onActiveValue.mockReset();
141+
act(() => {
142+
listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
143+
});
144+
expect(onActiveValue).toHaveBeenCalledWith(
145+
'2',
146+
expect.anything(),
147+
expect.objectContaining({ source: 'keyboard' }),
148+
);
149+
150+
act(() => {
151+
listRef.current.onKeyDown({ which: KeyCode.ENTER } as any);
152+
});
153+
expect(onSelect).toHaveBeenCalledTimes(1);
154+
expect(onSelect).toHaveBeenCalledWith('2', expect.objectContaining({ selected: true }));
155+
156+
onSelect.mockReset();
157+
onActiveValue.mockReset();
158+
act(() => {
159+
listRef.current.onKeyDown({ which: KeyCode.UP } as any);
160+
});
161+
expect(onActiveValue).toHaveBeenCalledWith(
162+
'1',
163+
expect.anything(),
164+
expect.objectContaining({ source: 'keyboard' }),
165+
);
166+
167+
act(() => {
168+
listRef.current.onKeyDown({ which: KeyCode.ENTER } as any);
169+
});
170+
expect(onSelect).toHaveBeenCalledTimes(1);
171+
expect(onSelect).toHaveBeenCalledWith('1', expect.objectContaining({ selected: true }));
172+
});
173+
124174
// mocked how we detect running platform in test environment
125175
it('special key operation on Mac', () => {
126176
const onActiveValue = jest.fn();

0 commit comments

Comments
 (0)