Number Field
A number input with buttons that step it up and down, formatted for the user's locale. The user types in an <input type="text" role="spinbutton"> that shows the number with Intl.NumberFormat, such as 1.234,50 € in German, and reads typed text back in the same language when focus leaves it or Enter is pressed. The arrow keys, Page Up, Page Down, Home and End step it, and the buttons repeat while held. The form receives the plain number, such as 1234.5, from a hidden number input, which the browser validates against min, max, step and required.
- Size
- 3.1 kB
Example
import { NumberField } from "menhir/number-field";
const step = "w-8 cursor-pointer disabled:cursor-default disabled:opacity-40";
export default function NumberFieldExample() {
return (
<div class="flex flex-col items-start gap-1">
<label for="tickets" class="text-sm font-medium">
Tickets
</label>
<NumberField.Root
id="tickets"
name="tickets"
defaultValue={2}
min={1}
max={10}
class="inline-flex rounded-md border border-stone-300 focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-stone-500 dark:border-stone-700"
>
<NumberField.Decrement aria-label="Fewer tickets" class={step}>
−
</NumberField.Decrement>
<NumberField.Input class="w-16 bg-transparent px-2 py-1.5 text-center outline-none" />
<NumberField.Increment aria-label="More tickets" class={step}>
+
</NumberField.Increment>
</NumberField.Root>
</div>
);
}
Anatomy
<NumberField.Root>
<NumberField.Decrement />
<NumberField.Input />
<NumberField.Increment />
</NumberField.Root>API reference
Root
A <div> around the parts, which holds the value, then a visually hidden <input type="number"> with the plain number for the form. Its id, aria-describedby and aria-invalid go on the Input, so a <label for> its id names the Input, and it joins a Field as Field.Control's render. Every part has data-disabled, data-readonly and data-required.
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | — | The number, when controlled. Pair it with onValueChange. An empty field is null. |
defaultValue | number | null | null | The number to start with, when uncontrolled. A form reset goes back to it. |
onValueChange | (value: number | null) => void | — | Called with the new number when the user commits typed text, steps it with a key or a button, or resets the form. Not on each key: typed text is read when focus leaves the Input or Enter is pressed. |
min | number | — | The lowest number the user can set: typed numbers and steps are clamped to it, Home sets it, and Decrement is disabled there. A value from you below it is shown, and the browser reports it. |
max | number | — | The highest number the user can set: typed numbers and steps are clamped to it, End sets it, and Increment is disabled there. |
step | number | 1 | How much Up, Down and the buttons change the number. Steps land on multiples of step counted from min, else from 0. When you set step, a typed number off it is kept, and the browser reports it as a step mismatch; without one, any typed number is valid. |
largeStep | number | step × 10 | How much Page Up and Page Down change the number. |
format | Intl.NumberFormatOptions | — | How the Input shows the number, such as { style: "currency", currency: "EUR" } or { style: "percent" }, where 0.25 shows as 25%. Typed text is read back in the same format, and a typed number is rounded to the digits it shows. |
locale | Intl.LocalesArgument | — | The language to format and read the number in, else the browser's. Pass it when rendering on the server, so that the server and the browser agree. |
name | string | — | The name the plain number is submitted under. |
required | boolean | false | Whether the form can be submitted only with a number. The browser blocks the submission and focuses the Input. |
disabled | boolean | false | Whether every part ignores the user, and the form submits nothing. |
readOnly | boolean | false | Whether the number can be read and selected but not changed. The buttons are disabled. |
form | string | — | The id of the form the number belongs to, when the field is not inside it. |
render | (props) => JSX.Element | — | Renders the Root as another element. Read once. |
Input
The spinbutton: an <input type="text" role="spinbutton" inputmode="decimal" autocomplete="off"> with aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext. It shows the number formatted, keeps text as the user types it, and reads it back on blur and on Enter: text that is not a number goes back to the number before. Pass inputmode="numeric" for whole numbers, or "text" where a minus sign must be typed on iOS, whose decimal pad has none.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the input with your own component, which must render an <input>. Read once. |
Increment
A <button type="button" tabindex="-1"> that increases the number by step, and repeats while held. It is out of the tab order, since the arrow keys do the same, keeps focus on the Input, and is disabled at max. It has no name of its own: give it aria-label or text, such as aria-label="Increase".
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the button with your own component, which must render a <button>. Read once. |
Decrement
A <button type="button" tabindex="-1"> that decreases the number by step, and repeats while held, disabled at min. Give it a name, such as aria-label="Decrease".
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the button with your own component, which must render a <button>. Read once. |
Keyboard
| Keys | Action |
|---|---|
| ArrowUp | Increases the number by step. |
| ArrowDown | Decreases the number by step. |
| PageUp | Increases the number by largeStep. |
| PageDown | Decreases the number by largeStep. |
| Home | Sets the number to min, if set; otherwise moves the caret. |
| End | Sets the number to max, if set; otherwise moves the caret. |
| Enter | Reads the typed text as the number, then submits the form as usual. |
In a Field
A Number Field joins a Field as Field.Control's render, with the Root as the Control: the Root hands the Field's id, description and aria-invalid to the Input, so the Label names it, and its name, required and disabled reach the hidden input and every part. The Field reads the hidden input's validity: with required, an empty field shows an error once touched, and a typed number off the step shows the browser's step mismatch message. Typed numbers are clamped to min and max, so only a value of yours can be out of range. Put the Input and the buttons inside Field.Control, which passes its children to the Root.
import { Field } from "menhir/field";
import { NumberField } from "menhir/number-field";
export default function NumberFieldInField() {
return (
<Field.Root name="price">
<Field.Label>Price</Field.Label>
<Field.Control
required
render={(p) => (
<NumberField.Root
{...p}
min={0}
step={0.5}
format={{ style: "currency", currency: "EUR" }}
locale="de-DE"
/>
)}
>
<NumberField.Input />
</Field.Control>
<Field.Description>In half euros.</Field.Description>
<Field.Error />
</Field.Root>
);
}