Fragiola

API: components

NumberField

A text field for a number, as the APG spinbutton, with localized formatting and parsing, keys, held buttons, the wheel and a scrub area, in each framework.

A text field for a number (the APG spinbutton pattern): the user types it in the locale's own writing, or steps it with the keyboard, the Increment and Decrement buttons (pressed once or held), the mouse wheel (opt-in) or a scrub area dragged sideways. Its value is a number, or null while empty. It works in a native <form> and in a Field.

Quantity field
Gallery

Behaviour

  • Value: a number or null, controlled or not; every change reports { reason, event }: input-change, input-clear, input-blur, keyboard, increment-press, decrement-press, wheel, scrub, none (a form reset).
  • Committing: onValueCommitted reports the value once per gesture that changed it: on the Input's blur after typing, at once for a key or a wheel step, at the release of a button press (once after a held repeat) or a scrub. A form reset is no commit. Save there instead of on every onValueChange.
  • Locale: format (Intl.NumberFormatOptions) and locale write the value (€1,234.50, 1.234,50 €, ‏١٬٢٣٤٫٥٠ €) and read what is typed: the locale's digits (Arabic-Indic, ...), group and decimal separators, any minus sign, the currency, unit and percent text (a percent field reads 25% as 0.25). A character no number of the locale holds is refused as it is typed; the blur writes the value formatted.
  • Stepping: step (Alt: smallStep, Shift: largeStep), clamped to min and max, with no floating-point noise; an empty field steps from 0. snapOnStep steps to the grid. clampOnBlur (on by default) clamps a typed value on blur; off, the native validity reports it.
  • Buttons: out of the tab order, named by you (aria-label); a press steps at once, and a held press repeats every 60 ms after 400 ms; a button is disabled at its bound.
  • Wheel: allowWheelScrub steps the value over the focused Input.
  • Scrub: a press on the ScrubArea dragged sideways (or up, direction="vertical") steps once per pixelSensitivity pixels; a mouse locks the pointer and the ScrubAreaCursor follows it.
  • Forms: a hidden type="number" input submits the plain number (1234.5) and validates required, min and max (step="any"); a reset returns to defaultValue.
  • No text: the buttons and the Input have no name of their own.

Anatomy

partelementdata-primitives-partrole
Rootdivnumber-field-rootholds the value; renders the hidden input after it
Groupdivnumber-field-groupgroup: the Input with its buttons
Inputinputnumber-field-inputthe spinbutton
Incrementbuttonnumber-field-incrementsteps up
Decrementbuttonnumber-field-decrementsteps down
ScrubAreaspannumber-field-scrub-areaa press dragged along it steps the value
ScrubAreaCursorspannumber-field-scrub-area-cursorthe virtual cursor of a locked scrub, in body
(hidden input)input type="number"number-field-hidden-inputwhat the form submits

Usage

import { Label } from "@fragiola/primitives-react/label";
import { NumberField } from "@fragiola/primitives-react/number-field";

<Label htmlFor="quantity">Quantity</Label>
<NumberField.Root id="quantity" name="quantity" defaultValue={1} min={1} max={10}>
    <NumberField.Group>
        <NumberField.Decrement aria-label="Decrease">−</NumberField.Decrement>
        <NumberField.Input />
        <NumberField.Increment aria-label="Increase">+</NumberField.Increment>
    </NumberField.Group>
</NumberField.Root>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. Vue binds the value as v-model:value and @value-change; Angular as [(value)] and (valueChangeDetails) ({ value, details }); the commit is @value-committed in Vue and (valueCommitted) ({ value, details }) in Angular.

Root

proptypedefaultdescription
valuenumber | nullthe value, controlled (null: empty)
defaultValuenumber | nullnullthe initial value, uncontrolled
onValueChange(value: number | null, details: { reason, event }) => voidevery change
onValueCommitted(value: number | null, details: { reason, event }) => voidthe value once a change is complete: on blur after typing, at a key or wheel step, at the release of a button press or a scrub
minnumberthe lower bound
maxnumberthe upper bound
stepnumber1the amount of a step
smallStepnumber0.1the amount of a step with Alt
largeStepnumber10the amount of a step with Shift, Page Up and Page Down
snapOnStepbooleanfalsea step from a value off the grid goes to the grid's next point
clampOnBlurbooleantruea typed value outside the bounds is clamped on blur
formatIntl.NumberFormatOptionsthe plain numberhow the Input writes (and reads) the value
localestringthe runtime'sthe locale of the format and the parsing (a BCP 47 tag)
allowWheelScrubbooleanfalsethe wheel steps the value over the focused Input
requiredbooleanfalsethe hidden input is required
readOnlybooleanfalsefocusable and submitted, but nothing changes it
disabledbooleanfalseignores every interaction, submits nothing
namestringthe hidden input's name (a Field's wins)
idstringgeneratedthe Input's id (a Field's control id inside one)

Group

No props of its own.

Input

No props of its own.

Increment

No props of its own.

Decrement

No props of its own.

ScrubArea

proptypedefaultdescription
direction"horizontal" | "vertical""horizontal"the axis it scrubs along (right or up steps up)
pixelSensitivitynumber2the pixels one step takes

ScrubAreaCursor

No props of its own.

State and part attributes

Every part carries data-disabled, data-readonly, data-required, data-scrubbing (while a scrub runs) and, inside a Field, the Field's state (data-invalid, data-valid, data-dirty, data-touched, data-filled, data-focused).

partattributes
Grouprole="group"
Inputrole="spinbutton", type="text", inputmode, autocomplete="off", aria-valuenow, aria-valuemin, aria-valuemax, aria-valuetext (the formatted value; none while empty), aria-required, aria-readonly
Increment, Decrementtype="button", tabindex="-1", aria-controls (the Input), disabled at the bound or while disabled or read-only
ScrubAreastructural touch-action: none, user-select: none
ScrubAreaCursorstructural position: fixed, pointer-events: none, transform: translate3d(<x>px, <y>px, 0)

Keyboard

keydoes
ArrowUp, ArrowDownstep up or down (Shift: largeStep, Alt: smallStep)
PageUp, PageDownstep by largeStep
Home, Endset min, max (when given)
any other keythe input's own text editing

Exports

entryexports
@fragiola/primitives-react/number-fieldChangeDetails, ChangeReason, NumberField, NumberFieldDecrementProps, NumberFieldGroupProps, NumberFieldIncrementProps, NumberFieldInputProps, NumberFieldRootProps, NumberFieldScrubAreaCursorProps, NumberFieldScrubAreaProps, NumberFieldState, RenderProp
@fragiola/primitives-vue/number-fieldChangeDetails, ChangeReason, NumberField, NumberFieldRootEmits, NumberFieldRootProps, NumberFieldScrubAreaProps, NumberFieldState, RenderSlotProps
@fragiola/primitives-angular/number-fieldChangeDetails, ChangeReason, NumberFieldDecrement, NumberFieldGroup, NumberFieldIncrement, NumberFieldInput, NumberFieldRoot, NumberFieldScrubArea, NumberFieldScrubAreaCursor, NumberFieldState, ScrubPoint, ValueChangeEvent