NumberField
This is a numeric input. The text has a locale format, the input has spinbutton semantics, and the field has step buttons. The value can also change when the user drags a pointer or turns the mouse wheel.
Anatomy
NumberField.Root gives the state to each part. Group contains the controls that the user sees. Input is the spinbutton text input. Decrement and Increment are the step buttons. ScrubArea adds the pointer drag control, and it can contain a ScrubAreaCursor.
<script>
import { NumberField } from '@human-kit/ui';
</script>
<NumberField.Root>
<NumberField.ScrubArea>
<NumberField.ScrubAreaCursor />
</NumberField.ScrubArea>
<NumberField.Group>
<NumberField.Decrement />
<NumberField.Input />
<NumberField.Increment />
</NumberField.Group>
</NumberField.Root>Format
Intl.NumberFormat makes the text that the user sees, and formatOptions controls it. The value in bind:value is always a number or null. Put the component in a LocaleProvider to control the locale of the format and of the text that the component reads.
Pointer drag
NumberField.ScrubArea makes any element into a drag control for the value. The allowWheelScrub prop lets the mouse wheel change the value while the input has the focus. Both parts show data-scrubbing for the styles.
Usage guidelines
- Give
NumberField.Inputan accessible name witharia-label, witharia-labelledby, or with a<label for>element that the user sees. - Use
bind:valuefor the state in the two directions. The value is always anumberornull. The component makes the text separately. - Put the component in a
LocaleProviderto control the locale of the format and of the text that the component reads. - With
allowOutOfRange={false}, the user can edit a value that is out of the range while the input has the focus. When the input loses the focus, the value goes tominor tomax. - In a percent format, the
%character is a localized suffix. If the user types50, the value becomes50, not0.5. - Use
nameonRootonly when the component must submit a raw numeric value in an HTML form. - Use
incrementAriaLabelanddecrementAriaLabelto replace the localized default names of the two buttons.
Accessibility
NumberField.Inputmakes a text input withrole="spinbutton". When they apply, the component setsaria-valuemin,aria-valuemax,aria-valuenow, andaria-valuetext.- The arrow keys change the value by
step.Shiftand the arrow keys change it bylargeStep.CtrlorCmdand the arrow keys change it bysmallStep. ThePageUp,PageDown,Home, andEndkeys also change the value. - The keyboard and the buttons do the same operations as the pointer drag.
- A bad value or a value out of the range sets the native custom validity on the input. Thus the form does not submit while the field is not valid.
API reference
Root
The container of the number field. It holds the numeric state, the format, and the validation, and it gives the context to all of the parts. With a `name`, it also makes a hidden input that submits the raw number.
* required. Native HTML attributes of the underlying element are also accepted.
Group
Contains the controls that the user sees: the input and the step buttons. It shows the state of the field in data attributes.
* required. Native HTML attributes of the underlying element are also accepted.
Input
The text input that the user sees, with role="spinbutton". It controls the text with its format, the keyboard steps, and the native validity.
* required. Native HTML attributes of the underlying element are also accepted.
Increment
The button that makes the value larger by `step`. It is not in the tab order, and it has a localized default name.
* required. Native HTML attributes of the underlying element are also accepted.
Decrement
The button that makes the value smaller by `step`. It is not in the tab order, and it has a localized default name.
* required. Native HTML attributes of the underlying element are also accepted.
ScrubArea
Makes its content into a drag control. While the user drags the pointer, the value changes.
* required. Native HTML attributes of the underlying element are also accepted.
ScrubAreaCursor
An optional cursor, in the DOM while the user drags. It follows the pointer in the drag area.
* required. Native HTML attributes of the underlying element are also accepted.