useInputNumber
Hook that manages numeric input with formatting, locale support, and spin button behavior.
Usage#
import { useInputNumber } from '@primereact/headless/inputnumber';
const { rootProps, state } = useInputNumber({
defaultValue: 0
});
return <input {...rootProps} />;useInputNumber handles numeric parsing, formatting, keyboard-driven increment/decrement, and boundary enforcement — see Primitive for a component-based API.
Features#
rootPropsreturns spread-ready attributes includingrole="spinbutton", ARIA value properties, and all event handlersstate.valueandstate.formattedValuetrack the raw numeric and display-formatted valuesincrement,decrement,stepUp, andstepDownmethods for programmatic and UI-driven value changesmaxBoundry()andminBoundry()helpers for checking boundary state- Auto-repeating spin on pointer hold with accelerating intervals
Behavior#
Default Value#
Set defaultValue for uncontrolled initial value.
const inputnumber = useInputNumber({ defaultValue: 42 });Controlled#
Pass value with onValueChange for controlled usage.
const [value, setValue] = React.useState(0);
const inputnumber = useInputNumber({
value,
onValueChange: (e) => setValue(e.value)
});Min and Max#
Set min and max to enforce boundaries. Values are clamped on blur and during spin.
const inputnumber = useInputNumber({ min: 0, max: 100 });Step#
Set step to control the increment per spin operation. Defaults to 1.
const inputnumber = useInputNumber({ step: 5 });Locale and Grouping#
Set locale for locale-aware formatting and useGrouping to toggle grouping separators.
const inputnumber = useInputNumber({
locale: 'de-DE',
useGrouping: true
});Currency Mode#
Set mode to 'currency' and specify the currency code for monetary formatting.
const inputnumber = useInputNumber({
mode: 'currency',
currency: 'USD',
locale: 'en-US'
});Fraction Digits#
Control decimal precision with minFractionDigits and maxFractionDigits.
const inputnumber = useInputNumber({
minFractionDigits: 2,
maxFractionDigits: 4
});Prefix and Suffix#
Add custom text around the number with prefix and suffix.
const inputnumber = useInputNumber({
prefix: '$',
suffix: ' USD'
});Highlight on Focus#
Set highlightOnFocus to automatically select the input text when the field receives focus.
const inputnumber = useInputNumber({ highlightOnFocus: true });Custom Styling with Data Attributes#
[data-scope='inputnumber'][data-part='root'] { ... }API#
useInputNumber#
| Name | Type | Default |
|---|---|---|
value | number | — |
| Specifies whether a inputnumber should be checked or not. | ||
defaultValue | number | — |
| Specifies whether a inputnumber should be checked or not. | ||
name | string | — |
| The name attribute for the element, typically used in form submissions. | ||
format | boolean | true |
| Whether to format the value. | ||
locale | string | — |
| Locale to be used in formatting. | ||
localeMatcher | "lookup" | "best fit" | 'best fit' |
| The locale matching algorithm to use. Possible values are 'lookup' and 'best fit'; the default is 'best fit'. See [Locale Negotation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_negotiation) for details. | ||
mode | "decimal" | "currency" | decimal |
| Defines the behavior of the component. | ||
prefix | string | — |
| Text to display before the value. | ||
suffix | string | — |
| Text to display after the value. | ||
currency | string | — |
| The currency to use in currency formatting. Possible values are the [ISO 4217 currency codes](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency), such as 'USD' for the US dollar, 'EUR' for the euro, or 'CNY' for the Chinese RMB. There is no default value; if the style is 'currency', the currency property must be provided. | ||
currencyDisplay | "symbol" | "code" | "name" | "narrowSymbol" | — |
| How to display the currency in currency formatting. Possible values are 'symbol' to use a localized currency symbol such as €, 'code' to use the ISO currency code, 'name' to use a localized currency name such as 'dollar'. | ||
useGrouping | boolean | true |
| Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. | ||
minFractionDigits | number | — |
| The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the [ISO 4217 currency code](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency) list (2 if the list doesn't provide that information). | ||
maxFractionDigits | number | — |
| The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the [ISO 4217 currency code](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency) list (2 if the list doesn't provide that information). | ||
roundingMode | "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | — |
| How decimals should be rounded. [further information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingmode). | ||
min | number | — |
| Minimum boundary value. | ||
max | number | — |
| Maximum boundary value. | ||
step | number | 1 |
| Step factor to increment/decrement the value. | ||
allowEmpty | boolean | true |
| Determines whether the input field is empty. | ||
highlightOnFocus | boolean | false |
| Highlights automatically the input value. | ||
onChange | (event: useInputNumberValueChangeEvent) => void | — |
| Callback to invoke when value changes. | ||
onValueChange | (event: useInputNumberValueChangeEvent) => void | — |
| Callback to invoke after validation check and value change. | ||
Accessibility#
See InputNumber Primitive for WAI-ARIA compliance details and keyboard support.