跳转到内容
+

输入框

Input 组件为用户提供了一个用于输入和编辑文本的字段。

useInput API

导入

import { useInput } from '@mui/base/useInput';
// or
import { useInput } from '@mui/base';

通过阅读这份关于最小化包大小的指南,了解更多关于差异的信息。

参数

名称类型描述
defaultValueunknown

默认值。当组件不受控制时使用。

disabledboolean

如果为 true,则组件被禁用。该 prop 默认为从父 FormControl 组件继承的值 (false)。

errorboolean

如果为 true,则 input 将通过设置 aria-invalid 属性来指示错误。该 prop 默认为从父 FormControl 组件继承的值 (false)。

inputRefReact.Ref<HTMLInputElement | HTMLTextAreaElement>
onBlurReact.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>
onChangeReact.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>
onClickReact.MouseEventHandler
onFocusReact.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>
requiredboolean

如果为 true,则 input 元素为必填项。该 prop 默认为从父 FormControl 组件继承的值 (false)。

valueunknown

返回值

名称类型描述
disabledboolean

如果为 true,则组件将被禁用。

errorboolean

如果为 true,则 input 将通过设置 aria-invalid 属性来指示错误。

focusedboolean

如果为 true,则 input 将被聚焦。

formControlContextFormControlState | undefined

useFormControlContext hook 返回的值。

getInputProps<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputInputSlotProps<ExternalProps>

input slot 的 props 的解析器。

getRootProps<ExternalProps extends Record<string, any> = {}>(externalProps?: ExternalProps) => UseInputRootSlotProps<ExternalProps>

root slot 的 props 的解析器。

inputRefReact.RefCallback<HTMLInputElement | HTMLTextAreaElement> | null
requiredboolean

如果为 true,则 input 将指示它是必填项。

valueunknown

input 元素的 value