NumberField
NumberField allows for numerical input.
Props
Example
NumberField will expand to fill the width of the parent container.
Example: Disabled
Example: Helper Text
Whenever you want to provide more information about a form field, you should use helperText
.
Example: Error message
NumberField can display an error message.
Simply pass in an errorMessage
when there is an error present and we will handle the rest.
Example: min/max/step
NumberField provides additional props specific to numerical input.
min
and max
can be used to define the acceptable bounds of the input (see the ref example for more about using these for validation).
step
determines the amount incremented or decremented when using the input's arrow buttons. Set this as a float to allow decimal input.
Example: ref
Set a ref on NumberField to use the Constraint Validation API or to anchor a Popover-based element.
Note that while the arrow buttons will not exceed the min/max (if set), the user is free to enter any number using the keyboard. Validation should be performed explicitly using the Constraint Validation API to ensure the value is within the specified range.
Autofocus
NumberField intentionally lacks support for autofocus. Generally speaking,
autofocus interrupts normal page flow for screen readers making it an
anti-pattern for accessibility.
onSubmit
NumberField is commonly used as an input in forms alongside submit buttons.
In these cases, users expect that pressing Enter or Return with the input
focused will submit the form.
Out of the box, NumberField doesn't expose an onSubmit
handler or
individual key event handlers due to the complexities of handling these
properly. Instead, developers are encouraged to wrap NumberField
in a <form>
and attach an onSubmit
handler to that <form>
.