Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is FormControl used for? Why is it used? How Should it be used?

Tags:

Can someone explain to me in layman's terms what function FormControl serves, and why/how one would want to use it?

I'm using Material-UI in React, and many of the form examples I see make use of FormControl, but I'm having a hard time understanding what it does, and if it's necessary or not for my project.

Right now I simply have a Component named Form.js and I'm containing all my form elements in a div like this:

return ( <div>   <FormControl className={classes.formControl}>     <InputLabel htmlFor="select-multiple-accounts">Account</InputLabel>     <Select       multiple       value={accountName}       onChange={handleAccountChange}       input={<Input id="select-multiple-accounts" />}       renderValue={         selected => (         <div className={classes.chips}>           {             selected.map(value => (             <Chip key={value} label={value} className={classes.chip} />           ))}         </div>       )}       MenuProps={MenuProps}     >       {accountNames.map(name => (         <MenuItem key={name.label} value={name.label} style={getStyles(name.label, accountName, theme)}>           {name.label}         </MenuItem>       ))}     </Select>   </FormControl>   <br /><br />   <TextField     id="job-number"     label="Job #"     className={classes.textField}     value={props.jobNumber}     onChange={handleJobNumberChange}     fullWidth   />   <br /><br /><br />   <TextField     id="project-description"     label="Project Description"     placeholder="Provide a detailed description of the project:"     className={classes.textField}     multiline     variant="outlined"     value={props.projectDescription}     onChange={handleProjectDescriptionChange}     fullWidth   /> </div>   ); } 

Is this ok? or am I supposed to be wrapping everything in a FormControl? If so.. can you please explain why? I'm not sure what the best practices are when coding a form into a React web application. I'm new to React Forms.

Thanks.

like image 866
Cmaxster Avatar asked Oct 01 '19 15:10

Cmaxster


People also ask

What is FormControl used for?

Form controls enable accessibility by taking a uniform approach to such features as captions, help text, tabbing and keyboard shortcuts. Internationalization issues are addressed by following the same design principles as in XHTML. All form controls are suitable for styling using Aural CSS (ACSS) style properties.

What is the use of FormControl in Angular?

What are form controls in Angular? In Angular, form controls are classes that can hold both the data values and the validation information of any form element. Every form input you have in a reactive form should be bound by a form control. These are the basic units that make up reactive forms.

What is FormGroup and FormControl in Angular why is it used for?

FormGroup is one of the four fundamental building blocks used to define forms in Angular, along with FormControl , FormArray , and FormRecord . When instantiating a FormGroup , pass in a collection of child controls as the first argument. The key for each child registers the name for the control.

What is the use of FormControl in material UI?

From the official Material UI Documentation FormControl API: Provides context such as filled/focused/error/required for form inputs. Relying on the context provides high flexibility and ensures that the state always stays consistent across the children of the FormControl.


Video Answer


1 Answers

From the official Material UI Documentation FormControl API:

Provides context such as filled/focused/error/required for form inputs. Relying on the context provides high flexibility and ensures that the state always stays consistent across the children of the FormControl. This context is used by the following components:

  • FormLabel
  • FormHelperText
  • Input
  • InputLabel

So if you want to use the mentioned features, you should wrap your form in a FormControl component.

like image 172
4l12 Avatar answered Oct 07 '22 02:10

4l12