Form disabled and readOnly states #9100
-
Thought I'd throw this idea out there as a possible inclusion to the actual library/api. My team has ran into situations where it makes sense to mass-disable form fields or mass-set form fields to a readOnly state. Both of these kind of states don't particularly need any As a general example of how I envisioned this being useful: import { FormProvider, useForm, useFormContext, useFormState } from 'react-hook-form';
const FormTextInput = ({ name }: { name: string }) => {
const { register } = useFormContext();
const { disabled, readOnly } = useFormState();
// up to the developer to handle styling/functionality
// for what disabled and readOnly props actually do
return <input {...register(name)} disabled={disabled} readOnly={readOnly} />;
};
const FormExample = () => {
const methods = useForm({
defaultValues: {
exampleField: ''
}
});
return (
<FormProvider {...methods}>
<button
onClick={() => {
methods.setDisabled();
// or, if it makes sense, method is under formState
methods.formState.setDisabled(true);
}}
>
Enable/Disable form
</button>
<button
onClick={() => {
methods.setReadOnly();
// or, if it makes sense, method is under formState
methods.formState.setReadOnly(true);
}}
>
Set readonly/remove readonly form
</button>
<FormTextInput name="exampleField" />
</FormProvider>
);
}; Thoughts? I could probably open a PR if there is interest in such a thing existing within the library. Otherwise I'm either A) going to develop a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
you can use
|
Beta Was this translation helpful? Give feedback.
-
Just found |
Beta Was this translation helpful? Give feedback.
you can use
<fieldset disbled></fieldset>
, this is a tricky topic for us to cover, mainly due to external controlled component (include react native) implementation it's difficult to assume all of them support disabled prop, while its certainly easy to implement for register input which offers input reference.