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.21is shown as21 %),integerordecimal.formatOptionsto fine-tune it (another currency, fixed decimals).thousandSeparator,decimalSeparatorandgroupStylewhen 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.withoutStepsin 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,
TextFieldor 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.
| Prop | Type | What it does |
|---|---|---|
labelrequired | string | — |
back | ReactNode | Content after the figure, inside the box: a unit (“kg”, “pcs”). |
clampBehavior | ClampBehavior | What 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. |
className | string | — |
decimalScale | number | How many decimals at most. Shortcut for `formatOptions.maximumFractionDigits`. |
decimalSeparator | string | The decimal separator, if it is not to be the language's. |
decrementAriaLabel | string | — |
defaultValue | number | — |
disabled | boolean | — |
error | string | — |
fixedDecimalScale | boolean | With `decimalScale`, always shows that many decimals (“12.50”, not “12.5”). |
format | NumberFormat | Format of the number. `currency` uses euros; for another currency, pass `formatOptions` directly. |
formatOptions | Intl.NumberFormatOptions | The `Intl.NumberFormat` options, if the format above is not enough. |
formatValue | (value: number) => string | Writes 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. |
front | ReactNode | Content before the figure, inside the box: a prefix (“≈”), an icon. |
groupStyle | GroupStyle | How 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). |
hiddenLabel | boolean | The 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. |
hint | string | — |
id | string | — |
incrementAriaLabel | string | — |
inputRef | Ref<HTMLInputElement> | The real `<input>`, to focus it or select its text from outside. |
invalid | boolean | — |
largeStep | number | The jump of Shift with an arrow and of Page Up and Page Down. Defaults to ten times `step`. |
maxValue | number | — |
minValue | number | — |
name | string | The 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) => void | Notifies the new figure. If the field is emptied, it notifies `NaN`: the usual "no value". |
onChangeEvent | (event: ChangeEvent<HTMLInputElement>) => void | The `<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 | () => void | Reports when the figure reaches the maximum. |
onMinReached | () => void | Reports when the figure reaches the minimum. |
onValueChange | (details: NumberValueDetails) => void | Like `onChange`, but with the figure and its already written text (`{ value: 1234.5, formatted: '1.234,50 €' }`). |
parseValue | (text: string) => number | null | Reads what has been typed, to measure. Returns `null` if there is no figure there. |
placeholder | string | — |
readOnly | boolean | — |
required | boolean | — |
selectOnFocus | boolean | Selects the whole figure on entering the field, to type over it without deleting it first. |
size | "sm" | "md" | "lg" | — |
smallStep | number | The jump of Alt with an arrow. Defaults to a tenth of `step`. |
step | number | — |
thousandSeparator | string | false | The 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`. |
value | number | — |
variant | FieldVariant | — |
wheel | boolean | The 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. |
withoutSteps | boolean | Removes 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:
