Skip to content

Repository files navigation

@rc-component/input-number

Ant Design Part of the Ant Design ecosystem.

🔢 Accessible React number input with precision, formatting, keyboard, and wheel support.

NPM versionnpm downloadsbuild statusCodecovbundle sizedumi

English | 简体中文

Highlights

  • Controlled and uncontrolled numeric input modes.
  • Decimal precision, custom parser, formatter, and decimal separator support.
  • Keyboard stepping, mouse wheel stepping, and custom step handlers.
  • Prefix, suffix, spinner controls, and semantic classNames / styles slots.
  • TypeScript generic value typing for number and string based value flows.

Install

npm install @rc-component/input-number

Usage

importInputNumberfrom'@rc-component/input-number';exportdefault()=><InputNumbermin={0}max={100}defaultValue={10}/>;
importInputNumberfrom'@rc-component/input-number';exportdefault()=>(<InputNumberstringModeformatter={(value)=>`$ ${value}`}parser={(displayValue)=>displayValue?.replace(/\$\s?/g,'')||''}/>);

Examples

Run the local dumi site:

npm install
npm start

Then open http://localhost:8000.

API

InputNumber

PropertyTypeDefaultDescription
autoFocusbooleanfalseFocus the input when mounted.
changeOnBlurbooleantrueCommit value changes on blur.
changeOnWheelbooleanfalseAllow value changes from the mouse wheel.
classNamestring-Class name for the root element.
classNamesPartial<Record<SemanticName, string>>-Semantic class names for input-number slots.
controlsbooleantrueShow increment and decrement controls.
decimalSeparatorstring-Decimal separator used by the display formatter.
defaultValueT-Initial value.
disabledbooleanfalseDisable the input.
downHandlerReactNode-Custom decrement control.
formatter(value: T | undefined, info: { userTyping: boolean; input: string }) => string-Format the displayed value.
inputModestring-Native input inputMode attribute.
keyboardbooleantrueEnable keyboard stepping.
maxT-Maximum value.
minT-Minimum value.
mode'input' | 'spinner'inputRender mode.
parser(displayValue: string | undefined) => T-Parse the displayed value back to a value.
precisionnumber-Display precision.
prefixReactNode-Prefix content.
prefixClsstringrc-input-numberClass name prefix.
readOnlybooleanfalseMark the input as read only.
stepnumber | string1Step size.
stringModebooleanfalseKeep values as strings for high precision decimals.
styleReact.CSSProperties-Inline styles for the root element.
stylesPartial<Record<SemanticName, React.CSSProperties>>-Semantic styles for input-number slots.
suffixReactNode-Suffix content.
upHandlerReactNode-Custom increment control.
valueT | null-Controlled value.
onChange(value: T | null) => void-Triggered when the committed value changes.
onInput(text: string) => void-Triggered when the raw input text changes.
onPressEnterReact.KeyboardEventHandler<HTMLInputElement>-Triggered when Enter is pressed.
onStep(value: T, info: { offset: number | string; type: 'up' | 'down'; emitter: 'handler' | 'keyboard' | 'wheel' }) => void-Triggered when the value changes by step.

Native input attributes such as id, name, placeholder, required, readOnly, and tabIndex are also supported unless explicitly overridden above.

Ref

importInputNumber,{typeInputNumberRef}from'@rc-component/input-number';constref=React.useRef<InputNumberRef>(null);ref.current?.focus();ref.current?.blur();
PropertyTypeDescription
focus(options?: InputFocusOptions) => voidFocus the input.
blur() => voidBlur the input.
nativeElementHTMLElementRoot native element.

Keyboard And Wheel

  • Arrow up and Arrow down change the value by step.
  • Shift + Arrow changes the value by 10 * step.
  • Ctrl / Command + Arrow changes the value by 0.1 * step.
  • Mouse wheel changes are opt-in through changeOnWheel.

Development

npm install
npm start
npm test
npm run tsc
npm run compile
npm run build

The dumi site runs at http://localhost:8000 by default.

Release

npm run prepublishOnly

The release flow is handled by @rc-component/np through the rc-np command after the package build.

License

@rc-component/input-number is released under the MIT license.

About

🔢 Accessible React number input with precision, formatting, keyboard, and wheel support.

Topics

Resources

Stars

323 stars

Watchers

27 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages