Number Input
The NumberInput component is similar to the Input component, but it has controls for incrementing or decrementing numeric values.
Import
Chakra UI exports 5 components for the NumberInput.
NumberInput
: The wrapper that provides context and logic to the components.NumberInputField
: Theinput
field itself.NumberInputStepper
: The wrapper for the input's stepper buttons.NumberIncrementStepper
: The button to increment the value of the input.NumberDecrementStepper
: The button to decrement the value of the input.
import {
NumberInput,
NumberInputField,
NumberInputStepper,
NumberIncrementStepper,
NumberDecrementStepper,
} from '@chakra-ui/react';
Usage
The NumberInput component follows the WAI-ARIA authoring practices for the spin button widget. It is composed of smaller components to give you control of the styling of each part.
Setting a minimum and maximum value
Pass the min
prop or max
prop to set an upper and lower limit for the input.
By default, the input will restrict the value to stay within the specified
range.
These props defaults to Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER . If they do not match your requirements you can use other
Number
properties asmin
ormax
.
Setting the step size
Pass the step
prop to change the step size when you increment or decrement the
value. By default, the value is rounded to match the number of decimals in the
step
.
Adjusting the precision of the value
In some cases, you might need the value to be rounded to specific decimal
points. Simply pass the precision
prop and set it to the number of decimal
points.
Clamp value when user blurs the input
In most cases, users can type custom values in the input field. If the typed
value is greater than the max
, the value is reset to max
when the user blur
out of the input.
To disable this behavior, pass clampValueOnBlur
and set to false
.
In this example, try to type a value greater than the max. It won't reset the value on blur.
Allowing out of range values
In some scenarios, you might not want to block out of range values. Pass the
keepWithinRange
and clampValueOnBlur
props and set them to false
to
support this use case.
The NumberInput will be set
isInvalid
totrue
internally when the value is out of range. Out of range means that thevalue
is great thanmax
or less thanmin
.
Formatting and Parsing the value
Changing the size of the input
Like the Input
component, you can pass the size
prop to change the size of
the input.
Changing the styles
You can change the style of any part of the components using style props. You can also change the icons used in the steppers.
Combining it with a Slider
A common use case is to combine the NumberInput
with a Slider
.
We recommend disabling focusThumbOnChange
on the Slider
, so the
NumberInput
won't lose focus after input.
Here's an example of how to do that:
Create a mobile spinner
Sometimes, you might need to create a mobile version of the number input. Here's
how you can leverage the useNumberInput
hook to build one.
Increment value using Mouse wheel
The NumberInput
component supports the ability to increment or decrement
values using the mouse wheel events. To activate this, pass the
allowMouseWheel
prop.
Accessibility
Aria Roles
- The input has
role
set tospinbutton
to denote that users are to select from a range of discrete values using an up and down arrows on the keyboard. - The input has
aria-valuemin
set to the minimum value allowed for the spinbutton. - The input has
aria-valuemax
set to the maximum value allowed for the spinbutton. attribute should be applied to the spinbutton. - The input has
aria-valuenow
set to the current value of theinput
. - The custom spinner (up and down buttons) has
aria-hidden
set totrue
to make them invisible to screen readers.
Keyboard Navigation
- When you hit the ⬆ or ⬇ key, the input value will be
increased or decreased by
step
. - Holding down Shift and pressing ⬆ or ⬇ will
update the value by
10 * step
. - Holding down Ctrl or ⌘, and pressing ⬆ or⬆ will update the value by `0.1 * step`.
- Long pressing the up and down stepper buttons will update the value at intervals.
Props
NumberInput Props
NumberInput composes Flex
with some additional props listed below.
allowMouseWheel
allowMouseWheel
If true
, the input's value will change based on mouse wheel
boolean
aria-describedby
aria-describedby
string
aria-label
aria-label
string
aria-labelledby
aria-labelledby
string
clampValueOnBlur
clampValueOnBlur
This controls the value update when you blur out of the input.
- If true
and the value is greater than max
, the value will be reset to max
- Else, the value remains the same.
boolean
true
colorScheme
colorScheme
Color Schemes for NumberInput
are not implemented in the default theme. You can extend the theme to implement them.
string
defaultValue
defaultValue
The initial value of the counter. Should be less than max
and greater than min
string | number
errorBorderColor
errorBorderColor
The border color when the input is invalid. Use color keys in `theme.colors` @example errorBorderColor = "red.500"
string
focusBorderColor
focusBorderColor
The border color when the input is focused. Use color keys in `theme.colors` @example focusBorderColor = "blue.500"
string
focusInputOnChange
focusInputOnChange
If true
, the input will be focused as you increment
or decrement the value with the stepper
boolean
true
format
format
If using a custom display format, this converts the default format to the custom format.
((value: string | number) => string | number)
getAriaValueText
getAriaValueText
This is used to format the value so that screen readers can speak out a more human-friendly value. It is used to set the `aria-valuetext` property of the input
((value: string | number) => string)
id
id
The id
to use for the number input field.
string
inputMode
inputMode
Hints at the type of data that might be entered by the user. It also determines the type of keyboard shown to the user on mobile devices
"text" | "search" | "none" | "tel" | "url" | "email" | "numeric" | "decimal"
"decimal"
isDisabled
isDisabled
Whether the input should be disabled
boolean
isInvalid
isInvalid
If true
, the input will have `aria-invalid` set to true
boolean
isReadOnly
isReadOnly
If true
, the input will be in readonly mode
boolean
isRequired
isRequired
Whether the input is required
boolean
isValidCharacter
isValidCharacter
Whether the pressed key should be allowed in the input. The default behavior is to allow DOM floating point characters defined by /^[Ee0-9+\-.]$/
((value: string) => boolean)
keepWithinRange
keepWithinRange
This controls the value update behavior in general.
- If true
and you use the stepper or up/down arrow keys,
the value will not exceed the max
or go lower than min
- If false
, the value will be allowed to go out of range.
boolean
true
max
max
The maximum value of the counter
number
Number.MAX_SAFE_INTEGER
min
min
The minimum value of the counter
number
Number.MIN_SAFE_INTEGER
name
name
The HTML name
attribute used for forms
string
onBlur
onBlur
FocusEventHandler<HTMLInputElement>
onChange
onChange
The callback fired when the value changes
((valueAsString: string, valueAsNumber: number) => void)
onFocus
onFocus
FocusEventHandler<HTMLInputElement>
onInvalid
onInvalid
((message: ValidityState, value: string, valueAsNumber: number) => void)
parse
parse
If using a custom display format, this converts the custom format to a format parseFloat
understands.
((value: string) => string)
pattern
pattern
The pattern used to check the <input> element's value against on form submission.
string
"[0-9]*(.[0-9]+)?"
precision
precision
The number of decimal points used to round the value
number
size
size
"xs" | "sm" | "md" | "lg"
"md"
step
step
The step used to increment or decrement the value
number
1
value
value
The value of the counter. Should be less than max
and greater than min
string | number
variant
variant
"outline" | "filled" | "flushed" | "unstyled"
"outline"
NumberInputField Props
NumberInputField
composes Input
so you can pass all Input
props. If you
want to change the size, pass the size
prop to the NumberInput
component
instead, as demonstrated above.
NumberInputStepper Props
NumberInputStepper
composes Flex
so you can pass all Flex
props.
NumberDecrementStepper and NumberIncrementStepper Props
NumberDecrementStepper
and NumberIncrementStepper
composes the Box
props
so you can pass all Box
props.