Components
Number Field
A numeric input element with increment and decrement buttons, and a scrub area.
Usage guidelines#
- Form controls must have an accessible name. It can be created using a
<label>element or the Field component.
Anatomy#
Import the component and assemble its parts:
import { NumberField } from '@egoist/solid-base/number-field';
<NumberField.Root>
<NumberField.ScrubArea>
<NumberField.ScrubAreaCursor />
</NumberField.ScrubArea>
<NumberField.Group>
<NumberField.Decrement />
<NumberField.Input />
<NumberField.Increment />
</NumberField.Group>
</NumberField.Root>;Examples#
Formatting#
Pass Intl.NumberFormat options to format (and optionally a locale) to display currencies,
percentages or units. The input parses what users type back into a number:
<NumberField.Root
defaultValue={1299}
format={{ style: 'currency', currency: 'USD' }}
locale="en-US"
>
{/* ... */}
</NumberField.Root>Controlled#
Pass value and update it from onValueChange. The value is null when the input is empty. Use
onValueCommitted to react only when the user finishes an interaction (blur, a key press, or
releasing a button or the scrub area):
const [amount, setAmount] = createSignal<number | null>(100);
<NumberField.Root value={amount()} onValueChange={(value) => setAmount(value)}>
{/* ... */}
</NumberField.Root>;API reference#
<NumberField.Root>
Groups all parts of the number field and manages its state. Renders a <div> element.
idstring—The id of the input element.
minnumber—The minimum value of the input element.
maxnumber—The maximum value of the input element.
allowOutOfRangebooleanfalseWhen true, direct text entry may be outside the min/max range without clamping, so native range underflow/overflow validation can occur. Step-based interactions (keyboard arrows, buttons, wheel, scrub) still clamp.
smallStepnumber0.1The small step value of the input element when incrementing while the alt key is held. Snaps to multiples of this value when snapOnStep is enabled.
stepnumber | 'any'1Amount to increment and decrement with the buttons and arrow keys, or to scrub with pointer movement in the scrub area. To always enable step validation on form submission, specify the min prop explicitly in conjunction with this prop. Specify step="any" to always disable step validation; interactive stepping then uses a base amount of 1, while the alt and shift keys still step by smallStep and largeStep.
largeStepnumber10The large step value of the input element when incrementing while the shift key is held. Snaps to multiples of this value when snapOnStep is enabled.
requiredbooleanfalseWhether the user must enter a value before submitting a form.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to change the field value.
namestring—Identifies the field when a form is submitted.
formstring—Identifies the form that owns the hidden input. Useful when the number field is rendered outside the form.
valuenumber | null—The raw numeric value of the field.
defaultValuenumber—The uncontrolled value of the field when it's initially rendered.
To render a controlled number field, use the value prop instead.
allowWheelScrubbooleanfalseWhether to allow the user to scrub the input value with the mouse wheel while focused and hovering over the input.
snapOnStepbooleanfalseWhether the value should snap to the nearest step when incrementing or decrementing.
formatIntl.NumberFormatOptions—Options to format the input value.
onValueChange| ((value: number | null, eventDetails: NumberFieldRoot.ChangeEventDetails) => void)—Callback fired when the number value changes.
The eventDetails.reason indicates what triggered the change: - 'input-change' for parseable typing or programmatic text updates - 'input-clear' when the field becomes empty - 'input-blur' when formatting (and clamping, if enabled) occurs on blur - 'input-paste' for paste interactions - 'keyboard' for arrow-key/Home/End stepping (typing digits uses 'input-change'/'input-clear') - 'increment-press' / 'decrement-press' for button presses on the increment and decrement controls - 'wheel' for wheel-based scrubbing - 'scrub' for scrub area drags
onValueCommitted| ((value: number | null, eventDetails: NumberFieldRoot.CommitEventDetails) => void)—Callback function that is fired when the value is committed. It runs later than onValueChange, when: - The input is blurred after typing a value. - The pointer is released after scrubbing or pressing the increment/decrement buttons.
It runs simultaneously with onValueChange when interacting with the keyboard or the mouse wheel.
**Warning**: This is a generic event not a change event.
localeIntl.LocalesArgument—The locale of the input element. Defaults to the user's runtime locale.
inputRefRefProp<HTMLInputElement>—A ref to access the hidden input element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.Group>
Groups the input with the increment and decrement buttons. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.Increment>
A stepper button that increases the field value when clicked. Renders a <button> element.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.Decrement>
A stepper button that decreases the field value when clicked. Renders a <button> element.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.Input>
The native input control in the number field. Renders an <input> element.
aria-roledescriptionstring'Number field'A user-friendly description of the input's role for assistive tech. This is a role description, not an accessible name — use Field.Label or aria-label to name the control.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.ScrubArea>
An interactive area where the user can click and drag to change the field value. Renders a <span> element.
direction'horizontal' | 'vertical''horizontal'Cursor movement direction in the scrub area.
pixelSensitivitynumber2Determines how many pixels the cursor must move before the value changes. A higher value will make scrubbing less sensitive.
teleportDistancenumber—If specified, determines the distance that the cursor may move from the center of the scrub area before it will loop back around.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).
<NumberField.ScrubAreaCursor>
A custom element to display instead of the native cursor while using the scrub area. Renders a <span> element.
This component uses the Pointer Lock API, which may prompt the browser to display a related notification. It is disabled in Safari to avoid a layout shift that this notification causes there.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-scrubbingPresent while scrubbing.
data-disabledPresent when the number field is disabled.
data-readonlyPresent when the number field is readonly.
data-requiredPresent when the number field is required.
data-validPresent when the number field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the number field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the number field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the number field's value has changed (when wrapped in Field.Root).
data-filledPresent when the number field is filled (when wrapped in Field.Root).
data-focusedPresent when the number field is focused (when wrapped in Field.Root).