add fn for field prop

This commit is contained in:
rizrmd 2024-05-16 22:46:10 -07:00
parent 285051b571
commit c09d6e2337
2 changed files with 14 additions and 16 deletions

View File

@ -28,12 +28,12 @@ export type FMProps = {
}; };
export type FieldProp = { export type FieldProp = {
name: string; name: string | (() => string);
label: string; label: string | (() => string);
desc?: string; desc?: string;
props?: any; props?: any;
fm: FMLocal; fm: FMLocal;
type: "text" | "relation" | "switch" | "input" | "single-option"| "multi-option"; type: "text" | "relation" | "switch" | "input" | "single-option" | "multi-option";
// | "number" // | "number"
// | "textarea" // | "textarea"
// | "dropdown" // | "dropdown"
@ -45,7 +45,7 @@ export type FieldProp = {
// | "slider" // | "slider"
// | "master-link" // | "master-link"
// | "custom"; // | "custom";
required: "y" | "n"; required: ("y" | "n") | (() => "y" | "n");
required_msg: (name: string) => string; required_msg: (name: string) => string;
options: FieldOptions; options: FieldOptions;
on_change: (arg: { value: any }) => void | Promise<void>; on_change: (arg: { value: any }) => void | Promise<void>;
@ -104,12 +104,6 @@ export type FMInternal = {
}; };
export type FMLocal = FMInternal & { render: () => void }; export type FMLocal = FMInternal & { render: () => void };
type FieldInternalProp = {
text: PropTypeText;
number: PropTypeText;
relation: PropTypeRelation;
switch: PropTypeSwitch;
};
export type FieldInternal<T extends FieldProp["type"]> = { export type FieldInternal<T extends FieldProp["type"]> = {
status: "init" | "loading" | "ready"; status: "init" | "loading" | "ready";
@ -130,7 +124,7 @@ export type FieldInternal<T extends FieldProp["type"]> = {
input: Record<string, any> & { input: Record<string, any> & {
render: () => void; render: () => void;
}; };
prop?: FieldInternalProp[T]; prop?: any;
}; };
export type FieldLocal = FieldInternal<any> & { export type FieldLocal = FieldInternal<any> & {
render: () => void; render: () => void;

View File

@ -11,16 +11,20 @@ export const useField = (arg: FieldProp) => {
input: {}, input: {},
} as any); } as any);
const name = typeof arg.name === 'string' ? arg.name : arg.name();
const label = typeof arg.label === 'string' ? arg.label : arg.label();
const required = typeof arg.required === 'string' ? arg.required : arg.required();
const update_field = { const update_field = {
name: arg.name.replace(/\s*/gi, ""), name: name.replace(/\s*/gi, ""),
label: arg.label, label: label,
type: arg.type, type: arg.type,
desc: arg.desc, desc: arg.desc,
prefix: arg.prefix, prefix: arg.prefix,
suffix: arg.suffix, suffix: arg.suffix,
width: arg.width, width: arg.width,
custom: arg.custom, custom: arg.custom,
required: arg.required === "y", required: required === "y",
required_msg: arg.required_msg, required_msg: arg.required_msg,
disabled: arg.disabled === "y", disabled: arg.disabled === "y",
}; };
@ -33,9 +37,9 @@ export const useField = (arg: FieldProp) => {
const fm = arg.fm; const fm = arg.fm;
useEffect(() => { useEffect(() => {
if (field.status === "init" || !fm.fields[arg.name]) { if (field.status === "init" || !fm.fields[name]) {
field.status = "ready"; field.status = "ready";
fm.fields[arg.name] = field; fm.fields[name] = field;
field.render(); field.render();
} }
}, []); }, []);