JGuang

Input

Input · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Input

Through mouse or keyboard input content, it is the most basic form field wrapper.

When To Use

  • A user input in a form field is needed.
  • A search input is required.

Examples

  • Basic usage · basic

  • Three sizes of Input · size

  • Variants · variant

  • Compact Style · compact-style

  • Search box · search-input

  • Search box with loading · search-input-loading

  • TextArea · textarea

  • Autosizing the height to fit the content · autosize-textarea

  • OTP · otp

  • Format Tooltip Input · tooltip

  • prefix and suffix · presuffix

  • Password box · password-input

  • With clear icon · allowClear

  • With character counting · show-count

  • Custom count logic · advance-count

  • Status · status

  • Focus · focus

  • Custom semantic dom styling · style-class

API

Common props ref:Common props

Input

PropertyDescriptionTypeDefaultVersionGlobal Config
addonAfterThe label text displayed after (on the right side of) the input field, please use Space.Compact insteadReactNode-×
addonBeforeThe label text displayed before (on the left side of) the input field, please use Space.Compact insteadReactNode-×
allowClearWhether to show a clear icon to remove input contentboolean | { clearIcon: ReactNode, disabled?: boolean }-disabled: 6.4.05.15.0
borderedWhether has border style, please use variant insteadbooleantrue4.5.0×
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-5.4.05.7.0
countCharacter count configCountConfig-5.10.0×
defaultValueThe initial input contentstring-×
disabledWhether the input is disabledbooleanfalse-×
idThe ID for inputstring-×
maxLengthThe maximum number of characters in Inputnumber-×
prefixThe prefix icon for the InputReactNode-×
showCountWhether to show character countboolean | { formatter: (info: { value: string, count: number, maxLength?: number }) => ReactNode }false4.18.0 info.value: 4.23.0×
statusSet validation status'error' | 'warning'-4.19.0×
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-5.4.05.7.0
sizeThe size of the input box. Note: in the context of a form, the medium size is usedlarge | medium | small-×
suffixThe suffix icon for the InputReactNode-×
typeThe type of input, see: MDN( use Input.TextArea instead of type="textarea")stringtext×
valueThe input content valuestring-×
variantVariants of Inputoutlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.05.19.0
onChangeCallback when user inputfunction(e)-×
onPressEnterThe callback function that is triggered when Enter key is pressedfunction(e)-×
onClearCallback when click the clear button() => void-5.20.0×

When Input is used in a Form.Item context, if the Form.Item has the id props defined then value, defaultValue, and id props of Input are automatically set.

The rest of the props of Input are exactly the same as the original input.

CountConfig

interface CountConfig {
  // Max character count. Different from the native `maxLength`, it will be marked warning but not truncated
  max?: number;
  // Custom character count, for example, the standard emoji length is greater than 1, you can customize the counting strategy to change it to 1
  strategy?: (value: string) => number;
  // Same as `showCount`
  show?: boolean | ((args: { value: string; count: number; maxLength?: number }) => ReactNode);
  // Custom clipping logic when the number of characters exceeds `count.max`, no clipping when not configured
  exceedFormatter?: (value: string, config: { max: number }) => string;
}

Input.TextArea

Same as Input, and more:

PropertyDescriptionTypeDefaultVersionGlobal Config
autoSizeHeight auto size feature, can be set to true | false or an object { minRows: 2, maxRows: 6 }boolean | objectfalse×
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-5.4.05.15.0
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-5.4.05.15.0

The rest of the props of Input.TextArea are the same as the original textarea.

Input.Search

PropertyDescriptionTypeDefaultVersionGlobal Config
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-6.0.06.0.0
enterButtonfalse displays the default button color, true uses the primary color, or you can provide a custom button. Conflicts with addonAfter.ReactNodefalse×
loadingSearch box with loadingbooleanfalse×
onSearchThe callback function triggered when you click on the search-icon, the clear-icon or press the Enter keyfunction(value, event, { source: "input" | "clear" })-×
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-6.0.06.0.0
searchIconCustomize the search iconReactNode-6.4.06.4.0
variantVariants of Inputoutlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.06.6.0

