无样式的输入框
The InputUnstyled component provides users with a field to enter and edit text.
简介
输入框是用来接受用户输入的文本的 UI 组件。 InputUnstyled
组件取代了原生的 HTML <input>
标签,可以在需要时转换成 <textarea>
。
功能特色
- ✨ Supports start and end adornments
- 🚀 可以添加
multiline
属性转换成<textarea>
- ♿️ Automatically adds the appropriate ARIA roles
Component
Usage
After installation, you can start building with this component using the following basic elements:
import InputUnstyled from '@mui/base/InputUnstyled';
export default function MyApp() {
return <InputUnstyled />;
}
Basics
InputUnstyled
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 InputUnstyled
component is composed of a root <div>
slot that houses one interior <input>
slot:
<div class="MuiInput-root">
<input class="MuiInput-input" />
</div>
Slot props
Use the component
prop to override the root slot with a custom element:
<InputUnstyled component="aside" />
Use the components
prop to override any interior slots in addition to the root:
<InputUnstyled components={{ Root: 'aside' }} />
Use the componentsProps
prop to pass custom props to internal slots. The following code snippet applies a CSS class called my-input
to the input slot:
<InputUnstyled componentsProps={{ input: { className: 'my-input' } }} />
Hook
import { useInput } from '@mui/base/InputUnstyled';
The useInput
hook lets you apply the functionality of InputUnstyled
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:
<CustomInput aria-label="Demo input" multiline placeholder="Type something…" />
If you want the <textarea>
to grow with the content, you can use the TextareaAutosize
component within the input.
When using TextareaAutosize
, 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 TextareaAutosize
into InputUnstyled
so that its height will grow with the length of the content:
<CustomInput aria-label="Demo input" multiline placeholder="Type something…" />