Skip to content

Commit 930b6fc

Browse files
MarioJames影和
andauthored
fix rendering exception caused in group select (#997)
* fix: fix rendering exception caused by the group options data being null/undefined. * fix: fix rendering exception caused by the group options data being null/undefined. * feat: pr comment fix --------- Co-authored-by: 影和 <[email protected]>
1 parent 026724e commit 930b6fc

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

docs/examples/optgroup.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22
import React from 'react';
3-
import Select, { Option, OptGroup } from 'rc-select';
3+
import Select from 'rc-select';
44
import '../../assets/index.less';
55

66
function onChange(value, option) {
@@ -16,27 +16,23 @@ const Test = () => (
1616
defaultValue="lucy"
1717
style={{ width: 500 }}
1818
onChange={onChange}
19-
>
20-
<OptGroup label="manager">
21-
<Option value="jack" test-prop="jack-prop">
22-
<b
23-
style={{
24-
color: 'red',
25-
}}
26-
>
27-
jack
28-
</b>
29-
</Option>
30-
<Option value="lucy" test-prop="lucy-prop">
31-
lucy
32-
</Option>
33-
</OptGroup>
34-
<OptGroup label="engineer">
35-
<Option value="yiminghe" test-prop="yiminghe-prop">
36-
yiminghe
37-
</Option>
38-
</OptGroup>
39-
</Select>
19+
options={[{
20+
label: 'manager',
21+
options: [
22+
{ label: 'jack', value: 'jack' },
23+
{ label: 'lucy', value: 'lucy' }
24+
],
25+
}, {
26+
label: 'engineer',
27+
options: [{ label: 'yiminghe', value: 'yiminghe' }]
28+
}, {
29+
label: 'bamboo',
30+
options: undefined,
31+
}, {
32+
label: 'mocha',
33+
options: null,
34+
}]}
35+
/>
4036
</div>
4137
</div>
4238
);

src/utils/valueUtil.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export function flattenOptions<OptionType extends BaseOptionType = DefaultOption
5151
} = fillFieldNames(fieldNames, false);
5252

5353
function dig(list: OptionType[], isGroupOption: boolean) {
54+
if (!Array.isArray(list)) {
55+
return;
56+
}
57+
5458
list.forEach((data) => {
5559
if (isGroupOption || !(fieldOptions in data)) {
5660
const value = data[fieldValue];

tests/Group.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,34 @@ describe('Select.Group', () => {
6666
expect(wrapper.find('.rc-select-item-group').prop('title')).toBeUndefined();
6767
});
6868
});
69+
70+
it('group options exist undefined/null', () => {
71+
const wrapper = mount(
72+
<Select
73+
open
74+
options={[
75+
{
76+
label: 'zombiej',
77+
options: [
78+
{ label: 'jackson', value: 'jackson' },
79+
],
80+
},
81+
{
82+
label: 'bamboo',
83+
options: undefined,
84+
},
85+
{
86+
label: 'mocha',
87+
options: null,
88+
}
89+
]}
90+
/>
91+
);
92+
93+
expect(wrapper.find('.rc-select-item-group').length).toEqual(3);
94+
expect(wrapper.find('.rc-select-item-option').length).toEqual(1);
95+
96+
expect(wrapper.find('.rc-select-item').at(2).prop('title')).toEqual('bamboo');
97+
expect(wrapper.find('.rc-select-item').at(3).prop('title')).toEqual('mocha');
98+
})
6999
});

0 commit comments

Comments
 (0)