useInputNumber
Hook that manages numeric input with formatting, locale support, and spin button behavior.
Usage#
import { useInputNumber } from '@primereact/headless/inputnumber';const { inputProps, state } = useInputNumber({
defaultValue: 0
});
return <input {...inputProps} value={state.formattedValue} />;useInputNumber handles numeric parsing, formatting, keyboard-driven increment/decrement, and boundary enforcement. See Primitive for a component-based API.
Features#
- Numeric value state — tracks raw value and a locale-formatted display string in
state.value/state.formattedValue - Spin button semantics — applies
role="spinbutton"and ARIA value properties on the input - Keyboard and pointer stepping —
increment,decrement,stepUp,stepDownplus auto-repeat on pointer hold - Locale-aware formatting — decimal, currency, percent, and grouping configuration via
Intl.NumberFormat - Boundary enforcement —
min/maxclamp on blur and during spin, withminBoundry()/maxBoundry()helpers - Affix support — optional
prefixandsuffixtext parsed back out of the displayed value
Working with callbacks#
Controlled value#
Pass value and onValueChange to drive the number from outside state.
const [value, setValue] = React.useState(0);
useInputNumber({
value,
onValueChange: (e) => setValue(e.value)
});Currency formatting#
Combine mode, currency, and locale to render monetary values without custom parsers.
useInputNumber({
mode: 'currency',
currency: 'USD',
locale: 'en-US'
});Clamped range with custom step#
Use min, max, and step together when the field represents a bounded quantity like a rating or percentage.
useInputNumber({
min: 0,
max: 100,
step: 5
});Fraction precision#
Control decimal precision with minFractionDigits and maxFractionDigits — useful for financial or scientific fields.
useInputNumber({
minFractionDigits: 2,
maxFractionDigits: 4
});Highlight on focus#
Set highlightOnFocus so the input selects its text on focus, letting users overwrite rather than edit.
useInputNumber({ highlightOnFocus: true });Styling with data attributes#
Each part exposes data-scope="inputnumber" and a data-part identifier (root, input). The root reflects state with data-disabled, data-invalid, and data-readonly so descendant selectors can react.
[data-scope='inputnumber'][data-part='input'] {
padding: 0.5rem;
border: 1px solid #ccc;
border-radius: 0.25rem;
}
[data-scope='inputnumber'][data-part='input']:focus {
border-color: var(--p-primary-color);
outline: none;
}
[data-scope='inputnumber'][data-part='root'][data-disabled] [data-part='input'] {
opacity: 0.5;
cursor: not-allowed;
}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 | "expand" | "ceil" | "floor" | "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. | ||
invalid | boolean | false |
| When present, it specifies that the component should have invalid state style. | ||
disabled | boolean | false |
| When present, it specifies that the component should be disabled. | ||
readOnly | boolean | false |
| When present, it specifies that an input field is read-only. | ||
onChange | (event: useInputNumberValueChangeEvent) => void | — |
| Callback to invoke when value changes. | ||
onValueChange | (event: useInputNumberValueChangeEvent) => void | — |
| Callback to invoke after validation check and value change. | ||
Accessibility#
Up/Down arrows increment or decrement, Shift+arrows step by 10, and type-ahead accepts numeric input. See Primitive for full WAI-ARIA compliance details.