Back to Ant Design

Input

components/input/index.zh-CN.md

6.3.711.0 KB
Original Source

何时使用 {#when-to-use}

  • 需要用户输入表单域内容时。
  • 提供组合型输入框,带搜索的输入框,还可以进行大小选择。

代码演示 {#examples}

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">基本使用</code> <code src="./demo/size.tsx">三种大小</code> <code src="./demo/variant.tsx" version="5.13.0">形态变体</code> <code src="./demo/filled-debug.tsx" debug>面性变体 Debug</code> <code src="./demo/addon.tsx" debug>前置/后置标签</code> <code src="./demo/compact-style.tsx">紧凑模式</code> <code src="./demo/group.tsx" debug>输入框组合</code> <code src="./demo/search-input.tsx">搜索框</code> <code src="./demo/search-input-loading.tsx">搜索框 loading</code> <code src="./demo/textarea.tsx">文本域</code> <code src="./demo/autosize-textarea.tsx">适应文本高度的文本域</code> <code src="./demo/otp.tsx" version="5.16.0">一次性密码框</code> <code src="./demo/tooltip.tsx">输入时格式化展示</code> <code src="./demo/presuffix.tsx">前缀和后缀</code> <code src="./demo/password-input.tsx">密码框</code> <code src="./demo/allowClear.tsx">带移除图标</code> <code src="./demo/show-count.tsx">带字数提示</code> <code src="./demo/advance-count.tsx" version=">= 5.10.0">定制计数能力</code> <code src="./demo/status.tsx">自定义状态</code> <code src="./demo/focus.tsx">聚焦</code> <code src="./demo/style-class.tsx" version="6.0.0">自定义语义结构的样式和类</code> <code src="./demo/borderless-debug.tsx" debug>Style Debug</code> <code src="./demo/align.tsx" debug>文本对齐</code> <code src="./demo/textarea-resize.tsx" debug>文本域</code> <code src="./demo/debug-addon.tsx" debug>debug 前置/后置标签</code> <code src="./demo/component-token.tsx" debug>debug token</code>

API

通用属性参考:通用属性

Input

参数说明类型默认值版本
addonAfter带标签的 input,设置后置标签,请使用 Space.Compact 替换ReactNode-
addonBefore带标签的 input,设置前置标签,请使用 Space.Compact 替换ReactNode-
allowClear可以点击清除图标删除内容boolean | { clearIcon: ReactNode }-
bordered是否有边框, 请使用 variant 替换booleantrue4.5.0
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
count字符计数配置CountConfig-5.10.0
defaultValue输入框默认内容string-
disabled是否禁用状态,默认为 falsebooleanfalse
id输入框的 idstring-
maxLength最大长度number-
prefix带有前缀图标的 inputReactNode-
showCount是否展示字数boolean | { formatter: (info: { value: string, count: number, maxLength?: number }) => ReactNode }false4.18.0 info.value: 4.23.0
status设置校验状态'error' | 'warning'-4.19.0
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
size控件大小。注:标准表单内的输入框大小限制为 mediumlarge | medium | small-
suffix带有后缀图标的 inputReactNode-
type声明 input 类型,同原生 input 标签的 type 属性,见:MDN(请直接使用 Input.TextArea 代替 type="textarea")stringtext
value输入框内容string-
variant形态变体outlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.0
onChange输入框内容变化时的回调function(e)-
onPressEnter按下回车的回调function(e)-
onClear按下清除按钮的回调() => void-5.20.0

如果 InputForm.Item 内,并且 Form.Item 设置了 id 属性,则 value defaultValueid 属性会被自动设置。

Input 的其他属性和 React 自带的 input 一致。

CountConfig

tsx
interface CountConfig {
  // 最大字符数,不同于原生 `maxLength`,超出后标红但不会截断
  max?: number;
  // 自定义字符计数,例如标准 emoji 长度大于 1,可以自定义计数策略将其改为 1
  strategy?: (value: string) => number;
  // 同 `showCount`
  show?: boolean | ((args: { value: string; count: number; maxLength?: number }) => ReactNode);
  // 当字符数超出 `count.max` 时的自定义裁剪逻辑,不配置时不进行裁剪
  exceedFormatter?: (value: string, config: { max: number }) => string;
}

Input.TextArea

同 Input 属性,外加:

参数说明类型默认值版本
autoSize自适应内容高度,可设置为 true | false 或对象:{ minRows: 2, maxRows: 6 }boolean | objectfalse
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM , CSSProperties> | (info: { props }) => Record<SemanticDOM , CSSProperties>-

Input.TextArea 的其他属性和浏览器自带的 textarea 一致。

参数说明类型默认值版本
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
enterButton是否有确认按钮,可设为按钮文字。该属性会与 addonAfter 冲突。ReactNodefalse
loading搜索 loadingbooleanfalse
onSearch点击搜索图标、清除图标,或按下回车键时的回调function(value, event, { source: "input" | "clear" })-
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM , CSSProperties> | (info: { props }) => Record<SemanticDOM , CSSProperties>-

其余属性和 Input 一致。

Input.Password

参数说明类型默认值版本
classNames语义化结构 classRecord<SemanticDOM, string>-
iconRender自定义切换按钮(visible) => ReactNode(visible) => (visible ? <EyeOutlined /> : <EyeInvisibleOutlined />)4.3.0
styles语义化结构 styleRecord<SemanticDOM, CSSProperties>-
visibilityToggle是否显示切换按钮或者控制密码显隐boolean | VisibilityToggletrue

Input.OTP

5.16.0 新增。

开发者注意事项:

mask 属性的类型为 string 时,我们强烈推荐接收单个字符或单个 emoji,如果传入多个字符或多个 emoji,则会在控制台抛出警告。

参数说明类型默认值版本
autoComplete输入元素的 autocomplete 属性,例如 one-time-code 可用于 OTP 自动填充string-6.3.0
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
defaultValue默认值string-
disabled是否禁用booleanfalse
formatter格式化展示,留空字段会被 填充(value: string) => string-
separator分隔符,在指定索引的输入框后渲染分隔符ReactNode |((i: number) => ReactNode)-5.24.0
mask自定义展示,和 formatter 的区别是不会修改原始值boolean | stringfalse5.17.0
length输入元素数量number6
status设置校验状态'error' | 'warning'-
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM , CSSProperties> | (info: { props }) => Record<SemanticDOM , CSSProperties>-
size输入框大小small | medium | largemedium
variant形态变体outlined | borderless | filled | underlinedoutlinedunderlined: 5.24.0
value输入框内容string-
onChange当输入框内容全部填充时触发回调(value: string) => void-
onInput输入值变化时触发的回调(value: string[]) => void-5.22.0

VisibilityToggle

参数说明类型默认值版本
visible用于手动控制密码显隐booleanfalse4.24
onVisibleChange显隐密码的回调(visible) => void-4.24

Input Methods

名称说明参数版本
blur取消焦点-
focus获取焦点(option?: { preventScroll?: boolean, cursor?: 'start' | 'end' | 'all' })option - 4.10.0

Semantic DOM

Input {#semantic-input}

<code src="./demo/_semantic_input.tsx" simplify="true"></code>

Input.TextArea {#semantic-textarea}

<code src="./demo/_semantic_textarea.tsx" simplify="true"></code>

Input.Search {#semantic-search}

<code src="./demo/_semantic_search.tsx" simplify="true"></code>

Input.Password {#semantic-password}

<code src="./demo/_semantic_password.tsx" simplify="true"></code>

Input.OTP {#semantic-otp}

<code src="./demo/_semantic_otp.tsx" simplify="true"></code>

主题变量(Design Token){#design-token}

<ComponentTokenTable component="Input"></ComponentTokenTable>

FAQ

为什么我动态改变 prefix/suffix/showCount 时,Input 会失去焦点? {#faq-lose-focus}

当 Input 动态添加或者删除 prefix/suffix/showCount 时,React 会重新创建 DOM 结构而新的 input 是没有焦点的。你可以预设一个空的 <span /> 来保持 DOM 结构不变:

jsx
const suffix = condition ? <Icon type="smile" /> : <span />;

<Input suffix={suffix} />;

为何 TextArea 受控时,value 可以超过 maxLength? {#faq-textarea-exceed-max}

受控时,组件应该按照受控内容展示。以防止在表单组件内使用时显示值和提交值不同的问题。