Skip to content

Commit a61f7fd

Browse files
committed
doc: tree add accordion demo
close #4849
1 parent 6854605 commit a61f7fd

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

components/tree/__tests__/__snapshots__/demo.test.js.snap

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`renders ./components/tree/demo/accordion.vue correctly 1`] = `
4+
<div role="tree" class="ant-tree ant-tree-icon-hide">
5+
<!---->
6+
<div><input style="width: 0px; height: 0px; display: flex; overflow: hidden; opacity: 0; border: 0px; padding: 0px; margin: 0px;" tabindex="0" aria-label="for screen reader"></div>
7+
<div class="ant-tree-treenode" aria-hidden="true" style="position: absolute; pointer-events: none; visibility: hidden; height: 0px; overflow: hidden;">
8+
<div class="ant-tree-indent">
9+
<div class="ant-tree-indent-unit"></div>
10+
</div>
11+
</div>
12+
<div style="position: relative;" class="ant-tree-list">
13+
<div class="ant-tree-list-holder">
14+
<div>
15+
<div style="display: flex; flex-direction: column;" class="ant-tree-list-holder-inner">
16+
<div class="ant-tree-treenode ant-tree-treenode-switcher-close"><span aria-hidden="true" class="ant-tree-indent"></span><span class="ant-tree-switcher ant-tree-switcher_close"><span role="img" aria-label="caret-down" class="anticon anticon-caret-down ant-tree-switcher-icon"><svg focusable="false" class="" data-icon="caret-down" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M840.4 300H183.6c-19.7 0-30.7 20.8-18.5 35l328.4 380.8c9.4 10.9 27.5 10.9 37 0L858.9 335c12.2-14.2 1.2-35-18.5-35z"></path></svg></span></span>
17+
<!----><span title="" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-close"><!----><span class="ant-tree-title">parent 1</span>
18+
<!----></span>
19+
</div>
20+
<div class="ant-tree-treenode ant-tree-treenode-switcher-close ant-tree-treenode-leaf-last"><span aria-hidden="true" class="ant-tree-indent"></span><span class="ant-tree-switcher ant-tree-switcher_close"><span role="img" aria-label="caret-down" class="anticon anticon-caret-down ant-tree-switcher-icon"><svg focusable="false" class="" data-icon="caret-down" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="0 0 1024 1024"><path d="M840.4 300H183.6c-19.7 0-30.7 20.8-18.5 35l328.4 380.8c9.4 10.9 27.5 10.9 37 0L858.9 335c12.2-14.2 1.2-35-18.5-35z"></path></svg></span></span>
21+
<!----><span title="" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-close"><!----><span class="ant-tree-title">parent 2</span>
22+
<!----></span>
23+
</div>
24+
</div>
25+
</div>
26+
</div>
27+
<!---->
28+
</div>
29+
</div>
30+
`;
31+
332
exports[`renders ./components/tree/demo/basic.vue correctly 1`] = `
433
<div role="tree" class="ant-tree ant-tree-icon-hide">
534
<!---->

components/tree/demo/accordion.vue

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<docs>
2+
---
3+
order: 0
4+
title:
5+
zh-CN: 手风琴模式
6+
en-US: Accordion
7+
---
8+
9+
## zh-CN
10+
11+
同一级的节点,每次只能展开一个
12+
13+
## en-US
14+
15+
Nodes of the same level can only be expanded one
16+
17+
</docs>
18+
<template>
19+
<a-tree
20+
v-model:selectedKeys="selectedKeys"
21+
:expanded-keys="expandedKeys"
22+
:tree-data="treeData"
23+
@expand="handleExpand"
24+
>
25+
<template #title="{ title, key }">
26+
<span v-if="key === '0-0-1-0'" style="color: #1890ff">{{ title }}</span>
27+
<template v-else>{{ title }}</template>
28+
</template>
29+
</a-tree>
30+
</template>
31+
<script lang="ts">
32+
import type { TreeProps } from 'ant-design-vue';
33+
import _ from 'lodash';
34+
import { defineComponent, ref, watch } from 'vue';
35+
36+
const treeData: TreeProps['treeData'] = [
37+
{
38+
title: 'parent 1',
39+
key: '0-0',
40+
children: [
41+
{
42+
title: 'parent 1-0',
43+
key: '0-0-0',
44+
children: [
45+
{ title: 'leaf', key: '0-0-0-0' },
46+
{ title: 'leaf', key: '0-0-0-1' },
47+
],
48+
},
49+
{
50+
title: 'parent 1-1',
51+
key: '0-0-1',
52+
children: [{ key: '0-0-1-0', title: 'sss' }],
53+
},
54+
],
55+
},
56+
{
57+
title: 'parent 2',
58+
key: '1-0',
59+
children: [
60+
{
61+
title: 'parent 2-0',
62+
key: '1-0-0',
63+
},
64+
{
65+
title: 'parent 2-1',
66+
key: '2-0-1',
67+
},
68+
],
69+
},
70+
];
71+
72+
export default defineComponent({
73+
setup() {
74+
const expandedKeys = ref<string[]>([]);
75+
const selectedKeys = ref<string[]>(['0-0-0', '0-0-1']);
76+
const checkedKeys = ref<string[]>(['0-0-0', '0-0-1']);
77+
watch(expandedKeys, () => {
78+
console.log('expandedKeys', expandedKeys);
79+
});
80+
watch(selectedKeys, () => {
81+
console.log('selectedKeys', selectedKeys);
82+
});
83+
watch(checkedKeys, () => {
84+
console.log('checkedKeys', checkedKeys);
85+
});
86+
const handleExpand = (keys: string[], { expanded, node }) => {
87+
// node.parent add from 3.0.0-alpha.10
88+
const tempKeys = ((node.parent ? node.parent.children : treeData) || []).map(
89+
({ key }) => key,
90+
);
91+
if (expanded) {
92+
expandedKeys.value = _.difference(keys, tempKeys).concat(node.key);
93+
} else {
94+
expandedKeys.value = keys;
95+
}
96+
};
97+
return {
98+
treeData,
99+
expandedKeys,
100+
selectedKeys,
101+
checkedKeys,
102+
handleExpand,
103+
};
104+
},
105+
});
106+
</script>

components/tree/demo/index.vue

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<replace-fields />
1212
<context-menu />
1313
<virtual-scroll />
14+
<accordion />
1415
</demo-sort>
1516
</template>
1617
<script lang="ts">
@@ -25,6 +26,7 @@ import SwitcherIcon from './switcher-icon.vue';
2526
import ReplaceFields from './replaceFields.vue';
2627
import ContextMenu from './context-menu.vue';
2728
import VirtualScroll from './virtual-scroll.vue';
29+
import Accordion from './accordion.vue';
2830
import CN from '../index.zh-CN.md';
2931
import US from '../index.en-US.md';
3032
import { defineComponent } from 'vue';
@@ -44,6 +46,7 @@ export default defineComponent({
4446
ReplaceFields,
4547
ContextMenu,
4648
VirtualScroll,
49+
Accordion,
4750
},
4851
setup() {
4952
return {};

0 commit comments

Comments
 (0)