Skip to content

Commit

Permalink
revert refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Oct 15, 2024
1 parent d7f5671 commit 982e986
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/utils/strategyUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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;
}
47 changes: 21 additions & 26 deletions src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import type { DataNode, FieldNames, SafeKey } from '../interface';
import type { DefaultOptionType, InternalFieldName } from '../TreeSelect';

export function toArray<T>(value: T | T[]): T[] {
if (Array.isArray(value)) {
return value;
}
return value !== undefined ? [value] : [];
}

export function fillFieldNames(fieldNames?: FieldNames) {
export const toArray = <T>(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'],
value: value || 'value',
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;

0 comments on commit 982e986

Please sign in to comment.