From 1d99aa5608dedc3fe0fc7dcf4e4ee66bbdedebc2 Mon Sep 17 00:00:00 2001 From: liuqiang Date: Thu, 8 May 2025 16:57:36 +0800 Subject: [PATCH 1/2] fix:Select with maxCount limit does not allow removal of selected options --- src/OptionList.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index fdcf12ff6..36dd1f216 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -226,7 +226,11 @@ const OptionList: React.ForwardRefRenderFunction = (_, r case KeyCode.ENTER: { // value const item = memoFlattenOptions[activeIndex]; - if (item && !item?.data?.disabled && !overMaxCount) { + if (!item || item.data.disabled) { + return onSelectValue(undefined); + } + + if (!overMaxCount || rawValues.has(item.value)) { onSelectValue(item.value); } else { onSelectValue(undefined); From 7da39494de3b9a2fdf605326182e31b0c03b596f Mon Sep 17 00:00:00 2001 From: liuqiang Date: Wed, 14 May 2025 19:13:24 +0800 Subject: [PATCH 2/2] update --- tests/OptionList.test.tsx | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/OptionList.test.tsx b/tests/OptionList.test.tsx index c148fd191..b73b370d0 100644 --- a/tests/OptionList.test.tsx +++ b/tests/OptionList.test.tsx @@ -462,6 +462,54 @@ describe('OptionList', () => { expect(onActiveValue).not.toHaveBeenCalledWith('3', expect.anything(), expect.anything()); }); + it('should deselect a selected option when Enter is pressed and maxCount is reached', () => { + const onSelect = jest.fn(); + const toggleOpen = jest.fn(); + const listRef = React.createRef(); + + // Initial selected values: '1' and '2' + const initialValues = new Set(['1', '2']); + + render( + generateList({ + multiple: true, + maxCount: 2, + options: [ + { value: '1', label: '1' }, + { value: '2', label: '2' }, + { value: '3', label: '3' }, + ], + values: initialValues, + onSelect, + toggleOpen, + ref: listRef, + }), + ); + + // Verify initial selection state + expect(initialValues.has('1')).toBe(true); // 1 is selected + expect(initialValues.has('2')).toBe(true); // 2 is selected + expect(initialValues.has('3')).toBe(false); // 3 is not selected + + act(() => { + toggleOpen(true); + }); + + act(() => { + listRef.current.onKeyDown({ which: KeyCode.ENTER } as any); + }); + + // Verify that onSelect was called to deselect '1' + expect(onSelect).toHaveBeenCalledWith('1', expect.objectContaining({ selected: false })); + + // Verify that onSelect was NOT called for '2' or '3' + expect(onSelect).not.toHaveBeenCalledWith('2', expect.anything()); + expect(onSelect).not.toHaveBeenCalledWith('3', expect.anything()); + + // Verify only one call was made (for deselecting '1') + expect(onSelect).toHaveBeenCalledTimes(1); + }); + describe('List.ScrollBar', () => { let mockElement; let boundingRect = {