Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree): 增加checkStrategy支持自定义勾选策略 #518

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/devui-vue/devui/tree/src/components/tree-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export default defineComponent({
const halfChecked = computed(() => {
const children = getChildren(data.value);
const checkedChildren = children.filter((item: IInnerTreeNode) => item.checked);
return checkedChildren.length > 0 && checkedChildren.length < children.length;

if (['upward', 'both'].includes(check.value)) {
return checkedChildren.length > 0 && checkedChildren.length < children.length;
} else {
return false;
}
});

return () => {
Expand Down
22 changes: 16 additions & 6 deletions packages/devui-vue/devui/tree/src/core/use-check.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Ref } from 'vue';
import { ref, Ref } from 'vue';
import { CheckStrategy, IInnerTreeNode, IUseCore } from './use-tree-types';

export default function(options?: {
export default function(options?: Ref<{
checkStrategy: CheckStrategy
}) {
}> = ref({
checkStrategy: 'both'
})) {
return function useCheck(data: Ref<IInnerTreeNode[]>, core: IUseCore) {
const { setNodeValue, getNode, getChildren, getParent } = core;

Expand Down Expand Up @@ -45,13 +47,21 @@ export default function(options?: {
const toggleCheckNode = (node: IInnerTreeNode): void => {
if (getNode(node).checked) {
setNodeValue(node, 'checked', false);
getChildren(node).forEach(item => setNodeValue(item, 'checked', false));

if (['downward', 'both'].includes(options.value.checkStrategy)) {
getChildren(node).forEach(item => setNodeValue(item, 'checked', false));
}
} else {
setNodeValue(node, 'checked', true);
getChildren(node).forEach(item => setNodeValue(item, 'checked', true));

if (['downward', 'both'].includes(options.value.checkStrategy)) {
getChildren(node).forEach(item => setNodeValue(item, 'checked', true));
}
}

controlParentNodeChecked(node);
if (['upward', 'both'].includes(options.value.checkStrategy)) {
controlParentNodeChecked(node);
}
}

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/devui-vue/devui/tree/src/core/use-core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, ComputedRef, Ref } from 'vue';
import { IInnerTreeNode, IUseCore, valueof } from './use-tree-types';
import { IInnerTreeNode, ITreeNode, IUseCore, valueof } from './use-tree-types';
import { generateInnerTree } from './utils';

export default function(options?){
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function(options?){
data.value[getIndex(node)][key] = value;
}

const setTree = (newTree: IInnerTreeNode[]): void => {
const setTree = (newTree: ITreeNode[]): void => {
data.value = generateInnerTree(newTree);
}

Expand Down
14 changes: 11 additions & 3 deletions packages/devui-vue/devui/tree/src/new-tree.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent, provide, renderSlot, toRefs, useSlots, watch } from 'vue';
import { defineComponent, provide, ref, renderSlot, toRefs, useSlots, watch } from 'vue';
import DTreeNode from './components/tree-node';
import DTreeNodeContent from './components/tree-node-content';
import DTreeNodeToggle from './components/tree-node-toggle';
Expand All @@ -21,8 +21,16 @@ export default defineComponent({
useOperate(),
];

if (check) {
userPlugins.push(useCheck());
const checkOptions = ref({
checkStrategy: check.value || 'both'
});

watch(check, (newVal) => {
checkOptions.value.checkStrategy = newVal;
})

if (check.value) {
userPlugins.push(useCheck(checkOptions));
}

const treeFactory = useTree(
Expand Down
44 changes: 41 additions & 3 deletions packages/devui-vue/docs/components/tree/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,24 @@ export default defineComponent({

```vue
<template>
<d-new-tree :data="data" check></d-new-tree>
<div class="flex flex-row" style="height: 28px;">
<label class="flex items-center mr-xl"><span class="inline-block mr-xs">开启勾选</span><d-switch v-model:checked="openCheck"></d-switch></label>
<d-radio-group v-if="openCheck" v-model="currentStrategy" direction="row">
<d-radio v-for="strategy of checkStrategy" :key="strategy" :value="strategy">{{ strategy }}</d-radio>
</d-radio-group>
</div>
<d-new-tree :data="data" :check="currentStrategy"></d-new-tree>
</template>

<script>
import { defineComponent, ref } from 'vue'
import { defineComponent, ref, watch } from 'vue'

export default defineComponent({
setup() {
const openCheck = ref(true);
const checkStrategy = ref(['both', 'downward', 'upward', 'none']);
const currentStrategy = ref('both');

const data = ref([
{
label: 'Parent node 1',
Expand All @@ -75,9 +85,37 @@ export default defineComponent({
},
{ label: 'Leaf node 2' }
]);

watch(openCheck, (newVal) => {
if (newVal === false) {
currentStrategy.value = false;
} else {
currentStrategy.value = 'both';

data.value = [
{
label: 'Parent node 1',
children: [
{
label: 'Parent node 1-1',
children: [
{ label: 'Leaf node 1-1-1' },
{ label: 'Leaf node 1-1-2' }
]
},
{ label: 'Leaf node 1-2' }
]
},
{ label: 'Leaf node 2' }
];
}
});

return {
data
data,
openCheck,
checkStrategy,
currentStrategy,
}
}
})
Expand Down