From 982e9864b1d3045bfb657eddb075ffac0092b3c5 Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 15 Oct 2024 12:26:28 +0800 Subject: [PATCH] revert refactor --- src/utils/strategyUtil.ts | 20 ++++++++--------- src/utils/valueUtil.ts | 47 +++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/utils/strategyUtil.ts b/src/utils/strategyUtil.ts index 5258a5fa..bf1b3523 100644 --- a/src/utils/strategyUtil.ts +++ b/src/utils/strategyUtil.ts @@ -17,8 +17,8 @@ export function formatStrategyValues( ): SafeKey[] { const valueSet = new Set(values); - const strategyHandlers = { - [SHOW_CHILD]: (key: SafeKey) => { + if (strategy === SHOW_CHILD) { + return values.filter(key => { const entity = keyEntities[key]; return ( !entity || @@ -28,14 +28,14 @@ export function formatStrategyValues( ({ node }) => isCheckDisabled(node) || valueSet.has(node[fieldNames.value]), ) ); - }, - [SHOW_PARENT]: (key: SafeKey) => { + }); + } + if (strategy === SHOW_PARENT) { + return values.filter(key => { const entity = keyEntities[key]; - const parent = entity?.parent; + const parent = entity ? entity.parent : null; return !parent || isCheckDisabled(parent.node) || !valueSet.has(parent.key); - }, - [SHOW_ALL]: () => true, - }; - - return values.filter(strategyHandlers[strategy] || strategyHandlers[SHOW_ALL]); + }); + } + return values; } diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index a66ba045..047e81e1 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -1,14 +1,10 @@ import type { DataNode, FieldNames, SafeKey } from '../interface'; import type { DefaultOptionType, InternalFieldName } from '../TreeSelect'; -export function toArray(value: T | T[]): T[] { - if (Array.isArray(value)) { - return value; - } - return value !== undefined ? [value] : []; -} - -export function fillFieldNames(fieldNames?: FieldNames) { +export const toArray = (value: T | T[]): T[] => + Array.isArray(value) ? value : value !== undefined ? [value] : []; + +export const fillFieldNames = (fieldNames?: FieldNames) => { const { label, value, children } = fieldNames || {}; return { _title: label ? [label] : ['title', 'label'], @@ -16,31 +12,30 @@ export function fillFieldNames(fieldNames?: FieldNames) { key: value || 'value', children: children || 'children', }; -} +}; -export function isCheckDisabled(node: DataNode) { - return !node || node.disabled || node.disableCheckbox || node.checkable === false; -} +export const isCheckDisabled = (node: DataNode): boolean => + !node || node.disabled || node.disableCheckbox || node.checkable === false; -/** 递归获取树中所有存在的键 */ -export function getAllKeys( +export const getAllKeys = ( treeData: DefaultOptionType[], fieldNames: InternalFieldName, -): SafeKey[] { +): SafeKey[] => { const keys: SafeKey[] = []; - const traverseTree = (nodes: DefaultOptionType[]): void => { - nodes.forEach(node => { - keys.push(node[fieldNames.value]); - const children = node[fieldNames.children]; - if (Array.isArray(children)) { - traverseTree(children); + + const dig = (list: DefaultOptionType[]): void => { + list.forEach(item => { + const children = item[fieldNames.children]; + if (children) { + keys.push(item[fieldNames.value]); + dig(children); } }); }; - traverseTree(treeData); + + dig(treeData); + return keys; -} +}; -export function isNil(val: any) { - return val === null || val === undefined; -} +export const isNil = (val: any): boolean => val === null || val === undefined;