Skip to content

Commit

Permalink
refactor(Tree): Based on the implementation of Check-Strictly, is it …
Browse files Browse the repository at this point in the history
…strictly followed that the father and son are not related to each other in the case of displaying the check box
  • Loading branch information
Lydanne committed Jan 31, 2021
1 parent 8b2bca2 commit 3aad5f4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/element3/src/components/Tree/src/TreeMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default {
checked: { type: Array as PropType<ID[]>, default: () => [] },
showCheckbox: Boolean,
checkOnClickNode: Boolean
checkOnClickNode: Boolean,
checkStrictly: Boolean
},
emits: ['update:modelValue', 'update:checked'],
setup(props, ctx) {
Expand All @@ -42,6 +43,7 @@ export default {
ctx.emit('update:modelValue', tree.rawNodesProxy)
const rootChildren = computed(() => tree.root.children)
tree.rootProxy.setStrictly(props.checkStrictly)
watchEffect(
() => {
tree.setCheckedByIds(props.checked)
Expand All @@ -51,7 +53,6 @@ export default {
// exec after wait component flush
}
)
watchEffect(() => {
ctx.emit('update:checked', tree.getCheckedIds())
})
Expand Down
27 changes: 23 additions & 4 deletions packages/element3/src/components/Tree/src/entity/TreeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,30 @@ export class TreeNode implements TreeNodePublicProp {
children: TreeNode[] = []
private _isLeaf = false
private _isChecked = false
private _isStrictly = false

get isStrictly(): boolean {
if (this._isStrictly) {
return true
}

return (this._isStrictly = this.parent?.isStrictly ?? false)
}

get isIndeterminate(): boolean {
if (this.isStrictly) {
return false
}
const checkedLen = this.getCheckedNodes().length
if (this.isLeaf || checkedLen === 0) {
return false
}

return this.getCheckedNodes().length !== this.children.length
return checkedLen !== this.children.length
}

get isChecked(): boolean {
if (this.isLeaf) {
if (this.isLeaf || this.isStrictly) {
return this._isChecked
}

Expand All @@ -62,19 +74,26 @@ export class TreeNode implements TreeNodePublicProp {
id: ID,
label: string,
children: TreeNode[] = [],
{ isLeaf = false, isChecked = false } = {}
{ isLeaf = false, isChecked = false, isStrictly = false } = {}
) {
this.id = id ?? idSeed++
this.label = label
this._isLeaf = isLeaf
this._isStrictly = isStrictly
this.setChecked(isChecked)

this.appendChild(...children)
}

setChecked(v = !this._isChecked): void {
this._isChecked = v
this.children.forEach((node) => node.setChecked(v))
if (!this.isStrictly) {
this.children.forEach((node) => node.setChecked(v))
}
}

setStrictly(v: boolean): void {
this._isStrictly = v
}

getCheckedNodes(): TreeNode[] {
Expand Down
35 changes: 35 additions & 0 deletions packages/element3/src/components/Tree/tests/TreeMain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,39 @@ describe('TreeMain.vue', () => {
await node2.trigger('click')
expect(wrapper.vm.checked).toEqual([2])
})

it('Implement, in the case of displaying checkboxes, whether to strictly follow the parent-child discordant practice', async () => {
const wrapper = mount({
template: `
<el-tree-main v-model="nodes" v-model:checked="checked" show-checkbox check-strictly></el-tree-main>
`,
components: { elTreeMain: TreeMain },
setup() {
const nodes = ref([
{
id: 1,
label: 'Node1',
children: [
{
id: 2,
label: 'Node2'
}
]
}
])
const checked = ref([])
return {
nodes,
checked
}
}
})

await nextTick()

const node2 = wrapper.find('#TreeNode2 .el-tree-node__content input')

await node2.trigger('click')
expect(wrapper.vm.checked).toEqual([2])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,43 @@ describe('TreeNode.ts', () => {

expect(root.getCheckedNodes()).toHaveLength(2)
})

it('test isStrictly', () => {
const root = new TreeNode(1, 'Node1', [
new TreeNode(
2,
'Node1-1',
[new TreeNode(3, 'Node1-1-1', [new TreeNode(4, 'Node1-1-1-1')])],
{
isStrictly: true
}
)
])

expect(root.findOne(4).isStrictly).toBeTruthy()
expect(root.findOne(2).isStrictly).toBeTruthy()
expect(root.isStrictly).toBeFalsy()
})

it('test strictly and setChecked', () => {
const root = new TreeNode(
1,
'Node1',
[new TreeNode(2, 'Node1-1', [new TreeNode(4, 'Node1-1-1')])],
{
isStrictly: true
}
)

root.findOne(2).setChecked(true)
expect(root.findOne(2).isChecked).toBeTruthy()
expect(root.isChecked).toBeFalsy()
expect(root.isIndeterminate).toBeFalsy()
expect(root.findOne(4).isChecked).toBeFalsy()

root.setChecked(true)
expect(root.isChecked).toBeTruthy()
expect(root.findOne(2).isChecked).toBeTruthy()
expect(root.findOne(4).isChecked).toBeFalsy()
})
})
1 change: 1 addition & 0 deletions packages/website/src/play/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ref="tree"
:show-checkbox="true"
:checkOnClickNode="true"
:checkStrictly="true"
></el-tree>
</template>

Expand Down

0 comments on commit 3aad5f4

Please sign in to comment.