nicecode-v2/packages/biz/src/treeTransfer/treeTransferHelper.ts

63 lines
1.7 KiB
TypeScript

export const isChecked = (selectedKeys: React.Key[], eventKey: React.Key) =>
selectedKeys.includes(eventKey);
function isObject(value: any) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
/**
* 通过子元素找到父级节点
* @param objects
* @param element
* @returns
*/
export const findParentByChild = (objects: any[], propertyValue: string | number, propertyKey: string = 'key') => {
for (let i = 0; i < objects.length; i++) {
const obj = objects[i];
if (obj[propertyKey] === propertyValue) {
return obj
} else if (typeof obj === 'object') {
const found = findParentByChild(Object.values(obj), propertyValue);
if (found) {
return obj
}
}
}
return null; // 如果找不到包含具有指定属性的子对象的父对象,返回 null
}
export const getAllRootKeyById = (val: string | number, list: any[], key: string = 'key') => {
let keys: any[] = []
const findParentByChild = (propertyValue: string | number, objects: any[], propertyKey: string) => {
for (let i = 0; i < objects.length; i++) {
const obj = objects[i];
if (obj[propertyKey] === propertyValue) {
console.log('obj', obj)
return obj
} else if (typeof obj === 'object') {
const found = findParentByChild(propertyValue, Object.values(obj), propertyKey);
if (found) {
if (isObject(found)) {
keys.push(found.key)
}
return obj
}
}
}
return null; // 如果找不到包含具有指定属性的子对象的父对象,返回 null
}
const data = findParentByChild(val, list, key)
data.key && keys.push(data.key)
return {
root: data,
keys
}
}