Currency Input
CurrencyInput reads and writes integer minor units, as Stripe and most payment APIs do, so there are no floating-point amounts in your state. Type more than £120 into the first field to see it clamp. The second field passes onCurrencyChange, so its currency code is a picker. JPY has no minor unit, so it has no decimals.
@dentil/currency-inputOn this page
Result:
2500 (max 12000)GBPUSDEURJPY
— JPYcomponents/demos/currency-input-overview.tsx
'use client';
import { useState } from 'react';
import { CurrencyInput } from '@/components/dentil/currency-input/currency-input';
export function CurrencyInputOverviewDemo() {
const [refund, setRefund] = useState<number | undefined>(2500);
const [fee, setFee] = useState<number>();
const [feeCurrency, setFeeCurrency] = useState('JPY');
return (
<div className="grid w-full max-w-xl gap-4 sm:grid-cols-2">
<div className="flex flex-col gap-2">
<CurrencyInput currency="GBP" value={refund} onChange={setRefund} maxAmount={12000} aria-label="Refund" />
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{refund ?? '—'}</code> (max 12000)
</span>
</div>
<div className="flex flex-col gap-2">
<CurrencyInput
currency={feeCurrency}
onCurrencyChange={setFeeCurrency}
currencies={['GBP', 'USD', 'EUR', 'JPY']}
value={fee}
onChange={setFee}
aria-label="Fee"
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{fee ?? '—'}</code> {feeCurrency}
</span>
</div>
</div>
);
}Installation
pnpm dlx shadcn@latest add @dentil/currency-inputnpx shadcn@latest add @dentil/currency-inputyarn dlx shadcn@latest add @dentil/currency-inputbunx --bun shadcn@latest add @dentil/currency-inputFiles land in components/dentil/currency-input/. Uses the stock shadcn input-group, react-number-format and @dentil/select-search for the searchable picker. The symbol and decimal places come from Intl.NumberFormat.
Examples
Searchable currency picker
searchable opens the picker as a popover over every currency the browser supports. Type a code or a name, such as yen.
Result:
4999 USDcomponents/demos/currency-input-searchable.tsx
'use client';
import { useState } from 'react';
import { CurrencyInput } from '@/components/dentil/currency-input/currency-input';
export function CurrencyInputSearchableDemo() {
const [amount, setAmount] = useState<number | undefined>(4999);
const [currency, setCurrency] = useState('USD');
return (
<div className="flex w-full max-w-xs flex-col gap-2">
<CurrencyInput
currency={currency}
onCurrencyChange={setCurrency}
searchable
value={amount}
onChange={setAmount}
aria-label="Price"
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{amount ?? '—'}</code> {currency}
</span>
</div>
);
}API reference
CurrencyInput
| Prop | Type | Default | Description |
|---|---|---|---|
| currency | string | — | ISO 4217 code, e.g. GBP. Sets the symbol and decimal places. |
| value | number | null | undefined | — | Amount in minor units: 1050 is £10.50, and 1050 is ¥1,050 for JPY. |
| onChange | (value: number | undefined) => void | — | Called with the amount in minor units, or undefined when the field is empty. |
| onCurrencyChange | (currency: string) => void | — | Turns the currency code into a picker. If the new currency has a different number of decimal places, the amount on screen is kept and onChange gets it in the new minor units. |
| currencies | string[] | — | ISO 4217 codes the picker offers. Defaults to every currency the browser supports. |
| searchable | boolean | false | Open the picker as a SelectSearch popover you can filter by code or name, instead of a native select. |
| maxAmount | number | — | Largest amount in minor units. Larger input is clamped to it. |
| allowNegative | boolean | false | Allow a leading minus. |
| locale | string | — | Locale for the currency symbol. Defaults to the browser’s. |
| inputClassName | string | — | Applied to the input. className goes on the group. |
| …props | ComponentProps<'input'> | — | Other props (id, aria-invalid, aria-describedby, onBlur…) are passed to the input. |