From 327d3d745e5ba1af864b33ecfe13bbcc7ecfaddf Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Wed, 2 Dec 2026 09:20:35 +0530 Subject: [PATCH] fix: depend on rawValues directly in OptionList selection callbacks --- src/OptionList.tsx | 4 +-- tests/OptionList.test.tsx | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index a151c8658..c2af90009 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -93,7 +93,7 @@ const OptionList: React.ForwardRefRenderFunction = (_, r } return rawValues.has(value); }, - [mode, [...rawValues].toString(), rawValues.size], + [mode, rawValues], ); // ========================== Active ========================== @@ -141,7 +141,7 @@ const OptionList: React.ForwardRefRenderFunction = (_, r } return rawValues.has(value); }, - [mode, searchValue, [...rawValues].toString(), rawValues.size], + [mode, searchValue, rawValues], ); // Auto scroll to item position in single mode diff --git a/tests/OptionList.test.tsx b/tests/OptionList.test.tsx index 9cbda0425..2beb12508 100644 --- a/tests/OptionList.test.tsx +++ b/tests/OptionList.test.tsx @@ -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( + ); + + 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( + , + ); + + 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'); + }); });