From d1caac57efef2d3d00a0e0dc9eb64f5d6af50a98 Mon Sep 17 00:00:00 2001 From: akmjenkins Date: Mon, 24 Jan 2022 21:27:19 -0400 Subject: [PATCH 1/3] Fix selecting an option with the keyboard while using fieldNames --- src/OptionList.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 03a9968e1..6734a6e9d 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -31,8 +31,16 @@ const OptionList: React.ForwardRefRenderFunction { - const { prefixCls, id, open, multiple, searchValue, toggleOpen, notFoundContent, onPopupScroll } = - useBaseProps(); + const { + prefixCls, + id, + open, + multiple, + searchValue, + toggleOpen, + notFoundContent, + onPopupScroll, + } = useBaseProps(); const { flattenOptions, onActiveValue, @@ -180,7 +188,7 @@ const OptionList: React.ForwardRefRenderFunction Date: Tue, 25 Jan 2022 00:37:58 -0400 Subject: [PATCH 2/3] Missed one --- src/OptionList.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 6734a6e9d..4162743bd 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -103,8 +103,7 @@ const OptionList: React.ForwardRefRenderFunction Date: Tue, 25 Jan 2022 00:38:48 -0400 Subject: [PATCH 3/3] Test cases for fieldNames and key operations --- tests/OptionList.test.tsx | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/OptionList.test.tsx b/tests/OptionList.test.tsx index 0ee5742b3..5abbcd699 100644 --- a/tests/OptionList.test.tsx +++ b/tests/OptionList.test.tsx @@ -24,7 +24,7 @@ describe('OptionList', () => { }); function generateList({ options, values, ref, ...props }: any) { - const fieldNames = fillFieldNames({}, false); + const fieldNames = fillFieldNames(props.fieldNames || {}, false); const flattenedOptions = flattenOptions(options, { fieldNames, childrenAsData: false, @@ -121,6 +121,56 @@ describe('OptionList', () => { ); }); + it('key operation with fieldNames', () => { + const onActiveValue = jest.fn(); + const toggleOpen = jest.fn(); + const onSelect = jest.fn(); + const listRef = React.createRef(); + mount( + generateList({ + fieldNames: { value: 'foo', label: 'bar' }, + options: [{ foo: '1' }, { foo: '2' }], + onActiveValue, + onSelect, + toggleOpen, + ref: listRef, + }), + ); + + onActiveValue.mockReset(); + 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.ENTER } as any); + }); + expect(onSelect).toHaveBeenCalledTimes(1); + expect(onSelect).toHaveBeenCalledWith('2', expect.objectContaining({ selected: true })); + + onSelect.mockReset(); + onActiveValue.mockReset(); + act(() => { + listRef.current.onKeyDown({ which: KeyCode.UP } as any); + }); + expect(onActiveValue).toHaveBeenCalledWith( + '1', + expect.anything(), + expect.objectContaining({ source: 'keyboard' }), + ); + + act(() => { + listRef.current.onKeyDown({ which: KeyCode.ENTER } as any); + }); + expect(onSelect).toHaveBeenCalledTimes(1); + expect(onSelect).toHaveBeenCalledWith('1', expect.objectContaining({ selected: true })); + }); + // mocked how we detect running platform in test environment it('special key operation on Mac', () => { const onActiveValue = jest.fn();