Apply suggestions from code review

Co-authored-by: Omar López <zomars@me.com>
pull/6560/head
Hariom Balhara 2023-02-16 11:13:12 +05:30 committed by GitHub
parent f50b4166f5
commit f26141a59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 26 deletions

View File

@ -51,97 +51,100 @@ export const FormBuilder = function FormBuilder({
description: string; description: string;
addFieldLabel: string; addFieldLabel: string;
}) { }) {
const FieldTypes: { const FieldTypesMap: Record<
string,
{
value: RhfForm["fields"][number]["type"]; value: RhfForm["fields"][number]["type"];
label: string; label: string;
needsOptions?: boolean; needsOptions?: boolean;
systemOnly?: boolean; systemOnly?: boolean;
isTextType?: boolean; isTextType?: boolean;
}[] = [ }
{ > = {
name: {
label: "Name", label: "Name",
value: "name", value: "name",
isTextType: true, isTextType: true,
}, },
{ email: {
label: "Email", label: "Email",
value: "email", value: "email",
isTextType: true, isTextType: true,
}, },
{ phone: {
label: "Phone", label: "Phone",
value: "phone", value: "phone",
isTextType: true, isTextType: true,
}, },
{ text: {
label: "Short Text", label: "Short Text",
value: "text", value: "text",
isTextType: true, isTextType: true,
}, },
{ number: {
label: "Number", label: "Number",
value: "number", value: "number",
isTextType: true, isTextType: true,
}, },
{ textarea: {
label: "Long Text", label: "Long Text",
value: "textarea", value: "textarea",
isTextType: true, isTextType: true,
}, },
{ select: {
label: "Select", label: "Select",
value: "select", value: "select",
needsOptions: true, needsOptions: true,
isTextType: true, isTextType: true,
}, },
{ multiselect: {
label: "MultiSelect", label: "MultiSelect",
value: "multiselect", value: "multiselect",
needsOptions: true, needsOptions: true,
isTextType: false, isTextType: false,
}, },
{ multiemail: {
label: "Multiple Emails", label: "Multiple Emails",
value: "multiemail", value: "multiemail",
isTextType: true, isTextType: true,
}, },
{ radioInput: {
label: "Radio Input", label: "Radio Input",
value: "radioInput", value: "radioInput",
isTextType: false, isTextType: false,
systemOnly: true, systemOnly: true,
}, },
{ checkbox: {
label: "Checkbox Group", label: "Checkbox Group",
value: "checkbox", value: "checkbox",
needsOptions: true, needsOptions: true,
isTextType: false, isTextType: false,
}, },
{ radio: {
label: "Radio Group", label: "Radio Group",
value: "radio", value: "radio",
needsOptions: true, needsOptions: true,
isTextType: false, isTextType: false,
}, },
{ boolean: {
label: "Checkbox", label: "Checkbox",
value: "boolean", value: "boolean",
isTextType: false, isTextType: false,
}, },
]; };
const FieldTypes = Object.values(FieldTypesMap);
// I would have liked to give Form Builder it's own Form but nested Forms aren't something that browsers support. // I would have liked to give Form Builder it's own Form but nested Forms aren't something that browsers support.
// So, this would reuse the same Form as the parent form. // So, this would reuse the same Form as the parent form.
const fieldsForm = useFormContext<RhfForm>(); const fieldsForm = useFormContext<RhfForm>();
// It allows any property name to be used for instead of `fields` property name
const rhfFormPropName = formProp as unknown as "fields";
const { t } = useLocale(); const { t } = useLocale();
const fieldForm = useForm<RhfFormField>(); const fieldForm = useForm<RhfFormField>();
const { fields, swap, remove, update, append } = useFieldArray({ const { fields, swap, remove, update, append } = useFieldArray({
control: fieldsForm.control, control: fieldsForm.control,
name: rhfFormPropName, // HACK: It allows any property name to be used for instead of `fields` property name
name: formProp as unknown as "fields",
}); });
function OptionsField({ function OptionsField({
@ -254,7 +257,7 @@ export const FormBuilder = function FormBuilder({
remove(index); remove(index);
}; };
const fieldType = FieldTypes.find((f) => f.value === fieldForm.watch("type")); const fieldType = FieldTypesMap[fieldForm.watch("type")];
const isFieldEditMode = fieldDialog.fieldIndex !== -1; const isFieldEditMode = fieldDialog.fieldIndex !== -1;
return ( return (
<div> <div>
@ -263,7 +266,7 @@ export const FormBuilder = function FormBuilder({
<p className="max-w-[280px] break-words py-1 text-sm text-gray-500 sm:max-w-[500px]">{description}</p> <p className="max-w-[280px] break-words py-1 text-sm text-gray-500 sm:max-w-[500px]">{description}</p>
<ul className="mt-2 rounded-md border"> <ul className="mt-2 rounded-md border">
{fields.map((field, index) => { {fields.map((field, index) => {
const fieldType = FieldTypes.find((f) => f.value === field.type); const fieldType = FieldTypesMap[field.type];
// Hidden fields can't be required // Hidden fields can't be required
const isRequired = field.required && !field.hidden; const isRequired = field.required && !field.hidden;
@ -409,7 +412,7 @@ export const FormBuilder = function FormBuilder({
} }
fieldForm.setValue("type", value); fieldForm.setValue("type", value);
}} }}
value={FieldTypes.find((fieldType) => fieldType.value === fieldForm.getValues("type"))} value={FieldTypesMap[fieldForm.getValues("type")]}
options={FieldTypes.filter((f) => !f.systemOnly)} options={FieldTypes.filter((f) => !f.systemOnly)}
label="Input Type" label="Input Type"
/> />