import React from 'react'; import { useForm, Controller, ValidationMode } from 'react-hook-form'; import ReactSelect from 'react-select'; import { TextField, Checkbox, Select, MenuItem, Switch, RadioGroup, FormControlLabel, Radio, } from '@mui/material'; import { useParams } from 'react-router-dom'; let renderCount = 0; const options = [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, ] as const; const defaultValues = { Native: '', TextField: '', Select: '', ReactSelect: '', Checkbox: false, switch: false, RadioGroup: '', }; type Form = { Native: string; TextField: string; Select: string; ReactSelect: string; Checkbox: boolean; switch: boolean; RadioGroup: string; }; const PureReactSelect = React.memo(ReactSelect); export default function Field() { const { mode } = useParams(); const methods = useForm
({ defaultValues, mode: mode as keyof ValidationMode, }); const { handleSubmit, formState: { errors }, reset, control, } = methods; const [, setRerender] = React.useState(0); renderCount++; const rerender = () => setRerender(Math.random()); return ( {})}>
( props.onChange(e.target.checked)} /> )} />
{errors.Checkbox &&

Checkbox Error

}
( } label="Female" /> } label="Male" /> } label="Other" /> )} rules={{ required: true }} name="RadioGroup" control={control} />
{errors.RadioGroup &&

RadioGroup Error

}
} name="TextField" control={control} rules={{ required: true }} />
{errors.TextField &&

TextField Error

}
( )} rules={{ required: true }} name="Select" control={control} />
{errors.Select &&

Select Error

}
( props.onChange(e.target.checked)} /> )} control={control} />
{errors.switch &&

switch Error

}
( )} name="ReactSelect" control={control} rules={{ required: true }} />
{errors.ReactSelect &&

ReactSelect Error

}
{renderCount}
); }