CurrencyInput

An amount in the currency it is counted in, in one card: the figure, the currency's pill and a list to choose from.

'use client';

import { CurrencyInput, type CurrencyOption } from '@/components/panelui/currency-input';

const CURRENCIES: CurrencyOption[] = [
  { code: 'USD', name: 'US dollar', symbol: '$', color: 'chart-2' },
  { code: 'EUR', name: 'Euro', symbol: '€', color: 'chart-1' },
  { code: 'GBP', name: 'Pound sterling', symbol: '£', color: 'chart-4' },
  { code: 'JPY', name: 'Japanese yen', symbol: '¥', decimals: 0, color: 'chart-5' },
];

export default function CurrencyInputOverview() {
  return <CurrencyInput currencies={CURRENCIES} label="Amount" defaultValue={{ code: 'USD', amount: 250 }} />;
}

Use it where a form asks for one amount and one currency: a budget, a price, an expense on a trip. The card carries its label, the amount in large figures, the currency's pill and a quieter line under them. Pressing the pill opens the currency list over the card.

It knows nothing about rates. The figure means what the chosen currency says it means, so changing the currency keeps the figure and rounds it to the new currency's minor unit. For two currencies at once — what one amount comes to in the other — use the Exchange.

Figures are written in locale, en-US by default. While a figure is being typed it is shown as typed; grouped once the field is left.

Installation

npx shadcn@latest add https://panelui.dev/r/web/currency-input.json

The source is copied into components/panelui/, where you can edit it.

Usage

import { CurrencyInput } from "@/components/panelui/currency-input"
<CurrencyInput currencies={currencies} label="Amount" defaultValue={{ code: 'USD', amount: 250 }} />

Examples

Held by you

Pass value and onValueChange to hold the currency and the amount yourself. The value carries both: here 500 pounds, and clearing the field reports amount: null.

'use client';

import { useState } from 'react';
import { CurrencyInput, type CurrencyOption, type CurrencyValue } from '@/components/panelui/currency-input';

const CURRENCIES: CurrencyOption[] = [
  { code: 'GBP', name: 'Pound sterling', symbol: '£', color: 'chart-4' },
  { code: 'EUR', name: 'Euro', symbol: '€', color: 'chart-1' },
  { code: 'AED', name: 'UAE dirham', symbol: 'د', color: 'chart-3' },
  { code: 'INR', name: 'Indian rupee', symbol: '₹', color: 'chart-5' },
];

export default function CurrencyInputHeld() {
  const [value, setValue] = useState<CurrencyValue>({ code: 'GBP', amount: 500 });

  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <CurrencyInput currencies={CURRENCIES} label="You paid" value={value} onValueChange={setValue} />
      <p className="text-xs text-muted-foreground">
        {value.amount === null ? 'No amount yet' : `${value.amount} ${value.code}`}
      </p>
    </div>
  );
}

A line under

detail is the quieter line under the amount, for one fact that changes how the figure reads — what is left to spend here. Leave it off and the card ends under the amount.

'use client';

import { CurrencyInput, type CurrencyOption } from '@/components/panelui/currency-input';

const CURRENCIES: CurrencyOption[] = [
  { code: 'USD', name: 'US dollar', symbol: '$', color: 'chart-2' },
  { code: 'EUR', name: 'Euro', symbol: '€', color: 'chart-1' },
  { code: 'CAD', name: 'Canadian dollar', symbol: '$', color: 'chart-5' },
];

export default function CurrencyInputBalance() {
  return (
    <CurrencyInput
      currencies={CURRENCIES}
      label="You pay"
      defaultValue={{ code: 'USD', amount: 128 }}
      detail="Balance $2,410.50"
    />
  );
}

In a card

In a Frame, the frame's header carries what the amount is for, and the button that acts on it sits under the card. The CurrencyInput fills the width it is given, up to max-w-sm.

'use client';

import { Button } from '@/components/panelui/button';
import { Frame } from '@/components/panelui/frame';
import { CurrencyInput, type CurrencyOption } from '@/components/panelui/currency-input';

const CURRENCIES: CurrencyOption[] = [
  { code: 'EUR', name: 'Euro', symbol: '€', color: 'chart-1' },
  { code: 'CHF', name: 'Swiss franc', symbol: 'F', color: 'chart-5' },
  { code: 'TRY', name: 'Turkish lira', symbol: '₺', color: 'chart-3' },
  { code: 'USD', name: 'US dollar', symbol: '$', color: 'chart-2' },
];

