From fc8d54816aa5b77e31aa99db0052f72f7677daa5 Mon Sep 17 00:00:00 2001 From: rizky Date: Sat, 13 Apr 2024 18:40:14 -0700 Subject: [PATCH] fix --- comps/form/field/raw/Dropdown.tsx | 1 + comps/form/field/raw/FieldLoading.tsx | 0 comps/form/field/type/TypeRelation.tsx | 16 ++++++++++++---- comps/md/MDAction.tsx | 20 ++++++-------------- gen/gen_form/new_field.ts | 1 + gen/gen_relation/gen_relation.ts | 18 ++++++++++++++++++ gen/gen_relation/new_field.ts | 2 +- 7 files changed, 39 insertions(+), 19 deletions(-) mode change 100644 => 100755 comps/form/field/raw/FieldLoading.tsx diff --git a/comps/form/field/raw/Dropdown.tsx b/comps/form/field/raw/Dropdown.tsx index 8d4c08d..0e0e607 100755 --- a/comps/form/field/raw/Dropdown.tsx +++ b/comps/form/field/raw/Dropdown.tsx @@ -101,6 +101,7 @@ export const RawDropdown: FC<{ local.render(); setTimeout(() => { local.input.el?.focus(); + local.input.el?.select(); }); }} ref={(el) => { diff --git a/comps/form/field/raw/FieldLoading.tsx b/comps/form/field/raw/FieldLoading.tsx old mode 100644 new mode 100755 diff --git a/comps/form/field/type/TypeRelation.tsx b/comps/form/field/type/TypeRelation.tsx index c8bd2c1..7415cdd 100755 --- a/comps/form/field/type/TypeRelation.tsx +++ b/comps/form/field/type/TypeRelation.tsx @@ -7,6 +7,7 @@ import { FieldLoading } from "../raw/FieldLoading"; export type PropTypeRelation = { type: "has-one" | "has-many"; on_load: (opt: { value?: any }) => Promise<{ items: any[]; pk: string }>; + label: (item: any, pk: string) => string; }; export const FieldTypeRelation: FC<{ field: FieldLocal; @@ -44,15 +45,22 @@ export const FieldTypeRelation: FC<{ if (input.list && input.pk && input.list.length) { for (const item of input.list) { if (typeof item !== "object") continue; - const label: string[] = []; + let label = ""; - for (const [k, v] of Object.entries(item)) { - if (k !== input.pk) label.push(v as any); + if (typeof prop.label === "function") { + label = prop.label(item, input.pk); + } else { + const label_arr: string[] = []; + + for (const [k, v] of Object.entries(item)) { + if (k !== input.pk) label_arr.push(v as any); + } + label = label_arr.join(" "); } list.push({ value: item[input.pk], - label: label.join(" "), + label, el: {child}, }); } diff --git a/comps/md/MDAction.tsx b/comps/md/MDAction.tsx index c86c360..a3a6801 100755 --- a/comps/md/MDAction.tsx +++ b/comps/md/MDAction.tsx @@ -3,7 +3,11 @@ import { FC, Fragment, isValidElement } from "react"; import { getProp } from "./utils/get-prop"; import { MDActions, MDLocal } from "./utils/typings"; -export const MDAction: FC<{ md: MDLocal }> = ({ md }) => { +export const MDAction: FC<{ md: MDLocal; PassProp: any; child: any }> = ({ + md, + PassProp, + child, +}) => { const local = useLocal({ last_render: Date.now(), }); @@ -30,19 +34,7 @@ export const MDAction: FC<{ md: MDLocal }> = ({ md }) => { return {e}; } if (typeof e === "object" && e.label) { - return ( -
{ - if (e.onClick && !isEditor) e.onClick(ev); - }} - > - {e.label} -
- ); + return {child}; } })} diff --git a/gen/gen_form/new_field.ts b/gen/gen_form/new_field.ts index f59f897..9aeb80a 100755 --- a/gen/gen_form/new_field.ts +++ b/gen/gen_form/new_field.ts @@ -59,6 +59,7 @@ export const newField = async (arg: GFCol) => { return { items: [], pk: "" }; }`, ], + label: [`() => {}`], gen_table: arg.relation.to.table, gen_fields: [value, value], child: {}, diff --git a/gen/gen_relation/gen_relation.ts b/gen/gen_relation/gen_relation.ts index 29452c1..0a8706d 100755 --- a/gen/gen_relation/gen_relation.ts +++ b/gen/gen_relation/gen_relation.ts @@ -43,6 +43,24 @@ export const gen_relation = async (modify: (data: any) => void, data: any) => { code.on_load = result["on_load"].value; } + if (data["label"]) { + result["label"] = data["label"]; + result["label"].value = `\ +(item:any, pk:string) => { + return \`${Object.entries(select) + .filter(([k, v]) => { + if (typeof v !== "object" && k !== pk?.name) { + return true; + } + }) + .map(([name]) => { + return `\${item.${name}}`; + }) + .join(" ")}\` +}`; + code.on_load = result["on_load"].value; + } + const res = await codeBuild(code); for (const [k, v] of Object.entries(res)) { result[k].valueBuilt = v[1]; diff --git a/gen/gen_relation/new_field.ts b/gen/gen_relation/new_field.ts index 558a9d9..149f0bc 100755 --- a/gen/gen_relation/new_field.ts +++ b/gen/gen_relation/new_field.ts @@ -46,7 +46,7 @@ export const newField = (arg: GFCol) => { l: 0, b: 0, t: 0, - r: 10, + r: 5, }, layout: { dir: "row",