Skip to content

NumberInput

The NumberInput component is similar to TextInput but is specifically designed for numeric input. It ensures that only valid numbers can be entered and includes optional increment and decrement controls for adjusting the value.

The component ensures numeric safety (preserving formatting, clamping min/max), and exposes utility methods via ref for programmatic changes.

To use the NumberInput component, simply import it and pass any required props.

import NumberInput from '@components/Basic/NumberInput/NumberInput';
const App = () => {
return (
<NumberInput />
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component.
refNumberInputRef | undefinedundefinedA reference to the component that gives you access to its methods and the underlying HTML element. Useful if you need to set the value of the input programmatically.
valuestring | number""The initial value of the input. Use this to set the initial value displayed on the input.
minnumberundefinedMinimum value that can be entered or selected.
maxnumberundefinedMaximum value that can be entered or selected.
stepnumber1Increment/decrement step for buttons.
disabledbooleanfalseDisables the input if set to true.
class-disabledstringundefinedOptional class to apply when the input is disabled.
readonlybooleanfalseSpecifies if the input is only able to be read. If set to true, the input will not accept values.
onChange(value: string | number) => voidundefinedA function that is called every time the input’s value has changed. It can be used to retrieve the up to date value of the input.
MethodParametersReturn ValueDescription
changeValue newValue: numbervoidProgrammatically sets a new value in the input field.
clear NonevoidClears the current value of the input.
increaseValue NonevoidIncreases value by the specified step (default is 1).
decreaseValue NonevoidDecreases value by the specified step (default is 1).
AccessorReturn ValueDescription
value string | numberReturns the current value of the input reactively. Use value()to read the latest input.

Defines a button that increases the input’s value. Can be positioned either before or after the input field.

Prop NameTypeDefaultDescription
positionbefore | after"after"Determines whether the button appears before or after the input field.
styleJSX.CSSProperties{}Inline styles applied directly to the visibility button container.
classstring""Additional CSS classes for styling the button.
childrenJSX.Element""Custom content to override the default icons.
import NumberInput from '@components/Basic/NumberInput/NumberInput';
const App = () => {
return (
<NumberInput >
<NumberInput.IncreaseControl>+</NumberInput.IncreaseControl>
</NumberInput>
);
};
export default App;

Defines a button that decreases the input’s value. Can be positioned either before or after the input field.

Prop NameTypeDefaultDescription
positionbefore | after"after"Determines whether the button appears before or after the input field.
styleJSX.CSSProperties{}Inline styles applied directly to the visibility button container.
classstring""Additional CSS classes for styling the button.
childrenJSX.Element""Custom content to override the default icons.
import NumberInput from '@components/Basic/NumberInput/NumberInput';
const App = () => {
return (
<NumberInput >
<NumberInput.DecreaseControl position="before">-</NumberInput.DecreaseControl>
</NumberInput>
);
};
export default App;

Used to override or extend the internal input element. This gives you full control over the input’s look and behavior.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the input element.
classstring""Additional CSS classes to style the input element.
import NumberInput from '@components/Basic/NumberInput/NumberInput';
const App = () => {
return (
<NumberInput >
<NumberInput.Input style={{ padding: '2vmax', color: 'white', background: 'black' }} />
</NumberInput>
);
};
export default App;

Use this to define a placeholder content which will be displayed when the input has no value.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the input element.
classstring""Additional CSS classes to style the input element.
childrenJSX.Element""Content to be rendered inside the placeholder slot. This can be used to add content as the input’s placeholder.
import NumberInput from '@components/Basic/NumberInput/NumberInput';
const App = () => {
return (
<NumberInput >
<NumberInput.Placeholder>Enter nickname</NumberInput.Placeholder>
</NumberInput>
);
};
export default App;

Increase or decrease value programmatically

Section titled “Increase or decrease value programmatically”

You can programmatically control the NumberInput value using its ref, making it easy to respond to keyboard events like arrow key presses.

  1. Declare a ref variable with type NumberInputRef and pass it to the NumberInput component.
  2. Call ref.increaseValue() or ref.decreaseValue() directly as needed.
import NumberInput, { NumberInputRef } from '@components/Basic/NumberInput/NumberInput';
const App = () => {
let inputRef!: NumberInputRef;
const handleKeyDown = (e: KeyboardEvent) => {
if (!inputRef) return;
if (e.keyCode === 38) {
inputRef.increaseValue();
} else if (e.keyCode === 40) {
inputRef.decreaseValue();
}
};
return (
<div onKeyDown={handleKeyDown} tabindex={0}>
<NumberInput ref={inputRef!} max={10} min={0} step={2} />
</div>
);
};
export default App;

All other usage patterns — like retrieving and setting values, or clearing the input — are identical to the TextInput component and can be seen here