跳转到内容

Input API

想要学习更多组件属性和 CSS API,可以查阅 React Input 组件的 API 文档。

导入

import Input from '@mui/material/Input';
// 或
import { Input } from '@mui/material';
你可以阅读这个关于最小化捆绑包的指南以了解以上二者的差异。

组件名称

在主题中,名称“MuiInput”可用于提供默认属性(props),或者样式覆盖

属性

InputBase 组件的属性也是可用的。

名称类型默认值描述
autoCompletestring
This prop helps users to fill forms faster, especially on mobile devices. The name can be confusing, as it's more like an autofill. You can learn more about it following the specification.
autoFocusboolfalse
If true, the input element is focused during the first mount.
classesobject
Override or extend the styles applied to the component. See CSS API below for more details.
color'primary'
| 'secondary'
| string
The color of the component. It supports both default and custom theme colors, which can be added as shown in the palette customization guide. The prop defaults to the value ('primary') inherited from the parent FormControl component.
components{ Input?: elementType, Root?: elementType }{}
The components used for each slot inside the InputBase. Either a string to use a HTML element or a component.
componentsProps{ input?: object, root?: object }{}
The props used for each slot inside the Input.
defaultValueany
The default input value. Use when the component is not controlled.
disabledboolfalse
If true, the input element is disabled. The prop defaults to the value (false) inherited from the parent FormControl component.
disableUnderlineboolfalse
If true, the input will not have an underline.
endAdornmentnode
End InputAdornment for this component.
errorboolfalse
If true, the input will indicate an error. The prop defaults to the value (false) inherited from the parent FormControl component.
fullWidthboolfalse
If true, the input will take up the full width of its container.
idstring
The id of the input element.
inputComponentelementType'input'
The component used for the input element. Either a string to use a HTML element or a component.
inputPropsobject{}
Attributes applied to the input element.
inputRefref
Pass a ref to the input element.
margin'dense'
| 'none'
If dense, will adjust vertical spacing. This is normally obtained via context from FormControl. The prop defaults to the value ('none') inherited from the parent FormControl component.
maxRowsnumber
| string
Maximum number of rows to display when multiline option is set to true.
minRowsnumber
| string
Minimum number of rows to display when multiline option is set to true.
multilineboolfalse
If true, a TextareaAutosize element is rendered.
namestring
Name attribute of the input element.
onChangefunc
Callback fired when the value is changed.

Signature:
function(event: object) => void
event: The event source of the callback. You can pull out the new value by accessing event.target.value (string).
placeholderstring
The short hint displayed in the input before the user enters a value.
readOnlyboolfalse
It prevents the user from changing the value of the field (not from interacting with the field).
requiredboolfalse
If true, the input element is required. The prop defaults to the value (false) inherited from the parent FormControl component.
rowsnumber
| string
Number of rows to display when multiline option is set to true.
startAdornmentnode
Start InputAdornment for this component.
sxArray<func
| object
| bool>
| func
| object
The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.
typestring'text'
Type of the input element. It should be a valid HTML5 input type.
valueany
The value of the input element, required for a controlled component.

ref 则会被传递到根元素中。

继承

尽管上文没有明文记录, InputBase 组件 的属性(props) 在组件Input上同样是可用的。 你可以利用这一点来 指向嵌套组件

CSS

规则名称全局类名描述
root.MuiInput-rootStyles applied to the root element.
formControl.MuiInput-formControlStyles applied to the root element if the component is a descendant of FormControl.
focused.Mui-focusedStyles applied to the root element if the component is focused.
disabled.Mui-disabledStyles applied to the root element if disabled={true}.
colorSecondary.MuiInput-colorSecondaryStyles applied to the root element if color secondary.
underline.MuiInput-underlineStyles applied to the root element unless disableUnderline={true}.
error.Mui-errorPseudo-class applied to the root element if error={true}.
sizeSmall.MuiInput-sizeSmallStyles applied to the input element if size="small".
multiline.MuiInput-multilineStyles applied to the root element if multiline={true}.
fullWidth.MuiInput-fullWidthStyles applied to the root element if fullWidth={true}.
input.MuiInput-inputStyles applied to the input element.
inputSizeSmall.MuiInput-inputSizeSmallStyles applied to the input element if size="small".
inputMultiline.MuiInput-inputMultilineStyles applied to the input element if multiline={true}.
inputTypeSearch.MuiInput-inputTypeSearchStyles applied to the input element if type="search".

您可以使用组件自定义选项对组件进行个性化:

演示项目