From 69753fe0e09ce5c068bbb918b0f9c0acfaf1d67c Mon Sep 17 00:00:00 2001 From: rizky Date: Fri, 12 Apr 2024 09:29:37 -0700 Subject: [PATCH] fix data input --- comps/custom/Breadcrumb.tsx | 38 ++- comps/form/Form.tsx | 10 +- comps/form/field/Field.tsx | 37 ++- comps/form/field/FieldInput.tsx | 110 +++++++ comps/form/field/Input.tsx | 71 ----- comps/form/field/Label.tsx | 31 +- comps/form/field/mapping.ts | 5 + .../InputText.tsx => type/TypeText.tsx} | 15 +- comps/form/typings.ts | 8 +- comps/form/utils/error.ts | 3 + comps/form/utils/gen-mitem.ts | 24 ++ comps/form/utils/use-field.tsx | 7 +- comps/form/utils/validate.ts | 14 +- data.ts | 1 + gen/gen_form/gen_form.ts | 65 ++-- gen/gen_form/new_field.ts | 300 +----------------- gen/utils.ts | 14 +- 17 files changed, 295 insertions(+), 458 deletions(-) create mode 100755 comps/form/field/FieldInput.tsx delete mode 100755 comps/form/field/Input.tsx create mode 100755 comps/form/field/mapping.ts rename comps/form/field/{input/InputText.tsx => type/TypeText.tsx} (73%) create mode 100755 comps/form/utils/gen-mitem.ts diff --git a/comps/custom/Breadcrumb.tsx b/comps/custom/Breadcrumb.tsx index 666e635..38cf362 100755 --- a/comps/custom/Breadcrumb.tsx +++ b/comps/custom/Breadcrumb.tsx @@ -29,27 +29,33 @@ export const Breadcrumb: FC = (_arg) => { if (_arg.value) { local.list = _arg.value; + local.status = "ready"; } - useEffect(() => { - (async () => { - if (_arg.value) { - local.status = "ready"; - local.render(); - return; - } - if (local.status === "init" && typeof on_load === "function") { - local.status = "loading"; - local.render(); + if (local.status === "init") { + let should_load = true; - local.list = await on_load(); - if (isEditor) breadcrumbData[_arg.item.id] = local.list; + if (isEditor && breadcrumbData[_arg.item.id]) { + local.list = breadcrumbData[_arg.item.id]; + local.status = "ready"; + should_load = false; + } + if (should_load && typeof on_load === "function") { + const callback = (res: any) => { + local.list = res; + breadcrumbData[_arg.item.id] = res; local.status = "ready"; - local.render(); - } - })(); - }, [on_load]); + }; + const res = on_load(); + if (res instanceof Promise) { + res.then((res) => { + callback(res); + local.render(); + }); + } else callback(res); + } + } if (isEditor && local.status !== "ready" && breadcrumbData[_arg.item.id]) { local.list = breadcrumbData[_arg.item.id]; diff --git a/comps/form/Form.tsx b/comps/form/Form.tsx index 8abd244..ae80dea 100755 --- a/comps/form/Form.tsx +++ b/comps/form/Form.tsx @@ -127,14 +127,10 @@ export const Form: FC = (props) => { {fm.status !== "init" && fm.size.width > 0 && childs.map((child, idx) => { - const props = {} as any; - for (const [k, v] of Object.entries(child.component.props)) { - props[k] = getProp(child, k); - } return ( - - - + + {child} + ); })} diff --git a/comps/form/field/Field.tsx b/comps/form/field/Field.tsx index 9633e03..590e5ba 100755 --- a/comps/form/field/Field.tsx +++ b/comps/form/field/Field.tsx @@ -2,7 +2,7 @@ import { FC, useEffect } from "react"; import { FieldProp } from "../typings"; import { useField } from "../utils/use-field"; import { validate } from "../utils/validate"; -import { FieldInput } from "./Input"; +import { FieldInput } from "./FieldInput"; import { Label } from "./Label"; export const Field: FC = (arg) => { @@ -15,10 +15,16 @@ export const Field: FC = (arg) => { useEffect(() => { validate(field, fm); fm.events.on_change(field.name, fm.data[field.name]); + fm.render(); }, [fm.data[field.name]]); if (field.status === "init" && !isEditor) return null; + const errors = fm.error.get(field.name); + + const props = { ...arg.props }; + delete props.className; + return (