Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable entire form elements with respect to a state. React

I am disabling the inputs using the isFetching prop, but this is getting reduntant as I have to keep this in every input field. Is there a way to disable the entire form? Like a disable property in <form> tag or something?

<form>   <input type="text" disabled={this.props.isFetching} />   <input type="text" disabled={this.props.isFetching} /> </form> 
like image 790
Pratish Shrestha Avatar asked Mar 25 '16 06:03

Pratish Shrestha


People also ask

How do I turn off the whole form in React?

You should insert your form inside an element <fieldset disabled="disabled"> . This will make the whole form disabled.

How do I turn off form elements?

To disable all form elements inside 'target', use the :input selector which matches all input, textarea, select and button elements.

How do you disable input in React based on condition?

To disable an input field inside a <Form /> function component, we will use isDisabled local state and it's setter method setIsDisabled , defined using the useState() hook.

How do I make a form disabled?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!


2 Answers

I think this should solve your problem https://stackoverflow.com/a/17186342/3298693.

You should insert your form inside an element <fieldset disabled="disabled">. This will make the whole form disabled.

like image 195
Mateus Zitelli Avatar answered Sep 20 '22 15:09

Mateus Zitelli


I had the same issue and this worked for me:

 <fieldset disabled={true}> 

Where true would be some "prop.setting"...

like image 27
Bertus Kruger Avatar answered Sep 21 '22 15:09

Bertus Kruger