plane/web/components/custom-attributes/attribute-forms/attribute-form.tsx

160 lines
5.4 KiB
TypeScript
Raw Normal View History

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<ICustomAttribute>;
handleDeleteAttribute: () => void;
handleUpdateAttribute: (data: Partial<ICustomAttribute>) => Promise<void>;
2023-09-14 07:39:21 +00:00
objectId: string;
type: TCustomAttributeTypes;
};
export type FormComponentProps = {
control: Control<Partial<ICustomAttribute>, any>;
2023-09-14 07:39:21 +00:00
objectId: string;
watch: UseFormWatch<Partial<ICustomAttribute>>;
};
const RenderForm: React.FC<{ type: TCustomAttributeTypes } & FormComponentProps> = ({
control,
objectId,
type,
watch,
}) => {
let FormToRender: any = <></>;
if (type === "checkbox")
FormToRender = <CheckboxAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "datetime")
FormToRender = <DateTimeAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "email")
FormToRender = <EmailAttributeForm control={control} objectId={objectId} watch={watch} />;
2023-09-15 07:06:51 +00:00
else if (type === "file")
2023-09-14 07:39:21 +00:00
FormToRender = <FileAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "multi_select")
FormToRender = <SelectAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "number")
FormToRender = <NumberAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "relation")
FormToRender = <RelationAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "select")
FormToRender = <SelectAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "text")
FormToRender = <TextAttributeForm control={control} objectId={objectId} watch={watch} />;
else if (type === "url")
FormToRender = <UrlAttributeForm control={control} objectId={objectId} watch={watch} />;
return FormToRender;
};
export const AttributeForm: React.FC<Props> = ({
data,
handleDeleteAttribute,
handleUpdateAttribute,
2023-09-14 07:39:21 +00:00
objectId,
type,
}) => {
const typeMetaData = CUSTOM_ATTRIBUTES_LIST[type];
const {
control,
formState: { isSubmitting },
handleSubmit,
reset,
watch,
} = useForm({ defaultValues: typeMetaData.defaultFormValues });
const handleFormSubmit = async (data: Partial<ICustomAttribute>) => {
await handleUpdateAttribute(data);
};
useEffect(() => {
if (!data) return;
reset({
...typeMetaData.defaultFormValues,
...data,
});
}, [data, reset, typeMetaData.defaultFormValues]);
return (
<Disclosure
as="div"
className="bg-custom-background-90 border border-custom-border-200 rounded"
>
{({ open }) => (
<>
<Disclosure.Button className="p-3 flex items-center justify-between gap-1 w-full">
<div className="flex items-center gap-2.5">
<typeMetaData.icon size={14} strokeWidth={1.5} />
2023-09-14 07:39:21 +00:00
<h6 className="text-sm">{data.display_name ?? typeMetaData.label}</h6>
</div>
<div className={`${open ? "-rotate-180" : ""} transition-all`}>
<ChevronDown size={16} strokeWidth={1.5} rotate="180deg" />
</div>
</Disclosure.Button>
<Disclosure.Panel>
<form onSubmit={handleSubmit(handleFormSubmit)} className="p-3 pl-9 pt-0">
2023-09-14 07:39:21 +00:00
{data.type && (
<RenderForm type={data.type} control={control} objectId={objectId} watch={watch} />
)}
<div className="mt-8 flex items-center justify-between">
<div className="flex-shrink-0 flex items-center gap-2">
2023-09-15 06:33:17 +00:00
{data.type !== "checkbox" && (
<>
<Controller
control={control}
name="is_required"
render={({ field: { onChange, value } }) => (
<ToggleSwitch value={value ?? false} onChange={onChange} />
)}
/>
<span className="text-xs">Mandatory field</span>
</>
)}
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={handleDeleteAttribute}
className="text-xs font-medium px-3 py-2 rounded bg-custom-background-100 border border-custom-border-200"
>
Remove
</button>
2023-09-14 07:39:21 +00:00
<PrimaryButton type="submit" loading={isSubmitting}>
{isSubmitting ? "Saving..." : "Save"}
</PrimaryButton>
</div>
</div>
</form>
</Disclosure.Panel>
</>
)}
</Disclosure>
);
};