import { useAutoAnimate } from "@formkit/auto-animate/react"; import { useEffect, useState } from "react"; import type { UseFormReturn } from "react-hook-form"; import { useWatch } from "react-hook-form"; import { Controller, useFieldArray } from "react-hook-form"; import { v4 as uuidv4 } from "uuid"; import Shell from "@calcom/features/shell/Shell"; import classNames from "@calcom/lib/classNames"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { BooleanToggleGroupField, Button, EmptyScreen, FormCard, Label, SelectField, Skeleton, TextField, } from "@calcom/ui"; import { Plus, FileText, X, ArrowUp, ArrowDown } from "@calcom/ui/components/icon"; import type { inferSSRProps } from "@lib/types/inferSSRProps"; import type { RoutingFormWithResponseCount } from "../../components/SingleForm"; import SingleForm, { getServerSidePropsForSingleFormView as getServerSideProps, } from "../../components/SingleForm"; export { getServerSideProps }; type HookForm = UseFormReturn; type SelectOption = { placeholder: string; value: string; id: string }; export const FieldTypes = [ { label: "Short Text", value: "text", }, { label: "Number", value: "number", }, { label: "Long Text", value: "textarea", }, { label: "Single Selection", value: "select", }, { label: "Multiple Selection", value: "multiselect", }, { label: "Phone", value: "phone", }, { label: "Email", value: "email", }, ]; function Field({ hookForm, hookFieldNamespace, deleteField, moveUp, moveDown, appUrl, }: { fieldIndex: number; hookForm: HookForm; hookFieldNamespace: `fields.${number}`; deleteField: { check: () => boolean; fn: () => void; }; moveUp: { check: () => boolean; fn: () => void; }; moveDown: { check: () => boolean; fn: () => void; }; appUrl: string; }) { const { t } = useLocale(); const [animationRef] = useAutoAnimate(); const [options, setOptions] = useState([ { placeholder: "< 10", value: "", id: uuidv4() }, { placeholder: "10-100", value: "", id: uuidv4() }, { placeholder: "100-500", value: "", id: uuidv4() }, { placeholder: "> 500", value: "", id: uuidv4() }, ]); const handleRemoveOptions = (index: number) => { const updatedOptions = options.filter((_, i) => i !== index); setOptions(updatedOptions); updateSelectText(updatedOptions); }; const handleAddOptions = () => { setOptions((prevState) => [ ...prevState, { placeholder: "New Option", value: "", id: uuidv4(), }, ]); }; useEffect(() => { const originalValues = hookForm.getValues(`${hookFieldNamespace}.selectText`); if (originalValues) { const values: SelectOption[] = originalValues.split("\n").map((fieldValue) => ({ value: fieldValue, placeholder: "", id: uuidv4(), })); setOptions(values); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const router = hookForm.getValues(`${hookFieldNamespace}.router`); const routerField = hookForm.getValues(`${hookFieldNamespace}.routerField`); const handleChange = (e: React.ChangeEvent, optionIndex: number) => { const updatedOptions = options.map((opt, index) => ({ ...opt, ...(index === optionIndex ? { value: e.target.value } : {}), })); setOptions(updatedOptions); updateSelectText(updatedOptions); }; const updateSelectText = (updatedOptions: SelectOption[]) => { hookForm.setValue( `${hookFieldNamespace}.selectText`, updatedOptions .filter((opt) => opt.value) .map((opt) => opt.value) .join("\n") ); }; const label = useWatch({ control: hookForm.control, name: `${hookFieldNamespace}.label`, }); const identifier = useWatch({ control: hookForm.control, name: `${hookFieldNamespace}.identifier`, }); function move(index: number, increment: 1 | -1) { const newList = [...options]; const type = options[index]; const tmp = options[index + increment]; if (tmp) { newList[index] = tmp; newList[index + increment] = type; } setOptions(newList); updateSelectText(newList); } return (
{ hookForm.setValue(`${hookFieldNamespace}.identifier`, e.target.value); }} />
{ const defaultValue = FieldTypes.find((fieldType) => fieldType.value === value); return ( ({ ...baseStyles, fontSize: "14px", }), option: (baseStyles) => ({ ...baseStyles, fontSize: "14px", }), }} label="Type" isDisabled={!!router} containerClassName="data-testid-field-type" options={FieldTypes} onChange={(option) => { if (!option) { return; } onChange(option.value); }} defaultValue={defaultValue} /> ); }} />
{["select", "multiselect"].includes(hookForm.watch(`${hookFieldNamespace}.type`)) ? (
{t("options")}
    {options.map((field, index) => (
  • {options.length && index !== 0 ? ( ) : null} {options.length && index !== options.length - 1 ? ( ) : null}
    handleChange(e, index)} addOnSuffix={ } />
  • ))}
) : null}
{ return ( ); }} />
); } const FormEdit = ({ hookForm, form, appUrl, }: { hookForm: HookForm; form: inferSSRProps["form"]; appUrl: string; }) => { const fieldsNamespace = "fields"; const { fields: hookFormFields, append: appendHookFormField, remove: removeHookFormField, swap: swapHookFormField, // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore https://github.com/react-hook-form/react-hook-form/issues/6679 } = useFieldArray({ control: hookForm.control, name: fieldsNamespace, }); const [animationRef] = useAutoAnimate(); const addField = () => { appendHookFormField({ // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore id: uuidv4(), // This is same type from react-awesome-query-builder type: "text", label: "", }); }; // hookForm.reset(form); if (!form.fields) { form.fields = []; } return hookFormFields.length ? (
{hookFormFields.map((field, key) => { return ( hookFormFields.length > 1, fn: () => { removeHookFormField(key); }, }} moveUp={{ check: () => key !== 0, fn: () => { swapHookFormField(key, key - 1); }, }} moveDown={{ check: () => key !== hookFormFields.length - 1, fn: () => { if (key === hookFormFields.length - 1) { return; } swapHookFormField(key, key + 1); }, }} key={key} /> ); })}
{hookFormFields.length ? (
) : null}
) : (
Create Field } />
); }; export default function FormEditPage({ form, appUrl, }: inferSSRProps & { appUrl: string }) { return ( } /> ); } FormEditPage.getLayout = (page: React.ReactElement) => { return ( {page} ); };