Skip to content
EspañolEnglish
All components (283)

Amount field

Fields · MoneyField

What it is and when to use it

A money input that understands how it is written in each language: decimal comma in Spanish, point in English, thousands separator, and the currency symbol if someone pastes it. What comes out of onChange is a number, never a string.

When to use it

  • A price, an instalment, an invoice amount: any sum of money that someone types.
  • currency decides the decimals and the symbol (EUR by default; the yen has no decimals).
  • min, max and allowsNegative raise the error when leaving the field, in words.

When not to

  • Displaying an amount: Amount.
  • A quantity that is not money (units, kilos): NumberField.

Keyboard: that of a text field, with the numeric keypad on mobile (inputmode="decimal"). While typing nothing is rearranged: neither the cursor nor a separator in the middle of a figure. On leaving, it is written in its format (1,234.56 €) and on entering it goes back to the editable form (1234.56).

Accessibility: it is a TextField, with its label, help text and error linked. The error appears on leaving, not on every keystroke.

Fine styling: no variables of its own: it uses the field ones, --amset-field-*, with fixed-width figures.

What it looks like

Base €129.90 + VAT €27.28 = €157.18.

<MoneyField label="Sale price" value={precio} onChange={setPrecio} max={5000} />
<VatRateSelect value={iva} onChange={setIva} />

Props

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

PropTypeWhat it does
labelrequiredstringRequired: a field without a visible label does not get into the library.
allowsNegativebooleanWith `false` it does not accept negative values. `false` by default.
backReactNodeContent behind the text: a unit ("€"), a copy button.
backPointerEvents"auto" | "none"The same for what goes after (`back`).
backWidthstringWidth of what goes after (`back`).
classNamestring—
clearablebooleanA button to empty the field, shown as soon as something is typed. Pressing it puts the focus back in the input.
clearLabelstringWhat the screen reader says on the clear button. By default, “Clear text”.
countByCountStrategyHow characters are counted: `graphemes` (default, an emoji is one), `characters` or a custom function.
counterbooleanShows "12 / 40" under the field, with the limit of `maxCount` or `maxLength`.
currencystringThe currency: `EUR` by default. It decides the decimals and the symbol.
decimalsnumberHow many decimals what is typed accepts, if more than the currency has: a unit price of 0.0125 € needs four. It is still shown with at least those of the currency. Only with `unit="amount"`.
disabledboolean—
errorstringError message. When it is passed, the field is marked as invalid.
errorMessage(details: NativeValidation) => string | null | undefinedThe message when the typed text does not meet the field's NATIVE rules (`minLength`, `maxLength`, `pattern`, `type` `email` or `url`): it receives the text and its `validity` (`tooShort`, `patternMismatch`, `typeMismatch`...) and returns the message, in your language and your words, instead of the browser's bubble. It is checked like `validate`: on leaving and, once it has failed, on every key. `validate` and `error` take precedence over this.
frontReactNodeContent before the text: an icon, a prefix ("+34").
frontPointerEvents"auto" | "none"Whether what goes before receives the mouse: `auto` (default) for a button or a link; `none` for an icon or a prefix, and pressing it leads to the field like pressing any other gap of the box.
frontWidthstringWidth of what goes before (`front`), when it has to be fixed to align several fields: `'3rem'`.
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.
hintstringPermanent hint text below the field. It does not replace the label.
initialValuenumber | nullThe amount at the start, without controlling it from outside.
inputRefRef<HTMLInputElement>The real `<input>`, to focus it, select its text or measure it from outside. It is separate from `ref` because the component paints a box with label, hint and error, and whoever asks for a ref almost always wants the control.
invalidboolean—
loadingbooleanSomething about this field is being checked or loaded (whether the name is free, the address): a ring inside the box and `aria-busy` on the field. Typing can go on.
loadingPosition"start" | "end"Where the ring goes: `end` (default) or `start`, in the place of the leading icon.
maxnumberThe maximum accepted.
maxCountnumberA limit that WARNS without cutting: typing can go on, the count turns red, the field is marked invalid and how many are left over is announced. To cut off hard there is `maxLength`. With `maxCount` the counter shows even if not asked for.
minnumberThe minimum accepted. Below it, the field shows the error on leaving.
onChange(value: number | null) => voidDelivers the number (`1234.56`) or `null` if the field is empty or cannot be understood.
onChangeEvent(event: ChangeEvent<HTMLInputElement>) => voidLike `onChange`, but with the native event of the `<input>`: `e.target.value`, `e.currentTarget`... It exists to migrate a front end that already writes its handlers with the event (AMS-735). If you only want the text, `onChange` already gives it to you.
onClear() => voidCalled when the field is emptied with the button, after `onChange('')`.
placeholderstring—
readOnlyboolean—
requiredboolean—
size"sm" | "md" | "lg"—
successstringThe confirmation that what was typed is valid ("Username available"). An error or a warning take precedence over it.
unit"amount" | "cents"The unit in which `value`, `initialValue`, `onChange`, `min`, `max` and the `name` field go in and out: `amount` (12.34, the default) or `cents` (1234, the minor unit of the currency, which is how Stripe and most databases store it). What is typed and shown is still the amount; in `cents` the number is always a whole number.
validate(value: string) => string | null | undefinedChecks what was typed and returns the error message, or nothing if it is valid. It runs when leaving the field and, once it has failed, on every keystroke, so the error goes away as soon as it is fixed. An external `error` takes precedence.
validationBehavior"aria" | "native"`aria` (the default) announces the state without the browser interrupting submission with its bubble; `native` also adds its own validation.
valuenumber | nullThe amount, controlled from outside. `null` means the empty field.
variantFieldVariant—
warningstringA warning that lets you go on, with its icon and colour, where the error would go. The field is still valid.

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: