Input
The Input component provides users with a field to enter and edit text.
Introduction
An input is a UI element that accepts text data from the user.
The Input component replaces the native HTML <input>
tag, and offers expanded customization and accessibility features.
It can also be transformed into a <textarea>
as needed.
Component
Usage
After installation, you can start building with this component using the following basic elements:
import Input from '@mui/base/Input';
export default function MyApp() {
return <Input />;
}
Basics
Input behaves similarly to the native HTML <input>
, except that it's nested inside of a root <div>
—see Anatomy for details.
The following demo shows how to create and style an input component, including placeholder
text:
Anatomy
The Input component is composed of a root <div>
slot that houses one interior <input>
slot:
<div class="MuiInput-root">
<input class="MuiInput-input" />
</div>
Custom structure
Use the slots
prop to override the root or any other interior slot:
<Input slots={{ root: 'aside', input: CustomInput }} />
Use the slotProps
prop to pass custom props to internal slots.
The following code snippet applies a CSS class called my-input
to the input slot:
<Input slotProps={{ input: { className: 'my-input' } }} />
Usage with TypeScript
In TypeScript, you can specify the custom component type used in the slots.root
as a generic parameter of the unstyled component. This way, you can safely provide the custom root's props directly on the component:
<Input<typeof CustomComponent> slots={{ root: CustomComponent }} customProp />
The same applies for props specific to custom primitive elements:
<Input<'textarea'> slots={{ root: 'textarea' }} rows={2} />
Hook
import useInput from '@mui/base/useInput';
The useInput
hook lets you apply the functionality of an input to a fully custom component.
It returns props to be placed on the custom component, along with fields representing the component's internal state.
Hooks do not support slot props, but they do support customization props.
The demo below shows how to use the useInput
hook to create a custom input component that receives all the necessary props:
Customization
Adornments
You can use the startAdornment
and endAdornment
props to add a prefix, suffix, or an action to an input.
Common use cases of adornments include:
- when an input receives a specific unit of measure (like weight or currency)
- when an icon button toggles hiding/showing a password
The following demo shows examples of both of these use cases:
If you want the <textarea>
to grow with the content, you can use the Textarea Autosize component within the input.
When using Textarea Autosize, the height of the <textarea>
element dynamically matches its content unless you set the rows
prop.
To set minimum and maximum sizes, add the minRows
and maxRows
props.
The following demo shows how to insert a Textarea Autosize component into an Input so that its height grows with the length of the content: