From 72bcba5bb5a688eaffb30a3cdecfcc3de1a821d9 Mon Sep 17 00:00:00 2001 From: rizky Date: Fri, 12 Apr 2024 19:33:11 -0700 Subject: [PATCH] fix --- comps/form/field/Field.tsx | 1 + comps/form/field/FieldInput.tsx | 23 ++++++++++++++++++++++- comps/form/field/mapping.ts | 11 +++++++++-- comps/form/typings.ts | 1 + comps/form/utils/gen-mitem.ts | 9 +++++++++ gen/gen_form/new_field.ts | 4 +++- gen/utils.ts | 23 +++++++++++++---------- 7 files changed, 58 insertions(+), 14 deletions(-) diff --git a/comps/form/field/Field.tsx b/comps/form/field/Field.tsx index 590e5ba..e2b1b1d 100755 --- a/comps/form/field/Field.tsx +++ b/comps/form/field/Field.tsx @@ -53,6 +53,7 @@ export const Field: FC = (arg) => { child={arg.child} _meta={arg._meta} _item={arg._item} + _sync={arg._sync} /> {field.desc && (
0 && "c-pb-1")}> diff --git a/comps/form/field/FieldInput.tsx b/comps/form/field/FieldInput.tsx index 844fcec..8c62128 100755 --- a/comps/form/field/FieldInput.tsx +++ b/comps/form/field/FieldInput.tsx @@ -4,7 +4,8 @@ import { FC, useEffect } from "react"; import { FMLocal, FieldLocal } from "../typings"; import { fieldMapping } from "./mapping"; import { Loader2 } from "lucide-react"; -import { genFieldMitem } from "../utils/gen-mitem"; +import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem"; +import { createItem } from "@/gen/utils"; const modify = { timeout: null as any, @@ -17,6 +18,7 @@ export const FieldInput: FC<{ child: any; _item: any; _meta: any; + _sync: (mitem: any, item: any) => void; }> = ({ field, fm, PassProp, child, _meta, _item }) => { const prefix = typeof field.prefix === "function" ? field.prefix() : null; const suffix = typeof field.suffix === "function" ? field.suffix() : null; @@ -31,6 +33,25 @@ export const FieldInput: FC<{ for (const child of childs) { if (child.component?.id === fieldMapping[field.type].id) { found = child; + + const item = createItem({ + component: { id: "--", props: fieldMapping[field.type].props }, + }); + + const props = found.component.props; + let should_update = false; + for (const [k, v] of Object.entries(item.component.props) as any) { + if (props[k] && props[k].valueBuilt === v.valueBuilt) { + continue; + } else { + props[k] = v; + should_update = true; + } + } + + if (should_update) { + updateFieldMItem(_meta, found); + } } } } diff --git a/comps/form/field/mapping.ts b/comps/form/field/mapping.ts index 1217ec8..7e28ed0 100755 --- a/comps/form/field/mapping.ts +++ b/comps/form/field/mapping.ts @@ -1,5 +1,12 @@ import { FieldProp } from "../typings"; -export const fieldMapping: Record = { - text: { id: "ca7ac237-8f22-4492-bb9d-4b715b1f5c25" }, +export const fieldMapping: Record< + FieldProp["type"], + { id: string; props: any } +> = { + text: { id: "ca7ac237-8f22-4492-bb9d-4b715b1f5c25", props: { type: "text" } }, + number: { + id: "ca7ac237-8f22-4492-bb9d-4b715b1f5c25", + props: { type: "number" }, + }, } as any; diff --git a/comps/form/typings.ts b/comps/form/typings.ts index 5dabacf..b222ff5 100755 --- a/comps/form/typings.ts +++ b/comps/form/typings.ts @@ -53,6 +53,7 @@ export type FieldProp = { width: "auto" | "full" | "½" | "⅓" | "¼"; _meta: any; _item: any; + _sync: any; }; export type FMInternal = { diff --git a/comps/form/utils/gen-mitem.ts b/comps/form/utils/gen-mitem.ts index ce22245..031ea25 100755 --- a/comps/form/utils/gen-mitem.ts +++ b/comps/form/utils/gen-mitem.ts @@ -22,3 +22,12 @@ export const genFieldMitem = (arg: { } } }; +export const updateFieldMItem = (_meta: any, _item: any) => { + const m = _meta[_item.id]; + if (m) { + const mitem = m.mitem; + if (mitem) { + + } + } +}; diff --git a/gen/gen_form/new_field.ts b/gen/gen_form/new_field.ts index 546c52c..5825930 100755 --- a/gen/gen_form/new_field.ts +++ b/gen/gen_form/new_field.ts @@ -40,7 +40,9 @@ export const newField = (arg: GFCol) => { createItem({ component: { id: "ca7ac237-8f22-4492-bb9d-4b715b1f5c25", - props: {}, + props: { + type: "text", + }, }, }), ], diff --git a/gen/utils.ts b/gen/utils.ts index 4aab338..1c4a9b1 100755 --- a/gen/utils.ts +++ b/gen/utils.ts @@ -92,16 +92,19 @@ export const createItem = (arg: SimplifiedItem): any => { valueBuilt: v[1], }; } else { - let newItem = createItem(v); - - component.props[k] = { - meta: { - type: "content-element", - }, - content: newItem, - value: "", - valueBuilt: "", - }; + if ((v as any)?.meta?.type) { + component.props[k] = v; + } else { + let newItem = createItem(v); + component.props[k] = { + meta: { + type: "content-element", + }, + content: newItem, + value: "", + valueBuilt: "", + }; + } } } else { component.props[k] = {