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/NumberField';
import {useState} from 'react';
function Example() {
let [value, setValue] = useState(25);
return (
<div>
<NumberField
label="Cookies to buy"
placeholder="Enter a number"
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.
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.
Prefix
Use the prefix prop to display an additional element before the input in the number field.
import {NumberField} from '@react-spectrum/s2/NumberField';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
function Example() {
return (
<div className={style({display: 'flex', flexDirection: 'column', gap: 8})}>
<div style={{font: 'title'}}>Dimensions</div>
<div className={style({display: 'flex', flexDirection: 'row', gap: 8})}>
<NumberField
aria-label="Width"
prefix="W"
formatOptions={{style: 'unit', unit: 'inch'}}
defaultValue={10}
placeholder="0"
styles={style({width: 100})}
hideStepper />
<NumberField
aria-label="Height"
prefix="H"
formatOptions={{style: 'unit', unit: 'inch'}}
defaultValue={10}
placeholder="0"
styles={style({width: 100})}
hideStepper />
</div>
</div>
);
}
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. | ||
prefix | ReactNode | Default: — |
The prefix to display in the NumberField. A non-interactive element that appears before the input. | ||
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. | ||
commitBehavior | 'snap' | 'validate' | Default: 'snap'
|
Controls the behavior of the number field when the user blurs the field after editing. 'snap' will clamp the value to the min/max values, and snap to the nearest step value. 'validate' will not clamp the value, and will validate that the value is within the min/max range and on a valid step. | ||
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 | ||
placeholder | string | Default: — |
Temporary text that occupies the text input when it is empty. See MDN. | ||
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. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
minValue | number | Default: — |
The smallest value allowed for the input. | ||
maxValue | number | Default: — |
The largest value allowed for the input. | ||
step | number | Default: — |
The amount that the input value changes with each increment or decrement "tick". | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
necessityIndicator | NecessityIndicator | Default: 'icon'
|
Whether the required state should be shown as an icon or text. | ||
description | ReactNode | Default: — |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode | | Default: — |
An error message for the field. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
onCopy | ClipboardEventHandler | Default: — |
Handler that is called when the user copies text. See MDN. | ||
onCut | ClipboardEventHandler | Default: — |
Handler that is called when the user cuts text. See MDN. | ||
onPaste | ClipboardEventHandler | Default: — |
Handler that is called when the user pastes text. See MDN. | ||
onCompositionStart | CompositionEventHandler | Default: — |
Handler that is called when a text composition system starts a new text composition session. See MDN. | ||
onCompositionEnd | CompositionEventHandler | Default: — |
Handler that is called when a text composition system completes or cancels the current text composition session. See MDN. | ||
onCompositionUpdate | CompositionEventHandler | Default: — |
Handler that is called when a new character is received in the current text composition session. See MDN. | ||
onSelect | ReactEventHandler | Default: — |
Handler that is called when text in the input is selected. See MDN. | ||
onBeforeInput | FormEventHandler | Default: — |
Handler that is called when the input value is about to be modified. See MDN. | ||
onInput | FormEventHandler | Default: — |
Handler that is called when the input value is modified. See MDN. | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||