Component · client
Money Input
A text input for amounts. It groups digits while you type, keeps the caret where you left it, refuses more decimals than the currency allows, and hands you a decimal string instead of a float.
Try it
US Dollar
Try 2.5k, paste ($1,234.50) or type a third decimal.
- value
- "1234.5"
- minor units
- 123450n
- formatted
- $1,234.50
Install
npx shadcn@latest add https://tellerline-ui.vercel.app/r/money-input.jsonThis adds the component and lib/money.ts, the parsing and formatting helpers it uses. It has no dependencies beyond React and the shadcn cn helper.
Usage
"use client"
import * as React from "react"
import { Label } from "@/components/ui/label"
import { MoneyInput } from "@/components/ui/money-input"
export function TransferAmount() {
const [amount, setAmount] = React.useState<string | null>(null)
return (
<div className="grid gap-2">
<Label htmlFor="amount">Amount</Label>
<MoneyInput
id="amount"
name="amount"
currency="EUR"
locale="de-DE"
value={amount}
onValueChange={setAmount}
/>
</div>
)
}The value is null when the field is empty and a string like "-1234.5" otherwise: a dot for the decimal, no grouping, no symbol. Send it to your API as is, or turn it into minor units:
import { toMinorUnits, fromMinorUnits } from "@/lib/money"
toMinorUnits("17890.4", 2) // 1789040n
toMinorUnits("1500", 0) // 1500n (JPY)
fromMinorUnits(1789040n, 2) // "17890.40"Behaviour
- Caret
- Separators are added and removed as you type, and the caret stays after the digit you typed. Backspace next to a separator deletes the digit beside it.
- Decimals
- The number of decimal places comes from the currency: none for JPY, three for KWD and BHD. A keystroke past that is refused and reported through onValueChange.
- Paste
- Pasted text is parsed as a whole, so “$1,234.50”, “1.234,50 €” and “(250.00)” for a negative all work.
- Shortcuts
- With allowShortcuts, 2.5k becomes 2500 and 1.2m becomes 1200000. The letter has to follow a digit, so currency codes aren't mistaken for one.
- Locales
- Grouping, the decimal mark and digits follow the locale. Arabic-Indic digits are accepted and shown when the locale uses them.
- Screen readers
- The visible symbol is hidden from assistive technology and the full currency name is added to the field's description instead.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| currency | string | — | ISO 4217 code. Required. |
| locale | string | "en-US" | BCP 47 locale for separators, digits and symbol placement. |
| value | string | null | — | Controlled value as a decimal string. |
| defaultValue | string | null | — | Starting value when uncontrolled. |
| onValueChange | (value, change) => void | — | Called with the new value. change.rejected says why a keystroke was refused. |
| fractionDigits | number | from currency | Overrides the currency's decimal places, for unit prices or FX rates. |
| allowNegative | boolean | false | Accepts a minus sign or parentheses. |
| allowShortcuts | boolean | false | Accepts k, m and b after a number. |
| currencyDisplay | "symbol" | "narrowSymbol" | "code" | "none" | "symbol" | What to show beside the amount. |
| containerClassName | string | — | Classes for the wrapper that draws the border. className goes on the input. |
Any other prop is passed to the underlying input.