Skip to content

Commit fe1b164

Browse files
committed
add tree
1 parent 41a1f85 commit fe1b164

20 files changed

+1008
-66
lines changed

components/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export { default as Table } from './table'
100100

101101
export { default as Transfer } from './transfer'
102102

103-
// export { default as Tree } from './tree'
103+
export { default as Tree } from './tree'
104104

105105
// export { default as TreeSelect } from './tree-select'
106106

components/style.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ import './progress/style'
4040
import './timeline/style'
4141
import './input-number/style'
4242
import './transfer/style'
43+
import './tree/style'
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<cn>
2+
#### 受控操作示例
3+
受控操作示例
4+
</cn>
5+
6+
<us>
7+
#### basic controlled example
8+
basic controlled example
9+
</us>
10+
11+
```html
12+
<template>
13+
<a-tree
14+
checkable
15+
@expand="onExpand"
16+
:expandedKeys="expandedKeys"
17+
:autoExpandParent="autoExpandParent"
18+
@check="onCheck"
19+
:checkedKeys="checkedKeys"
20+
@select="onSelect"
21+
:selectedKeys="selectedKeys"
22+
:data="treeData"
23+
/>
24+
</template>
25+
<script>
26+
const treeData = [{
27+
title: '0-0',
28+
key: '0-0',
29+
children: [{
30+
title: '0-0-0',
31+
key: '0-0-0',
32+
children: [
33+
{ title: '0-0-0-0', key: '0-0-0-0' },
34+
{ title: '0-0-0-1', key: '0-0-0-1' },
35+
{ title: '0-0-0-2', key: '0-0-0-2' },
36+
],
37+
}, {
38+
title: '0-0-1',
39+
key: '0-0-1',
40+
children: [
41+
{ title: '0-0-1-0', key: '0-0-1-0' },
42+
{ title: '0-0-1-1', key: '0-0-1-1' },
43+
{ title: '0-0-1-2', key: '0-0-1-2' },
44+
],
45+
}, {
46+
title: '0-0-2',
47+
key: '0-0-2',
48+
}],
49+
}, {
50+
title: '0-1',
51+
key: '0-1',
52+
children: [
53+
{ title: '0-1-0-0', key: '0-1-0-0' },
54+
{ title: '0-1-0-1', key: '0-1-0-1' },
55+
{ title: '0-1-0-2', key: '0-1-0-2' },
56+
],
57+
}, {
58+
title: '0-2',
59+
key: '0-2',
60+
}]
61+
62+
export default {
63+
data () {
64+
return {
65+
expandedKeys: ['0-0-0', '0-0-1'],
66+
autoExpandParent: true,
67+
checkedKeys: ['0-0-0'],
68+
selectedKeys: [],
69+
treeData,
70+
}
71+
},
72+
methods: {
73+
onExpand (expandedKeys) {
74+
console.log('onExpand', arguments)
75+
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
76+
// or, you can remove all expanded children keys.
77+
this.expandedKeys = expandedKeys
78+
this.autoExpandParent = false
79+
},
80+
onCheck (checkedKeys) {
81+
console.log('onCheck', checkedKeys)
82+
this.checkedKeys = checkedKeys
83+
},
84+
onSelect (selectedKeys, info) {
85+
console.log('onSelect', info)
86+
this.selectedKeys = selectedKeys
87+
},
88+
},
89+
}
90+
91+
</script>
92+
93+
```

