Skip to content

Text Field

Text fields let users enter and edit text.

Text fields allow users to enter text into a UI. They typically appear in forms and dialogs.

TextField

The TextField wrapper component is a complete form control including a label, input and help text.

It supports standard, outlined and filled styling.

<form className={classes.root} noValidate autoComplete="off">
  <TextField id="standard-basic" label="Standard" />
  <TextField id="filled-basic" label="Filled" variant="filled" />
  <TextField id="outlined-basic" label="Outlined" variant="outlined" />
</form>

Note: The standard variant of the TextField is no longer documented in the Material Design guidelines (here's why), but Material-UI will continue to support it.

Form props

Standard form attributes are supported e.g. required, disabled, type, etc. as well as a helperText which is used to give context about a field’s input, such as how the input will be used.

Some important text

Some important text

Some important text

Validation

The error prop toggles the error state, the helperText prop can then be used to provide feedback to the user about the error.

Incorrect entry.

Incorrect entry.

Incorrect entry.

Multiline

The multiline prop transforms the text field into a <textarea> element. Unless the rows prop is set, the height of the text field dynamically matches its content (using TextareaAutosize). You can use the rowsMin and rowsMax props to bound it.

Select

The select prop makes the text field use the Select component internally.

Please select your currency

Please select your currency

Please select your currency

Please select your currency

Please select your currency

Please select your currency

Icons

There are multiple ways to display an icon with a text field.

Input Adornments

The main way is with an InputAdornment. This can be used to add a prefix, a suffix or an action to an input. For instance, you can use an icon button to hide or reveal the password.

Kg

Kg

Weight

$

Kg

Kg

Weight

$

Kg

Kg

Weight

$

Sizes

Fancy smaller inputs? Use the size prop.

Layout

margin prop can be used to alter the vertical spacing of inputs. Using none (default) will not apply margins to the FormControl, whereas dense and normal will. dense and normal alter other styles to meet the specification.

fullWidth can be used to make the input take up the full width of its container.

Full width!

Some important text

Some important text

Some important text

Full width!

Some important text

Some important text

Some important text

Full width!

Some important text

Some important text

Some important text

Uncontrolled vs Controlled

The component can be controlled or uncontrolled.

Components

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

You might also have noticed that some native HTML input properties are missing from the TextField component. This is on purpose. The component takes care of the most used properties, then it's up to the user to use the underlying component shown in the following demo. Still, you can use inputProps (and InputProps, InputLabelProps properties) if you want to avoid some boilerplate.

Some important helper text

Disabled

Error

Inputs

Color

The color prop changes the highlight color of the text field when focused.

Customized inputs

Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.

Customization does not stop at CSS, you can use composition to build custom components and give your app a unique feel. Below is an example using the InputBase component, inspired by Google Maps.


🎨 If you are looking for inspiration, you can check MUI Treasury's customization examples.

Limitations

Shrink

The input label "shrink" state isn't always correct. The input label is supposed to shrink as soon as the input is displaying something. In some circumstances, we can't determine the "shrink" state (number input, datetime input, Stripe input). You might notice an overlap.

shrink

To workaround the issue, you can force the "shrink" state of the label.

<TextField InputLabelProps={{ shrink: true }} />

or

<InputLabel shrink>Count</InputLabel>

Floating label

The floating label is absolutely positioned, it won't impact the layout of the page. You need to make sure that the input is larger than the label to display correctly.

type="number"

Inputs of type="number" have potential usability issues:

  • Allowing certain non-numeric characters ('e', '+', '-', '.') and silently discarding others
  • The functionality of scrolling to increment/decrement the number can cause accidental and hard-to-notice changes

and more - see this article by the GOV.UK Design System team for a more detailed explanation.

For number validation, one viable alternative is to use the default input type="text" with the pattern attribute, for example:

<TextField inputProps={{ inputMode: 'numeric', pattern: '[0-9]*' }} />

In the future, we might provide a number input component.

Helper text

The helper text prop affects the height of the text field. If two text fields are placed side by side, one with a helper text and one without, they will have different heights. For example:

Please enter your name

<TextField helperText="Please enter your name" label="Name" />
<TextField label="Name" />

This can be fixed by passing a space character to the helperText prop:

Please enter your name

<TextField helperText="Please enter your name" label="Name" />
<TextField helperText=" " label="Name" />

Integration with 3rd party input libraries

You can use third-party libraries to format an input. You have to provide a custom implementation of the <input> element with the inputComponent property.

The following demo uses the react-text-mask and react-number-format libraries. The same concept could be applied to e.g. react-stripe-element.

The provided input component should expose a ref with a value that implements the following interface:

interface InputElement {
  focus(): void;
  value?: string;
}
const MyInputComponent = React.forwardRef((props, ref) => {
  const { component: Component, ...other } = props;

  // implement `InputElement` interface
  React.useImperativeHandle(ref, () => ({
    focus: () => {
      // logic to focus the rendered component from 3rd party belongs here
    },
    // hiding the value e.g. react-stripe-elements
  }));

  // `Component` will be your `SomeThirdPartyComponent` from below
  return <Component {...other} />;
});

// usage
<TextField
  InputProps={{
    inputComponent: MyInputComponent,
    inputProps: {
      component: SomeThirdPartyComponent,
    },
  }}
/>;

Accessibility

In order for the text field to be accessible, the input should be linked to the label and the helper text. The underlying DOM nodes should have this structure:

<div class="form-control">
  <label for="my-input">Email address</label>
  <input id="my-input" aria-describedby="my-helper-text" />
  <span id="my-helper-text">We'll never share your email.</span>
</div>
  • If you are using the TextField component, you just have to provide a unique id.
  • If you are composing the component:
<FormControl>
  <InputLabel htmlFor="my-input">Email address</InputLabel>
  <Input id="my-input" aria-describedby="my-helper-text" />
  <FormHelperText id="my-helper-text">
    We'll never share your email.
  </FormHelperText>
</FormControl>

Complementary projects

For more advanced use cases you might be able to take advantage of: