Number Field
Takes a number in a text input that writes it in the reader's locale, with stepper buttons, bounds and keyboard stepping.
Chai is reordered when stock falls to this level.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';export default function NumberFieldBasic() { return ( <Field required> <FieldLabel>Reorder point</FieldLabel> <NumberField defaultValue={25} end="units" max={500} min={0} step={5} /> <FieldDescription>Chai is reordered when stock falls to this level.</FieldDescription> </Field> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { NumberField, NumberFieldDefaultsProvider, NumberFieldScrub } from '@slotted/react';Sizes
size sets the height, the padding, the type and the stepper glyphs to one step of the control scale: sm for dense tables and toolbars, md for most forms, lg for sparse forms. Inside a field, a number field that states no size takes the field’s.
import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldSizes() { return ( <Stack gap={4}> <NumberField aria-label="Units per case" defaultValue={24} min={1} size="sm" /> <NumberField aria-label="Cases ordered" defaultValue={12} min={1} size="md" /> <NumberField aria-label="Pallets ordered" defaultValue={2} min={1} size="lg" /> </Stack> );}Variants
variant sets how much frame the control draws. outline, the default, draws a border on the ground it sits on. subtle fills the control with a sunken ground. plain draws no border and no ground at rest and keeps the same box, so a table cell that holds one does not change size under the pointer; it pairs with stepper="none" for an amount that is typed and never spun.
import { Field, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldVariants() { return ( <Stack gap={4}> <Field> <FieldLabel>Unit price</FieldLabel> <NumberField defaultValue={18} format={{ currency: 'USD', style: 'currency' }} min={0} variant="outline" /> </Field> <Field> <FieldLabel>Discount</FieldLabel> <NumberField defaultValue={0.05} format={{ style: 'percent' }} max={1} min={0} step={0.01} variant="subtle" /> </Field> <Field> <FieldLabel>Freight</FieldLabel> <NumberField defaultValue={32.38} format={{ currency: 'USD', style: 'currency' }} min={0} stepper="none" variant="plain" /> </Field> </Stack> );}Steppers
stepper places the two buttons. stacked, the default, puts increment over decrement in one column at the inline end, which is what a form expects. split puts decrement before the value and increment after it, the arrangement of a quantity picker and the one whose targets are at least 24 pixels in both directions. none removes them. Held down, a button steps once, waits, and then repeats. Neither button is in the tab order: the arrow keys in the input do the same.
import { Field, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldSteppers() { return ( <Stack gap={4}> <Field> <FieldLabel>Reorder quantity</FieldLabel> <NumberField defaultValue={40} min={0} step={10} stepper="stacked" /> </Field> <Field> <FieldLabel>Quantity</FieldLabel> <NumberField defaultValue={3} max={99} min={1} stepper="split" /> </Field> <Field> <FieldLabel>Units in stock</FieldLabel> <NumberField defaultValue={39} min={0} stepper="none" /> </Field> </Stack> );}Bounds and step
min and max bound the value, and the button that would leave the range is disabled at the bound. Home and End move to min and max. step is what one press or one arrow key adds, 1 by default. With snapToStep, stepping lands on multiples of step counted from min, and a typed value goes to the nearest one when the field commits: type 20 into the second field and leave it.
A 53-foot trailer holds 26 pallets.
Sold in cases of 6. Type 20 and leave the field.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldRange() { return ( <Stack gap={5}> <Field> <FieldLabel>Pallets on the trailer</FieldLabel> <NumberField defaultValue={26} max={26} min={0} /> <FieldDescription>A 53-foot trailer holds 26 pallets.</FieldDescription> </Field> <Field> <FieldLabel>Bottles of Chartreuse verte</FieldLabel> <NumberField defaultValue={12} min={6} snapToStep step={6} /> <FieldDescription>Sold in cases of 6. Type 20 and leave the field.</FieldDescription> </Field> </Stack> );}Keyboard and wheel
ArrowUp and ArrowDown move by step. Shift with an arrow, Page Up and Page Down move by largeStep, which defaults to ten steps. Alt with an arrow moves by smallStep, which defaults to a tenth of a step. wheel lets the mouse wheel step the value while the input has focus and the pointer is over it; it is off by default, so scrolling a form never changes an amount. Escape is not bound.
Arrows: 1 kg. Shift or Page keys: 10 kg. Alt: 0.1 kg.
With the field focused, the mouse wheel steps the value.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldKeyboard() { return ( <Stack gap={5}> <Field> <FieldLabel>Shipping weight</FieldLabel> <NumberField defaultValue={12.5} format={{ maximumFractionDigits: 1, style: 'unit', unit: 'kilogram' }} largeStep={10} min={0} smallStep={0.1} step={1} /> <FieldDescription>Arrows: 1 kg. Shift or Page keys: 10 kg. Alt: 0.1 kg.</FieldDescription> </Field> <Field> <FieldLabel>Safety stock</FieldLabel> <NumberField defaultValue={15} min={0} wheel /> <FieldDescription> With the field focused, the mouse wheel steps the value. </FieldDescription> </Field> </Stack> );}Out of range
clamp decides what happens to a typed value outside min and max. blur, the default, accepts any keystroke and returns the value to the range when the field commits, on blur or Enter. strict refuses the keystroke that would leave the range. none keeps the value and sets aria-invalid on the input and data-invalid on the frame while it is out of range, for a form that shows an error instead of changing the reader’s number.
Type 40 and leave the field: it returns to 25.
A keystroke that would pass 25 is refused.
40 stays, and the field is marked invalid.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldClamp() { return ( <Stack gap={5}> <Field> <FieldLabel>Line discount</FieldLabel> <NumberField clamp="blur" defaultValue={10} end="%" max={25} min={0} /> <FieldDescription>Type 40 and leave the field: it returns to 25.</FieldDescription> </Field> <Field> <FieldLabel>Order discount</FieldLabel> <NumberField clamp="strict" defaultValue={10} end="%" max={25} min={0} /> <FieldDescription>A keystroke that would pass 25 is refused.</FieldDescription> </Field> <Field> <FieldLabel>Contract discount</FieldLabel> <NumberField clamp="none" defaultValue={10} end="%" max={25} min={0} /> <FieldDescription>40 stays, and the field is marked invalid.</FieldDescription> </Field> </Stack> );}Currency, percent and units
format takes Intl.NumberFormatOptions, so currency, percent and unit are ways of writing one number rather than separate components. The value stays a plain number: the credit limit below holds 25000 and the sales tax 0.0825. The input reads back what it writes, so a pasted $25,000.00 parses.
import { Field, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldFormat() { return ( <Stack gap={4}> <Field> <FieldLabel>Credit limit</FieldLabel> <NumberField defaultValue={25000} format={{ currency: 'USD', style: 'currency' }} min={0} step={500} /> </Field> <Field> <FieldLabel>Sales tax</FieldLabel> <NumberField defaultValue={0.0825} format={{ maximumFractionDigits: 2, style: 'percent' }} max={1} min={0} step={0.0025} /> </Field> <Field> <FieldLabel>Pallet volume</FieldLabel> <NumberField defaultValue={1.8} format={{ style: 'unit', unit: 'liter', unitDisplay: 'long' }} min={0} step={0.1} /> </Field> </Stack> );}Decimals
Precision is set in format with minimumFractionDigits and maximumFractionDigits. The text is rounded and padded when the field commits, never while you type, and the committed number is the one the input shows. With maximumFractionDigits: 0 the input refuses the decimal separator. The on-screen keyboard follows the props: numeric for a whole number at or above zero, decimal when the format allows a fraction, and the full keyboard when min allows a negative.
Four decimals: 1.1 becomes 1.1000 on blur.
Whole cartons: the decimal separator is refused.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldDecimals() { return ( <Stack gap={5}> <Field> <FieldLabel>Exchange rate, EUR to USD</FieldLabel> <NumberField defaultValue={1.0842} format={{ maximumFractionDigits: 4, minimumFractionDigits: 4 }} min={0} step={0.0001} /> <FieldDescription>Four decimals: 1.1 becomes 1.1000 on blur.</FieldDescription> </Field> <Field> <FieldLabel>Cartons</FieldLabel> <NumberField defaultValue={40} format={{ maximumFractionDigits: 0 }} min={0} /> <FieldDescription>Whole cartons: the decimal separator is refused.</FieldDescription> </Field> </Stack> );}Locale
locale takes a BCP 47 tag and sets the decimal separator, the grouping and the digits the input writes and reads. Without it, the number field uses the locale of SlottedProvider in React and of provideSlotted in Angular, then en-US. Set the locale once for the application unless one amount is written differently from the rest.
import { Field, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldLocale() { return ( <Stack gap={4}> <Field> <FieldLabel>Invoice total, United States</FieldLabel> <NumberField defaultValue={14800.5} format={{ currency: 'USD', style: 'currency' }} locale="en-US" /> </Field> <Field> <FieldLabel>Invoice total, Germany</FieldLabel> <NumberField defaultValue={14800.5} format={{ currency: 'EUR', style: 'currency' }} locale="de-DE" /> </Field> <Field> <FieldLabel>Invoice total, Egypt</FieldLabel> <NumberField defaultValue={14800.5} format={{ currency: 'EGP', style: 'currency' }} locale="ar-EG" /> </Field> </Stack> );}When the text is formatted
formatOn="change", the default, regroups the text on every keystroke and keeps the caret on the character it was on, so a long number is readable while you type it. formatOn="commit" leaves your text as typed until blur, Enter, a stepper press or an arrow key, for an amount that is pasted and checked rather than typed. Under both, the number itself changes on every keystroke that parses.
Type 1250000: the grouping appears with each digit.
Your text stays as typed until Enter or blur.
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldFormatOn() { return ( <Stack gap={5}> <Field> <FieldLabel>Annual purchase budget</FieldLabel> <NumberField format={{ currency: 'USD', style: 'currency' }} formatOn="change" min={0} /> <FieldDescription>Type 1250000: the grouping appears with each digit.</FieldDescription> </Field> <Field> <FieldLabel>Amount from bank statement</FieldLabel> <NumberField format={{ currency: 'USD', style: 'currency' }} formatOn="commit" min={0} /> <FieldDescription>Your text stays as typed until Enter or blur.</FieldDescription> </Field> </Stack> );}Adornments
start and end hold content before and after the value in the reading direction: a currency mark, a unit or a code. They sit beside the number and are never selected, copied or parsed with it. A symbol that belongs inside the value, such as the $ a currency format writes, comes from format instead.
import { Field, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldAdornments() { return ( <Stack gap={4}> <Field> <FieldLabel>Purchase price</FieldLabel> <NumberField defaultValue={1480} end="USD" format={{ minimumFractionDigits: 2 }} min={0} start="$" /> </Field> <Field> <FieldLabel>Lead time</FieldLabel> <NumberField defaultValue={14} end="days" max={120} min={0} /> </Field> </Stack> );}Scrubbing
NumberFieldScrub in React and slNumberFieldScrub in Angular, placed in start or end, moves the value when it is dragged sideways: one step per sensitivity pixels, 2 by default, largeStep with Shift and smallStep with Alt. Dragging towards the inline end steps up, so a right-to-left document steps up towards the left. A press moves focus to the input. The mark is hidden from assistive technology, because the arrow keys do the same.
import { Field, FieldLabel } from '@slotted/react/field';import { Grid } from '@slotted/react/grid';import { NumberField, NumberFieldScrub } from '@slotted/react/number-field';export default function NumberFieldScrubExample() { return ( <Grid columns={2} gap={4}> <Field> <FieldLabel>Label width</FieldLabel> <NumberField defaultValue={100} end="mm" max={210} min={20} start={<NumberFieldScrub>W</NumberFieldScrub>} /> </Field> <Field> <FieldLabel>Label height</FieldLabel> <NumberField defaultValue={150} end="mm" max={297} min={20} start={<NumberFieldScrub sensitivity={4}>H</NumberFieldScrub>} /> </Field> </Grid> );}States
A number field inside a field reads invalid, readOnly and disabled from it. invalid sets aria-invalid on the input and draws the frame in the danger tone. readOnly sets the native readonly: the value keeps full contrast, stays focusable and is still submitted, and both buttons are disabled. disabled sets the native disabled, which takes the input out of the tab order and out of form submission. A state written on the number field itself wins over the field’s.
Error:Only 39 units of Chai are in stock.
Counted from open purchase orders.
Released when the order ships.
import { Field, FieldDescription, FieldError, FieldLabel } from '@slotted/react/field';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldStates() { return ( <Stack gap={5}> <Field invalid> <FieldLabel>Quantity to ship</FieldLabel> <NumberField defaultValue={120} min={1} /> <FieldError>Only 39 units of Chai are in stock.</FieldError> </Field> <Field readOnly> <FieldLabel>Units on order</FieldLabel> <NumberField defaultValue={40} /> <FieldDescription>Counted from open purchase orders.</FieldDescription> </Field> <Field disabled> <FieldLabel>Units reserved</FieldLabel> <NumberField defaultValue={12} /> <FieldDescription>Released when the order ships.</FieldDescription> </Field> </Stack> );}Controlled value and empty
value with onValueChange in React, and [value] with (valueChange) in Angular, make the number yours. null is empty, and empty is not zero: an empty field sets no aria-valuenow, and the first press of a stepper starts from stepFrom, which defaults to min and then to 0. Remove the limit below, then press increment.
import { useState } from 'react';import { Button } from '@slotted/react/button';import { Field, FieldLabel } from '@slotted/react/field';import { Inline } from '@slotted/react/inline';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';const USD = new Intl.NumberFormat('en-US', { currency: 'USD', style: 'currency' });export default function NumberFieldControlled() { const [limit, setLimit] = useState<number | null>(5000); return ( <Stack gap={4}> <Field> <FieldLabel>Credit limit for Ernst Handel</FieldLabel> <NumberField format={{ currency: 'USD', style: 'currency' }} min={0} onValueChange={setLimit} step={500} stepFrom={5000} value={limit} /> </Field> <Inline gap={3} justify="between"> <span>{limit === null ? 'No credit limit' : `Orders up to ${USD.format(limit)}`}</span> <Button disabled={limit === null} onClick={() => setLimit(null)} variant="outline"> Remove limit </Button> </Inline> </Stack> );}Stepping from your own code
handle in React reaches increment() and decrement(); in Angular, export the input as slNumberFieldInput and call the same methods from the template. Both move by one step, respect the bounds and report the reason programmatic. Use them for a shortcut or a button elsewhere on the page.
import { useRef } from 'react';import { Button } from '@slotted/react/button';import { Field, FieldLabel } from '@slotted/react/field';import { Inline } from '@slotted/react/inline';import { NumberField, NumberFieldHandle } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldHandleExample() { const cases = useRef<NumberFieldHandle>(null); return ( <Stack gap={3}> <Field> <FieldLabel>Cases of Chang</FieldLabel> <NumberField defaultValue={4} handle={cases} max={48} min={0} /> </Field> <Inline gap={2}> <Button onClick={() => cases.current?.decrement()} variant="outline"> Remove a case </Button> <Button onClick={() => cases.current?.increment()} variant="outline"> Add a case </Button> </Inline> </Stack> );}Forms and validation
name writes the raw number to a hidden input beside the visible one, so the form submits 12 rather than formatted text. Validate when the form is submitted, then set invalid on the field and write the error. The React example owns the value through value and onValueChange; the Angular example binds the input to a reactive form control, because slNumberFieldInput is a ControlValueAccessor that hands the form the number. Enter commits the value and submits the form. Submit the empty form, then a quantity.
import { useState, type SubmitEvent } from 'react';import { Button } from '@slotted/react/button';import { Field, FieldDescription, FieldError, FieldLabel } from '@slotted/react/field';import { Inline } from '@slotted/react/inline';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';export default function NumberFieldValidation() { const [quantity, setQuantity] = useState<number | null>(null); const [submitted, setSubmitted] = useState(false); const [sent, setSent] = useState(''); const error = submitted && quantity === null ? 'Enter how many units to ship.' : ''; const submit = (event: SubmitEvent<HTMLFormElement>) => { event.preventDefault(); setSubmitted(true); const data = new FormData(event.currentTarget); setSent(quantity === null ? '' : `Sent quantity=${String(data.get('quantity'))}`); }; return ( <form noValidate onSubmit={submit}> <Stack gap={4}> <Field required invalid={error !== '' ? true : undefined}> <FieldLabel>Units of Chai to ship</FieldLabel> <NumberField max={39} min={1} name="quantity" onValueChange={(next) => { setQuantity(next); setSent(''); }} value={quantity} /> <FieldDescription>39 units in stock.</FieldDescription> <FieldError live="polite" reserve> {error} </FieldError> </Field> <Inline gap={3}> <Button type="submit">Create shipment</Button> {sent === '' ? null : <span role="status">{sent}</span>} </Inline> </Stack> </form> );}Defaults for a section
NumberFieldDefaultsProvider in React and provideNumberFieldDefaults in Angular set size, variant, stepper, formatOn and clamp once for every number field under them. A number field that states a value of its own keeps it, and inside a field the field’s size wins over the provider’s.
import { Field, FieldLabel } from '@slotted/react/field';import { Grid } from '@slotted/react/grid';import { NumberField, NumberFieldDefaultsProvider } from '@slotted/react/number-field';export default function NumberFieldDefaults() { return ( <NumberFieldDefaultsProvider size="sm" stepper="split" variant="subtle"> <Grid gap={3} minItemWidth="xs"> <Field> <FieldLabel>Cases</FieldLabel> <NumberField defaultValue={6} min={0} /> </Field> <Field> <FieldLabel>Loose units</FieldLabel> <NumberField defaultValue={11} min={0} /> </Field> <Field> <FieldLabel>Pallets</FieldLabel> <NumberField defaultValue={1} min={0} stepper="stacked" /> </Field> </Grid> </NumberFieldDefaultsProvider> );}Order lines
A quantity per line, with stepper="split" at size="sm", max set to the stock on hand and the line total computed from the controlled value. An emptied quantity counts as zero in the total.
import { useState } from 'react';import { Grid } from '@slotted/react/grid';import { Inline } from '@slotted/react/inline';import { NumberField } from '@slotted/react/number-field';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const USD = new Intl.NumberFormat('en-US', { currency: 'USD', style: 'currency' });const LINES = [ { id: 'cabrales', product: 'Queso Cabrales', price: 14, stock: 22 }, { id: 'hokkien', product: 'Singaporean Hokkien Fried Mee', price: 9.8, stock: 26 }, { id: 'mozzarella', product: 'Mozzarella di Giovanni', price: 34.8, stock: 14 },];export default function NumberFieldOrderLine() { const [quantities, setQuantities] = useState<Record<string, number | null>>({ cabrales: 12, hokkien: 10, mozzarella: 5, }); const total = LINES.reduce((sum, line) => sum + line.price * (quantities[line.id] ?? 0), 0); return ( <Stack gap={5}> <Stack gap={4}> {LINES.map((line) => ( <Grid key={line.id} columns={3} gap={4} minItemWidth="xs"> <Stack gap={1}> <Text weight="medium">{line.product}</Text> <Text size="sm" tone="muted"> {USD.format(line.price)} each · {line.stock} in stock </Text> </Stack> <Stack justify="center"> <NumberField aria-label={`Quantity of ${line.product}`} max={line.stock} min={0} onValueChange={(next) => setQuantities((current) => ({ ...current, [line.id]: next })) } size="sm" stepper="split" value={quantities[line.id] ?? null} /> </Stack> <Stack justify="center"> <Text align="end" numeric="tabular"> {USD.format(line.price * (quantities[line.id] ?? 0))} </Text> </Stack> </Grid> ))} </Stack> <Inline gap={4} justify="between"> <Text weight="semibold">Order total</Text> <Text numeric="tabular" weight="semibold"> {USD.format(total)} </Text> </Inline> </Stack> );}Price editor
Two controlled inputs in currency and percent, and a read-only result computed from them with stepper="none". The markup holds 0.5 and shows 50%; Shift with an arrow moves it by ten points through largeStep.
Unit cost plus markup.
import { useState } from 'react';import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';import { Grid } from '@slotted/react/grid';import { NumberField } from '@slotted/react/number-field';const CURRENCY = { currency: 'USD', style: 'currency' } as const;export default function NumberFieldPriceEditor() { const [cost, setCost] = useState<number | null>(175); const [markup, setMarkup] = useState<number | null>(0.5); const price = cost === null || markup === null ? null : Math.round(cost * (1 + markup) * 100) / 100; return ( <Grid gap={4} minItemWidth="xs"> <Field required> <FieldLabel>Unit cost</FieldLabel> <NumberField format={CURRENCY} min={0} onValueChange={setCost} step={0.5} value={cost} /> </Field> <Field required> <FieldLabel>Markup</FieldLabel> <NumberField format={{ style: 'percent' }} largeStep={0.1} max={3} min={0} onValueChange={setMarkup} step={0.01} value={markup} /> </Field> <Field readOnly> <FieldLabel>List price</FieldLabel> <NumberField format={CURRENCY} stepper="none" value={price} /> <FieldDescription>Unit cost plus markup.</FieldDescription> </Field> </Grid> );}Accessibility
| Key | Effect | Focus |
|---|---|---|
| ArrowUp | step | — |
| ArrowDown | step | — |
| Shift+ArrowUp | step | — |
| Shift+ArrowDown | step | — |
| Alt+ArrowUp | step | — |
| Alt+ArrowDown | step | — |
| PageUp | step | — |
| PageDown | step | — |
| Home | min | — |
| End | max | — |
| Enter | commit | — |
- NumberField follows the WAI-ARIA APG Spinbutton pattern: role="spinbutton" on the input with aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext, ArrowUp and ArrowDown to step, Home and End to reach the bounds, and Page Up and Page Down for the larger step.
- aria-valuetext carries the formatted text, so a screen reader announces "$1,480.00" rather than 1480. When the number field is empty, neither aria-valuenow nor aria-valuetext is set: no value is not the value zero.
- The frame has no role. WAI-ARIA requires the children of a spinbutton to be non-interactive, so the two buttons are siblings of the input inside a box that announces nothing.
- The buttons are out of the tab order, as the APG pattern recommends, and stay in the accessibility tree with translatable names and aria-controls pointing at the input. A form of twenty amounts keeps twenty tab stops instead of sixty. A button at a bound is disabled, so its name is still announced and its appearance shows why it does nothing.
- Inside a required field the input receives aria-required, never the native required, which would turn on the browser’s constraint validation. disabled and readonly are the native attributes.
- The input is type="text", and its inputMode comes from the props: numeric for a whole number at or above zero, decimal when the format allows a fraction, and text when min allows a negative, because neither numeric keypad offers a minus sign on every platform.
- The mouse wheel is not bound unless wheel is set, and Escape is not bound at all, so scrolling a form never changes an amount.
- The invalid state is never conveyed by color alone: aria-invalid announces it and the field’s error states it in words. In the high-contrast scheme the border does not carry it.
API
NumberField
Props
valuenumber | null- Makes the value controlled for the life of the component. null is empty, and empty is not zero: a required amount left blank and an amount of zero are different values.
defaultValuenumber | null- The value an uncontrolled number field starts from. It is read once, and a native form reset returns to it.
minnumber- The smallest value. The decrement button is disabled at it, Home moves to it, and the snapToStep grid is counted from it.
maxnumber- The largest value. The increment button is disabled at it, and End moves to it.
stepnumberDefault1- What the arrow keys and the two buttons add or subtract.
largeStepnumberDefault step × 10- What Shift with an arrow, Page Up and Page Down add or subtract, so a wide range is crossed without holding a key.
smallStepnumberDefault step ÷ 10- What Alt with an arrow adds or subtracts.
stepFromnumberDefault min, or 0- The value the first step produces when the number field is empty.
snapToStepbooleanDefaultfalse- Steps to multiples of step counted from min, so with min 2 and step 3 the values are 2, 5, 8 and 11. A typed value moves to the nearest multiple when the number field commits.
clamp'blur' | 'strict' | 'none'Defaultblur- On NumberField, NumberFieldDefaultsProviderWhat happens to a value outside min and max. blur returns it to the range when the number field commits, strict refuses the keystroke that would leave the range, and none keeps the value and sets aria-invalid and data-invalid while it is out of range.
formatIntl.NumberFormatOptions- How the value is written and read back. Currency, percent and unit are formats of one number, and precision is set with minimumFractionDigits and maximumFractionDigits. When the number field commits, the number is rounded to the precision the input shows.
formatOn'change' | 'commit'Defaultchange- On NumberField, NumberFieldDefaultsProviderWhen the text takes the format. change regroups it on every keystroke; commit keeps the typed text until blur, Enter, a stepper press or an arrow key. Under both, the number changes on every keystroke that parses.
localestringDefault the locale of SlottedProvider, then en-US- A BCP 47 tag. It sets the decimal separator, the grouping and the digits, for the text written and the text read back.
stepper'stacked' | 'split' | 'none'Defaultstacked- On NumberField, NumberFieldDefaultsProviderWhere the two buttons sit. split places them at either end of the frame, and its targets are at least 24 CSS pixels in both directions; none removes them.
wheelbooleanDefaultfalse- Lets the mouse wheel step the value while the input has focus and the pointer is over it. It is off by default, so scrolling a form never changes an amount.
size'sm' | 'md' | 'lg'Defaultmd- On NumberField, NumberFieldDefaultsProviderThe step of the control scale. When unset, the number field takes the size of its field, then of the provider. Height, padding, type and glyphs come from the control scale the theme sets per density.
variant'outline' | 'subtle' | 'plain'Defaultoutline- On NumberField, NumberFieldDefaultsProviderHow much frame the number field draws. plain draws no border at rest and keeps the same box, for a dense table cell.
startReactNode- Content before the value in the reading direction. It is never selected, copied or parsed with the number.
endReactNode- Content after the value in the reading direction.
NumberFieldScrub sensitivitynumberDefault2- On NumberFieldScrubPixels of horizontal drag per step. Use fewer pixels for a wide range and more for a fine one.
disabledbooleanDefault from the field, or false- Unset rather than false, so the number field follows the field around it. When set, it wins over the field and writes the native disabled, which removes the input from the tab order and from form submission.
readOnlybooleanDefault from the field, or false- Unset rather than false, so the number field follows the field around it. Writes the native readonly: the input stays focusable, selectable and submitted, and both buttons are disabled.
invalidbooleanDefault from the field, or false- Unset rather than false, so the number field follows the field around it. Writes aria-invalid on the input and data-invalid on the frame.
requiredboolean- The element’s own attribute, passed through unchanged; NumberField never writes it. Inside a required field, the input receives aria-required instead, because the native attribute turns on the browser’s constraint validation.
name / formstring- Reach a hidden input beside the visible one, which submits the raw number: 1480, not "$1,480.00".
messagesPartial<NumberFieldMessages>- Replaces words of the active language, key by key: increment and decrement are the accessible names of the two buttons.
Events
| Name | Payload | When |
|---|---|---|
onValueChange | (value: number | null, request: ChangeRequest<number | null>) | Before the value changes. request.value is the new number and request.previous the number it replaces. request.reason is keyboard for typing, an arrow, a page key, Home, End or Enter; pointer for a stepper press, a wheel turn or a scrub; blur for the commit that formats, rounds and clamps on blur; programmatic for handle.increment() and handle.decrement(). request.source is the originating event, and request.cancel() refuses the change before it is applied. A request for the number NumberField already holds is not emitted. |
onChange, onBlur, onKeyDown, onFocus | the element’s own events | Whenever the input fires them. NumberField composes your handler with its own instead of replacing it: yours runs first and always runs, and calling preventDefault() on a key stops NumberField from acting on it. |
Composition
render(props: NumberFieldRootProps) => ReactNode- Renders another element in place of the frame and passes it every attribute. The props are typed as HTMLAttributes<HTMLElement>, so they spread onto any element. The input and the two buttons cannot be replaced.
refRef<HTMLInputElement>- Reaches the input element. focus(), select() and setSelectionRange() are the element’s own.
handleRef<NumberFieldHandle>- Reaches increment() and decrement(), for a shortcut or a button elsewhere on the page. Both move by one step within the bounds and report the reason programmatic.
className / stylestring / CSSProperties- Reach the frame. Every other native prop reaches the input.
NumberFieldDefaultsProvider{ size, variant, stepper, formatOn, clamp }- On a subtreeSets size, variant, stepper, formatOn and clamp once for every number field under it. A number field that states a value keeps it. The locale comes from SlottedProvider, so every amount in the application is written and read the same way.
useField()() => FieldContextValue | undefined- On inside a FieldWhat the number field reads to wire itself: the id, the description and the four shared states.
Parts
| Part | Element | Required | Purpose |
|---|---|---|---|
root | div, or the element render returns | Yes | The frame. It carries data-ui="number-field", data-size, data-variant and data-stepper, and data-disabled, data-invalid, data-readonly and data-scrubbing while they apply. It has no role: a spinbutton may hold only non-interactive children, so the two buttons are siblings of the input inside a box that announces nothing. className and style reach this element. |
input | input[type=text][role=spinbutton] | Yes | The control. It holds the formatted text, the id, and aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext. It is type="text" because type="number" empties itself whenever the text is not a plain decimal number, so no grouping separator or currency symbol would survive it. ref reaches this element. |
start | span | No | Content before the value in the reading direction. It sits beside the number and is never selected, copied or parsed with it. A symbol that belongs inside the value comes from format instead. |
end | span | No | Content after the value in the reading direction, such as a unit or a currency code. |
scrub | span (NumberFieldScrub) | No | A mark in start or end that moves the value when it is dragged sideways: one step per sensitivity pixels towards the inline end, largeStep with Shift and smallStep with Alt. The mark and the frame carry data-scrubbing while it is dragged. It is hidden from assistive technology, because the arrow keys in the input make the same change. |
stepper | span | No | The column that holds both buttons under stepper="stacked", inset from the frame and drawn without a border of its own; each button takes the inset back as hit area. It is not rendered under split, where the buttons are direct children of the frame. |
decrement | button[type=button][tabindex=-1] | No | Steps down by step, by largeStep with Shift and by smallStep with Alt, and repeats while it is held. It is disabled at min, and it is out of the tab order because the arrow keys in the input make the same change. |
increment | button[type=button][tabindex=-1] | No | Steps up by the same amounts and is disabled at max. Both buttons carry a translatable accessible name and aria-controls pointing at the input. |
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every number field inside it.
Number Field tokens
| Token | Default |
|---|---|
--slotted-number-field-action-size | min(calc(var(--_icon-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size)) |
--slotted-number-field-adornment-color | var(--slotted-text-muted, GrayText) |
--slotted-number-field-background | var(--slotted-control-surface, Field) |
--slotted-number-field-border-color | var(--slotted-control-border-color, ButtonBorder) |
--slotted-number-field-color | var(--slotted-text, CanvasText) |
--slotted-number-field-disabled-background | var(--slotted-disabled-background) |
--slotted-number-field-disabled-border-color | var(--slotted-disabled-border) |
--slotted-number-field-disabled-color | var(--slotted-disabled-foreground) |
--slotted-number-field-hover-border-color | var(--slotted-tone-accent-border) |
--slotted-number-field-icon-size-min | 14px |
--slotted-number-field-invalid-border-color | var(--slotted-tone-danger-border) |
--slotted-number-field-invalid-edge-width | var(--slotted-control-invalid-edge-width, 1px) |
--slotted-number-field-invalid-focus-ring-color | var(--slotted-focus-ring-color-invalid, Highlight) |
--slotted-number-field-placeholder-color | var(--slotted-text-muted, GrayText) |
--slotted-number-field-plain-hover-background | var(--slotted-tone-secondary-subtle-hover) |
--slotted-number-field-radius | var(--slotted-control-radius, 4px) |
--slotted-number-field-readonly-background | var(--slotted-tone-secondary-subtle-hover) |
--slotted-number-field-readonly-border-color | var(--slotted-border-subtle, ButtonBorder) |
--slotted-number-field-scrub-active-color | var(--slotted-text, CanvasText) |
--slotted-number-field-shadow | var(--slotted-control-shadow, none) |
--slotted-number-field-stepper-active-background | var(--slotted-tone-secondary-subtle-active) |
--slotted-number-field-stepper-color | var(--slotted-text-muted, GrayText) |
--slotted-number-field-stepper-disabled-background | var(--slotted-disabled-background) |
--slotted-number-field-stepper-disabled-color | var(--slotted-disabled-foreground) |
--slotted-number-field-stepper-hover-background | var(--slotted-tone-secondary-subtle-hover) |
--slotted-number-field-stepper-size | max( var(--slotted-target-min, 24px), min(calc(var(--_icon-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size)) ) |
--slotted-number-field-subtle-background | var(--slotted-surface-sunken, Canvas) |
--slotted-number-field-subtle-border-color | var(--slotted-border-subtle, ButtonBorder) |
--slotted-number-field-subtle-hover-background | var(--slotted-tone-secondary-subtle-hover) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-border-subtle | Set per scheme by the theme |
--slotted-control-border-color | Set per scheme by the theme |
--slotted-control-border-width | var(--slotted-border-width-thin) |
--slotted-control-focus-ring-offset | var(--slotted-focus-ring-offset) |
--slotted-control-font-family | var(--slotted-font-family-sans) |
--slotted-control-font-size-lg | comfortable 14px · compact 13px · dense 12px |
--slotted-control-font-size-md | comfortable 13px · compact 12px · dense 12px |
--slotted-control-font-size-sm | comfortable 12px · compact 12px · dense 12px |
--slotted-control-gap-lg | comfortable 8px · compact 6px · dense 6px |
--slotted-control-gap-md | comfortable 8px · compact 6px · dense 4px |
--slotted-control-gap-sm | comfortable 6px · compact 4px · dense 4px |
--slotted-control-height-lg | comfortable 38px · compact 32px · dense 30px |
--slotted-control-height-md | comfortable 34px · compact 28px · dense 26px |
--slotted-control-height-sm | comfortable 30px · compact 26px · dense 24px |
--slotted-control-icon-size-lg | comfortable 18px · compact 16px · dense 14px |
--slotted-control-icon-size-md | comfortable 16px · compact 14px · dense 13px |
--slotted-control-icon-size-sm | comfortable 14px · compact 13px · dense 12px |
--slotted-control-inner-size-lg | comfortable 28px · compact 24px · dense 22px |
--slotted-control-inner-size-md | comfortable 24px · compact 20px · dense 18px |
--slotted-control-inner-size-sm | comfortable 22px · compact 18px · dense 16px |
--slotted-control-invalid-edge-width | var(--slotted-border-width-thin) |
--slotted-control-padding-inline-lg | comfortable 16px · compact 12px · dense 10px |
--slotted-control-padding-inline-md | comfortable 13px · compact 10px · dense 8px |
--slotted-control-padding-inline-sm | comfortable 10px · compact 8px · dense 6px |
--slotted-control-radius | var(--slotted-radius-sm) |
--slotted-control-shadow | Set per scheme by the theme |
--slotted-control-shadow-focus-geometry | 0 1px 4px -1px |
--slotted-control-surface | Set per scheme by the theme |
--slotted-control-transition-duration | var(--slotted-duration-short) |
--slotted-control-transition-easing | var(--slotted-easing-standard) |
--slotted-disabled-background | Set per scheme by the theme |
--slotted-disabled-border | Set per scheme by the theme |
--slotted-disabled-foreground | Set per scheme by the theme |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-color-invalid | Set per scheme by the theme |
--slotted-focus-ring-offset | var(--slotted-border-width-thick) |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |
--slotted-radius-none | 0px |
--slotted-radius-xs | 2px |
--slotted-space-1 | 0.125rem |
--slotted-space-2 | 0.25rem |
--slotted-surface-sunken | Set per scheme by the theme |
--slotted-target-comfortable | 44px |
--slotted-target-min | 24px |
--slotted-text | Set per scheme by the theme |
--slotted-text-muted | Set per scheme by the theme |
--slotted-tone-accent-border | Set per scheme by the theme |
--slotted-tone-danger-border | Set per scheme by the theme |
--slotted-tone-secondary-subtle-active | Set per scheme by the theme |
--slotted-tone-secondary-subtle-hover | Set per scheme by the theme |