Supports all props of Input.

Input.Password

PropertyDescriptionTypeDefaultVersionGlobal Config
classNamesSemantic DOM classRecord<SemanticDOM, string>-5.4.06.4.0
iconRenderCustom toggle button(visible) => ReactNode(visible) => (visible ? <EyeOutlined /> : <EyeInvisibleOutlined />)4.3.06.4.0
stylesSemantic DOM styleRecord<SemanticDOM, CSSProperties>-5.4.06.4.0
variantVariants of Inputoutlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.06.6.0
visibilityToggleWhether show toggle button or control password visibleboolean | VisibilityToggletrue×

Input.OTP

Added in 5.16.0.

Notes for developers

When the mask prop is string, we recommend receiving a single character or a single emoji. If multiple characters or multiple emoji are passed, a warning will be thrown.

PropertyDescriptionTypeDefaultVersionGlobal Config
autoCompleteThe autocomplete attribute for input elements, e.g. one-time-code for OTP autofillstring-6.3.0×
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-6.0.06.0.0
defaultValueDefault valuestring-×
disabledWhether the input is disabledbooleanfalse×
formatterFormat display, blank fields will be filled with (value: string) => string-×
separatorrender the separator after the input box of the specified indexReactNode |((i: number) => ReactNode)-5.24.0×
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-6.0.06.0.0
maskCustom display, the original value will not be modifiedboolean | stringfalse5.17.0×
lengthThe number of input elementsnumber6×
statusSet validation status'error' | 'warning'-×
sizeThe size of the input boxsmall | medium | largemedium×
variantVariants of Inputoutlined | borderless | filled | underlinedoutlinedunderlined: 5.24.06.6.0
valueThe input content valuestring-×
onChangeTrigger when all the fields are filled(value: string) => void-×
onInputTrigger when the input value changes(value: string[]) => void-5.22.0×

VisibilityToggle

PropertyDescriptionTypeDefaultVersion
tabIndexSet the tabIndex of the toggle buttonnumber06.5.0
visibleWhether the password is show or hidebooleanfalse4.24.0
onVisibleChangeCallback executed when visibility of the password is changed(visible) => void-4.24.0

Input Methods

NameDescriptionParametersVersion
blurRemove focus-
focusGet focus(option?: { preventScroll?: boolean, cursor?: 'start' | 'end' | 'all' })option - 4.10.0

Semantic DOM

Input

Semantic nodeNative descriptionVersion
rootRoot element with relative positioning, inline-block display, width, min-width, padding, colors, fonts, line-height, border-radius, transitions and other input container basic styles—
prefixPrefix wrapper element with layout and styling for prefix content—
inputInput element with core interactive styles and text input related styling—
suffixSuffix wrapper element with layout and styling for suffix content—
clearClear button element with layout, visibility, and interaction styles for clearing content6.4.0
countCharacter count element with font and color styles for count display—

Semantic description source: vendor/components/input/demo/_semantic_input.tsx

Input.TextArea

Semantic nodeNative descriptionVersion
rootRoot element with textarea wrapper styles, border, border radius, transition animation and state control—
textareaTextarea element with font, line height, padding, color, background, border, text input and multi-line text display styles—
clearClear button element with layout, visibility, and interaction styles for clearing content6.4.0
countCount element with character count display position, font, color and numeric statistics styles—

Semantic description source: vendor/components/input/demo/_semantic_textarea.tsx

Semantic nodeNative descriptionVersion
rootroot element—
prefixprefix element—
inputinput element—
suffixsuffix element—
clearclear button element6.4.0
countcount element—
button.rootbutton root element—
button.iconbutton icon element—
button.contentbutton content element—

Semantic description source: vendor/components/input/demo/_semantic_search.tsx

Input.Password

