import { useEffect } from "react"; // headless ui import { Disclosure } from "@headlessui/react"; // react-hook-form import { Control, Controller, UseFormWatch, useForm } from "react-hook-form"; // components import { CheckboxAttributeForm, DateTimeAttributeForm, EmailAttributeForm, FileAttributeForm, NumberAttributeForm, RelationAttributeForm, SelectAttributeForm, TextAttributeForm, UrlAttributeForm, } from "components/custom-attributes"; // ui import { PrimaryButton, ToggleSwitch } from "components/ui"; // icons import { ChevronDown } from "lucide-react"; // types import { ICustomAttribute, TCustomAttributeTypes } from "types"; // constants import { CUSTOM_ATTRIBUTES_LIST } from "constants/custom-attributes"; type Props = { data: Partial; handleDeleteAttribute: () => void; handleUpdateAttribute: (data: Partial) => Promise; objectId: string; type: TCustomAttributeTypes; }; export type FormComponentProps = { control: Control, any>; objectId: string; watch: UseFormWatch>; }; const RenderForm: React.FC<{ type: TCustomAttributeTypes } & FormComponentProps> = ({ control, objectId, type, watch, }) => { let FormToRender: any = <>; if (type === "checkbox") FormToRender = ; else if (type === "datetime") FormToRender = ; else if (type === "email") FormToRender = ; else if (type === "file") FormToRender = ; else if (type === "multi_select") FormToRender = ; else if (type === "number") FormToRender = ; else if (type === "relation") FormToRender = ; else if (type === "select") FormToRender = ; else if (type === "text") FormToRender = ; else if (type === "url") FormToRender = ; return FormToRender; }; export const AttributeForm: React.FC = ({ data, handleDeleteAttribute, handleUpdateAttribute, objectId, type, }) => { const typeMetaData = CUSTOM_ATTRIBUTES_LIST[type]; const { control, formState: { isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues: typeMetaData.defaultFormValues }); const handleFormSubmit = async (data: Partial) => { await handleUpdateAttribute(data); }; useEffect(() => { if (!data) return; reset({ ...typeMetaData.defaultFormValues, ...data, }); }, [data, reset, typeMetaData.defaultFormValues]); return ( {({ open }) => ( <>
{data.display_name ?? typeMetaData.label}
{data.type && ( )}
{data.type !== "checkbox" && ( <> ( )} /> Mandatory field )}
{isSubmitting ? "Saving..." : "Save"}
)}
); };