useNumberField

Provides the behavior and accessibility implementation for a number field component. Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.

Example
NumberField.tsx
NumberField.css
Button.css
Form.css
import {NumberField} from './NumberField';

<NumberField label="Price" defaultValue={6} formatOptions={{style: 'currency', currency: 'USD'}} />

API

Shows a number field with labels pointing to its parts, including the label, group, input, increment button, and decrement button elements.1280LabelInputLabelIncrement buttonDecrement buttonGroup
<NumberField>
  <Label />
  <Group>
    <Input />
    <Button slot="increment" />
    <Button slot="decrement" />
  </Group>
  <Text slot="description" />
  <FieldError />
</NumberField>
useNumberFieldState(props: ): useNumberField( props: , state: , inputRef: RefObject<HTMLInputElementnull> ):

NumberFieldState

Properties

NameType
numberValuenumber

The currently parsed number value, or NaN if a valid number could not be parsed. Updated based on the inputValue as the user types.

defaultNumberValuenumber

The default value of the input.

canIncrementboolean

Whether the current value can be incremented according to the maximum value and step.

canDecrementboolean

Whether the current value can be decremented according to the minimum value and step.

realtimeValidation

Realtime validation results, updated as the user edits the value.

displayValidation

Currently displayed validation results, updated when the user commits their changes.

inputValuestring

The current text value of the input. Updated as the user types, and formatted according to formatOptions on blur.

Methods

validate(value: string): boolean
Validates a user input string according to the current locale and format options. Values can be partially entered, and may be valid even if they cannot currently be parsed to a number. Can be used to implement validation as a user types.
setInputValue(val: string): void
Sets the current text value of the input.
setNumberValue(val: number): void
Sets the number value.
commit(value?: string): void
Commits the current input value. The value is parsed to a number, clamped according to the minimum and maximum values of the field, and snapped to the nearest step value. This will fire the onChange prop with the new value, and if uncontrolled, update the numberValue. Typically this is called when the field is blurred.
increment(): void
Increments the current input value to the next step boundary, and fires onChange.
decrement(): void
Decrements the current input value to the next step boundary, and fires onChange.
incrementToMax(): void
Sets the current value to the maxValue if any, and fires onChange.
decrementToMin(): void
Sets the current value to the minValue if any, and fires onChange.
updateValidation(result: ): void
Updates the current validation result. Not displayed to the user until commitValidation is called.
resetValidation(): void
Resets the displayed validation state to valid when the user resets the form.
commitValidation(): void
Commits the realtime validation so it is displayed to the user.

AriaNumberFieldProps

NameTypeDefault
decrementAriaLabelstringDefault:

A custom aria-label for the decrement button. If not provided, the localized string "Decrement" is used.

incrementAriaLabelstringDefault:

A custom aria-label for the increment button. If not provided, the localized string "Increment" is used.

isWheelDisabledbooleanDefault:

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.

isDisabledbooleanDefault:

Whether the input is disabled.

isReadOnlybooleanDefault:

Whether the input can be selected but not changed by the user.

placeholderstringDefault:

Temporary text that occupies the text input when it is empty.

valuenumberDefault:

The current value (controlled).

defaultValuenumberDefault:

The default value (uncontrolled).

onChange(value: T) => voidDefault:

Handler that is called when the value changes.

formatOptionsIntl.NumberFormatOptionsDefault:

Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user.

NumberFieldAria

NameType
labelPropsLabelHTMLAttributes<HTMLLabelElement>

Props for the label element.

groupProps

Props for the group wrapper around the input and stepper buttons.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the input element.

incrementButtonProps

Props for the increment button, to be passed to useButton.

decrementButtonProps

Props for the decrement button, to be passed to useButton.

descriptionPropsDOMAttributes

Props for the number field's description element, if any.

errorMessagePropsDOMAttributes

Props for the number field's error message element, if any.

validationDetailsValidityState

The native validation details for the input.