JGuang

InputNumber

InputNumber · Ant Design

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

InputNumber

Enter a number within certain range with the mouse or keyboard.

When To Use

When a numeric value needs to be provided.

Examples

  • Basic · basic

  • Sizes · size

  • Disabled · disabled

  • High precision decimals · digit

  • Formatter · formatter

  • Keyboard · keyboard

  • Wheel · change-on-wheel

  • Variants · variant

  • Spinner · spinner

  • Out of range · out-of-range

  • Prefix / Suffix · presuffix

  • Status · status

  • Focus · focus

  • Custom semantic dom styling · style-class

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersionGlobal Config
addonAfterThe label text displayed after (on the right side of) the input field, please use Space.Compact insteadReactNode-4.17.0×
addonBeforeThe label text displayed before (on the left side of) the input field, please use Space.Compact insteadReactNode-4.17.0×
changeOnBlurTrigger onChange when blur. e.g. reset value in range by blurbooleantrue5.11.0×
changeOnWheelAllows control with mouse wheelboolean-5.14.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
controlsWhether to show +- controls, or set custom arrow iconsboolean | { upIcon?: React.ReactNode; downIcon?: React.ReactNode; }-×
decimalSeparatorDecimal separatorstring--×
placeholderPlaceholderstring-×
defaultValueThe initial valuenumber--×
disabledIf the input is disabledbooleanfalse-×
formatterSpecifies the format of the value presentedfunction(value: number | string, info: { userTyping: boolean, input: string }): string-×
keyboardIf keyboard behavior is enabledbooleantrue×
maxThe max valuenumberNumber.MAX_SAFE_INTEGER-×
minThe min valuenumberNumber.MIN_SAFE_INTEGER-×
parserSpecifies the value extracted from formatterfunction(string): number--×
precisionThe precision of input value. Will use formatter when config of formatternumber--×
readOnlyIf the input is readonlybooleanfalse-×
statusSet validation status'error' | 'warning'-×
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
prefixThe prefix icon for the InputReactNode-×
suffixThe suffix icon for the InputReactNode-5.20.0×
sizeThe height of input boxlarge | medium | small--×
stepThe number to which the current value is increased or decreased. It can be an integer or decimalnumber | string1-×
stringModeSet value as string to support high precision decimals. Will return string value by onChangebooleanfalse4.13.0×
modeShow input or spinner'input' | 'spinner''input'×
valueThe current value of the componentnumber--×
variantVariants of Inputoutlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.05.19.0
onChangeThe callback triggered when the value is changedfunction(value: number | string | null)--×
onPressEnterThe callback function that is triggered when Enter key is pressedfunction(e)--×
onStepThe callback function that is triggered when click up or down buttons / Keyboard / Wheel(value: number, info: { offset: number, type: 'up' | 'down', emitter: 'handler' | 'keydown' | 'wheel' }) => void--×
borderedWhether has border style, please use variant insteadbooleantrue-×

Ref

NameDescriptionTypeVersion
blur()Remove focus-
focus()Get focus(option?: { preventScroll?: boolean, cursor?: 'start' | 'end' | 'all' })cursor - 5.22.0
nativeElementThe native DOM element-5.17.3

Semantic DOM

Semantic nodeNative descriptionVersion
rootRoot element, sets inline-block layout, width, border radius and reset styles—
prefixPrefix wrapper element, sets flex layout, alignment and right margin styles—
inputInput element, sets font, line height, text input and interaction styles—
suffixSuffix wrapper element, sets flex layout, margin and transition animation styles—
actionsActions element, sets absolute positioning, width, flex layout and number adjustment button styles—
actionSingle action button element, sets button styling, hover effects and click interactions—

Semantic description source: vendor/components/input-number/demo/_semantic.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.

TokenTypeNative description
controlWidthnumberWidth of input
handleWidthnumberWidth of control button
handleFontSizenumberIcon size of control button
handleVisible'auto' | trueHandle visible
handleBgstringBackground color of handle
handleActiveBgstringActive background color of handle
handleHoverColorstringHover color of handle
handleBorderColorstringBorder color of handle
filledHandleBgstringBackground color of handle in filled variant
handleOpacitynumberSee native declaration and inherited types
handleVisibleWidthnumberSee native declaration and inherited types

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

import { FastColor } from '@ant-design/fast-color';

import type { SharedComponentToken, SharedInputToken } from '../../input/style/token';

import { initComponentToken } from '../../input/style/token';

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

export interface ComponentToken extends SharedComponentToken {
  /**
   * @desc 输入框宽度
   * @descEN Width of input
   */
  controlWidth: number;
  /**
   * @desc 操作按钮宽度
   * @descEN Width of control button
   */
  handleWidth: number;
  /**
   * @desc 操作按钮图标大小
   * @descEN Icon size of control button
   */
  handleFontSize: number;
  /**
   * Default `auto`. Set `true` will always show the handle
   * @desc 操作按钮可见性
   * @descEN Handle visible
   */
  handleVisible: 'auto' | true;
  /**
   * @desc 操作按钮背景色
   * @descEN Background color of handle
   */
  handleBg: string;
  /**
   * @desc 操作按钮激活背景色
   * @descEN Active background color of handle
   */
  handleActiveBg: string;
  /**
   * @desc 操作按钮悬浮颜色
   * @descEN Hover color of handle
   */
  handleHoverColor: string;
  /**
   * @desc 操作按钮边框颜色
   * @descEN Border color of handle
   */
  handleBorderColor: string;
  /**
   * @desc 面性变体操作按钮背景色
   * @descEN Background color of handle in filled variant
   */
  filledHandleBg: string;
  /**
   * @internal
   */
  handleOpacity: number;
  /**
   * @internal
   */
  handleVisibleWidth: number;
}

export type InputNumberToken = FullToken<'InputNumber'> & SharedInputToken;

Notes

Per issues #21158, #17344, #9421, and documentation about inputs, it appears this community does not support native inclusion of the type="number" in the <Input /> attributes, so please feel free to include it as needed, and be aware that it is heavily suggested that server side validation be utilized, as client side validation can be edited by power users.

FAQ

Why value can exceed min or max in control?

Developer handle data by their own in control. It will make data out of sync if InputNumber changes display value. It also cause potential data issues when use in form.

Why dynamic change min or max which makes value out of range will not trigger onChange?

onChange is user trigger event. Auto-triggering would prevent form libraries from detecting the data modification source.

Why onBlur or other event can not get correct value?

InputNumber's value is wrapped by internal logic. The event.target.value you get from onBlur or other event is the DOM element's value instead of the actual value of InputNumber. For example, if you change the display format through formatter or decimalSeparator, you will get the formatted string in the DOM. You should always get the current value through onChange.

Why changeOnWheel unable to control whether the mouse scroll wheel changes value?

The use of the type attribute is deprecated

The InputNumber component allows you to use all the attributes of the input element and ultimately pass them to the input element, This attribute will also be added to the input element when you pass in type='number', which will activate native behavior (allowing the mouse wheel to change the value), As a result changeOnWheel cannot control whether the mouse wheel changes the value.

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-number/index.tsx
  • vendor/components/input-number/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.