diff --git a/comps/form/field/type/TypeDropdown.tsx b/comps/form/field/type/TypeDropdown.tsx index 573e3ba..59004e0 100755 --- a/comps/form/field/type/TypeDropdown.tsx +++ b/comps/form/field/type/TypeDropdown.tsx @@ -26,7 +26,6 @@ export const TypeDropdown: FC<{ options.then((res) => { if (Array.isArray(res)) { const list: any = res.map((e: any) => { - console.log({e}) return { label: arg.opt_get_label(e), value: e.value, diff --git a/comps/form/gen/gen-label.ts b/comps/form/gen/gen-label.ts index 2cb1993..5131de7 100755 --- a/comps/form/gen/gen-label.ts +++ b/comps/form/gen/gen-label.ts @@ -1,7 +1,7 @@ export const gen_label = ({ pk, table, - select + select, }: { pk: string; table: string; @@ -16,21 +16,22 @@ export const gen_label = ({ } return `\ - (row: { value: string; label: string; item?: any }) => { - const cols = ${JSON.stringify(cols)}; - const getLabel = (data: any) => { - const result = []; - cols.map((e) => { - if (data[e]) { - result.push(data[e]); - } - }); - return result.join(" - "); - }; - if (isEditor) { - return row.label; +(row: { value: string; label: string; item?: any }) => { + const cols = ${JSON.stringify(cols)}; + + if (isEditor) { + return row.label; + } + const result = []; + if (!!row.item && !Array.isArray(row.item)) { + cols.map((e) => { + if (row.item[e]) { + result.push(row.item[e]); } - return getLabel(row.item); - } + }); + return result.join(" - "); + } + return row.label; +} `; }; diff --git a/comps/form/gen/on_load_rel.ts b/comps/form/gen/on_load_rel.ts index 370e716..727fb77 100755 --- a/comps/form/gen/on_load_rel.ts +++ b/comps/form/gen/on_load_rel.ts @@ -29,49 +29,49 @@ export const on_load_rel = ({ } return `\ - (arg: { - reload: () => Promise; - orderBy?: Record; - paging: { take: number; skip: number }; - mode: 'count' | 'query' - }) => { - if (isEditor) return [${JSON.stringify(sample)}]; - - return new Promise(async (done) => { - if (arg.mode === 'count') { - return await db.${table}.count(); +(arg: { + reload: () => Promise; + orderBy?: Record; + paging: { take: number; skip: number }; + mode: 'count' | 'query' +}) => { + if (isEditor) return [${JSON.stringify(sample)}]; + + return new Promise(async (done) => { + if (arg.mode === 'count') { + return await db.${table}.count(); + } + + const items = await db.${table}.findMany({ + select: ${JSON.stringify(select)}, + orderBy: arg.orderBy || { + ${pk}: "desc" + }, + ...arg.paging, + }); + if(items.length){ + const cols = ${JSON.stringify(cols)}; + const getLabel = (data: any) => { + const result = []; + cols.map((e) => { + if(data[e]){ + result.push(data[e]); + } + }) + return result.join(" - "); } - - const items = await db.${table}.findMany({ - select: ${JSON.stringify(select)}, - orderBy: arg.orderBy || { - ${pk}: "desc" - }, - ...arg.paging, - }); - if(items.length){ - const cols = ${JSON.stringify(cols)}; - const getLabel = (data: any) => { - const result = []; - cols.map((e) => { - if(data[e]){ - result.push(data[e]); - } - }) - return result.join(" - "); - } - done(items.map((e) => { - return { - value: e.${pk}, - label: getLabel(e), - data: e, - } - })) - } else { - done([]) - } - }) - } + done(items.map((e) => { + return { + value: e.${pk}, + label: getLabel(e), + data: e, + } + })) + } else { + done([]) + } + }) +} `; }; \ No newline at end of file diff --git a/comps/form/utils/init.tsx b/comps/form/utils/init.tsx index 2863a3d..2cf7af4 100755 --- a/comps/form/utils/init.tsx +++ b/comps/form/utils/init.tsx @@ -54,13 +54,20 @@ export const formInit = (fm: FMLocal, props: FMProps) => { } } if (should_load) { - const res = on_load({ fm }); - - if (typeof res === "object" && res instanceof Promise) { - fm.data = await res; + const on_load_result = on_load({ fm }); + let result = undefined; + if ( + typeof on_load_result === "object" && + on_load_result instanceof Promise + ) { + result = await on_load_result; } else { - fm.data = res; + result = on_load_result; } + if (!!result) { + fm.data = result; + } + if (!fm.data) { fm.data = {}; }