Skip to content

Commit 85372b2

Browse files
authored
fix: 兼容 labelInValue 时 value 为 null 引起的报错 (#590)
* fix: 兼容 labelInValue 时 value 为 null 引起的报错 * fix: 兼容 labelInValue 场景下 value 为 null 引起的报错 * test: not crash when labelInValue and value is null
1 parent 45856a8 commit 85372b2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/utils/commonUtil.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ export function toInnerValue(
2222
): [RawValueType[], Map<RawValueType, LabelValueType>] {
2323
const valueMap = new Map<RawValueType, LabelValueType>();
2424

25-
if (value === undefined || (value === '' && combobox)) {
25+
if (
26+
value === undefined ||
27+
(value === '' && combobox)
28+
) {
2629
return [[], valueMap];
2730
}
2831

2932
const values = Array.isArray(value) ? value : [value];
3033
let rawValues = values as RawValueType[];
3134

3235
if (labelInValue) {
33-
rawValues = (values as LabelValueType[]).map((itemValue: LabelValueType) => {
34-
const { key, value: val } = itemValue;
35-
const finalVal = val !== undefined ? val : key;
36-
valueMap.set(finalVal, itemValue);
37-
return finalVal;
38-
});
36+
rawValues = (values as LabelValueType[])
37+
.filter(item => !!item)
38+
.map((itemValue: LabelValueType) => {
39+
const { key, value: val } = itemValue;
40+
const finalVal = val !== undefined ? val : key;
41+
valueMap.set(finalVal, itemValue);
42+
return finalVal;
43+
});
3944
}
4045

4146
return [rawValues, valueMap];

tests/Select.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,10 @@ describe('Select.Basic', () => {
14501450
mount(<Select options={null} />);
14511451
});
14521452

1453+
it('not crash when labelInValue and value is null', () => {
1454+
mount(<Select labelInValue value={null} />);
1455+
});
1456+
14531457
it('not open when `notFoundCount` is empty & no data', () => {
14541458
const wrapper = mount(<Select options={null} notFoundContent={null} open showSearch />);
14551459
expect(wrapper.find('SelectTrigger').props().visible).toBeFalsy();

0 commit comments

Comments
 (0)