{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "currency-input",
  "type": "registry:ui",
  "title": "Currency Input",
  "description": "An amount of money typed the way the locale writes it (its decimal mark, its grouping, its symbol before or after), shown formatted when the field is left, kept as a plain number, with Up and Down to step it and a range it is held to.",
  "categories": [
    "ui"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/input",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/currency-input.tsx",
      "type": "registry:ui",
      "target": "components/ui/currency-input.tsx",
      "content": "\"use client\";\n/**\n * @name currency-input\n * @category ui\n * @description An amount of money typed the way the locale writes it (its decimal mark, its grouping, its symbol before or after), shown formatted when the field is left, kept as a plain number, with Up and Down to step it and a range it is held to.\n * @when Prices, budgets, transfers, any amount in a currency. Also numbers with a unit through `unit` instead of `currency` (kilograms, percent). For a small whole number with buttons use number-stepper; for a range use slider.\n * @example\n * <CurrencyInput aria-label=\"Budget\" currency=\"EUR\" locale=\"de-DE\" defaultValue={1250.5} min={0} name=\"budget\" />\n */\nimport { useId, useState, type ComponentProps, type KeyboardEvent } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\nimport { inputVariants } from \"@/components/ui/input\";\n\nexport type CurrencyInputProps = Omit<ComponentProps<\"input\">, \"value\" | \"defaultValue\" | \"onChange\" | \"type\" | \"size\" | \"min\" | \"max\" | \"step\"> & {\n  /** The amount, or null when the field is empty. */\n  value?: number | null;\n  defaultValue?: number | null;\n  onValueChange?: (value: number | null) => void;\n  /** An ISO 4217 code: \"EUR\", \"USD\", \"NOK\". */\n  currency?: string;\n  /** A unit instead of a currency: \"kilogram\", \"percent\". */\n  unit?: string;\n  /** BCP 47. Default \"en-US\". */\n  locale?: string;\n  min?: number;\n  max?: number;\n  /** What Up and Down add or take away. Default 1. Shift with them: ten times as much. */\n  step?: number;\n  invalid?: boolean;\n};\n\nexport function CurrencyInput({\n  value,\n  defaultValue = null,\n  onValueChange,\n  currency = \"USD\",\n  unit,\n  locale = \"en-US\",\n  min,\n  max,\n  step = 1,\n  invalid,\n  name,\n  className,\n  onBlur,\n  onFocus,\n  ...props\n}: CurrencyInputProps) {\n  const [amount, setAmount] = useControllableState<number | null>({ value, defaultValue, onChange: onValueChange });\n  const [draft, setDraft] = useState<string | null>(null);\n  const affixId = useId();\n  const options: Intl.NumberFormatOptions = unit ? { style: \"unit\", unit } : { style: \"currency\", currency };\n  const display = new Intl.NumberFormat(locale, options);\n  const digits = display.resolvedOptions().maximumFractionDigits ?? 2;\n  const plain = new Intl.NumberFormat(locale, { minimumFractionDigits: digits, maximumFractionDigits: digits, useGrouping: false });\n  const parts = new Intl.NumberFormat(locale).formatToParts(12345.6);\n  const decimal = parts.find((p) => p.type === \"decimal\")?.value ?? \".\";\n  const group = parts.find((p) => p.type === \"group\")?.value ?? \",\";\n  // Where the symbol or unit goes, and what it is, from the locale's own format of a sample.\n  const sample = display.formatToParts(1);\n  const affix = sample\n    .filter((p) => p.type === \"currency\" || p.type === \"unit\" || (p.type === \"percentSign\" && unit === \"percent\"))\n    .map((p) => p.value)\n    .join(\"\");\n  const before = sample.findIndex((p) => p.type === \"currency\" || p.type === \"unit\") < sample.findIndex((p) => p.type === \"integer\");\n  const clamp = (n: number): number => Math.min(max ?? Infinity, Math.max(min ?? -Infinity, n));\n  const round = (n: number): number => Math.round(n * 10 ** digits) / 10 ** digits;\n  const parse = (text: string): number | null => {\n    const cleaned = text\n      .replaceAll(group === \" \" || group === \" \" ? /[\\s  ]/g : group, \"\")\n      .replace(/\\s/g, \"\")\n      .replace(decimal, \".\")\n      .replace(/[^\\d.-]/g, \"\");\n    if (cleaned === \"\" || cleaned === \"-\" || cleaned === \".\") return null;\n    const n = Number(cleaned);\n    return Number.isFinite(n) ? n : null;\n  };\n  const stepBy = (event: KeyboardEvent<HTMLInputElement>, sign: 1 | -1): void => {\n    event.preventDefault();\n    const base = draft === null ? (amount ?? 0) : (parse(draft) ?? 0);\n    const next = clamp(round(base + sign * step * (event.shiftKey ? 10 : 1)));\n    setAmount(next);\n    setDraft(plain.format(next));\n  };\n  const shown = draft ?? (amount === null ? \"\" : new Intl.NumberFormat(locale, { minimumFractionDigits: digits, maximumFractionDigits: digits }).format(amount));\n\n  return (\n    <div data-slot=\"currency-input\" className={cn(\"relative w-full min-w-0\", className)}>\n      <span id={affixId} className=\"sr-only\">\n        {unit ? unit : (new Intl.DisplayNames(locale, { type: \"currency\" }).of(currency) ?? currency)}\n      </span>\n      <span aria-hidden=\"true\" className={cn(\"pointer-events-none absolute inset-y-0 flex items-center text-sm text-muted-foreground\", before ? \"start-3\" : \"end-3\")}>\n        {affix}\n      </span>\n      <input\n        type=\"text\"\n        inputMode=\"decimal\"\n        autoComplete=\"off\"\n        value={shown}\n        aria-invalid={invalid || undefined}\n        aria-describedby={affixId}\n        onFocus={(event) => {\n          setDraft(amount === null ? \"\" : plain.format(amount));\n          onFocus?.(event);\n        }}\n        onChange={(event) => {\n          setDraft(event.target.value);\n          setAmount(parse(event.target.value));\n        }}\n        onBlur={(event) => {\n          const parsed = draft === null ? amount : parse(draft);\n          setAmount(parsed === null ? null : clamp(round(parsed)));\n          setDraft(null);\n          onBlur?.(event);\n        }}\n        onKeyDown={(event) => {\n          if (event.key === \"ArrowUp\") stepBy(event, 1);\n          else if (event.key === \"ArrowDown\") stepBy(event, -1);\n        }}\n        className={cn(inputVariants(), \"text-end tabular-nums\", before ? \"ps-[calc(var(--spacing)*3+var(--affix,2ch))]\" : \"pe-[calc(var(--spacing)*3+var(--affix,2ch))]\")}\n        style={{ [\"--affix\" as string]: `${Math.max(1, affix.length) + 1}ch` }}\n        {...props}\n      />\n      {name ? <input type=\"hidden\" name={name} value={amount === null ? \"\" : String(amount)} /> : null}\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Prices, budgets, transfers, any amount in a currency. Also numbers with a unit through `unit` instead of `currency` (kilograms, percent). For a small whole number with buttons use number-stepper; for a range use slider.",
    "example": "<CurrencyInput aria-label=\"Budget\" currency=\"EUR\" locale=\"de-DE\" defaultValue={1250.5} min={0} name=\"budget\" />",
    "client": true
  }
}
