import { useAutoAnimate } from "@formkit/auto-animate/react"; import { EventTypeCustomInputType } from "@prisma/client"; import type { CustomInputParsed } from "pages/event-types/[type]"; import { FC } from "react"; import { Control, Controller, useFieldArray, useForm, UseFormRegister, useWatch } from "react-hook-form"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { Button, Label, Select, TextField } from "@calcom/ui"; import { FiPlus, FiX } from "@calcom/ui/components/icon"; interface OptionTypeBase { label: string; value: EventTypeCustomInputType; options?: { label: string; type: string }[]; } interface Props { onSubmit: (output: CustomInputParsed) => void; onCancel: () => void; selectedCustomInput?: CustomInputParsed; } type IFormInput = CustomInputParsed; /** * Getting a random ID gives us the option to know WHICH field is changed * when the user edits a custom field. * This UUID is only used to check for changes in the UI and not the ID we use in the DB * There is very very very slim chance that this will cause a collision * */ const randomId = () => Math.floor(Math.random() * 1000000 + new Date().getTime()); const CustomInputTypeForm: FC = (props) => { const { t } = useLocale(); const inputOptions: OptionTypeBase[] = [ { value: EventTypeCustomInputType.TEXT, label: t("text") }, { value: EventTypeCustomInputType.TEXTLONG, label: t("multiline_text") }, { value: EventTypeCustomInputType.NUMBER, label: t("number") }, { value: EventTypeCustomInputType.BOOL, label: t("checkbox") }, { value: EventTypeCustomInputType.RADIO, label: t("radio"), }, { value: EventTypeCustomInputType.PHONE, label: t("phone_number") }, ]; const { selectedCustomInput } = props; const defaultValues = selectedCustomInput ? { ...selectedCustomInput, id: selectedCustomInput?.id || randomId() } : { id: randomId(), type: EventTypeCustomInputType.TEXT, }; const { register, control, getValues } = useForm({ defaultValues, }); const selectedInputType = useWatch({ name: "type", control }); const selectedInputOption = inputOptions.find((e) => selectedInputType === e.value); const onCancel = () => { props.onCancel(); }; return (
(
); }; function RadioInputHandler({ register, control, }: { register: UseFormRegister; control: Control; }) { const { t } = useLocale(); const { fields, append, remove } = useFieldArray({ control, name: "options", shouldUnregister: true, }); const [animateRef] = useAutoAnimate(); return (
    <> {fields.map((option, index) => (
  • { remove(index); }} /> } />
  • ))}
); } export default CustomInputTypeForm;