diff --git a/comps/form/field/type/TypeDropdown.tsx b/comps/form/field/type/TypeDropdown.tsx index 0e35ff9..7567b89 100755 --- a/comps/form/field/type/TypeDropdown.tsx +++ b/comps/form/field/type/TypeDropdown.tsx @@ -14,7 +14,7 @@ export const TypeDropdown: FC<{ options: [] as { value: string; label: string; data: any }[], }); - const reload = () => { + const reload = (callback?: () => void) => { if (typeof arg.on_load === "function") { local.loaded = false; local.render(); @@ -71,14 +71,21 @@ export const TypeDropdown: FC<{ local.loaded = true; local.render(); + callback?.(); }); } else { local.loaded = true; local.options = Array.isArray(options) ? options : ([] as any); local.render(); + callback?.(); } } }; + field.reload_options = () => { + return new Promise((done) => { + reload(done); + }); + }; if ((arg.load_trigger?.deps || []).length > 0 && !isEditor) { useEffect( diff --git a/comps/form/typings.ts b/comps/form/typings.ts index b5bb11d..fd61054 100755 --- a/comps/form/typings.ts +++ b/comps/form/typings.ts @@ -196,6 +196,7 @@ export type FieldInternal = { options: { on_load?: () => Promise<{ value: string; label: string }[]>; }; + reload_options: () => Promise; on_change?: (arg: { value: any; name: string; diff --git a/comps/form/utils/use-field.tsx b/comps/form/utils/use-field.tsx index 7c957de..f59c35c 100755 --- a/comps/form/utils/use-field.tsx +++ b/comps/form/utils/use-field.tsx @@ -16,32 +16,35 @@ export const useField = ( input: {}, ref: null as any, } as any); - const ref = useRef(null as any) + const ref = useRef(null as any); field.ref = ref; - 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 = { - name: name.replace(/\s*/gi, ""), - label: label, - type: arg.type, - desc: arg.desc, - prefix: arg.prefix, - suffix: arg.suffix, - width: arg.width, - custom: arg.custom, - required: required === "y", - required_msg: arg.required_msg, - disabled: typeof arg.disabled === "function" ? arg.disabled : arg.disabled === "y", - on_change: arg.on_change, - max_date: arg.max_date, - min_date: arg.min_date, - }; if (field.status === "init" || isEditor) { - for (const [k, v] of Object.entries(update_field)) { + for (const [k, v] of Object.entries({ + name: name.replace(/\s*/gi, ""), + label: label, + type: arg.type, + desc: arg.desc, + prefix: arg.prefix, + suffix: arg.suffix, + width: arg.width, + custom: arg.custom, + required: required === "y", + required_msg: arg.required_msg, + disabled: + typeof arg.disabled === "function" + ? arg.disabled + : arg.disabled === "y", + on_change: arg.on_change, + max_date: arg.max_date, + min_date: arg.min_date, + options: {}, + reload_options: () => {}, + })) { (field as any)[k] = v; } } diff --git a/utils/prasi-events.ts b/utils/prasi-events.ts index e693e3d..0c1974d 100755 --- a/utils/prasi-events.ts +++ b/utils/prasi-events.ts @@ -1,8 +1,8 @@ import { FieldLocal } from "lib/comps/form/typings"; -import { FMLocal } from "../.."; -import { set } from "./set"; import { MDLocal } from "lib/comps/md/utils/typings"; +import { FMLocal } from "../.."; import { Prisma } from "../../typings/prisma"; +import { set } from "./set"; const events = { form: { @@ -20,9 +20,9 @@ const events = { }, }, field: { - relation_load: async (fm: FMLocal, field: FieldLocal) => { - return {} as Record; - }, + on_init: async (fm: FMLocal, field: FieldLocal) => {}, + on_change: async (fm: FMLocal, field: FieldLocal) => {}, + options_load: async (fm: FMLocal, field: FieldLocal) => {}, }, tablelist: { after_load: async ( @@ -34,6 +34,11 @@ const events = { }, }; +type TriggerOnChange = { + fm: FMLocal; + md: any; + where: any; +}; type PRASI_EVENT = typeof events; export const prasi_events = < K extends keyof PRASI_EVENT,