Skip to content

Commit c2fa2be

Browse files
committedDec 19, 2022
feat: 🚀 allow nested tree enum data
1 parent 36d047a commit c2fa2be

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎src/components/ProTable/interface/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BreakPoint, Responsive } from "@/components/Grid/interface";
44
export interface EnumProps {
55
label: string; // 选项框显示的文字
66
value: any; // 选项框值
7+
childrenKey: string; // key to search children
78
disabled?: boolean; // 是否禁用此选项
89
tagType?: string; // 当 tag 为 true 时,此选择会指定 tag 显示类型
910
children?: EnumProps[]; // 为树形选择时,可以通过 children 属性指定子选项

‎src/utils/util.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,17 @@ export function filterEnum(
304304
): string {
305305
const value = searchProps?.value ?? "value";
306306
const label = searchProps?.label ?? "label";
307+
const childrenKey = searchProps?.childrenKey ?? "children";
307308
let filterData: any = {};
308-
if (Array.isArray(enumData)) filterData = enumData.find((item: any) => item[value] === callValue);
309+
if (Array.isArray(enumData)) filterData = findItemNested(enumData, value, callValue, childrenKey);
309310
if (type == "tag") return filterData?.tagType ? filterData.tagType : "";
310311
return filterData ? filterData[label] : "--";
311312
}
313+
314+
export function findItemNested(arr: any, itemKey: string, itemValue: any, nestingKey: string) {
315+
return arr.reduce((a: any, item: any) => {
316+
if (a) return a;
317+
if (item[itemKey] === itemValue) return item;
318+
if (item[nestingKey]) return findItemNested(item[nestingKey], itemKey, itemValue, nestingKey);
319+
}, null);
320+
}

0 commit comments

Comments
 (0)