This commit is contained in:
rizky 2024-04-01 07:22:38 -07:00
parent 59f10f944a
commit b5531a74d1
3 changed files with 56 additions and 36 deletions

View File

@ -38,7 +38,8 @@ export const Field: FC<{
| "datetime" | "datetime"
| "money" | "money"
| "slider" | "slider"
| "master-link"; | "master-link"
| "custom";
required: "y" | "n"; required: "y" | "n";
options: FieldOptions; options: FieldOptions;
slider: () => Promise<SliderOptions>; slider: () => Promise<SliderOptions>;
@ -76,7 +77,8 @@ export const Field: FC<{
rel_table, rel_table,
rel_query, rel_query,
}) => { }) => {
const value = form?.hook.getValues()[name]; const values = form?.hook.getValues();
const value = values[name];
const local = useLocal({ const local = useLocal({
date: { date: {
// label: "", // label: "",
@ -177,6 +179,15 @@ export const Field: FC<{
</FormLabel> </FormLabel>
<FormControl> <FormControl>
<> <>
{type === "custom" && (
<div
className={cx(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
)}
>
{custom}
</div>
)}
{type === "slider" && ( {type === "slider" && (
<div className="c-flex-1 c-min-h-[40px] c-flex"> <div className="c-flex-1 c-min-h-[40px] c-flex">
<div className="c-flex c-flex-col c-items-center"> <div className="c-flex c-flex-col c-items-center">

View File

@ -1,7 +1,7 @@
import { Form as FForm } from "@/comps/ui/form"; import { Form as FForm } from "@/comps/ui/form";
import { Toaster } from "@/comps/ui/sonner"; import { Toaster } from "@/comps/ui/sonner";
import { useLocal } from "@/utils/use-local"; import { useLocal } from "@/utils/use-local";
import { FC, useEffect } from "react"; import { FC, useEffect, useState } from "react";
import { createPortal } from "react-dom"; import { createPortal } from "react-dom";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { toast } from "sonner"; import { toast } from "sonner";
@ -9,6 +9,7 @@ import { FormHook } from "./utils/utils";
import { AlertTriangle, Check, Loader2 } from "lucide-react"; import { AlertTriangle, Check, Loader2 } from "lucide-react";
import { cn } from "@/utils"; import { cn } from "@/utils";
import { Skeleton } from "../ui/skeleton"; import { Skeleton } from "../ui/skeleton";
import { getPathname } from "../../..";
export const Form: FC<{ export const Form: FC<{
on_init: (arg: { submit: any; reload: any }) => any; on_init: (arg: { submit: any; reload: any }) => any;
@ -88,10 +89,14 @@ export const Form: FC<{
} }
} }
await on_submit({ const res = on_submit({
form: data, form: data,
error: form.hook.formState.errors, error: form.hook.formState.errors,
}); });
await res;
toast.dismiss();
setTimeout(() => {
toast.dismiss(); toast.dismiss();
if (Object.keys(form.hook.formState.errors).length > 0) { if (Object.keys(form.hook.formState.errors).length > 0) {
@ -123,6 +128,7 @@ export const Form: FC<{
} }
); );
} }
}, 100);
}, 300); }, 300);
}; };
@ -141,11 +147,13 @@ export const Form: FC<{
local.render(); local.render();
}, },
}); });
const res = on_load(); const res = on_load();
const loaded = (values: any) => { const loaded = (values: any) => {
setTimeout(() => { setTimeout(() => {
toast.dismiss(); toast.dismiss();
}); });
if (!!values) { if (!!values) {
for (const [k, v] of Object.entries(values)) { for (const [k, v] of Object.entries(values)) {
form.hook.setValue(k, v); form.hook.setValue(k, v);
@ -224,7 +232,8 @@ export const Form: FC<{
} else { } else {
local.layout = "2-col"; local.layout = "2-col";
} }
local.render();
local.render(true);
} }
}} }}
className={cx( className={cx(