-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |