useNumberFieldState
Provides state management for a number field component. Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.
install | yarn add react-stately |
---|---|
version | 3.33.0 |
usage | import {useNumberFieldState} from 'react-stately' |
API#
useNumberFieldState(
(props: NumberFieldStateOptions
)): NumberFieldState
Interface#
Properties
Name | Type | Description |
inputValue | string | The current text value of the input. Updated as the user types,
and formatted according to |
numberValue | number | The currently parsed number value, or NaN if a valid number could not be parsed.
Updated based on the |
canIncrement | boolean | Whether the current value can be incremented according to the maximum value and step. |
canDecrement | boolean | Whether the current value can be decremented according to the minimum value and step. |
realtimeValidation | ValidationResult | Realtime validation results, updated as the user edits the value. |
displayValidation | ValidationResult | Currently displayed validation results, updated when the user commits their changes. |
minValue | number | The minimum value of the number field. |
maxValue | number | The maximum value of the number field. |
Methods
Method | Description |
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(): 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 |
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: ValidationResult
)): 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. |
Example#
See the docs for useNumberField in react-aria for an example of useNumberFieldState
.