Skip to content

Commit

Permalink
fix rendering exception caused in group select (#997)
Browse files Browse the repository at this point in the history
* 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: 影和 <mocha.wyh@digital-engine.com>
  • Loading branch information
MarioJames and 影和 committed Dec 11, 2023
1 parent 026724e commit 930b6fc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
40 changes: 18 additions & 22 deletions docs/examples/optgroup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import React from 'react';
import Select, { Option, OptGroup } from 'rc-select';
import Select from 'rc-select';
import '../../assets/index.less';

function onChange(value, option) {
Expand All @@ -16,27 +16,23 @@ const Test = () => (
defaultValue="lucy"
style={{ width: 500 }}
onChange={onChange}
>
<OptGroup label="manager">
<Option value="jack" test-prop="jack-prop">
<b
style={{
color: 'red',
}}
>
jack
</b>
</Option>
<Option value="lucy" test-prop="lucy-prop">
lucy
</Option>
</OptGroup>
<OptGroup label="engineer">
<Option value="yiminghe" test-prop="yiminghe-prop">
yiminghe
</Option>
</OptGroup>
</Select>
options={[{
label: 'manager',
options: [
{ label: 'jack', value: 'jack' },
{ label: 'lucy', value: 'lucy' }
],
}, {
label: 'engineer',
options: [{ label: 'yiminghe', value: 'yiminghe' }]
}, {
label: 'bamboo',
options: undefined,
}, {
label: 'mocha',
options: null,
}]}
/>
</div>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export function flattenOptions<OptionType extends BaseOptionType = DefaultOption
} = fillFieldNames(fieldNames, false);

function dig(list: OptionType[], isGroupOption: boolean) {
if (!Array.isArray(list)) {
return;
}

list.forEach((data) => {
if (isGroupOption || !(fieldOptions in data)) {
const value = data[fieldValue];
Expand Down
30 changes: 30 additions & 0 deletions tests/Group.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,34 @@ describe('Select.Group', () => {
expect(wrapper.find('.rc-select-item-group').prop('title')).toBeUndefined();
});
});

it('group options exist undefined/null', () => {
const wrapper = mount(
<Select
open
options={[
{
label: 'zombiej',
options: [
{ label: 'jackson', value: 'jackson' },
],
},
{
label: 'bamboo',
options: undefined,
},
{
label: 'mocha',
options: null,
}
]}
/>
);

expect(wrapper.find('.rc-select-item-group').length).toEqual(3);
expect(wrapper.find('.rc-select-item-option').length).toEqual(1);

expect(wrapper.find('.rc-select-item').at(2).prop('title')).toEqual('bamboo');
expect(wrapper.find('.rc-select-item').at(3).prop('title')).toEqual('mocha');
})
});

1 comment on commit 930b6fc

@vercel
Copy link

@vercel vercel bot commented on 930b6fc Dec 11, 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-git-master-react-component.vercel.app
select.vercel.app
select-react-component.vercel.app

Please sign in to comment.