Semantic nodeNative descriptionVersion
rootroot element—
prefixprefix element—
inputinput element—
suffixsuffix element—
clearclear button element6.4.0
countcount element—

Semantic description source: vendor/components/input/demo/_semantic_password.tsx

Input.OTP

Semantic nodeNative descriptionVersion
rootRoot element, set inline flex layout, alignment, column gap and wrapper styles—
inputInput element, set text center, padding and number input styles—
separatorSeparator element, set separator display styles between OTP input boxes—

Semantic description source: vendor/components/input/demo/_semantic_otp.tsx

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

Native ComponentToken declares no directly extractable own fields. Shared tokens and inheritance are defined by the declarations below.

Declaration source: vendor/components/input/style/token.ts

import { mergeToken } from '../../theme/internal';

import type { AliasToken, FullToken } from '../../theme/internal';

export interface SharedComponentToken {
  /**
   * @desc 输入框横向内边距
   * @descEN Horizontal padding of input
   */
  paddingInline: number;
  /**
   * @desc 小号输入框横向内边距
   * @descEN Horizontal padding of small input
   */
  paddingInlineSM: number;
  /**
   * @desc 大号输入框横向内边距
   * @descEN Horizontal padding of large input
   */
  paddingInlineLG: number;
  /**
   * @desc 输入框纵向内边距
   * @descEN Vertical padding of input
   */
  paddingBlock: number;
  /**
   * @desc 小号输入框纵向内边距
   * @descEN Vertical padding of small input
   */
  paddingBlockSM: number;
  /**
   * @desc 大号输入框纵向内边距
   * @descEN Vertical padding of large input
   */
  paddingBlockLG: number;
  /**
   * @desc 前/后置标签背景色
   * @descEN Background color of addon
   */
  addonBg: string;
  /**
   * @desc 悬浮态边框色
   * @descEN Hover border color
   */
  hoverBorderColor: string;
  /**
   * @desc 激活态边框色
   * @descEN Active border color
   */
  activeBorderColor: string;
  /**
   * @desc 激活态阴影
   * @descEN Box-shadow when active
   */
  activeShadow: string;
  /**
   * @desc 错误状态时激活态阴影
   * @descEN Box-shadow when active in error status
   */
  errorActiveShadow: string;
  /**
   * @desc 警告状态时激活态阴影
   * @descEN Box-shadow when active in warning status
   */
  warningActiveShadow: string;
  /**
   * @desc 输入框hover状态时背景颜色
   * @descEN Background color when the input box hovers
   */
  hoverBg: string;
  /**
   * @desc 输入框激活状态时背景颜色
   * @descEN Background color when the input box is activated
   */
  activeBg: string;
  /**
   * @desc 字体大小
   * @descEN Font size
   */
  inputFontSize: number;
  /**
   * @desc 大号字体大小
   * @descEN Font size of large
   */
  inputFontSizeLG: number;
  /**
   * @desc 小号字体大小
   * @descEN Font size of small
   */
  inputFontSizeSM: number;
}

export interface ComponentToken extends SharedComponentToken {}

export interface SharedInputToken {
  inputAffixPadding: number;
}

export interface InputToken extends FullToken<'Input'>, SharedInputToken {}

FAQ

Why Input loses focus when change prefix/suffix/showCount

When Input dynamic add or remove prefix/suffix/showCount will make React recreate the dom structure and new input will be not focused. You can set an empty <span /> element to keep the dom structure:

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

<Input suffix={suffix} />;

Why TextArea in control can make value exceed maxLength?

When in control, component should show as what it set to avoid submit value not align with store value in Form.

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/input/Group.tsx
  • vendor/components/input/Input.tsx
  • vendor/components/input/OTP/OTPInput.tsx
  • vendor/components/input/OTP/index.tsx
  • vendor/components/input/Password.tsx
  • vendor/components/input/Search.tsx
  • vendor/components/input/TextArea.tsx
  • vendor/components/input/index.tsx
  • vendor/components/input/style/token.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.