export default function CurrencyInputFramed() {
  return (
    <Frame variant="band" className="w-full max-w-sm">
      <Frame.Header>
        <Frame.Title>Travel money</Frame.Title>
        <Frame.Action>Budget</Frame.Action>
      </Frame.Header>
      <Frame.Panel className="flex flex-col gap-4 p-3">
        <CurrencyInput currencies={CURRENCIES} label="You spent" defaultValue={{ code: 'EUR', amount: 64 }} />
        <Button className="w-full">Save the expense</Button>
      </Frame.Panel>
    </Frame>
  );
}

A long list of currencies

The list scrolls once it has more rows than room. It opens on the chosen currency, the arrow keys, Home and End move through it, and Enter chooses. A currency with no minor unit, such as the won, takes decimals: 0.

'use client';

import { CurrencyInput, type CurrencyOption } from '@/components/panelui/currency-input';

const CURRENCIES: CurrencyOption[] = [
  { code: 'USD', name: 'US dollar', symbol: '$', color: 'chart-2' },
  { code: 'EUR', name: 'Euro', symbol: '€', color: 'chart-1' },
  { code: 'GBP', name: 'Pound sterling', symbol: '£', color: 'chart-4' },
  { code: 'JPY', name: 'Japanese yen', symbol: '¥', decimals: 0, color: 'chart-5' },
  { code: 'CHF', name: 'Swiss franc', symbol: 'F', color: 'chart-3' },
  { code: 'CAD', name: 'Canadian dollar', symbol: '$', color: 'chart-5' },
  { code: 'AUD', name: 'Australian dollar', symbol: '$', color: 'chart-4' },
  { code: 'SAR', name: 'Saudi riyal', symbol: 'ر', color: 'chart-3' },
  { code: 'AED', name: 'UAE dirham', symbol: 'د', color: 'chart-1' },
  { code: 'INR', name: 'Indian rupee', symbol: '₹', color: 'chart-2' },
  { code: 'KRW', name: 'South Korean won', symbol: '₩', decimals: 0, color: 'chart-4' },
  { code: 'BRL', name: 'Brazilian real', symbol: 'R', color: 'chart-5' },
];

export default function CurrencyInputMany() {
  return (
    <CurrencyInput currencies={CURRENCIES} label="You pay" defaultValue={{ code: 'KRW', amount: 85000 }} />
  );
}

API Reference

CurrencyInput

PropTypeDefaultDescription
currenciesreadonly CurrencyOption[]—Every currency that can be chosen, in the order the list shows them.
labelReactNode—What the card is, above its amount: "Budget", "You pay".
valueCurrencyValue—The currency and the amount, when you hold them.
defaultValueCurrencyValue—The currency and the amount to start with, when the control holds them.
onValueChange(value: Required<CurrencyValue>) => void—Called with every change: a figure typed or cleared, a currency chosen.
placeholderstring'0'Shown in an empty field. Default 0.
detailReactNode—The quieter line under the amount: a balance, what the amount covers.
localestring'en-US'The locale figures are written in. Default en-US.
disabledbooleanfalseShows the figures but takes no input.

Every part also takes the props of the element it renders, and a className.

Notes

Typing an amount

The field takes digits and one decimal mark, and no more decimals than the currency has. Grouping commas are added once the field is left. While typing, a single comma with no point is read as the decimal mark, so 1,50 from a keypad that offers a comma is one and a half; a pasted 1,250.50 keeps its grouping. Clearing the field reports amount: null.

A value that changes while the field has focus replaces what is shown, so an owner that rounds or refuses an edit is seen at once.

Choosing a currency

The list takes over the card while it is open, and the card is out of the tab order behind it. Its header is the card's own label, so an open list is never unclear about which field it is choosing for. Choosing a currency keeps the amount and rounds it to that currency's minor unit — 10.555 becomes 11 in yen. Escape, the close button or a press outside the control shuts the list and puts focus back on the pill. Setting disabled while the list is open closes it.

A short viewport

The list opens towards whichever side of the container it sits in has room: over the card where it is deep enough, upwards from it where a phone's keyboard has taken the space below. The container is the nearest ancestor that clips — a card, a dialog, a scroll — or the viewport where nothing does, so the rows stop at the edge that would actually cut them off rather than running past it. The list measures again while it is open, so turning the phone or closing the keyboard refits it.

Figures

Figures are written in locale, en-US by default, so the digits are Western whatever the device's language. Pass another locale to change how a finished figure is grouped.

Reduced motion

With reduced motion set, the list opens without scaling.

On this page