NumberField
NumberFields allow users to input number values with a keyboard or increment/decrement with step buttons.
Value
Use the value
or defaultValue
prop to set the number value. The onChange
event is called when the user finishes editing the value (e.g. on blur, incrementing, or decrementing).
Current value: 25
import {NumberField} from '@react-spectrum/s2';
import {useState} from 'react';
function Example() {
let [value, setValue] = useState(25);
return (
<div>
<NumberField
label="Cookies to buy"
value={value}
onChange={setValue}
/>
<p>Current value: {value}</p>
</div>
);
}
Format options
Use the formatOptions
prop to control how the value is formatted (according to the user's locale). This is compatible with the Intl.NumberFormat API.
formatOptions
Value scale
Use the minValue
, maxValue
, and step
props to set the allowed values. Steps are calculated starting from the minimum. For example, if minValue={2}
, and step={3}
, the valid step values would be 2, 5, 8, 11, etc.
Numbering system
By default, NumberField
displays the value using the numbering system for the user's locale. Use <Provider>
to override the numbering system by setting the Unicode numbering system locale extension. The Latin, Arabic, Devanagari, Bengali, and Han positional decimal numbering systems are currently supported.
Forms
Use the name
prop to submit the raw number value (not a formatted string) to the server. Set the isRequired
prop to validate that the user enters a value, or implement custom client or server-side validation. See the Forms guide to learn more.
API
Name | Type | Default |
---|---|---|
hideStepper | boolean | Default: false
|
Whether to hide the increment and decrement buttons. | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the NumberField. | ||
decrementAriaLabel | string | Default: — |
A custom aria-label for the decrement button. If not provided, the localized string "Decrement" is used. | ||
incrementAriaLabel | string | Default: — |
A custom aria-label for the increment button. If not provided, the localized string "Increment" is used. | ||
isWheelDisabled | boolean | Default: — |
Enables or disables changing the value with scroll. | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
Whether the input can be selected but not changed by the user. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the style() macro. | ||
value | number | Default: — |
The current value (controlled). | ||
defaultValue | number | Default: — |
The default value (uncontrolled). | ||
onChange |
| Default: — |
Handler that is called when the value changes. | ||
formatOptions | Intl.NumberFormatOptions | Default: — |
Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user. | ||