This commit is contained in:
rizky 2024-04-14 17:33:49 -07:00
parent 532fa0b7b0
commit 9cde4fb165
6 changed files with 79 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import { FMLocal, FieldLocal } from "../typings";
import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem"; import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem";
import { fieldMapping } from "./mapping"; import { fieldMapping } from "./mapping";
import { FieldLoading } from "./raw/FieldLoading"; import { FieldLoading } from "./raw/FieldLoading";
import { TypeCustom } from "./type/TypeCustom";
const modify = { const modify = {
timeout: null as any, timeout: null as any,
@ -28,7 +29,7 @@ export const FieldInput: FC<{
); );
let found = null as any; let found = null as any;
if (childs && childs.length > 0) { if (childs && childs.length > 0 && field.type !== "custom") {
for (const child of childs) { for (const child of childs) {
const mp = (fieldMapping as any)[field.type]; const mp = (fieldMapping as any)[field.type];
if (child.component?.id === mp.id) { if (child.component?.id === mp.id) {
@ -65,7 +66,7 @@ export const FieldInput: FC<{
} }
useEffect(() => { useEffect(() => {
if (isEditor && !found) { if (isEditor && !found && field.type !== "custom") {
genFieldMitem({ _meta, _item, _sync, field, fm }); genFieldMitem({ _meta, _item, _sync, field, fm });
} }
}, []); }, []);
@ -100,16 +101,22 @@ export const FieldInput: FC<{
) : ( ) : (
<div <div
className={cx( className={cx(
"field-inner c-flex-1 c-flex c-items-center c-justify-center", "field-inner c-flex-1 c-flex c-items-center",
field.disabled && "c-pointer-events-none" field.disabled && "c-pointer-events-none"
)} )}
> >
{field.type === "custom" ? (
<TypeCustom fm={fm} field={field} />
) : (
<>
{!found && <FieldLoading />} {!found && <FieldLoading />}
{found && ( {found && (
<PassProp field={field} fm={fm}> <PassProp field={field} fm={fm}>
{found} {found}
</PassProp> </PassProp>
)} )}
</>
)}
</div> </div>
)} )}
{suffix && <></>} {suffix && <></>}

View File

@ -0,0 +1,29 @@
import { FC, isValidElement } from "react";
import { FMLocal, FieldLocal } from "../../typings";
import { useLocal } from "@/utils/use-local";
import { FieldTypeText } from "./TypeText";
export const TypeCustom: FC<{ field: FieldLocal; fm: FMLocal }> = ({
field,
fm,
}) => {
const local = useLocal({ custom: null as any }, async () => {
if (field.custom) {
local.custom = await field.custom();
local.render();
}
});
let el = null as any;
if (local.custom) {
if (isValidElement(local.custom)) {
el = local.custom;
} else {
if (local.custom.field === "text") {
el = <FieldTypeText field={field} fm={fm} prop={local.custom} />;
}
}
}
return <>{el}</>;
};

View File

@ -17,6 +17,7 @@ export const FieldTypeText: FC<{
field.prop = prop; field.prop = prop;
return ( return (
<>
<input <input
type={prop.type} type={prop.type}
onChange={(ev) => { onChange={(ev) => {
@ -36,5 +37,6 @@ export const FieldTypeText: FC<{
field.render(); field.render();
}} }}
/> />
</>
); );
}; };

View File

@ -1,5 +1,5 @@
import { GFCol } from "@/gen/utils"; import { GFCol } from "@/gen/utils";
import { ReactNode } from "react"; import { ReactElement, ReactNode } from "react";
import { FieldOptions } from "../form-old/type"; import { FieldOptions } from "../form-old/type";
import { FormHook } from "../form-old/utils/utils"; import { FormHook } from "../form-old/utils/utils";
import { editorFormData } from "./utils/ed-data"; import { editorFormData } from "./utils/ed-data";
@ -56,6 +56,7 @@ export type FieldProp = {
_meta: any; _meta: any;
_item: any; _item: any;
_sync: any; _sync: any;
custom?: () => Promise<CustomField>;
}; };
export type FMInternal = { export type FMInternal = {
@ -112,6 +113,7 @@ export type FieldInternal<T extends FieldProp["type"]> = {
required_msg: FieldProp["required_msg"]; required_msg: FieldProp["required_msg"];
col?: GFCol; col?: GFCol;
Child: () => ReactNode; Child: () => ReactNode;
custom: FieldProp["custom"];
input: Record<string, any> & { input: Record<string, any> & {
render: () => void; render: () => void;
}; };
@ -196,3 +198,12 @@ export const fieldType = (item: any, meta: any, fm: FMLocal) => {
`; `;
} }
}; };
export type CustomField =
| { field: "text"; type: "text" | "password" | "number" }
| { field: "relation"; type: "has-many" | "has-one" };
export const FieldTypeCustom = `type CustomField =
{ field: "text", type: "text" | "password" | "number" }
| { field: "relation", type: "has-many" | "has-one" }
`;

View File

@ -19,6 +19,7 @@ export const useField = (arg: FieldProp) => {
prefix: arg.prefix, prefix: arg.prefix,
suffix: arg.suffix, suffix: arg.suffix,
width: arg.width, width: arg.width,
custom: arg.custom,
required: arg.required === "y", required: arg.required === "y",
required_msg: arg.required_msg, required_msg: arg.required_msg,
disabled: arg.disabled === "y", disabled: arg.disabled === "y",

View File

@ -1,3 +1,4 @@
export { FieldTypeCustom } from "@/comps/form/typings";
export { FieldTypeText } from "./comps/form/field/type/TypeText"; export { FieldTypeText } from "./comps/form/field/type/TypeText";
export { FieldTypeRelation } from "./comps/form/field/type/TypeRelation"; export { FieldTypeRelation } from "./comps/form/field/type/TypeRelation";
export { Form } from "@/comps/form/Form"; export { Form } from "@/comps/form/Form";