{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "amount-input",
  "title": "Amount Input",
  "description": "Large borderless decimal input for token amounts that keeps a string value (no float precision loss). type=text with inputMode=decimal; refuses invalid keystrokes and digits past the token's decimals; accepts ',' as the decimal point; cleans up pasted amounts like '$1,000.50'. Optional max with a Max button and an over-max state (aria-invalid). onValueChange returns the string plus the parsed bigint. Exports sanitizeAmountInput and getAmountState.",
  "dependencies": [
    "viem"
  ],
  "registryDependencies": [
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/amount-input.tsx",
      "content": "// Docs: https://0xui.io/components/amount-input.md\nimport {\n  useId,\n  type ClipboardEvent,\n  type ComponentProps,\n  type ReactNode,\n} from \"react\";\nimport { formatUnits } from \"viem\";\nimport { parseAmount } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\n/**\n * Clean up what the user typed into a decimal string the input can hold, or\n * return `undefined` to reject the keystroke. Accepts \",\" as the decimal\n * point, turns a leading \".\" into \"0.\", and refuses digits beyond `decimals`.\n */\nexport function sanitizeAmountInput(raw: string, decimals: number) {\n  const text = raw.replace(/,/g, \".\").replace(/\\s/g, \"\");\n  if (text === \"\") return \"\";\n  if (!/^\\d*\\.?\\d*$/.test(text)) return undefined;\n  const [, fraction = \"\"] = text.split(\".\");\n  if (fraction.length > decimals) return undefined;\n  return text.startsWith(\".\") ? `0${text}` : text;\n}\n\nexport type AmountState = {\n  /** Raw amount, or undefined when the input is empty or just \".\". */\n  amount: bigint | undefined;\n  /** True when `max` is set and the amount is above it. */\n  exceedsMax: boolean;\n};\n\n/** Parse an amount input value against decimals and an optional maximum. */\nexport function getAmountState(\n  value: string,\n  decimals: number,\n  max?: bigint\n): AmountState {\n  const amount = value ? parseAmount(value, decimals) : undefined;\n  return {\n    amount,\n    exceedsMax: amount !== undefined && max !== undefined && amount > max,\n  };\n}\n\nexport type AmountInputProps = Omit<\n  ComponentProps<\"input\">,\n  \"value\" | \"onChange\" | \"type\" | \"max\"\n> & {\n  /** Decimal string, e.g. \"1.5\". Keep it in state; never store amounts as numbers. */\n  value: string;\n  /** Called with the new decimal string and its parsed state. */\n  onValueChange: (value: string, state: AmountState) => void;\n  /** Token decimals. Typing past them is refused. */\n  decimals: number;\n  /** Maximum allowed amount, e.g. the balance. Enables the Max button and the over-max state. */\n  max?: bigint;\n  /** Show a Max button when `max` is set. */\n  showMax?: boolean;\n  /** Line under the amount, e.g. the fiat value. */\n  hint?: ReactNode;\n  /** Accessible label. Defaults to \"Amount\". */\n  label?: string;\n};\n\n/**\n * Large decimal amount input for token amounts. Keeps the value as a\n * string, so precision is never lost to floats. Refuses invalid keystrokes,\n * clamps to the token's decimals, and cleans up pasted amounts such as\n * \"$1,000.50\".\n */\nexport function AmountInput({\n  value,\n  onValueChange,\n  decimals,\n  max,\n  showMax = true,\n  hint,\n  label = \"Amount\",\n  className,\n  id,\n  ...props\n}: AmountInputProps) {\n  const fallbackId = useId();\n  const inputId = id ?? fallbackId;\n  const hintId = `${inputId}-hint`;\n  const state = getAmountState(value, decimals, max);\n\n  const update = (next: string) =>\n    onValueChange(next, getAmountState(next, decimals, max));\n\n  const onPaste = (event: ClipboardEvent<HTMLInputElement>) => {\n    const text = event.clipboardData.getData(\"text\");\n    // Let normal input handle anything the sanitizer already accepts.\n    if (sanitizeAmountInput(text, decimals) !== undefined) return;\n    const parsed = parseAmount(text, decimals);\n    if (parsed === undefined) return;\n    event.preventDefault();\n    update(formatUnits(parsed, decimals));\n  };\n\n  return (\n    <div\n      data-slot=\"amount-input\"\n      data-exceeds-max={state.exceedsMax || undefined}\n      className={cn(\"flex min-w-0 flex-col gap-1\", className)}\n    >\n      <div className=\"flex items-center gap-2\">\n        <input\n          id={inputId}\n          type=\"text\"\n          inputMode=\"decimal\"\n          autoComplete=\"off\"\n          autoCorrect=\"off\"\n          spellCheck={false}\n          placeholder=\"0\"\n          aria-label={label}\n          aria-invalid={state.exceedsMax || undefined}\n          aria-describedby={hint ? hintId : undefined}\n          value={value}\n          onChange={(event) => {\n            const next = sanitizeAmountInput(event.target.value, decimals);\n            if (next !== undefined) update(next);\n          }}\n          onPaste={onPaste}\n          className={cn(\n            \"w-full min-w-0 bg-transparent text-3xl font-medium tabular-nums outline-none placeholder:text-muted-foreground/50\",\n            state.exceedsMax && \"text-destructive\"\n          )}\n          {...props}\n        />\n        {showMax && max !== undefined && (\n          <button\n            type=\"button\"\n            onClick={() => update(formatUnits(max, decimals))}\n            className=\"shrink-0 rounded-md px-2 py-1 text-xs font-medium text-primary hover:bg-muted\"\n          >\n            Max\n          </button>\n        )}\n      </div>\n      {hint && (\n        <div id={hintId} className=\"text-sm text-muted-foreground tabular-nums\">\n          {hint}\n        </div>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/amount-input.md",
  "categories": [
    "tokens"
  ],
  "type": "registry:component"
}