InputNumber is an input component to provide numerical input.
import { InputNumber } from 'primereact/inputnumber';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
InputNumber is used as a controlled input with value and onValueChange properties. Component always provides a number type although formatting on the input is a string.
<InputNumber value={value} onValueChange={(e) => setValue(e.value)} />
Format is defined using the mode property, "decimal" is the default value allowing only integers when there is no other configuration.
<InputNumber value={value} onValueChange={(e) => setValue(e.value)} mode="decimal" />
Fractions are configured with the minFractionDigits property. Optionally maxFractionDigits can be used to defined a boundary for the maximum digits.
<InputNumber value={value1} onValueChange={(e) => setValue1(e.value)} mode="decimal" minFractionDigits={2} />
<InputNumber value={value2} onValueChange={(e) => setValue2(e.value)} mode="decimal" minFractionDigits={2} maxFractionDigits={2} />
locale option is available to set the localization information such as grouping and decimal symbols where default value is the browser locale. Locales are defined per BCP Language Tag.
User Locale
<InputNumber value={value1} onValueChange={(e) => setValue1(e.value)} mode="decimal" minFractionDigits={2} />
United State Locale
<InputNumber value={value2} onValueChange={(e) => setValue2(e.value)} mode="decimal" locale="en-US" minFractionDigits={2}/>
German Locale
<InputNumber value={value3} onValueChange={(e) => setValue3(e.value)} mode="decimal" locale="de-DE" minFractionDigits={2}/>
Indian Locale
<InputNumber value={value4} onValueChange={(e) => setValue4(e.value)} mode="decimal" locale="en-IN" minFractionDigits={2} />
Currency formatting is specified by setting the mode option to currency and currency property. In addition currencyDisplay option allows how the currency is displayed, valid values are "symbol" (default) or "code".
United States
<InputNumber value={value1} onValueChange={(e) => setValue1(e.value)} mode="currency" currency="USD" locale="en-US" />
Germany
<InputNumber value={value2} onValueChange={(e) => setValue2(e.value)} mode="currency" currency="EUR" locale="de-DE" />
India
<InputNumber value={value3} onValueChange={(e) => setValue3(e.value)} mode="currency" currency="INR" currencyDisplay="code" locale="en-IN"/>
Japan
<InputNumber value={value4} onValueChange={(e) => setValue4(e.value)} mode="currency" currency="JPY" locale="jp-JP"/>
Custom texts e.g. units can be placed before or after the input section with the prefix and suffix properties.
Mile
<InputNumber value={value1} onValueChange={(e) => setValue1(e.value)} suffix=" mi" />
Percent
<InputNumber value={value2} onValueChange={(e) => setValue2(e.value)} prefix="%" />
Expiry
<InputNumber value={value3} onValueChange={(e) => setValue3(e.value)} prefix="Expires in " suffix=" days" />
Temperature
<InputNumber value={value4} onValueChange={(e) => setValue4(e.value)} prefix="↑ " suffix="℃" min={0} max={40} />
Spinner buttons is enabled using the showButtons options and layout is defined with the buttonLayout. Default value is "stacked" whereas "horizontal" and "stacked" are alternatives. Note that even there are no buttons, up and down arrow keys can be used to spin the values with keyboard.
Stacked
<InputNumber value={value1} onValueChange={(e) => setValue1(e.value)} showButtons mode="currency" currency="USD" />
Horizontal
<InputNumber value={value2} onValueChange={(e) => setValue2(e.value)} showButtons buttonLayout="horizontal"
decrementButtonClassName="p-button-danger" incrementButtonClassName="p-button-success" incrementButtonIcon="pi pi-plus" decrementButtonIcon="pi pi-minus" mode="currency" currency="EUR"/>
Vertical
<InputNumber value={value3} onValueChange={(e) => setValue3(e.value)} mode="decimal" showButtons buttonLayout="vertical" style={{width: '6em'}}
decrementButtonClassName="p-button-secondary" incrementButtonClassName="p-button-secondary" incrementButtonIcon="pi pi-plus" decrementButtonIcon="pi pi-minus" />
Step factor is 1 by default and can be customized with step option.
<InputNumber value={value} onValueChange={(e) => setValue(e.value)} step={0.25} />
Value to be entered can be restricted by configuring the min and max options.
<InputNumber value={value} onValueChange={(e) => setValue1(e.value)} min={0} max={100} />
Name | Type | Default | Description |
---|---|---|---|
value | number | null | Value of the component. |
format | boolean | true | Whether to format the value. |
showButtons | boolean | false | Displays spinner buttons. |
buttonLayout | string | stacked | Layout of the buttons, valid values are "stacked" (default), "horizontal" and "vertical". |
incrementButtonClassName | string | null | Style class of the increment button. |
decrementButtonClassName | string | null | Style class of the decrement button. |
incrementButtonIcon | string | pi pi-caret-up | Style class of the increment button. |
decrementButtonIcon | string | pi pi-caret-down | Style class of the decrement button. |
locale | string | null | Locale to be used in formatting. |
localeMatcher | string | best fit | The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". See Locale Negotation for details. |
mode | string | decimal | Defines the behavior of the component, valid values are "decimal" and "currency". |
prefix | string | null | Text to display before the value. |
suffix | string | decimal | Text to display after the value. |
currency | string | null | The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, 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 | string | symbol | 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"; the default is "symbol". |
useGrouping | boolean | true | Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. |
minFractionDigits | number | null | 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 list (2 if the list doesn't provide that information). |
maxFractionDigits | number | null | 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 list(2 if the list doesn't provide that information). |
id | string | null | Identifier of the element. |
name | string | null | Name of the input element. |
type | string | text | Type of the input element. |
allowEmpty | boolean | true | Determines whether the input field is empty. |
step | number | 1 | Step factor to increment/decrement the value. |
min | number | null | Mininum boundary value. |
max | number | null | Maximum boundary value. |
disabled | boolean | false | When present, it specifies that the element should be disabled. |
required | boolean | false | When present, it specifies that an input field must be filled out before submitting the form. |
tabIndex | number | null | Index of the element in tabbing order. |
autoFocus | boolean | false | When present, it specifies that the component should automatically get focus on load. |
pattern | string | null | The pattern attribute specifies a regular expression that the element's value is checked against on form submission. |
inputmode | string | null | The inputmode attribute provides a hint to browsers for devices with onscreen keyboards to help them decide which keyboard to display. |
placeholder | string | null | Hint text for the input field. |
readOnly | boolean | false | When present, it specifies that the element should be read-only. |
size | number | null | Size of the input field. |
style | string | null | Inline style of the component. |
className | string | null | Style class of the element. |
inputId | string | null | Identifier of the input element. |
inputStyle | string | null | Inline style of the input field. |
inputClassName | string | null | Inline style of the input field. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
Name | Parameters | Description |
---|---|---|
onValueChange | event.originalEvent: Browser event event.value: New value | Callback to invoke after validation check and value change. |
onChange | event.originalEvent: Browser event event.value: New value | Callback to invoke on value change. |
onFocus | event: Browser event | Callback to invoke when input receives focus. |
onBlur | event: Browser event | Callback to invoke when input loses focus. |
onKeyDown | event: Browser event. | Callback to invoke when the key pressed. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-inputnumber | Container element |
p-inputnumber-stacked | Container element with stacked buttons. |
p-inputnumber-horizontal | Container element with horizontal buttons. |
p-inputnumber-vertical | Container element with vertical buttons. |
p-inputnumber-input | Input element |
p-inputnumber-button | Input element |
p-inputnumber-button-up | Increment button |
p-inputnumber-button-down | Decrement button |
p-inputnumber-button-icon | Button icon |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.