{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-input",
  "title": "Number Input",
  "description": "Number field with decrement and increment buttons, min, max and step, following the spinbutton keyboard pattern.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/button",
    "@corsair-ui/input-group"
  ],
  "files": [
    {
      "path": "registry/default/ui/number-input.tsx",
      "content": "\"use client\";\n\nimport { MinusIcon, PlusIcon } from \"lucide-react\";\nimport { useState, type ComponentProps, type KeyboardEvent } from \"react\";\n\nimport { cn } from \"@/registry/default/lib/utils\";\nimport { Button } from \"@/registry/default/ui/button\";\nimport { InputGroup, InputGroupAddon, InputGroupInput } from \"@/registry/default/ui/input-group\";\n\ninterface NumberInputProps extends Omit<\n  ComponentProps<\"input\">,\n  \"value\" | \"defaultValue\" | \"onChange\" | \"type\" | \"min\" | \"max\" | \"step\"\n> {\n  /** `null` when empty. */\n  value?: number | null;\n  defaultValue?: number | null;\n  onValueChange?: (value: number | null) => void;\n  min?: number;\n  max?: number;\n  /** Arrow keys and the buttons move by this much. */\n  step?: number;\n  /** Page Up / Page Down and Shift + arrow keys move by this much. Defaults to `step * 10`. */\n  largeStep?: number;\n  decrementLabel?: string;\n  incrementLabel?: string;\n  /** Classes for the bordered group around the input and the buttons. */\n  groupClassName?: string;\n}\n\nfunction decimalsOf(n: number) {\n  const text = String(n);\n  return text.includes(\".\") ? text.split(\".\")[1]!.length : 0;\n}\n\n/** Accepts a dot or a comma as the decimal separator; anything else that is not a number is null. */\nfunction parse(text: string) {\n  const trimmed = text.trim().replace(\",\", \".\");\n  if (trimmed === \"\" || trimmed === \"-\" || trimmed === \".\") return null;\n  const number = Number(trimmed);\n  return Number.isFinite(number) ? number : null;\n}\n\n/**\n * A number field with − and + buttons, following the spinbutton pattern:\n * arrow keys, Page Up / Page Down, Home and End. Typed values are checked and\n * clamped on blur or Enter. The buttons stay out of the tab order, since the\n * keys already cover them.\n */\nfunction NumberInput(props: NumberInputProps) {\n  const {\n    value,\n    defaultValue,\n    onValueChange,\n    min,\n    max,\n    step = 1,\n    largeStep = step * 10,\n    decrementLabel = \"Decrease\",\n    incrementLabel = \"Increase\",\n    className,\n    groupClassName,\n    disabled,\n    readOnly,\n    onBlur,\n    onKeyDown,\n    ...inputProps\n  } = props;\n  const controlled = \"value\" in props;\n  const [internal, setInternal] = useState<number | null>(defaultValue ?? null);\n  const current = controlled ? (value ?? null) : internal;\n  // What the user is typing, until it is committed; null shows `current`.\n  const [draft, setDraft] = useState<string | null>(null);\n\n  const clamp = (n: number) => Math.min(max ?? Infinity, Math.max(min ?? -Infinity, n));\n  const commit = (next: number | null) => {\n    setDraft(null);\n    if (next === current) return;\n    if (!controlled) setInternal(next);\n    onValueChange?.(next);\n  };\n  const base = () => (draft === null ? current : parse(draft));\n  const stepBy = (delta: number) => {\n    const from = base();\n    const decimals = Math.max(\n      decimalsOf(step),\n      decimalsOf(largeStep),\n      from === null ? 0 : decimalsOf(from)\n    );\n    const next = from === null ? clamp(min ?? 0) : clamp(from + delta);\n    commit(Number(next.toFixed(decimals)));\n  };\n\n  const locked = disabled || readOnly;\n  const atMin = min !== undefined && current !== null && current <= min;\n  const atMax = max !== undefined && current !== null && current >= max;\n\n  const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {\n    onKeyDown?.(event);\n    if (event.defaultPrevented || locked) return;\n    const keys: Record<string, () => void> = {\n      ArrowUp: () => stepBy(event.shiftKey ? largeStep : step),\n      ArrowDown: () => stepBy(-(event.shiftKey ? largeStep : step)),\n      PageUp: () => stepBy(largeStep),\n      PageDown: () => stepBy(-largeStep),\n      Home: () => min !== undefined && commit(min),\n      End: () => max !== undefined && commit(max),\n      Enter: () => {\n        const parsed = base();\n        commit(parsed === null ? null : clamp(parsed));\n      },\n    };\n    const action = keys[event.key];\n    if (!action) return;\n    if (event.key !== \"Enter\") event.preventDefault();\n    action();\n  };\n\n  const button = (label: string, delta: number, blocked: boolean, Icon: typeof PlusIcon) => (\n    <Button\n      type=\"button\"\n      variant=\"ghost\"\n      size=\"icon-sm\"\n      tabIndex={-1}\n      aria-label={label}\n      disabled={locked || blocked}\n      // Keep focus (and the caret) in the input.\n      onMouseDown={(event) => event.preventDefault()}\n      onClick={() => stepBy(delta)}\n      className=\"size-7\"\n    >\n      <Icon aria-hidden=\"true\" />\n    </Button>\n  );\n\n  return (\n    <InputGroup data-slot=\"number-input\" className={groupClassName}>\n      <InputGroupInput\n        {...inputProps}\n        type=\"text\"\n        role=\"spinbutton\"\n        inputMode={decimalsOf(step) > 0 || (min ?? -1) < 0 ? \"decimal\" : \"numeric\"}\n        autoComplete=\"off\"\n        aria-valuenow={current ?? undefined}\n        aria-valuemin={min}\n        aria-valuemax={max}\n        disabled={disabled}\n        readOnly={readOnly}\n        value={draft ?? (current === null ? \"\" : String(current))}\n        onChange={(event) => {\n          // Digits, one separator and a leading minus; anything else is ignored.\n          if (/^-?\\d*[.,]?\\d*$/.test(event.target.value)) setDraft(event.target.value);\n        }}\n        onBlur={(event) => {\n          if (draft !== null) {\n            const parsed = parse(draft);\n            commit(parsed === null ? null : clamp(parsed));\n          }\n          onBlur?.(event);\n        }}\n        onKeyDown={handleKeyDown}\n        className={cn(\"tabular-nums\", className)}\n      />\n      <InputGroupAddon align=\"inline-end\" className=\"gap-0.5 pr-1\">\n        {button(decrementLabel, -step, atMin, MinusIcon)}\n        {button(incrementLabel, step, atMax, PlusIcon)}\n      </InputGroupAddon>\n    </InputGroup>\n  );\n}\n\nexport { NumberInput, type NumberInputProps };\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}