{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "currency-input",
  "title": "Currency Input",
  "description": "Money input that reads and writes minor units, with the currency symbol and decimal places taken from Intl and an optional searchable currency picker.",
  "dependencies": [
    "lucide-react",
    "react-number-format@^5.4.5"
  ],
  "registryDependencies": [
    "input-group",
    "https://dentil.dev/r/select-search.json?via=currency-input"
  ],
  "files": [
    {
      "path": "components/dentil/currency-input/currency-input.tsx",
      "content": "'use client';\n\nimport { ChevronDownIcon } from 'lucide-react';\nimport * as React from 'react';\nimport { NumericFormat } from 'react-number-format';\n\nimport { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from '@/components/ui/input-group';\nimport { cn } from '@/lib/utils';\n\nimport { SelectSearch } from '../select-search/select-search';\n\nexport interface CurrencyInputProps extends Omit<\n  React.ComponentProps<'input'>,\n  'value' | 'onChange' | 'type' | 'defaultValue' | 'ref' | 'size' | 'prefix' | 'max'\n> {\n  /** ISO 4217 code, e.g. `'GBP'`. */\n  currency: string;\n  /** Amount in minor units (pence, cents). `1050` is £10.50. */\n  value: number | null | undefined;\n  /** Called with the amount in minor units, or `undefined` when the field is empty. */\n  onChange: (value: number | undefined) => void;\n  /** Turns the currency code into a picker. Called with the new ISO 4217 code. */\n  onCurrencyChange?: (currency: string) => void;\n  /** Codes the picker offers. Defaults to every currency the browser supports. */\n  currencies?: string[];\n  /** Use a searchable popover for the picker instead of the native select. */\n  searchable?: boolean;\n  /** Largest allowed amount, in minor units. Larger input is clamped to it. */\n  maxAmount?: number;\n  allowNegative?: boolean;\n  /** Locale for the currency symbol. */\n  locale?: string;\n  inputClassName?: string;\n}\n\n/** The narrow symbol (`£`, `$`) and minor-unit digits for a currency. Unknown codes fall back to the code and 2 digits. */\nexport function getCurrencyFormat(currency: string, locale?: string) {\n  try {\n    const format = new Intl.NumberFormat(locale, { style: 'currency', currency, currencyDisplay: 'narrowSymbol' });\n    const symbol = format.formatToParts(0).find((part) => part.type === 'currency')?.value ?? currency;\n    return { symbol, digits: format.resolvedOptions().maximumFractionDigits ?? 2 };\n  } catch {\n    return { symbol: currency.toUpperCase(), digits: 2 };\n  }\n}\n\nexport function CurrencyInput({\n  currency,\n  value,\n  onChange,\n  onCurrencyChange,\n  currencies,\n  searchable = false,\n  maxAmount,\n  allowNegative = false,\n  locale,\n  disabled = false,\n  className,\n  inputClassName,\n  placeholder,\n  onFocus,\n  ...props\n}: CurrencyInputProps) {\n  const { symbol, digits } = getCurrencyFormat(currency, locale);\n  const code = currency.toUpperCase();\n  const picking = onCurrencyChange != null;\n  const options = React.useMemo(() => {\n    if (!picking) return [];\n    const codes = (currencies ?? Intl.supportedValuesOf('currency')).map((c) => c.toUpperCase());\n    return codes.includes(code) ? codes : [code, ...codes];\n  }, [picking, currencies, code]);\n  const factor = 10 ** digits;\n  const toMinor = (amount: number | undefined) => (amount === undefined ? undefined : Math.round(amount * factor));\n\n  const changeCurrency = (next: string) => {\n    onCurrencyChange?.(next);\n    const nextDigits = getCurrencyFormat(next, locale).digits;\n    // Keep the displayed amount when the number of minor-unit digits changes (GBP 10.50 → JPY 11).\n    if (typeof value === 'number' && nextDigits !== digits) onChange(Math.round(value * 10 ** (nextDigits - digits)));\n  };\n\n  return (\n    <InputGroup data-slot=\"currency-input\" data-disabled={disabled} className={className}>\n      <InputGroupAddon>\n        <InputGroupText>{symbol}</InputGroupText>\n      </InputGroupAddon>\n      <NumericFormat\n        customInput={InputGroupInput}\n        className={cn('tabular-nums', inputClassName)}\n        placeholder={placeholder ?? (0).toFixed(digits)}\n        inputMode=\"decimal\"\n        autoComplete=\"off\"\n        thousandSeparator\n        decimalScale={digits}\n        fixedDecimalScale\n        allowLeadingZeros={false}\n        allowNegative={allowNegative}\n        disabled={disabled}\n        value={typeof value === 'number' ? value / factor : ''}\n        onValueChange={(values, { source }) => {\n          if (source !== 'event') return;\n          const next = toMinor(values.floatValue);\n          onChange(next !== undefined && maxAmount !== undefined ? Math.min(next, maxAmount) : next);\n        }}\n        isAllowed={(values) => {\n          const next = toMinor(values.floatValue);\n          if (maxAmount === undefined || next === undefined || next <= maxAmount) return true;\n          onChange(maxAmount);\n          return false;\n        }}\n        onFocus={(e) => {\n          e.target.select();\n          onFocus?.(e);\n        }}\n        {...props}\n      />\n      {onCurrencyChange && searchable ? (\n        <CurrencySearch\n          value={code}\n          options={options}\n          locale={locale}\n          disabled={disabled}\n          onChange={changeCurrency}\n        />\n      ) : onCurrencyChange ? (\n        <CurrencySelect\n          value={code}\n          options={options}\n          locale={locale}\n          disabled={disabled}\n          onChange={changeCurrency}\n        />\n      ) : (\n        <InputGroupAddon align=\"inline-end\">\n          <InputGroupText>{code}</InputGroupText>\n        </InputGroupAddon>\n      )}\n    </InputGroup>\n  );\n}\n\ninterface CurrencySelectProps {\n  value: string;\n  options: string[];\n  locale: string | undefined;\n  disabled: boolean;\n  onChange: (currency: string) => void;\n}\n\nfunction currencyNamer(locale: string | undefined) {\n  const names = new Intl.DisplayNames(locale, { type: 'currency', fallback: 'code' });\n  return (code: string) => {\n    try {\n      return names.of(code) ?? code;\n    } catch {\n      return code;\n    }\n  };\n}\n\n/** Every code stacked in one grid cell with only `value` visible, so the picker keeps the widest code's width. */\nfunction CodeStack({ value, options }: { value: string; options: string[] }) {\n  return (\n    <span className=\"grid justify-items-start\">\n      {options.map((c) => (\n        <span key={c} className={cn('[grid-area:1/1]', c !== value && 'invisible')}>\n          {c}\n        </span>\n      ))}\n    </span>\n  );\n}\n\nfunction CurrencySearch({ value, options, locale, disabled, onChange }: CurrencySelectProps) {\n  const nameOf = currencyNamer(locale);\n  const selectOptions = React.useMemo(() => {\n    const name = currencyNamer(locale);\n    return options.map((c) => ({\n      value: c,\n      label: c,\n      description: name(c),\n      labelClassName: 'w-[3ch] font-mono',\n    }));\n  }, [options, locale]);\n\n  return (\n    <SelectSearch\n      data-slot=\"currency-input-select\"\n      data-align=\"inline-end\"\n      aria-label={`Currency: ${nameOf(value)}`}\n      options={selectOptions}\n      value={value}\n      onChange={(next) => {\n        if (next) onChange(next);\n      }}\n      disabled={disabled}\n      searchPlaceholder=\"Search currencies...\"\n      emptyText=\"No currency found.\"\n      align=\"end\"\n      contentClassName=\"min-w-72\"\n      renderValue={() => <CodeStack value={value} options={options} />}\n      className=\"text-muted-foreground hover:text-muted-foreground focus-visible:bg-accent order-last h-full w-auto gap-1 rounded-l-none rounded-r-md border-0 border-l bg-transparent font-medium shadow-none group-data-[disabled=true]/input-group:opacity-50 focus-visible:border-border focus-visible:ring-0 has-[>svg]:ps-2.5 has-[>svg]:pe-2 dark:border-border dark:bg-transparent dark:hover:bg-accent\"\n    />\n  );\n}\n\nfunction CurrencySelect({ value, options, locale, disabled, onChange }: CurrencySelectProps) {\n  const nameOf = currencyNamer(locale);\n\n  // Not an InputGroupAddon: its click handler focuses the input, which would close the native picker.\n  return (\n    <div\n      data-slot=\"currency-input-select\"\n      data-align=\"inline-end\"\n      className=\"text-muted-foreground hover:bg-accent has-focus-visible:bg-accent relative order-last flex h-full shrink-0 items-center gap-1 rounded-r-md border-l ps-2.5 pe-2 text-sm font-medium transition-colors select-none has-disabled:pointer-events-none group-data-[disabled=true]/input-group:opacity-50\">\n      <CodeStack value={value} options={options} />\n      <ChevronDownIcon className=\"size-4 opacity-50\" aria-hidden=\"true\" />\n      <select\n        disabled={disabled}\n        value={value}\n        onChange={(e) => onChange(e.target.value)}\n        className=\"absolute inset-0 text-sm opacity-0\"\n        aria-label=\"Currency\">\n        {options.map((c) => (\n          <option key={c} value={c}>\n            {c} – {nameOf(c)}\n          </option>\n        ))}\n      </select>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/dentil/currency-input/currency-input.tsx"
    }
  ],
  "type": "registry:component"
}
