This commit is contained in:
rizky 2024-08-06 23:49:25 -07:00
parent ae8ab53182
commit dc22fc5a45
4 changed files with 41 additions and 25 deletions

View File

@ -14,7 +14,7 @@ export const TypeDropdown: FC<{
options: [] as { value: string; label: string; data: any }[], options: [] as { value: string; label: string; data: any }[],
}); });
const reload = () => { const reload = (callback?: () => void) => {
if (typeof arg.on_load === "function") { if (typeof arg.on_load === "function") {
local.loaded = false; local.loaded = false;
local.render(); local.render();
@ -71,14 +71,21 @@ export const TypeDropdown: FC<{
local.loaded = true; local.loaded = true;
local.render(); local.render();
callback?.();
}); });
} else { } else {
local.loaded = true; local.loaded = true;
local.options = Array.isArray(options) ? options : ([] as any); local.options = Array.isArray(options) ? options : ([] as any);
local.render(); local.render();
callback?.();
} }
} }
}; };
field.reload_options = () => {
return new Promise((done) => {
reload(done);
});
};
if ((arg.load_trigger?.deps || []).length > 0 && !isEditor) { if ((arg.load_trigger?.deps || []).length > 0 && !isEditor) {
useEffect( useEffect(

View File

@ -196,6 +196,7 @@ export type FieldInternal<T extends FieldProp["type"]> = {
options: { options: {
on_load?: () => Promise<{ value: string; label: string }[]>; on_load?: () => Promise<{ value: string; label: string }[]>;
}; };
reload_options: () => Promise<void>;
on_change?: (arg: { on_change?: (arg: {
value: any; value: any;
name: string; name: string;

View File

@ -16,32 +16,35 @@ export const useField = (
input: {}, input: {},
ref: null as any, ref: null as any,
} as any); } as any);
const ref = useRef(null as any) const ref = useRef(null as any);
field.ref = ref; field.ref = ref;
const name = typeof arg.name === "string" ? arg.name : arg.name(); const name = typeof arg.name === "string" ? arg.name : arg.name();
const label = typeof arg.label === "string" ? arg.label : arg.label(); const label = typeof arg.label === "string" ? arg.label : arg.label();
const required = const required =
typeof arg.required === "string" ? arg.required : arg.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) { 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; (field as any)[k] = v;
} }
} }

View File

@ -1,8 +1,8 @@
import { FieldLocal } from "lib/comps/form/typings"; import { FieldLocal } from "lib/comps/form/typings";
import { FMLocal } from "../..";
import { set } from "./set";
import { MDLocal } from "lib/comps/md/utils/typings"; import { MDLocal } from "lib/comps/md/utils/typings";
import { FMLocal } from "../..";
import { Prisma } from "../../typings/prisma"; import { Prisma } from "../../typings/prisma";
import { set } from "./set";
const events = { const events = {
form: { form: {
@ -20,9 +20,9 @@ const events = {
}, },
}, },
field: { field: {
relation_load: async (fm: FMLocal, field: FieldLocal) => { on_init: async (fm: FMLocal, field: FieldLocal) => {},
return {} as Record<string, any>; on_change: async (fm: FMLocal, field: FieldLocal) => {},
}, options_load: async (fm: FMLocal, field: FieldLocal) => {},
}, },
tablelist: { tablelist: {
after_load: async <T extends Prisma.ModelName>( after_load: async <T extends Prisma.ModelName>(
@ -34,6 +34,11 @@ const events = {
}, },
}; };
type TriggerOnChange = {
fm: FMLocal;
md: any;
where: any;
};
type PRASI_EVENT = typeof events; type PRASI_EVENT = typeof events;
export const prasi_events = < export const prasi_events = <
K extends keyof PRASI_EVENT, K extends keyof PRASI_EVENT,