This commit is contained in:
rizky 2024-07-30 13:25:04 -07:00
parent d4f26f7c78
commit 9590e14ff8
5 changed files with 73 additions and 18 deletions

View File

@ -45,6 +45,8 @@ export const Field: FC<FieldProp> = (arg) => {
props.className.split(" ").find((e: string) => e.startsWith("s-")) || "";
}
if (field.hidden) return <></>;
return (
<LabelDiv
mode={sub_type === "table-edit" ? "div" : "label"}

View File

@ -14,19 +14,10 @@ export const TypeDropdown: FC<{
options: [] as { value: string; label: string; data: any }[],
});
let value =
typeof arg.opt_get_value === "function"
? arg.opt_get_value({
fm,
name: field.name,
options: local.options,
type: field.type,
})
: fm.data[field.name];
useEffect(() => {
if (isEditor) return;
const reload = () => {
if (typeof arg.on_load === "function") {
local.loaded = false;
local.render();
const options = arg.on_load({ field });
if (options instanceof Promise) {
options.then((res) => {
@ -67,7 +58,7 @@ export const TypeDropdown: FC<{
options: local.options,
selected: [local.options[0]?.value],
});
} else if ( value) {
} else if (value) {
arg.opt_set_value({
fm,
name: field.name,
@ -87,7 +78,35 @@ export const TypeDropdown: FC<{
local.render();
}
}
}, []);
};
if ((arg.load_trigger?.deps || []).length > 0 && !isEditor) {
useEffect(
() => {
reload();
},
arg.load_trigger?.deps.map((e) => fm.data[e])
);
} else {
useEffect(() => {
if (isEditor) {
local.loaded = true;
local.render();
return;
}
reload();
}, []);
}
let value =
typeof arg.opt_get_value === "function"
? arg.opt_get_value({
fm,
name: field.name,
options: local.options,
type: field.type,
})
: fm.data[field.name];
let popupClassName = "";
@ -120,6 +139,7 @@ export const TypeDropdown: FC<{
typeof field.disabled === "function" ? field.disabled() : field.disabled;
if (!local.loaded) return <FieldLoading />;
if (field.type === "single-option") {
if (value === null) {
fm.data[field.name] = undefined;

View File

@ -177,13 +177,15 @@ export const generateForm = async (
copyProps(old_item, new_item, [
"placeholder",
"label",
"link__url",
"ext__width",
"opt__load_trigger",
"ext__on_change",
"ext__description",
"ext__show_label",
"ext__disabled",
"ext__prefix",
"ext__suffxi",
"ext__suffix",
]);
}

View File

@ -56,7 +56,30 @@ async (arg: {
ext_select[rel__id_parent] = true;
}
const where = (await call_prasi_events("field", "relation_load", [fm, arg.field]) || {}) as Prisma.${table}WhereInput;
let where =
(await call_prasi_events("field", "relation_load", [fm, arg.field]) || {}) as Prisma.${table}WhereInput;
if (typeof opt__load_trigger === "object" && typeof opt__load_trigger?.on_change === "function") {
const trigger = await opt__load_trigger.on_change({ md, fm, where });
if (trigger.hidden) {
done([]);
arg.field.hidden = true;
arg.field.render();
return;
} else {
if (arg.field.hidden) {
arg.field.hidden = false;
arg.field.render();
}
}
if (trigger.result) {
done(trigger.result);
return;
} else if (trigger.where) {
where = trigger.where;
}
}
let items = await db.${table}.findMany({
select: {

View File

@ -48,6 +48,13 @@ export type FieldProp = {
label: string;
desc?: string;
props?: any;
load_trigger?: {
deps: any[];
on_change: (arg: {
fm: FMLocal;
where: any;
}) => Promise<{ where?: any; result?: any[] }>;
};
link: {
text:
| string
@ -117,7 +124,7 @@ export type FieldProp = {
model_upload?: "upload" | "import";
max_date?: any;
min_date?: any;
upload_style?: "inline" | "full"
upload_style?: "inline" | "full";
};
export type FMInternal = {
@ -176,6 +183,7 @@ export type FieldInternal<T extends FieldProp["type"]> = {
width: FieldProp["width"];
required: boolean;
focused: boolean;
hidden: boolean;
disabled: boolean | (() => boolean);
required_msg: FieldProp["required_msg"];
col?: GFCol;