From 60e5db1e498023b0ceeb6600c8c138218fef517e Mon Sep 17 00:00:00 2001 From: snowingfox <1503401882@qq.com> Date: Fri, 21 Oct 2022 09:47:18 +0800 Subject: [PATCH] refactor(components): [tree-v2] switch to script-setup syntax (#10133) --- .../tree-v2/src/composables/useCheck.ts | 7 +- .../tree-v2/src/composables/useTree.ts | 13 +- packages/components/tree-v2/src/tree-node.vue | 87 ++++------ packages/components/tree-v2/src/tree.vue | 151 ++++++++---------- packages/components/tree-v2/src/types.ts | 2 +- .../components/tree-v2/src/virtual-tree.ts | 5 +- 6 files changed, 120 insertions(+), 145 deletions(-) diff --git a/packages/components/tree-v2/src/composables/useCheck.ts b/packages/components/tree-v2/src/composables/useCheck.ts index 33be1c89e1..d462d18d6f 100644 --- a/packages/components/tree-v2/src/composables/useCheck.ts +++ b/packages/components/tree-v2/src/composables/useCheck.ts @@ -4,6 +4,7 @@ import { NODE_CHECK_CHANGE, SetOperationEnum, } from '../virtual-tree' +import type { CheckboxValueType } from '@element-plus/components/checkbox' import type { Ref } from 'vue' import type { Tree, TreeKey, TreeNode, TreeNodeData, TreeProps } from '../types' @@ -78,11 +79,11 @@ export function useCheck(props: TreeProps, tree: Ref) { const toggleCheckbox = ( node: TreeNode, - isChecked: boolean, + isChecked: CheckboxValueType, nodeClick = true ) => { const checkedKeySet = checkedKeys.value - const toggle = (node: TreeNode, checked: boolean) => { + const toggle = (node: TreeNode, checked: CheckboxValueType) => { checkedKeySet[checked ? SetOperationEnum.ADD : SetOperationEnum.DELETE]( node.key ) @@ -102,7 +103,7 @@ export function useCheck(props: TreeProps, tree: Ref) { } } - const afterNodeCheck = (node: TreeNode, checked: boolean) => { + const afterNodeCheck = (node: TreeNode, checked: CheckboxValueType) => { const { checkedNodes, checkedKeys } = getChecked() const { halfCheckedNodes, halfCheckedKeys } = getHalfChecked() emit(NODE_CHECK, node.data, { diff --git a/packages/components/tree-v2/src/composables/useTree.ts b/packages/components/tree-v2/src/composables/useTree.ts index a8f9194490..f3d109bb41 100644 --- a/packages/components/tree-v2/src/composables/useTree.ts +++ b/packages/components/tree-v2/src/composables/useTree.ts @@ -1,4 +1,3 @@ -// @ts-nocheck import { computed, nextTick, ref, shallowRef, watch } from 'vue' import { isObject } from '@element-plus/utils' import { @@ -10,6 +9,9 @@ import { } from '../virtual-tree' import { useCheck } from './useCheck' import { useFilter } from './useFilter' +import type { SetupContext } from 'vue' +import type { treeEmits } from '../virtual-tree' +import type { CheckboxValueType } from '@element-plus/components/checkbox' import type { Tree, TreeData, @@ -19,7 +21,10 @@ import type { TreeProps, } from '../types' -export function useTree(props: TreeProps, emit) { +export function useTree( + props: TreeProps, + emit: SetupContext['emit'] +) { const expandedKeySet = ref>(new Set(props.defaultExpandedKeys)) const currentKey = ref() const tree = shallowRef() @@ -213,7 +218,7 @@ export function useTree(props: TreeProps, emit) { } } - function handleNodeCheck(node: TreeNode, checked: boolean) { + function handleNodeCheck(node: TreeNode, checked: CheckboxValueType) { toggleCheckbox(node, checked) } @@ -224,7 +229,7 @@ export function useTree(props: TreeProps, emit) { const { treeNodeMap } = tree.value keySet.forEach((key) => { const treeNode = treeNodeMap.get(key) - if (node && node.level === treeNode.level) { + if (node && node.level === treeNode?.level) { keySet.delete(key) } }) diff --git a/packages/components/tree-v2/src/tree-node.vue b/packages/components/tree-v2/src/tree-node.vue index d001ac39ca..10aed03803 100644 --- a/packages/components/tree-v2/src/tree-node.vue +++ b/packages/components/tree-v2/src/tree-node.vue @@ -48,10 +48,8 @@ - diff --git a/packages/components/tree-v2/src/tree.vue b/packages/components/tree-v2/src/tree.vue index f26ebf8743..3c61b041fc 100644 --- a/packages/components/tree-v2/src/tree.vue +++ b/packages/components/tree-v2/src/tree.vue @@ -38,99 +38,84 @@ - diff --git a/packages/components/tree-v2/src/types.ts b/packages/components/tree-v2/src/types.ts index e692e72bc1..b1a9fc7401 100644 --- a/packages/components/tree-v2/src/types.ts +++ b/packages/components/tree-v2/src/types.ts @@ -32,7 +32,7 @@ export interface TreeNode { } export interface TreeContext { - ctx: SetupContext + ctx: Omit, 'expose' | 'attrs'> instance: ComponentInternalInstance props: TreeProps } diff --git a/packages/components/tree-v2/src/virtual-tree.ts b/packages/components/tree-v2/src/virtual-tree.ts index 330a160308..18d1194e0c 100644 --- a/packages/components/tree-v2/src/virtual-tree.ts +++ b/packages/components/tree-v2/src/virtual-tree.ts @@ -4,8 +4,9 @@ import { iconPropType, mutable, } from '@element-plus/utils' +import type { CheckboxValueType } from '@element-plus/components/checkbox' import type { InjectionKey } from 'vue' -import type { TreeNodeData } from '../../tree/src/tree.type' +import type { TreeNodeData } from '@element-plus/components/tree/src/tree.type' import type { CheckedInfo, FilterMethod, @@ -183,6 +184,6 @@ export const treeEmits = { export const treeNodeEmits = { click: (node: TreeNode, e: MouseEvent) => !!(node && e), toggle: (node: TreeNode) => !!node, - check: (node: TreeNode, checked: boolean) => + check: (node: TreeNode, checked: CheckboxValueType) => node && typeof checked === 'boolean', }