Number Field
Number fields wrap a native number input: the browser handles entry, min, max, step and the keyboard, with optional stepper buttons.
Try it
Usage
<rui-number-field> is a thin host around a native <input type="number">. The input owns the value, as a RuiInput's native input does: its value attribute is the server value, the default and reset value, and the step base when there is no min. The input keeps its name, min, max and step on the server and in the browser, and submits itself with its form. The browser validates the step grid (it starts at min), the range and a bad entry, shows the spin buttons and formats the number for the visitor's locale.
import { RuiNumberField } from '@ecopages/radiant-ui/number-field';
<RuiNumberField name="quantity" value={3} minValue={0} maxValue={10} step={1} label="Quantity" />Set name, the bounds, step, disabled and readOnly on the host: it writes them to the input. The view writes value on its default input. The host keeps no copy of the value: host.value reads the input's number and writing it writes the input, so a number typed before the script loaded, a native reset and a stepper all read back as they are.
Stepper buttons
Compose RuiNumberFieldSteppers beside the input for styled buttons, and write value on the composed input. A press calls stepUp() or stepDown() on the input and dispatches input and change on it, and holding the button keeps stepping after half a second, so the buttons behave like the spin buttons. A touch steps when it lifts, or once held, so a scroll that starts on a button does not step, and stepping stops at a bound. Once rui-number-field is defined, a field that holds stepper buttons hides the native spin buttons, so it shows one set. Leave the stepper buttons out to keep the native ones.
import {
RuiNumberField,
RuiNumberFieldGroup,
RuiNumberFieldInput,
RuiNumberFieldDecrementButton,
RuiNumberFieldIncrementButton,
RuiNumberFieldSteppers,
} from '@ecopages/radiant-ui/number-field';
<RuiNumberField name="quantity" minValue={0} maxValue={10}>
<RuiNumberFieldGroup>
<RuiNumberFieldInput value={3} />
<RuiNumberFieldSteppers>
<RuiNumberFieldDecrementButton />
<RuiNumberFieldIncrementButton />
</RuiNumberFieldSteppers>
</RuiNumberFieldGroup>
</RuiNumberField>Custom markup
<rui-number-field> coordinates any light-DOM tree that matches its query contract. The RuiNumberField helpers stamp these targets; they are not required.
<rui-number-field name="quantity" min-value="0" max-value="10">
<div class="rui-number-field__group" data-number-field-group>
<input type="number" value="3" data-number-field-input data-rui-control data-rui-control-type="number" class="rui-number-field__input" />
<button type="button" data-number-field-action="decrement" class="rui-number-field__stepper">−</button>
<button type="button" data-number-field-action="increment" class="rui-number-field__stepper">+</button>
</div>
</rui-number-field>BEM classes are presentation-only.
Range and step
The step base follows the HTML rules: min when there is one, otherwise the input's value attribute. A value off the grid, such as 10 with max 10, min 0 and step 3, is reported by the input's validity.stepMismatch and blocks native submission; stepUp() and the arrow keys stop on the grid, so 9 is the largest value there.
Theming
Number field surfaces map to semantic surface roles, never Tailwind palette steps:
| Part | CSS roles |
|---|---|
Group (.rui-number-field__group) | border, background, rounded-control |
Input (.rui-number-field__input) | on-background text |
Steppers (.rui-number-field__steppers) | border divider |
Stepper (.rui-number-field__stepper) | on-surface, surface hover, surface-container-low active |
| Focus / invalid | focus-ring / error |
Geometry shares control tokens (--size-control-sm, --space-control-*, --radius-control, --text-control) with RuiInput and RuiButton so form rows align. Override at the theme layer, not in component CSS.
API
RuiNumberField is a custom element (<rui-number-field>). Standalone it renders the input in a group; compose the view helpers for stepper buttons.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
min-value | number | -Infinity | The input's min, and the step base. |
max-value | number | Infinity | The input's max. |
step | number | 1 | The input's step. |
disabled | boolean | false | Disables the input and the steppers. |
read-only | boolean | false | Sets readonly on the input and disables the steppers. |
label | string | '' | Accessible name when there is no visible RuiLabel. |
name | string | '' | Form field name on the input. |
increment-aria-label | string | Increment | Accessible name for the increment stepper. |
decrement-aria-label | string | Decrement | Accessible name for the decrement stepper. |
The value property is not an attribute: it reads the input's number, undefined while the input holds none, and writing it writes the input. Set value after the element is defined, or write it as the input's value attribute: a write before upgrade shadows the property and is lost, as for any member that is not a @prop. RuiNumberField takes a value prop for its default input.
Light-DOM contract
| Target | Required | Host writes | Author owns |
|---|---|---|---|
[data-number-field-input] | yes | id, name, min, max, step, disabled, readonly | type="number" and value; stamp data-rui-control on the input for field wiring. |
[data-number-field-action="decrement"] | no | disabled, aria-label | decrement button |
[data-number-field-action="increment"] | no | disabled, aria-label | increment button |
[data-number-field-group] | no | none | presentation wrapper only |
Nested hosts: none.
Events
The input's own input and change bubble through the host, and host.value reads the input, so any listener reads the new number; the events carry no detail. Each stepper step dispatches both on the input after it moves the value, so holding a stepper sends a pair per step. A programmatic write to value dispatches nothing.
View helpers
| Component | Target stamped | Notes |
|---|---|---|
RuiNumberField | default group when children omitted | The input alone, holding value; add RuiNumberFieldSteppers for buttons. |
RuiNumberFieldGroup | [data-number-field-group] | Presentation only; not queried. |
RuiNumberFieldInput | [data-number-field-input] | Always stamps data-rui-control. |
RuiNumberFieldIncrementButton | [data-number-field-action="increment"] | Default label "Increment". |
RuiNumberFieldDecrementButton | [data-number-field-action="decrement"] | Default label "Decrement". |
RuiNumberFieldSteppers | stepper column wrapper | Presentation only. |
CSS classes
Public BEM classes (documented via @cssclass):
| Class | Description |
|---|---|
.rui-number-field | Root field wrapper. |
.rui-number-field__group | Control-height bordered row. |
.rui-number-field__input | Borderless input. |
.rui-number-field__steppers | Trailing stepper column. |
.rui-number-field__stepper | Icon button cell (+ / −). |
Theme roles
| Part | CSS variables consumed |
|---|---|
| Group | --border, --background, --radius-control, --duration-normal |
| Input | --on-background, --space-control-x, --space-control-y, --text-control |
| Steppers | --size-control-sm |
| Stepper states | --on-surface, --surface, --surface-container-low |
| Focus / invalid | --focus-ring / --error |
Accessibility
- The native number input is a spin button; the browser announces its value, range and step.
- Stepper buttons need accessible names; "Increment" and "Decrement" are provided by default.
- Announce min and max constraints in helper text when the valid range is not obvious.
- Disabled fields should explain why editing is unavailable.