diff --git a/comps/form/Form.tsx b/comps/form/Form.tsx index e19c9dc..860b54b 100755 --- a/comps/form/Form.tsx +++ b/comps/form/Form.tsx @@ -113,6 +113,7 @@ export const Form: FC = (props) => { className={cx( "form c-flex-1 c-w-full c-h-full c-relative c-overflow-auto" )} + action="#" > {toaster_el && createPortal(, toaster_el)}
= (props) => { ); })} +
); diff --git a/comps/form/field/FieldInput.tsx b/comps/form/field/FieldInput.tsx index eb75d51..5e44c36 100755 --- a/comps/form/field/FieldInput.tsx +++ b/comps/form/field/FieldInput.tsx @@ -1,11 +1,10 @@ -import { Skeleton } from "@/comps/ui/skeleton"; +import { createItem } from "@/gen/utils"; import get from "lodash.get"; import { FC, useEffect } from "react"; import { FMLocal, FieldLocal } from "../typings"; -import { fieldMapping } from "./mapping"; -import { Loader2 } from "lucide-react"; import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem"; -import { createItem } from "@/gen/utils"; +import { fieldMapping } from "./mapping"; +import { FieldLoading } from "./raw/FieldLoading"; const modify = { timeout: null as any, @@ -97,28 +96,7 @@ export const FieldInput: FC<{ > {prefix && <>} {fm.status === "loading" ? ( -
-
- - -
- -
+ ) : (
- {!found && } + {!found && } {found && ( {found} diff --git a/comps/form/field/raw/FieldLoading.tsx b/comps/form/field/raw/FieldLoading.tsx new file mode 100644 index 0000000..c10c5f2 --- /dev/null +++ b/comps/form/field/raw/FieldLoading.tsx @@ -0,0 +1,28 @@ +import { Skeleton } from "@/comps/ui/skeleton"; + +export const FieldLoading = () => { + return ( +
+
+ + +
+ +
+ ); +}; diff --git a/comps/form/field/type/TypeRelation.tsx b/comps/form/field/type/TypeRelation.tsx index cdf1207..c8bd2c1 100755 --- a/comps/form/field/type/TypeRelation.tsx +++ b/comps/form/field/type/TypeRelation.tsx @@ -2,7 +2,7 @@ import { useLocal } from "@/utils/use-local"; import { FC, useEffect } from "react"; import { FMLocal, FieldLocal } from "../../typings"; import { OptionItem, RawDropdown } from "../raw/Dropdown"; -import { Loader2 } from "lucide-react"; +import { FieldLoading } from "../raw/FieldLoading"; export type PropTypeRelation = { type: "has-one" | "has-many"; @@ -68,9 +68,7 @@ export const FieldTypeRelation: FC<{ return ( <> {field.status === "loading" ? ( -
- -
+ ) : ( { field.focused = true; diff --git a/comps/form/typings.ts b/comps/form/typings.ts index 9695043..53965b8 100755 --- a/comps/form/typings.ts +++ b/comps/form/typings.ts @@ -9,7 +9,7 @@ import { PropTypeRelation } from "./field/type/TypeRelation"; export type FMProps = { on_init: (arg: { fm: FMLocal; submit: any; reload: any }) => any; on_load: (arg: { fm: FMLocal }) => any; - on_submit: (arg: { form: any; error: any }) => Promise; + on_submit: (arg: { fm: FMLocal; form: any; error: any }) => Promise; body: any; form: FormHook; PassProp: any; @@ -68,6 +68,7 @@ export type FMInternal = { fields: Record; field_def: Record; error: { + readonly object: Record; readonly list: { name: string; error: string[] }[]; set: (name: string, error: string[]) => void; get: (name: string) => string[]; diff --git a/comps/form/utils/error.ts b/comps/form/utils/error.ts index fac962f..79b7e53 100755 --- a/comps/form/utils/error.ts +++ b/comps/form/utils/error.ts @@ -12,6 +12,13 @@ export const formError = (fm: FMLocal) => { return res; }, + get object() { + const result: any = {}; + Object.entries(this._internal).map(([name, error]) => { + result[name] = error.join("\n"); + }); + return {}; + }, clear(name) { if (name) delete this._internal[name]; else this._internal = {}; diff --git a/comps/form/utils/init.tsx b/comps/form/utils/init.tsx index d1f53d5..e8943d5 100755 --- a/comps/form/utils/init.tsx +++ b/comps/form/utils/init.tsx @@ -86,6 +86,12 @@ export const formInit = (fm: FMLocal, props: FMProps) => { return promise; }; - fm.submit = async () => {}; - fm.props.on_init({ fm, submit: fm.submit, reload: fm.reload }); + fm.submit = async () => { + if (typeof fm.props.on_submit === "function") { + fm.props.on_submit({ fm, form: fm.data, error: fm.error.object }); + } + }; + if (typeof fm.props.on_init === "function") { + fm.props.on_init({ fm, submit: fm.submit, reload: fm.reload }); + } }; diff --git a/gen/gen_form/gen_form.ts b/gen/gen_form/gen_form.ts index ab06f85..501e231 100755 --- a/gen/gen_form/gen_form.ts +++ b/gen/gen_form/gen_form.ts @@ -24,6 +24,9 @@ export const gen_form = async (modify: (data: any) => void, data: any) => { }; for (const r of f.relation.fields) { select[f.name].select[r.name] = true; + if (r.is_pk) { + pks[f.name] = r.name; + } } } diff --git a/gen/gen_form/on_load.ts b/gen/gen_form/on_load.ts index 523eece..1738405 100755 --- a/gen/gen_form/on_load.ts +++ b/gen/gen_form/on_load.ts @@ -56,20 +56,6 @@ async (opt) => { select: ${JSON.stringify(select, null, 2).split("\n").join("\n ")}, }); - if (item){ - for (const [k, v] of Object.entries(item)) { - ${Object.entries(pks) - .map(([k, v]) => { - return `\ - if (k === "${k}" && !(v as any)?.connect) { - if (v?.["${v}"]) item[k] = { connect: { ${v}: v?.["${v}"] } } as any; - else delete item[k]; - }`; - }) - .join("\n")} - } - } - ${opt?.after_load ? opt?.after_load : ""} return item; diff --git a/gen/gen_form/on_submit.ts b/gen/gen_form/on_submit.ts index 52be7ae..f8e9e58 100755 --- a/gen/gen_form/on_submit.ts +++ b/gen/gen_form/on_submit.ts @@ -29,7 +29,22 @@ async ({ form, error }: IForm) => { try { const data = { ...form }; delete data.${id}; - + + if (data) { + const pks = ${JSON.stringify(pks)}; + for (const [k, v] of Object.entries(pks)) { + if (typeof data[k] === 'object') { + if (data[k][v]) { + data[k] = { + connect: { + [v]: data[k][v] + } + } + } + } + } + } + if (form.${id}) { await db.${table}.update({ where: { diff --git a/gen/gen_relation/new_field.ts b/gen/gen_relation/new_field.ts index a19af35..558a9d9 100755 --- a/gen/gen_relation/new_field.ts +++ b/gen/gen_relation/new_field.ts @@ -38,6 +38,16 @@ export const newField = (arg: GFCol) => {
`, jsBuilt: `render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, "") }), item["${arg.name}"]));`, }, + dim: { + h: "full", + w: "fit", + }, + padding: { + l: 0, + b: 0, + t: 0, + r: 10, + }, layout: { dir: "row", align: "left", diff --git a/gen/utils.ts b/gen/utils.ts index 34f3529..dbd6332 100755 --- a/gen/utils.ts +++ b/gen/utils.ts @@ -66,6 +66,7 @@ type SimplifiedItem = { jsBuilt: string; }; padding?: any; + dim?: any; layout?: any; }; @@ -121,10 +122,12 @@ export const createItem = (arg: SimplifiedItem): any => { return { id: createId(), - dim: { - h: "full", - w: "full", - }, + dim: arg.dim + ? arg.dim + : { + h: "full", + w: "full", + }, padding: arg.padding, layout: arg.layout, name: arg.name || "item",