components/tree/demo/basic.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<cn>
2+
#### 基本用法
3+
最简单的用法,展示可勾选,可选中,禁用,默认展开等功能。
4+
</cn>
5+
6+
<us>
7+
#### Basic
8+
The most basic usage, tell you how to use checkable, selectable, disabled, defaultExpandKeys, and etc.
9+
</us>
10+
11+
```html
12+
<template>
13+
<a-tree
14+
checkable
15+
:data="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+
>
22+
<span slot="title0010" style="color: #1890ff">sss</span>
23+
</a-tree>
24+
</template>
25+
<script>
26+
const treeData = [{
27+
title: 'parent 1',
28+
key: '0-0',
29+
children: [{
30+
title: 'parent 1-0',
31+
key: '0-0-0',
32+
disabled: true,
33+
children: [
34+
{ title: 'leaf', key: '0-0-0-0', disableCheckbox: true },
35+
{ title: 'leaf', key: '0-0-0-1' },
36+
],
37+
}, {
38+
title: 'parent 1-1',
39+
key: '0-0-1',
40+
children: [
41+
{ key: '0-0-1-0', slots: { title: 'title0010' }},
42+
],
43+
}],
44+
}]
45+
46+
export default {
47+
data () {
48+
return {
49+
treeData,
50+
}
51+
},
52+
methods: {
53+
onSelect (selectedKeys, info) {
54+
console.log('selected', selectedKeys, info)
55+
},
56+
onCheck (checkedKeys, info) {
57+
console.log('onCheck', checkedKeys, info)
58+
},
59+
},
60+
}
61+
62+
</script>
63+
64+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<cn>
2+
#### 自定义图标
3+
可以针对不同的节点定制图标。
4+
</cn>
5+
6+
<us>
7+
#### Customize Icon
8+
You can customize icons for different nodes.
9+
</us>
10+
11+
```html
12+
<template>
13+
<a-tree
14+
:data="treeData"
15+
showIcon
16+
defaultExpandAll
17+
:defaultSelectedKeys="['0-0-0']"
18+
>
19+
<a-icon slot="smile" type="smile-o" />
20+
<a-icon slot="meh" type="smile-o" />
21+
<template slot="custom" slot-scope="{selected}">
22+
<a-icon :type="selected ? 'frown':'frown-o'" />
23+
</template>
24+
</a-tree>
25+
</template>
26+
<script>
27+
const treeData = [{
28+
title: 'parent 1',
29+
key: '0-0',
30+
slots: {
31+
icon: 'smile',
32+
},
33+
children: [
34+
{ title: 'leaf', key: '0-0-0', slots: { icon: 'meh' }},
35+
{ title: 'leaf', key: '0-1-1', scopedSlots: { icon: 'custom' }}],
36+
}]
37+
38+
export default {
39+
data () {
40+
return {
41+
treeData,
42+
}
43+
},
44+
methods: {
45+
onSelect (selectedKeys, info) {
46+
console.log('selected', selectedKeys, info)
47+
},
48+
onCheck (checkedKeys, info) {
49+
console.log('onCheck', checkedKeys, info)
50+
},
51+
},
52+
}
53+
54+
</script>
55+
56+
```

components/tree/demo/draggable.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<cn>
2+
#### 拖动示例
3+
将节点拖拽到其他节点内部或前后。
4+
</cn>
5+
6+
<us>
7+
#### draggable
8+
Drag treeNode to insert after the other treeNode or insert into the other parent TreeNode.
9+
</us>
10+
11+
```html
12+
<template>
13+
<a-tree
14+
class="draggable-tree"
15+
:defaultExpandedKeys="expandedKeys"
16+
draggable
17+
@dragenter="onDragEnter"
18+
@drop="onDrop"
19+
:data="gData"
20+
/>
21+
</template>
22+
23+
<script>
24+
const x = 3
25+
const y = 2
26+
const z = 1
27+
const gData = []
28+
29+
const generateData = (_level, _preKey, _tns) => {
30+
const preKey = _preKey || '0'
31+
const tns = _tns || gData
32+
33+
const children = []
34+
for (let i = 0; i < x; i++) {
35+
const key = `${preKey}-${i}`
36+
tns.push({ title: key, key })
37+
if (i < y) {
38+
children.push(key)
39+
}
40+
}
41+
if (_level < 0) {
42+
return tns
43+
}
44+
const level = _level - 1
45+
children.forEach((key, index) => {
46+
tns[index].children = []
47+
return generateData(level, key, tns[index].children)
48+
})
49+
}
50+
generateData(z)
51+
export default {
52+
data () {
53+
return {
54+
gData,
55+
expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],
56+
}
57+
},
58+
methods: {
59+
onDragEnter (info) {
60+
console.log(info)
61+
// expandedKeys 需要受控时设置
62+
// this.expandedKeys = info.expandedKeys
63+
},
64+
onDrop (info) {
65+
console.log(info)
66+
const dropKey = info.node.eventKey
67+
const dragKey = info.dragNode.eventKey
68+
const dropPos = info.node.pos.split('-')
69+
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1])
70+
// const dragNodesKeys = info.dragNodesKeys;
71+
const loop = (data, key, callback) => {
72+
data.forEach((item, index, arr) => {
73+
if (item.key === key) {
74+
return callback(item, index, arr)
75+
}
76+
if (item.children) {
77+
return loop(item.children, key, callback)
78+
}
79+
})
80+
}
81+
const data = [...this.gData]
82+
let dragObj
83+
loop(data, dragKey, (item, index, arr) => {
84+
arr.splice(index, 1)
85+
dragObj = item
86+
})
87+
if (info.dropToGap) {
88+
let ar
89+
let i
90+
loop(data, dropKey, (item, index, arr) => {
91+
ar = arr
92+
i = index
93+
})
94+
if (dropPosition === -1) {
95+
ar.splice(i, 0, dragObj)
96+
} else {
97+
ar.splice(i + 1, 0, dragObj)
98+
}
99+
} else {
100+
loop(data, dropKey, (item) => {
101+
item.children = item.children || []
102+
// where to insert 示例添加到尾部,可以是随意位置
103+
item.children.push(dragObj)
104+
})
105+
}
106+
this.gData = data
107+
},
108+
},
109+
}
110+
</script>
111+
112+
```

components/tree/demo/dynamic.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<cn>
2+
#### 异步数据加载
3+
点击展开节点,动态加载数据。
4+
</cn>
5+
6+
<us>
7+
#### load data asynchronously
8+
To load data asynchronously when click to expand a treeNode.
9+
</us>
10+
11+
```html
12+
<template>
13+
14+
</template>
15+
<script>
16+
17+
</script>
18+
```

0 commit comments

Comments
 (0)