Skip to content
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
6 changes: 5 additions & 1 deletion src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, 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);
Expand Down
48 changes: 48 additions & 0 deletions tests/OptionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefOptionListProps>();

// 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 = {
Expand Down