Skip to content

Commit 5f18a02

Browse files
authored
fix: null should show options (#506)
1 parent 095300c commit 5f18a02

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

src/generate.tsx

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,24 +454,33 @@ export default function generateSelector<
454454
}, [mergedSearchValue]);
455455

456456
// ============================ Selector ============================
457-
let displayValues = React.useMemo<DisplayLabelValueType[]>(
458-
() =>
459-
mergedRawValue.map((val: RawValueType) => {
460-
const valueOptions = getValueOption([val]);
461-
const displayValue = getLabeledValue(val, {
462-
options: valueOptions,
463-
prevValue: baseValue,
464-
labelInValue: mergedLabelInValue,
465-
optionLabelProp: mergedOptionLabelProp,
466-
});
467-
468-
return {
469-
...displayValue,
470-
disabled: isValueDisabled(val, valueOptions),
471-
};
472-
}),
473-
[baseValue, mergedOptions],
474-
);
457+
let displayValues = React.useMemo<DisplayLabelValueType[]>(() => {
458+
const tmpValues = mergedRawValue.map((val: RawValueType) => {
459+
const valueOptions = getValueOption([val]);
460+
const displayValue = getLabeledValue(val, {
461+
options: valueOptions,
462+
prevValue: baseValue,
463+
labelInValue: mergedLabelInValue,
464+
optionLabelProp: mergedOptionLabelProp,
465+
});
466+
467+
return {
468+
...displayValue,
469+
disabled: isValueDisabled(val, valueOptions),
470+
};
471+
});
472+
473+
if (
474+
!mode &&
475+
tmpValues.length === 1 &&
476+
tmpValues[0].value === null &&
477+
tmpValues[0].label === null
478+
) {
479+
return [];
480+
}
481+
482+
return tmpValues;
483+
}, [baseValue, mergedOptions, mode]);
475484

476485
// Polyfill with cache label
477486
displayValues = useCacheDisplayValue(displayValues);

tests/Select.test.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,11 +1593,25 @@ describe('Select.Basic', () => {
15931593
});
15941594
});
15951595

1596-
it('show placeholder when searchValue is controlled', () => {
1597-
const wrapper = mount(<Select searchValue="light" placeholder="bamboo" />);
1598-
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeTruthy();
1599-
toggleOpen(wrapper);
1600-
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
1596+
describe('show placeholder', () => {
1597+
it('when searchValue is controlled', () => {
1598+
const wrapper = mount(<Select searchValue="light" placeholder="bamboo" />);
1599+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeTruthy();
1600+
toggleOpen(wrapper);
1601+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
1602+
});
1603+
1604+
it('when value is null', () => {
1605+
const wrapper = mount(<Select value={null} placeholder="bamboo" />);
1606+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeTruthy();
1607+
});
1608+
1609+
it('not when value is null but it is an Option', () => {
1610+
const wrapper = mount(
1611+
<Select value={null} placeholder="bamboo" options={[{ value: null, label: 'light' }]} />,
1612+
);
1613+
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
1614+
});
16011615
});
16021616

16031617
it('Remove options can keep the cache', () => {

0 commit comments

Comments
 (0)