React Input Number Component
This package provides a foundational and unstyled React component for handling numeric input. It includes built-in increment and decrement controls, making it suitable for building custom number input experiences. The current stable version, under its new scope, is `@rc-component/input-number@1.6.2`. Historically, it was known as `rc-input-number` before being renamed to the scoped package in version 1.5.0. The project maintains an active release cadence, with frequent patch and minor updates, indicating ongoing development and improvements. Its primary differentiator is its low-level, highly customizable nature, providing core functionality without imposing specific UI/UX styles, which allows other UI libraries (like Ant Design) to build upon it, rather than being a full-featured, opinionated component itself.
Common errors
-
Module not found: Can't resolve 'rc-input-number'
cause Attempting to import the component using its old package name after the rename to `@rc-component/input-number`.fixChange your import statement from `import InputNumber from 'rc-input-number';` to `import InputNumber from '@rc-component/input-number';` and update your `package.json`. -
Property 'type' does not exist on type 'InputNumberProps'. Did you mean 'mode'?
cause Using the deprecated `type` prop instead of the `mode` prop on `InputNumber` after upgrading to `@rc-component/input-number@1.5.0` or later.fixRename the prop from `type` to `mode` in your `InputNumber` component usage.
Warnings
- breaking The package name was changed from `rc-input-number` to `@rc-component/input-number` in version 1.5.0. All imports must be updated.
- breaking The `type` prop was renamed to `mode` at `@rc-component/input-number@1.5.0`. This is a breaking API change if you were using the `type` prop.
- breaking The component underwent a semantic refactor at `@rc-component/input-number@1.6.0`, which might introduce subtle behavioral changes or require adjustments if relying on internal DOM structure or specific event handling previously.
- gotcha The `InputNumber` component is unstyled by default. Consumers are responsible for providing their own CSS or styling solutions to make it visually appealing and functional within their application's design system.
Install
-
npm install rc-input-number -
yarn add rc-input-number -
pnpm add rc-input-number
Imports
- InputNumber
import InputNumber from 'rc-input-number';
import InputNumber from '@rc-component/input-number';
- InputNumber (CommonJS)
const InputNumber = require('@rc-component/input-number');const InputNumber = require('@rc-component/input-number').default; - InputNumberProps (TypeScript type)
import { InputNumberProps } from 'rc-input-number';import type { InputNumberProps } from '@rc-component/input-number';
Quickstart
import React, { useState } from 'react';
import InputNumber from '@rc-component/input-number';
// Basic CSS for visibility (you would typically integrate with your design system)
const inputNumberStyles = {
width: 120,
padding: 8,
borderRadius: 4,
border: '1px solid #ccc',
fontSize: 16
};
export default function MyInputNumber() {
const [value, setValue] = useState(10);
const onChange = (newValue: number | null) => {
console.log('Value changed:', newValue);
// The component can return null for empty input, handle as needed
setValue(newValue === null ? 0 : newValue);
};
return (
<div>
<h3>Basic InputNumber Example</h3>
<InputNumber
min={0}
max={100}
step={1}
defaultValue={10}
value={value}
onChange={onChange}
placeholder="Enter a number"
style={inputNumberStyles}
// Use prefixCls for targeted CSS customization
prefixCls="my-custom-input-number"
/>
<p>Current Value: {value}</p>
<button onClick={() => setValue(value + 5)}>Increment by 5</button>
<p>This component is unstyled and requires custom CSS for visual appearance.</p>
</div>
);
}