Skip to content

Commit 5ab8892

Browse files
feat(tree): 添加form-props-'replaceFields'字段
1 parent 548f10d commit 5ab8892

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

components/tree/Tree.jsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ function TreeProps() {
8787
openAnimation: PropTypes.any,
8888
treeNodes: PropTypes.array,
8989
treeData: PropTypes.array,
90+
/**
91+
* @default{title,key,children}
92+
* 替换treeNode中 title,key,children字段为treeData中对应的字段
93+
*/
94+
replaceFields: PropTypes.object,
9095
};
9196
}
9297

@@ -151,13 +156,15 @@ export default {
151156
},
152157
updateTreeData(treeData) {
153158
const { $slots, $scopedSlots } = this;
159+
const defaultFields = { children: 'children', title: 'title', key: 'key' };
160+
const replaceFields = { ...defaultFields, ...this.$props.replaceFields };
154161
return treeData.map(item => {
162+
const key = item[replaceFields.key];
163+
const children = item[replaceFields.children];
155164
const {
156-
children,
157165
on = {},
158166
slots = {},
159167
scopedSlots = {},
160-
key,
161168
class: cls,
162169
style,
163170
...restProps
@@ -171,7 +178,7 @@ export default {
171178
title:
172179
$slots[slots.title] ||
173180
($scopedSlots[scopedSlots.title] && $scopedSlots[scopedSlots.title](item)) ||
174-
restProps.title,
181+
restProps[replaceFields.title],
175182
dataRef: item,
176183
on,
177184
key,

components/tree/demo/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Dynamic from './dynamic';
77
import Line from './line';
88
import Search from './search';
99
import Directory from './directory';
10+
import ReplaceFields from './replaceFields';
1011
1112
import CN from '../index.zh-CN.md';
1213
import US from '../index.en-US.md';
@@ -37,6 +38,7 @@ export default {
3738
<md cn={md.cn} us={md.us} />
3839
<BasicControlled />
3940
<Basic />
41+
<ReplaceFields/>
4042
<CustomizedIcon />
4143
<Draggable />
4244
<Dynamic />

components/tree/demo/replaceFields.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<cn>
2+
#### 基本用法
3+
替换treeNode中 title,key,children字段为treeData中对应的字段
4+
</cn>
5+
6+
<us>
7+
#### ReplaceFields
8+
Replace the title,key and children fields in treeNode with the corresponding fields in treeData.
9+
</us>
10+
11+
```tpl
12+
<template>
13+
<a-tree
14+
checkable
15+
:treeData="treeData"
16+
:defaultExpandedKeys="['0-0-0', '0-0-1']"
17+
:defaultSelectedKeys="['0-0-0', '0-0-1']"
18+
:defaultCheckedKeys="['0-0-0', '0-0-1']"
19+
@select="this.onSelect"
20+
@check="this.onCheck"
21+
:replaceFields="replaceFields"/>
22+
</template>
23+
<script>
24+
const treeData = [
25+
{
26+
name: 'parent 1',
27+
key: '0-0',
28+
child: [
29+
{
30+
name: '张晨成',
31+
key: '0-0-0',
32+
disabled: true,
33+
child: [
34+
{ name: 'leaf', key: '0-0-0-0', disableCheckbox: true },
35+
{ name: 'leaf', key: '0-0-0-1' },
36+
],
37+
},
38+
{
39+
name: 'parent 1-1',
40+
key: '0-0-1',
41+
child: [{ key: '0-0-1-0', name:'zcvc' }],
42+
},
43+
],
44+
},
45+
];
46+
47+
export default {
48+
data() {
49+
return {
50+
treeData,
51+
replaceFields:{
52+
children:'child',
53+
title:'name'
54+
}
55+
};
56+
},
57+
methods: {
58+
onSelect(selectedKeys, info) {
59+
console.log('selected', selectedKeys, info);
60+
},
61+
onCheck(checkedKeys, info) {
62+
console.log('onCheck', checkedKeys, info);
63+
},
64+
},
65+
};
66+
</script>
67+
```

components/tree/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| Property | Description | Type | Default |
66
| --- | --- | --- | --- |
77
| treeData | treeNode of tree, please use `treeNodes` before v1.1.4 | array | - |
8+
| replaceFields | Replace the title,key and children fields in treeNode with the corresponding fields in treeData | object | { children:'children', title:'title', key:'key' } |
89
| autoExpandParent | Whether to automatically expand a parent treeNode | boolean | true |
910
| checkable | Adds a `Checkbox` before the treeNodes | boolean | false |
1011
| checkedKeys(v-model) | (Controlled) Specifies the keys of the checked treeNodes (PS: When this specifies the key of a treeNode which is also a parent treeNode, all the children treeNodes of will be checked; and vice versa, when it specifies the key of a treeNode which is a child treeNode, its parent treeNode will also be checked. When `checkable` and `checkStrictly` is true, its object has `checked` and `halfChecked` property. Regardless of whether the child or parent treeNode is checked, they won't impact each other. | string\[] \| number\[] \| {checked: string\[] \| number\[], halfChecked: string\[] \| number\[]} | \[] |

components/tree/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| 参数 | 说明 | 类型 | 默认值 |
66
| --- | --- | --- | --- |
77
| treeData | 节点的配置描述,具体项见下表, 1.1.4 之前的版本使用`treeNodes` | array | -- |
8+
| replaceFields | 替换treeNode中 title,key,children字段为treeData中对应的字段 | object |{children:'children', title:'title', key:'key' } |
89
| autoExpandParent | 是否自动展开父节点 | boolean | true |
910
| checkable | 节点前添加 Checkbox 复选框 | boolean | false |
1011
| checkedKeys(v-model) | (受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点 key,则子节点自动选中;相应当子节点 key 都传入,父节点也自动选中。当设置`checkable``checkStrictly`,它是一个有`checked``halfChecked`属性的对象,并且父子节点的选中与否不再关联 | string\[] \| number\[] \| {checked: string\[] \| number\[], halfChecked: string\[] \| number\[]} | \[] |

site/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Api from './components/api';
1212
import './components';
1313
import demoBox from './components/demoBox';
1414
import demoContainer from './components/demoContainer';
15-
import Test from '../components/table/demo/index.vue';
15+
import Test from '../components/tree/demo/index.vue';
1616
import zhCN from './theme/zh-CN';
1717
import enUS from './theme/en-US';
1818
Vue.use(Vuex);

types/tree/tree.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export declare class Tree extends AntdComponent {
1616
*/
1717
treeData: TreeNode[];
1818

19+
/**
20+
* @default{ children:'children', title:'title', key:'key' }
21+
* Replace the title,key and children fields in treeNode with the corresponding fields in treeData
22+
*/
23+
replaceFields?: {
24+
children?: string;
25+
title?: string;
26+
key?: string;
27+
};
28+
1929
/**
2030
* Whether to automatically expand a parent treeNode
2131
* @default true

0 commit comments

Comments
 (0)