Back to Ant Design

Input

components/input/index.en-US.md

6.3.711.3 KB
Original Source

When To Use

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

Examples

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">Basic usage</code> <code src="./demo/size.tsx">Three sizes of Input</code> <code src="./demo/variant.tsx" version="5.13.0">Variants</code> <code src="./demo/filled-debug.tsx" debug>Filled Debug</code> <code src="./demo/addon.tsx" debug>Pre / Post tab</code> <code src="./demo/compact-style.tsx">Compact Style</code> <code src="./demo/group.tsx" debug>Input Group</code> <code src="./demo/search-input.tsx">Search box</code> <code src="./demo/search-input-loading.tsx">Search box with loading</code> <code src="./demo/textarea.tsx">TextArea</code> <code src="./demo/autosize-textarea.tsx">Autosizing the height to fit the content</code> <code src="./demo/otp.tsx" version="5.16.0">OTP</code> <code src="./demo/tooltip.tsx">Format Tooltip Input</code> <code src="./demo/presuffix.tsx">prefix and suffix</code> <code src="./demo/password-input.tsx">Password box</code> <code src="./demo/allowClear.tsx">With clear icon</code> <code src="./demo/show-count.tsx">With character counting</code> <code src="./demo/advance-count.tsx" version=">= 5.10.0">Custom count logic</code> <code src="./demo/status.tsx">Status</code> <code src="./demo/focus.tsx">Focus</code> <code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code> <code src="./demo/borderless-debug.tsx" debug>Style Debug</code> <code src="./demo/align.tsx" debug>Text Align</code> <code src="./demo/textarea-resize.tsx" debug>TextArea</code> <code src="./demo/debug-addon.tsx" debug>debug Pre / Post tab</code> <code src="./demo/component-token.tsx" debug>debug token</code>

API

Common props ref:Common props

Input

PropertyDescriptionTypeDefaultVersion
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-
allowClearIf allow to remove input content with clear iconboolean | { clearIcon: ReactNode }false
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>-
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>-
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.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

tsx
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:

PropertyDescriptionTypeDefaultVersion
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>-
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-

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

PropertyDescriptionTypeDefaultVersion
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-
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>-

Supports all props of Input.

Input.Password

PropertyDescriptionTypeDefaultVersion
classNamesSemantic DOM classRecord<SemanticDOM, string>-
iconRenderCustom toggle button(visible) => ReactNode(visible) => (visible ? <EyeOutlined /> : <EyeInvisibleOutlined />)4.3.0
stylesSemantic DOM styleRecord<SemanticDOM, CSSProperties>-
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.

PropertyDescriptionTypeDefaultVersion
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>-
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>-
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.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
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-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

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

FAQ

Why Input loses focus when change prefix/suffix/showCount {#faq-lose-focus}

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:

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

<Input suffix={suffix} />;

Why TextArea in control can make value exceed maxLength? {#faq-textarea-exceed-max}

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