{
  "name": "number-input",
  "type": "registry:ui",
  "framework": "react",
  "title": "Number Input",
  "description": "A number field with decrement and increment buttons that respect min, max and step.",
  "files": [
    {
      "path": "ui/number-input.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface NumberInputProps extends Omit<\n  React.InputHTMLAttributes<HTMLInputElement>,\n  \"type\" | \"value\" | \"defaultValue\" | \"onChange\" | \"min\" | \"max\" | \"step\"\n> {\n  /** Controlled value. Leave undefined to let the component own it. */\n  value?: number\n  defaultValue?: number\n  onValueChange?: (value: number) => void\n  min?: number\n  max?: number\n  step?: number\n  invalid?: boolean\n  /** Accessible names of the two stepper buttons. */\n  decrementLabel?: string\n  incrementLabel?: string\n}\n\n/** As many decimals as the step has, so 0.1 + 0.2 reads 0.3. */\nfunction decimalsOf(step: number): number {\n  const text = String(step)\n  const point = text.indexOf(\".\")\n  return point === -1 ? 0 : text.length - point - 1\n}\n\nconst stepClassName =\n  \"inline-flex w-9 shrink-0 items-center justify-center text-newt-text-secondary transition-colors duration-fast ease-newt hover:bg-newt-bg-hover hover:text-newt-text-primary focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-newt-text-link disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:bg-transparent disabled:hover:text-newt-text-secondary\"\n\n/*\n * A number field with its two steppers drawn as real buttons. The input stays\n * `type=\"number\"`, so the arrow keys and the announced range are the\n * browser's. What is typed is kept as text until it parses, so a reader can\n * clear the field or type a minus sign on the way to a number; the value is\n * clamped to `min` and `max` when the field is left.\n */\nconst NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(\n  (\n    {\n      className,\n      value,\n      defaultValue,\n      onValueChange,\n      min,\n      max,\n      step = 1,\n      invalid = false,\n      disabled = false,\n      decrementLabel = \"Decrease\",\n      incrementLabel = \"Increase\",\n      onBlur,\n      ...props\n    },\n    ref\n  ) => {\n    const isControlled = value !== undefined\n    const [text, setText] = React.useState(() =>\n      String(value ?? defaultValue ?? \"\")\n    )\n    const parsed = text.trim() === \"\" ? Number.NaN : Number(text)\n    /* A controlled value the text does not already spell wins over the text. */\n    const shown =\n      isControlled && !Number.isNaN(parsed) && parsed !== value\n        ? String(value)\n        : text\n    const current = isControlled ? value : parsed\n\n    const clamp = (point: number) =>\n      Math.min(\n        max ?? Number.POSITIVE_INFINITY,\n        Math.max(min ?? Number.NEGATIVE_INFINITY, point)\n      )\n\n    const commit = (point: number) => {\n      const next = Number(clamp(point).toFixed(decimalsOf(step)))\n      setText(String(next))\n      onValueChange?.(next)\n    }\n\n    const stepBy = (direction: 1 | -1) => {\n      const from = Number.isNaN(current)\n        ? (min ?? 0) - direction * step\n        : current\n      commit(from + direction * step)\n    }\n\n    return (\n      <div\n        data-invalid={invalid || undefined}\n        className={cn(\n          \"inline-flex items-stretch overflow-hidden rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input transition-colors duration-fast ease-newt focus-within:border-newt-brand data-[invalid]:border-newt-danger group-data-[invalid]/field:border-newt-danger\",\n          disabled && \"opacity-50\",\n          className\n        )}\n      >\n        <button\n          type=\"button\"\n          aria-label={decrementLabel}\n          disabled={disabled || (min !== undefined && current <= min)}\n          onClick={() => stepBy(-1)}\n          className={stepClassName}\n        >\n          <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"h-3.5 w-3.5\">\n            <path\n              fill=\"currentColor\"\n              d=\"M3 7h10a1 1 0 1 1 0 2H3a1 1 0 0 1 0-2Z\"\n            />\n          </svg>\n        </button>\n        <input\n          ref={ref}\n          type=\"number\"\n          inputMode={decimalsOf(step) === 0 ? \"numeric\" : \"decimal\"}\n          min={min}\n          max={max}\n          step={step}\n          value={shown}\n          disabled={disabled}\n          aria-invalid={invalid || undefined}\n          onChange={(event) => {\n            setText(event.target.value)\n            const next =\n              event.target.value.trim() === \"\"\n                ? Number.NaN\n                : Number(event.target.value)\n            if (!Number.isNaN(next)) onValueChange?.(next)\n          }}\n          onBlur={(event) => {\n            onBlur?.(event)\n            if (!Number.isNaN(parsed) && clamp(parsed) !== parsed) {\n              commit(parsed)\n            }\n          }}\n          className=\"w-16 border-0 bg-transparent px-1 py-2.5 text-center font-[family-name:var(--newt-font-sans)] text-sm tabular-nums text-newt-text-primary [appearance:textfield] focus:outline-none disabled:cursor-not-allowed [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none\"\n          {...props}\n        />\n        <button\n          type=\"button\"\n          aria-label={incrementLabel}\n          disabled={disabled || (max !== undefined && current >= max)}\n          onClick={() => stepBy(1)}\n          className={stepClassName}\n        >\n          <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"h-3.5 w-3.5\">\n            <path\n              fill=\"currentColor\"\n              d=\"M8 2a1 1 0 0 1 1 1v4h4a1 1 0 1 1 0 2H9v4a1 1 0 1 1-2 0V9H3a1 1 0 0 1 0-2h4V3a1 1 0 0 1 1-1Z\"\n            />\n          </svg>\n        </button>\n      </div>\n    )\n  }\n)\nNumberInput.displayName = \"NumberInput\"\n\nexport { NumberInput }\n",
      "type": "registry:ui"
    }
  ]
}