Skip to content

Commit

Permalink
refactor(Tree): expand all node
Browse files Browse the repository at this point in the history
  • Loading branch information
Lydanne committed Feb 1, 2021
1 parent 79ec122 commit 2dbad32
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/element3/src/components/Tree/src/TreeMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default {
accordion: Boolean,
autoExpandParent: { type: Boolean, default: true },
expandOnClickNode: { type: Boolean, default: true },
expanded: { type: Array as PropType<ID[]>, default: () => [] }
expanded: { type: Array as PropType<ID[]>, default: () => [] },
defaultExpandAll: Boolean
},
emits: ['update:modelValue', 'update:checked', 'update:expanded'],
setup(props, ctx) {
Expand All @@ -64,6 +65,9 @@ export default {
ctx.emit('update:checked', tree.getCheckedIds())
})
if (props.defaultExpandAll) {
tree.expandAll()
}
watchEffect(
() => {
tree.expandNodeByIds(props.expanded)
Expand Down
6 changes: 6 additions & 0 deletions packages/element3/src/components/Tree/src/entity/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ export class Tree<RawNode extends RawNodeBase> {

return ids
}

expandAll(v = true): void {
this.root.depthEach((currentNode) => {
currentNode.expand(v, false)
})
}
}
38 changes: 38 additions & 0 deletions packages/element3/src/components/Tree/tests/entity/Tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,42 @@ describe('Tree.ts', () => {
rawNodes[0].childs[0].key
)
})

it('expand all node', () => {
type RawNode = { key: number; text: string; childs?: RawNode[] }
const rawNodes: RawNode[] = [
{
key: 1,
text: 'Node1',
childs: [
{
key: 11,
text: 'Node011'
}
]
},
{
key: 2,
text: 'Node2',
childs: [
{
key: 21,
text: 'Node021'
}
]
}
]

const tree = new Tree(rawNodes, {
id: 'key',
label: 'text',
children: 'childs'
})

tree.expandAll(true)

expect(tree.root.isExpanded).toBeTruthy()
expect(tree.root.findOne(1).isExpanded).toBeTruthy()
expect(tree.root.findOne(2).isExpanded).toBeTruthy()
})
})
1 change: 1 addition & 0 deletions packages/website/src/play/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:accordion="false"
:checkOnClickNode="false"
:checkStrictly="false"
:defaultExpandAll="true"
:defaultNodeKey="{
isDisabled: 'disabled'
}"
Expand Down

0 comments on commit 2dbad32

Please sign in to comment.