Money Input

A currency field that reads and writes integer minor units. Money that has been through a float has already lost — the value that leaves this control is a whole number of cents.

src/components/ui/money-input.tsx

About

From the component's own source, where it was written.

"US dollars" rather than "USD", falling back to the code where unsupported.

Install

npx astralyx-ui add money-input

Usage

import { MoneyInput } from '@/components/ui/money-input'

<MoneyInput value={cents} onValueChange={setCents} currency="USD" />

Composer

Change the props and the source below follows.

Props
tsx
<MoneyInput
  value={cents}
  onValueChange={setCents}
  currency="USD"
  size="md"
/>

Examples

Round-tripping minor units

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

MoneyInput

Extends Omit<ComponentProps<'input'>, 'value' | 'defaultValue' | 'onChange' | 'size' | 'min' | 'max'>.

PropTypeDefaultDescription
valuenumber—Minor units. 250 is $2.50, never 2.5.
defaultValuenumber—Minor units. 250 is $2.50, never 2.5.
onValueChange(minorUnits: number) => void—Fires with the integer value, or undefined when the field is cleared — which is not the same as zero.
currencystring'USD'ISO 4217. Drives the symbol, its side, and the number of decimal places — JPY has none.
localestring'en-GB'Grouping and decimal separators. A German user typing 1.234,56 means 1234.56.
sizekeyof typeof fieldSize'md'—
variant'default' | 'secondary' | 'ghost''default'—
errorbooleanfalse—
minnumber—Minor units, clamped on blur rather than while typing, so you can still delete a digit.
maxnumber—Minor units, clamped on blur rather than while typing, so you can still delete a digit.