Component · in flow
Number field
A number with a decimal comma or dot, typed or stepped with − and +. A text input with the right phone keyboard, never type=number: the wheel does not change it.
From −50 to 100.
Too low for this plan.
In a form
Sent:
React
Value: . onValueChange:
Usage
Astro:
---
import NumberField from '@subfloor/astro/NumberField.astro'
---
<NumberField id="quantity" name="quantity" label="Množství" suffix="kg"
locale="cs" decimals={1} step={0.5} min={0} max={50} buttons
decreaseLabel="Ubrat" increaseLabel="Přidat"
invalidMessage="Zadejte číslo." rangeMessage="Zadejte {min} až {max}." />
<!-- No buttons, no step: a number to type, such as an amount. -->
<NumberField id="amount" name="amount" label="Částka" suffix="Kč"
locale="cs" max={100000} required />React:
import { NumberField } from '@subfloor/react'
// A number, or null for an empty field. Never the text.
<NumberField label="Množství" suffix="kg" locale="cs" decimals={1}
step={0.5} min={0} max={50} buttons value={qty} onValueChange={setQty}
decreaseLabel="Ubrat" increaseLabel="Přidat" />On the server, the form sends the text as shown:
import { parseNumber } from '@subfloor/core'
const amount = parseNumber(form.get('amount') ?? '')
// null: empty. NaN: not a number. Check min and max here too.Markup
<div class="sf-field">
<label class="sf-field__label" for="quantity">Quantity</label>
<div class="sf-field__row">
<div class="sf-number" data-sf-number>
<button type="button" class="sf-number__button" data-step="down"
tabindex="-1" aria-label="Decrease: Quantity">…</button>
<input class="sf-field__control" type="text" id="quantity" name="quantity"
inputmode="decimal" autocomplete="off" value="1,5"
pattern="(?=.*\d)\s*[+\-−]?[\d\s]*(?:[.,][\d\s]*)?%?\s*"
aria-describedby="quantity-suffix" data-locale="cs" data-decimals="1"
data-min="0" data-max="50" data-step="0.5">
<button type="button" class="sf-number__button" data-step="up"
tabindex="-1" aria-label="Increase: Quantity">…</button>
</div>
<span class="sf-field__suffix" id="quantity-suffix">kg</span>
</div>
</div>
<script type="module">
import { initNumberFields } from '@subfloor/core'
initNumberFields()
</script>Behaviour
- A text input with
inputmode="decimal"(numericfor whole numbers that cannot be negative), nevertype="number": the mouse wheel never changes it, and text it cannot read is still there to fix. - It reads
,or.as the decimal mark, spaces as thousands separators,-or−as a minus, a leading+and a trailing%. When you leave it, it rounds todecimals(2 by default) and writes the number in itslocale, with a real minus sign. - Text that is not a number, and a number outside
minandmax, stay as typed and make the field invalid: the form does not submit, and the message isinvalidMessageorrangeMessage. A typed number is never pulled into range. - With
stepit is a spinbutton: ↑ ↓ step it, Page Up and Page Down by ten, to the next value on the step grid, never pastminormax; a number already out of range goes to the nearer end, as a native spinbutton does. Withbuttons, − and + do the same; they are not Tab stops, a press does not move focus (a phone's keyboard stays closed), and at the ends they arearia-disabled. - The iPhone's decimal keyboard has no minus key: a negative number comes from the − button or a hardware keyboard.
- React's
onValueChangegets the number, ornullfor an empty field, once per change, also for one typed before React hydrated; text it cannot read is not reported.
Without JavaScript
A text field with the right keyboard. Its pattern stops text that is not a number; rounding, range checks, the arrow keys and the buttons need the script, and the buttons are hidden.
Customisation
It is the kit's field: the field tokens set its look, and the buttons are --sf-touch-target square.
| Token | Default |
|---|---|
--sf-touch-target | 44px |
--sf-field-border-color | #71717a |
--sf-field-font-size | 1rem |
--sf-field-gap | var(--sf-space-1) |
--sf-field-padding | var(--sf-space-3) |
--sf-field-short-width | 6rem |
--sf-field-suffix-gap | var(--sf-space-2) |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.