Skip to content

Commit

Permalink
fix: incorrect display when value is null in multiple mode (#1009)
Browse files Browse the repository at this point in the history
* fix: incorrect display when value is null in multiple mode

* test: optimize case
  • Loading branch information
madocto authored Dec 27, 2023
1 parent 5773bff commit 3fb972e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,16 @@ const Select = React.forwardRef(

// Merged value with LabelValueType
const rawLabeledValues = React.useMemo(() => {
const values = convert2LabelValues(internalValue);
const newInternalValue = multiple && internalValue === null ? [] : internalValue;
const values = convert2LabelValues(newInternalValue);

// combobox no need save value when it's no value (exclude value equal 0)
if (mode === 'combobox' && isComboNoValue(values[0]?.value)) {
return [];
}

return values;
}, [internalValue, convert2LabelValues, mode]);
}, [internalValue, convert2LabelValues, mode, multiple]);

// Fill label with cache to avoid option remove
const [mergedValues, getMixedOption] = useCache(rawLabeledValues, valueOptions);
Expand Down
18 changes: 18 additions & 0 deletions tests/Multiple.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,24 @@ describe('Select.Multiple', () => {
expect(wrapper.find('Selector').props().values.length).toEqual(0);
});

it('display correctly when value is undefined or null', () => {
const wrapper1 = mount(
<Select mode="multiple" value={undefined}>
<Option value={1}>1</Option>
<Option value={2}>2</Option>
</Select>,
);
const wrapper2 = mount(
<Select mode="multiple" value={null}>
<Option value={1}>1</Option>
<Option value={2}>2</Option>
</Select>,
);

expect(wrapper1.find('.rc-select-selection-item').length).toBe(0);
expect(wrapper2.find('.rc-select-selection-item').length).toBe(0);
});

describe('optionLabelProp', () => {
it('basic', () => {
const wrapper = mount(
Expand Down

1 comment on commit 3fb972e

@vercel
Copy link

@vercel vercel bot commented on 3fb972e Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

select – ./

select-react-component.vercel.app
select.vercel.app
select-git-master-react-component.vercel.app

Please sign in to comment.