In Material UI when do we use Input vs. TextField for building a form?
Asked Answered
B

2

70

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

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

Please suggest.

Bloodandthunder answered 14/5, 2019 at 2:29 Comment(0)
B
81

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.

Benoit answered 14/5, 2019 at 17:2 Comment(0)
Q
27

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

Quesada answered 2/10, 2021 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.