Currency field
An amount of money: plain number while editing, formatted for the page language on blur.
Example
<div class="stack"> <focsd-currency-field label="Price" currency="EUR" value="1234.5" max="10000"></focsd-currency-field> <focsd-currency-field label="Budget" currency="JPY" currency-display="code"></focsd-currency-field></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-currency-field.js';</script>
<focsd-currency-field label="Price" currency="EUR" value="1234.5" max="10000"></focsd-currency-field>import { CurrencyField } from '@focsd-dev/react';
export function Example() { return <CurrencyField onChange={(e) => console.log(e.target)} label="Price" currency="EUR" value="1234.5" max="10000"></CurrencyField>;}<script setup>import '@focsd-dev/components/focsd-currency-field.js';</script>
<template> <focsd-currency-field @change="onChange" label="Price" currency="EUR" value="1234.5" max="10000"></focsd-currency-field></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-currency-field.js';
// template<focsd-currency-field (change)="onChange($event)" label="Price" currency="EUR" value="1234.5" max="10000"></focsd-currency-field><script> import '@focsd-dev/components/focsd-currency-field.js';</script>
<focsd-currency-field onchange={(e) => console.log(e.target)} label="Price" currency="EUR" value="1234.5" max="10000"></focsd-currency-field>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
currency | currency | string | USD |
currency-display | currencyDisplay | 'symbol' | 'narrowSymbol' | 'code' | 'name' | symbol |
min | min | string | number | undefined | — |
max | max | string | number | undefined | — |
step | step | string | number | undefined | — |
allow-negative | allowNegative | boolean | false |
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | — |
type | type | 'text' | 'email' | 'password' | 'search' | 'tel' | 'url' | text |
placeholder | placeholder | string | — |
autocomplete | autocomplete | AutoFill | — |
minlength | minlength | number | — |
maxlength | maxlength | number | — |
pattern | pattern | string | undefined | — |
size | size | number | undefined | — |
dirname | dirname | string | undefined | — |
inputmode | inputMode | — | — |
enterkeyhint | enterKeyHint | — | — |
autocapitalize | autocapitalize | — | — |
autocorrect | autocorrect | — | — |
spellcheck | spellcheck | — | — |
Methods
| Method | Description |
|---|---|
setRangeText(replacement, start, end, selectMode) | Replace text like the native method, on the plain number the person edits (1234.50, as
shown while focused) rather than the formatted amount shown on blur. Updates value without
firing input. |
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
select() | Select all text in the input, like native. |
setSelectionRange(start, end, direction) | Set the text selection in the input, like native. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change. |
CSS parts
Style the inside of the component with ::part(), for example focsd-currency-field::part(label).
| Part | Description |
|---|---|
label | The label. |
field | The box around the input and the currency. |
prefix | The currency before the number (while editing). |
suffix | The currency after the number (while editing). |
control | The input. |
layout | The box around the label and the body; `data-position` is the label position. |
body | The field and its messages. |
hint | The hint. |
message | The shown message; also `error`, `warning` or `success` for its kind. |
message-icon | The icon in the message. |
announcer | The visually hidden status region that announces a new message. |
Accessibility
- An APG
role="spinbutton"whosearia-valuetextis the formatted amount ("€1,234.50"). - The currency name and code are part of the accessible description.
- Typing accepts the page language's decimal and group symbols and pasted currency signs.
- The value and form entry are a dot-decimal string with the currency's decimals ("1234.50", "100" for yen).
- Out-of-range amounts report a formatted message ("Enter €10,000.00 or less").
Keyboard
| Key | Action |
|---|---|
| Arrow Up / Down | Step by step (default: the smallest unit, such as one cent) |
| Page Up / Down | Step by 10 × step |
| Home / End | Jump to min / max (when set) |
| Enter | Submits the owning form |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.