Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
}
return rawValues.has(value);
},
[mode, [...rawValues].toString(), rawValues.size],
[mode, rawValues],
);

// ========================== Active ==========================
Expand Down Expand Up @@ -141,7 +141,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
}
return rawValues.has(value);
},
[mode, searchValue, [...rawValues].toString(), rawValues.size],
[mode, searchValue, rawValues],
);

// Auto scroll to item position in single mode
Expand Down
58 changes: 58 additions & 0 deletions tests/OptionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,62 @@ describe('OptionList', () => {
});
});
});

it('should update selected state when value changes between number and string with same string form', () => {
const options = [
{ key: 'num-1', value: 1, label: 'Number 1' },
{ key: 'str-1', value: '1', label: 'String 1' },
];

const { container, rerender } = render(
<Select open virtual={false} options={options} value={1} />,
);

let items = container.querySelectorAll('.rc-select-item-option');
expect(items[0]).toHaveClass('rc-select-item-option-selected');
expect(items[0]).toHaveAttribute('aria-selected', 'true');
expect(items[1]).not.toHaveClass('rc-select-item-option-selected');
expect(items[1]).toHaveAttribute('aria-selected', 'false');

rerender(<Select open virtual={false} options={options} value="1" />);

items = container.querySelectorAll('.rc-select-item-option');
expect(items[0]).not.toHaveClass('rc-select-item-option-selected');
expect(items[0]).toHaveAttribute('aria-selected', 'false');
expect(items[1]).toHaveClass('rc-select-item-option-selected');
expect(items[1]).toHaveAttribute('aria-selected', 'true');
});

it('should update selected state when multiple values have the same comma-joined string', () => {
const options = [
{ value: 'a,b', label: 'a,b' },
{ value: 'c', label: 'c' },
{ value: 'a', label: 'a' },
{ value: 'b,c', label: 'b,c' },
];

const { container, rerender } = render(
<Select open mode="multiple" virtual={false} options={options} value={['a,b', 'c']} />,
);

let items = container.querySelectorAll('.rc-select-item-option');
expect(items[0]).toHaveClass('rc-select-item-option-selected');
expect(items[1]).toHaveClass('rc-select-item-option-selected');
expect(items[2]).not.toHaveClass('rc-select-item-option-selected');
expect(items[3]).not.toHaveClass('rc-select-item-option-selected');

rerender(
<Select open mode="multiple" virtual={false} options={options} value={['a', 'b,c']} />,
);

items = container.querySelectorAll('.rc-select-item-option');
expect(items[0]).not.toHaveClass('rc-select-item-option-selected');
expect(items[0]).toHaveAttribute('aria-selected', 'false');
expect(items[1]).not.toHaveClass('rc-select-item-option-selected');
expect(items[1]).toHaveAttribute('aria-selected', 'false');
expect(items[2]).toHaveClass('rc-select-item-option-selected');
expect(items[2]).toHaveAttribute('aria-selected', 'true');
expect(items[3]).toHaveClass('rc-select-item-option-selected');
expect(items[3]).toHaveAttribute('aria-selected', 'true');
});
});
Loading