Skip to content

Commit

Permalink
fix: render correct label with fieldNames(#49631) (#538)
Browse files Browse the repository at this point in the history
* fix:  render correct label with `fieldNames`(#49631)

* fix: update

* Update src/TreeSelect.tsx

Co-authored-by: afc163 <afc163@gmail.com>

---------

Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
huangkairan and afc163 authored Jun 28, 2024
1 parent 981cd31 commit 42b19b1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
78 changes: 54 additions & 24 deletions examples/fieldNames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,59 @@ import TreeSelect from '../src';

export default () => {
return (
<TreeSelect
treeDefaultExpandAll
treeData={[
{
myLabel: 'Parent',
myValue: 'parent',
myChildren: [
{
myLabel: 'Sub 1',
myValue: 'sub_1',
},
{
myLabel: 'Sub 2',
myValue: 'sub_2',
},
],
},
]}
fieldNames={{
label: 'myLabel',
value: 'myValue',
children: 'myChildren',
}}
/>
<div>
<h2>basic</h2>
<TreeSelect
treeDefaultExpandAll
treeData={[
{
myLabel: 'Parent',
myValue: 'parent',
myChildren: [
{
myLabel: 'Sub 1',
myValue: 'sub_1',
},
{
myLabel: 'Sub 2',
myValue: 'sub_2',
},
],
},
]}
fieldNames={{
label: 'myLabel',
value: 'myValue',
children: 'myChildren',
}}
/>

<h2>title render</h2>
<TreeSelect
treeDefaultExpandAll
treeTitleRender={node => <span>{node.myLabel}</span>}
treeData={[
{
myLabel: 'Parent',
myValue: 'parent',
myChildren: [
{
myLabel: 'Sub 1',
myValue: 'sub_1',
},
{
myLabel: 'Sub 2',
myValue: 'sub_2',
},
],
},
]}
fieldNames={{
label: 'myLabel',
value: 'myValue',
children: 'myChildren',
}}
/>
</div>
);
};
3 changes: 1 addition & 2 deletions src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)

// Fill missing label & status
if (entity) {
rawLabel = rawLabel ?? getLabel(entity.node);
rawLabel = treeTitleRender ? treeTitleRender(entity.node) : rawLabel ?? getLabel(entity.node);
rawDisabled = entity.node.disabled;
} else if (rawLabel === undefined) {
// We try to find in current `labelInValue` value
Expand All @@ -377,7 +377,6 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
);
rawLabel = labelInValueItem.label;
}

return {
label: rawLabel,
value: rawValue,
Expand Down
33 changes: 33 additions & 0 deletions tests/Select.props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,39 @@ describe('TreeSelect.props', () => {
);
expect(wrapper.getSelection(0).text()).toBe('Value 0-0');
});

it('with fieldNames', () => {
const wrapper = mount(
<div>
<TreeSelect
defaultValue={'parent'}
treeTitleRender={node => node.myLabel}
fieldNames={{
label: 'myLabel',
value: 'myValue',
children: 'myChildren',
}}
treeData={[
{
myLabel: 'Parent',
myValue: 'parent',
myChildren: [
{
myLabel: 'Sub 1',
myValue: 'sub_1',
},
{
myLabel: 'Sub 2',
myValue: 'sub_2',
},
],
},
]}
/>
</div>,
);
expect(wrapper.getSelection(0).text()).toBe('Parent');
});
});
});
});

0 comments on commit 42b19b1

Please sign in to comment.