Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: render correct label with fieldNames(#49631) #538

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});
});
Loading