-
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.
refactor: refactor utils and hooks (#585)
- Loading branch information
Showing
5 changed files
with
87 additions
and
107 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
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
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,49 +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) { | ||
const { label, value, children } = fieldNames || {}; | ||
|
||
const mergedValue = value || 'value'; | ||
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: mergedValue, | ||
key: mergedValue, | ||
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; | ||
|
||
/** Loop fetch all the keys exist in the tree */ | ||
export function getAllKeys(treeData: DefaultOptionType[], fieldNames: InternalFieldName) { | ||
export const getAllKeys = ( | ||
treeData: DefaultOptionType[], | ||
fieldNames: InternalFieldName, | ||
): SafeKey[] => { | ||
const keys: SafeKey[] = []; | ||
|
||
function dig(list: DefaultOptionType[]) { | ||
const dig = (list: DefaultOptionType[]): void => { | ||
list.forEach(item => { | ||
const children = item[fieldNames.children]; | ||
if (children) { | ||
keys.push(item[fieldNames.value]); | ||
dig(children); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
dig(treeData); | ||
|
||
return keys; | ||
} | ||
}; | ||
|
||
export function isNil(val: any) { | ||
return val === null || val === undefined; | ||
} | ||
export const isNil = (val: any): boolean => val === null || val === undefined; |