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.

kg

From −50 to 100.

%

In a form

Kč

Sent: nothing yet

React

kg

Value: null. onValueChange: none

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

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.

TokenDefault
--sf-touch-target44px
--sf-field-border-color#71717a
--sf-field-font-size1rem
--sf-field-gapvar(--sf-space-1)
--sf-field-paddingvar(--sf-space-3)
--sf-field-short-width6rem
--sf-field-suffix-gapvar(--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.