Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MUI when do we use Input vs. TextField for building a form?

Maybe this is just a basic question, but so far have not found any reasonable explanation. I am a beginner in react and using MUI very recently. I am not very clear on when to use an Input vs. Textfield for building a form?

Looking at documentation, it feels like TextField is a superset of what Input can do, but not sure. MUI site uses examples for TextField and Input both, without stating benefits of one over the other and any use cases.

Please suggest.

like image 398
raghuvd Avatar asked May 14 '19 02:05

raghuvd


People also ask

Is TextField an input?

The TextField wrapper component is a complete form control including a label, input, and help text. It comes with three variants: outlined (default), filled, and standard.

How do I create a form in material UI?

Create a Form Let's start by creating a new file in our project called Form. js . This component will return a form and we will be using the TextField and Button components from Material-UI. We will pass down the handleClose prop from the ModalDialog component to use in a cancel button.

How do I make TextField required MUI?

We can use TextField component in Material UI to show input textbox. import TextField from "@material-ui/core/TextField"; When using the TextField component, we can pass the required attribute to mark the component as required.

How do I get TextField value in material UI?

To get data from the React Material UI TextField component, we can get the input value from the onChange function. In the function, we can get the input value from the e. target. value property and set that as the value of a state.


2 Answers

For most use cases, you should use TextField rather than the lower-level components that it delegates to (such as Input).

The relevant part of the documentation is here.

Particularly this line:

TextField is composed of smaller components ( FormControl, Input, FilledInput, InputLabel, OutlinedInput, and FormHelperText ) that you can leverage directly to significantly customize your form inputs.

The main reason to use these lower-level components is if you need to customize your form input in some way that isn't supported using TextField.

like image 98
Ryan Cogswell Avatar answered Sep 19 '22 18:09

Ryan Cogswell


This is a simplified definition of the TextField component:

<FormControl {...other}>   {label && (     <InputLabel {...InputLabelProps}>       {label}     </InputLabel>   )}    {select ? (     <Select {...SelectProps}>       {children}     </Select>   ) : (     <Input {...InputProps} />   )}    {helperText && (     <FormHelperText {...FormHelperTextProps}>       {helperText}     </FormHelperText>   )} </FormControl> 

As you can see, the TextField is more than an Input, it consists of:

  • FormControl: It's just a context provider that is used to pass the TextField state (focus, error, hover...) down to the children and make sure they stay consistent. You usually don't have to use this component directly.

  • InputLabel: The label of the TextField, Input doesn't have one on its own, so if you want to add a label to your Input, use TextField.

  • FormHelperText: The helper text placed below the Input, used to describe the TextField or display some error message in form validation.

  • Input: The input itself. There are actually 3 input components in different variants: Input, OutlinedInput and FilledInput which can be set by the variant prop in TextField. This is one more reason to use TextField, instead of finding and importing different input components, you can just set the variant prop and TextField will know what to render.

  • Select: TextField can be either a Select or an Input. Set the select prop to change the input type to select.

So when to use TextField? In a form where you want to display an Input with a label and have a way to set the error message in a clean API, which is most of the time.

When to use Input? When you don't need all of the above except something to type in, or when you have a need for extreme customization and the API of TextField is not enough for you.

Codesandbox Demo

like image 40
NearHuscarl Avatar answered Sep 23 '22 18:09

NearHuscarl