Skip to content

Commit 42b19b1

Browse files
huangkairanafc163
andauthored
fix: render correct label with fieldNames(#49631) (#538)
* fix: render correct label with `fieldNames`(#49631) * fix: update * Update src/TreeSelect.tsx Co-authored-by: afc163 <[email protected]> --------- Co-authored-by: afc163 <[email protected]>
1 parent 981cd31 commit 42b19b1

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

examples/fieldNames.tsx

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,59 @@ import TreeSelect from '../src';
55

66
export default () => {
77
return (
8-
<TreeSelect
9-
treeDefaultExpandAll
10-
treeData={[
11-
{
12-
myLabel: 'Parent',
13-
myValue: 'parent',
14-
myChildren: [
15-
{
16-
myLabel: 'Sub 1',
17-
myValue: 'sub_1',
18-
},
19-
{
20-
myLabel: 'Sub 2',
21-
myValue: 'sub_2',
22-
},
23-
],
24-
},
25-
]}
26-
fieldNames={{
27-
label: 'myLabel',
28-
value: 'myValue',
29-
children: 'myChildren',
30-
}}
31-
/>
8+
<div>
9+
<h2>basic</h2>
10+
<TreeSelect
11+
treeDefaultExpandAll
12+
treeData={[
13+
{
14+
myLabel: 'Parent',
15+
myValue: 'parent',
16+
myChildren: [
17+
{
18+
myLabel: 'Sub 1',
19+
myValue: 'sub_1',
20+
},
21+
{
22+
myLabel: 'Sub 2',
23+
myValue: 'sub_2',
24+
},
25+
],
26+
},
27+
]}
28+
fieldNames={{
29+
label: 'myLabel',
30+
value: 'myValue',
31+
children: 'myChildren',
32+
}}
33+
/>
34+
35+
<h2>title render</h2>
36+
<TreeSelect
37+
treeDefaultExpandAll
38+
treeTitleRender={node => <span>{node.myLabel}</span>}
39+
treeData={[
40+
{
41+
myLabel: 'Parent',
42+
myValue: 'parent',
43+
myChildren: [
44+
{
45+
myLabel: 'Sub 1',
46+
myValue: 'sub_1',
47+
},
48+
{
49+
myLabel: 'Sub 2',
50+
myValue: 'sub_2',
51+
},
52+
],
53+
},
54+
]}
55+
fieldNames={{
56+
label: 'myLabel',
57+
value: 'myValue',
58+
children: 'myChildren',
59+
}}
60+
/>
61+
</div>
3262
);
3363
};

src/TreeSelect.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
368368

369369
// Fill missing label & status
370370
if (entity) {
371-
rawLabel = rawLabel ?? getLabel(entity.node);
371+
rawLabel = treeTitleRender ? treeTitleRender(entity.node) : rawLabel ?? getLabel(entity.node);
372372
rawDisabled = entity.node.disabled;
373373
} else if (rawLabel === undefined) {
374374
// We try to find in current `labelInValue` value
@@ -377,7 +377,6 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
377377
);
378378
rawLabel = labelInValueItem.label;
379379
}
380-
381380
return {
382381
label: rawLabel,
383382
value: rawValue,

tests/Select.props.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,39 @@ describe('TreeSelect.props', () => {
660660
);
661661
expect(wrapper.getSelection(0).text()).toBe('Value 0-0');
662662
});
663+
664+
it('with fieldNames', () => {
665+
const wrapper = mount(
666+
<div>
667+
<TreeSelect
668+
defaultValue={'parent'}
669+
treeTitleRender={node => node.myLabel}
670+
fieldNames={{
671+
label: 'myLabel',
672+
value: 'myValue',
673+
children: 'myChildren',
674+
}}
675+
treeData={[
676+
{
677+
myLabel: 'Parent',
678+
myValue: 'parent',
679+
myChildren: [
680+
{
681+
myLabel: 'Sub 1',
682+
myValue: 'sub_1',
683+
},
684+
{
685+
myLabel: 'Sub 2',
686+
myValue: 'sub_2',
687+
},
688+
],
689+
},
690+
]}
691+
/>
692+
</div>,
693+
);
694+
expect(wrapper.getSelection(0).text()).toBe('Parent');
695+
});
663696
});
664697
});
665698
});

0 commit comments

Comments
 (0)