Skip to content
EspañolEnglish
All components (283)

Number field

Fields · NumberField

What it is and when to use it

A number formatted for the language: in es-ES an amount is written and read 1234,56 €, with a decimal comma. With buttons it works like the quantity selector, − 12 +: the minus in front, the plus behind and the figure in the middle.

When to use it

  • Amounts, percentages, units, weights: everything that is added up, compared or has a minimum and maximum.
  • format: currency (euros), percent (stored as a fraction of one: 0.21 is shown as 21 %), integer or decimal. formatOptions to fine-tune it (another currency, fixed decimals).
  • thousandSeparator, decimalSeparator and groupStyle when the format is not the language's: a space for thousands, a decimal point, or Indian grouping (12,34,567, lakh) or in fours (1234,5678, wan). It is typed and read the same way, and what travels in a form is still the clean figure.
  • withoutSteps in amount columns or when the step makes no sense (a weight with decimals): without buttons, the figure goes to the right.

When not to

  • A number that is not operated on (a phone number, a postal code, an ID number): that is text, TextField or its composite fields.
  • A cart quantity: QuantityPicker, more compact.

Keyboard: it is a text field with a decimal keyboard, not an <input type="number">: the mouse wheel does not change the figure. Up Arrow and Down Arrow raise and lower one step, Page Up and Page Down ten steps, and Home and End take it to the minimum and the maximum if there are any; minValue, maxValue and step are respected. What is typed is adjusted to the format on leaving the field or with Enter. The buttons are not in the Tab order: with the keyboard the arrows are used.

Accessibility: the label is required. The help and the error are read as the field's description. With touch, the box becomes 48 px, the buttons 44 and the font 16 px, so the iPhone does not zoom the page.

Fine-tuning: it has no variables of its own. The height, padding, font, radius, background and border come from those of the field (--amset-field-*), and the increase and decrease buttons are sized from --amset-field-height, so changing it moves them along with the box. To tweak only this field, the rule goes on the .amset-field of the container that wraps it, not on :root.

Context: it goes inside LocaleProvider (or AmsetProvider), which decides the format.

What it looks like

<NumberField label="Units" format="integer" defaultValue={3} minValue={0} />
<NumberField label="Amount" format="currency" defaultValue={1234.5} minValue={0} step={0.01} />
<NumberField label="VAT" format="percent" defaultValue={0.21} minValue={0} maxValue={1} step={0.01} />

Props

Import it with import { NumberField } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
labelrequiredstring—
backReactNodeContent after the figure, inside the box: a unit (“kg”, “pcs”).
clampBehaviorClampBehaviorWhat happens to a figure outside `minValue` or `maxValue`: `blur` (the default) adjusts it on leaving the field, `strict` also stops it from being typed above the maximum, and `none` leaves it as it is so the form can flag it with an error.
classNamestring—
decimalScalenumberHow many decimals at most. Shortcut for `formatOptions.maximumFractionDigits`.
decimalSeparatorstringThe decimal separator, if it is not to be the language's.
decrementAriaLabelstring—
defaultValuenumber—
disabledboolean—
errorstring—
fixedDecimalScalebooleanWith `decimalScale`, always shows that many decimals (“12.50”, not “12.5”).
formatNumberFormatFormat of the number. `currency` uses euros; for another currency, pass `formatOptions` directly.
formatOptionsIntl.NumberFormatOptionsThe `Intl.NumberFormat` options, if the format above is not enough.
formatValue(value: number) => stringWrites the figure to measure instead of with `Intl` ("12 units", "1.5 h"). Always goes with `parseValue`, which has to be able to read what this returns.
frontReactNodeContent before the figure, inside the box: a prefix (“≈”), an icon.
groupStyleGroupStyleHow digits are grouped: `thousand` in threes (1.234.567), `lakh` the Indian way, three and then in twos (12,34,567) and `wan` in fours, as in Chinese and Japanese (123,4567).
hiddenLabelbooleanThe label for the screen reader only. For when the field sits inside something that already explains itself (a table cell, a group of filters), never to save space.
hintstring—
idstring—
incrementAriaLabelstring—
inputRefRef<HTMLInputElement>The real `<input>`, to focus it or select its text from outside.
invalidboolean—
largeStepnumberThe jump of Shift with an arrow and of Page Up and Page Down. Defaults to ten times `step`.
maxValuenumber—
minValuenumber—
namestringThe name it travels under in a native form. What is sent is the number (`10.5`, `0.21`), with a dot and no formatting, and empty if there is no figure: not the text you see (`10,5`, `21 %`), which a server cannot read.
onChange(value: number) => voidNotifies the new figure. If the field is emptied, it notifies `NaN`: the usual "no value".
onChangeEvent(event: ChangeEvent<HTMLInputElement>) => voidThe `<input>` event on every keystroke (AMS-735). Careful: `e.target.value` is the TEXT being typed ("1.234,5"), not the number; the number comes from `onChange`, once what was typed is valid.
onMaxReached() => voidReports when the figure reaches the maximum.
onMinReached() => voidReports when the figure reaches the minimum.
onValueChange(details: NumberValueDetails) => voidLike `onChange`, but with the figure and its already written text (`{ value: 1234.5, formatted: '1.234,50 €' }`).
parseValue(text: string) => number | nullReads what has been typed, to measure. Returns `null` if there is no figure there.
placeholderstring—
readOnlyboolean—
requiredboolean—
selectOnFocusbooleanSelects the whole figure on entering the field, to type over it without deleting it first.
size"sm" | "md" | "lg"—
smallStepnumberThe jump of Alt with an arrow. Defaults to a tenth of `step`.
stepnumber—
thousandSeparatorstring | falseThe thousands separator, if it is not to be the language's: `' '`, `'.'`, `"'"`. `false` separates nothing. It is typed and read the same way, and must differ from `decimalSeparator`.
valuenumber—
variantFieldVariant—
wheelbooleanThe mouse wheel moves the figure up and down, but ONLY with the field focused: passing over it while scrolling the page changes nothing. Off by default, which is what a form expects.
withoutStepsbooleanRemoves the decrease and increase buttons, which sit on either side of the value (`− 12 +`) with the figure centred. Without them the figure goes to the right, which is how amounts line up in a column. The keyboard works the same.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: