{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-field",
  "type": "registry:ui",
  "title": "Number Field",
  "description": "The number-field primitive.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/input-group.json",
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/number-field.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/number-field.tsx",
      "content": "import { forwardRef, useEffect, useId, useState, type KeyboardEvent } from 'react';\nimport { Minus, Plus } from 'lucide-react';\nimport { cn } from '../lib/utils';\nimport { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from './input-group';\n\nexport interface NumberFieldProps {\n  value?: number | null;\n  defaultValue?: number | null;\n  onValueChange?: (value: number | null) => void;\n  min?: number;\n  max?: number;\n  /** What the arrows and the buttons add. Default 1. Its decimals set how many the field shows (0.25 shows two). */\n  step?: number;\n  /** What Page Up and Page Down add. Default ten steps. */\n  largeStep?: number;\n  /** A unit after the number (\"kg\", \"nights\"). Said as part of the value to assistive tech. */\n  unit?: string;\n  /** Submits the number with a form (a hidden input). */\n  name?: string;\n  id?: string;\n  disabled?: boolean;\n  invalid?: boolean;\n  'aria-label'?: string;\n  'aria-labelledby'?: string;\n  'aria-describedby'?: string;\n  /** The two buttons' names; pass them translated. */\n  decreaseLabel?: string;\n  increaseLabel?: string;\n  /** The language numbers are written in (decimal comma or point). */\n  locale?: string;\n  className?: string;\n}\n\nconst decimalsOf = (n: number) => (String(n).split('.')[1] ?? '').length;\n\n/**\n * A number you can type or step: the WAI-ARIA spin button. Up and Down add the step, Page Up and Page Down a larger one, Home and\n * End jump to the limits. Typed text is read when you leave the field or press Enter, kept inside the limits, and put back if it is\n * not a number. The two buttons are for the pointer; the keyboard has the arrow keys.\n */\nexport const NumberField = forwardRef<HTMLInputElement, NumberFieldProps>(function NumberField(\n  { value, defaultValue = null, onValueChange, min, max, step = 1, largeStep, unit, name, id, disabled, invalid, decreaseLabel = 'Decrease', increaseLabel = 'Increase', locale = 'en', className, ...aria },\n  ref,\n) {\n  const auto = useId();\n  const nid = id ?? auto;\n  const dec = decimalsOf(step);\n  const fmt = (n: number | null) => (n === null ? '' : new Intl.NumberFormat(locale, { minimumFractionDigits: dec, maximumFractionDigits: dec, useGrouping: false }).format(n));\n  const [own, setOwn] = useState<number | null>(defaultValue);\n  const current = value === undefined ? own : value;\n  const [text, setText] = useState(fmt(current));\n  useEffect(() => setText(fmt(current)), [current, dec, locale]); // eslint-disable-line react-hooks/exhaustive-deps\n  const clamp = (n: number) => {\n    const lo = min ?? -Infinity;\n    const hi = max ?? Infinity;\n    return Number(Math.min(hi, Math.max(lo, n)).toFixed(Math.max(dec, 6)));\n  };\n  const commit = (n: number | null) => {\n    setOwn(n);\n    setText(fmt(n));\n    if (n !== current) onValueChange?.(n);\n  };\n  /** Reads what was typed: a decimal point or comma, a minus sign, optional spaces; anything else is not a number. */\n  const read = (s: string): number | null | undefined => {\n    const t = s.replace(/\\s/g, '').replace(',', '.').replace(/−/g, '-');\n    if (t === '') return null;\n    return /^-?\\d*\\.?\\d+$|^-?\\d+\\.?$/.test(t) ? clamp(Number(t)) : undefined;\n  };\n  /** From empty, the first step up goes to the minimum (or 0) and the first step down to the maximum (or 0). */\n  const bump = (by: number) => commit(clamp(current === null ? (by > 0 ? (min ?? 0) : (max ?? 0)) : current + by));\n  const settle = () => {\n    const n = read(text);\n    if (n === undefined) setText(fmt(current));\n    else commit(n);\n  };\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    const big = largeStep ?? step * 10;\n    const act: Record<string, () => void> = {\n      ArrowUp: () => bump(step),\n      ArrowDown: () => bump(-step),\n      PageUp: () => bump(big),\n      PageDown: () => bump(-big),\n      Home: () => min !== undefined && commit(min),\n      End: () => max !== undefined && commit(max),\n      Enter: settle,\n    };\n    const f = act[e.key];\n    if (!f || (e.key === 'Home' && min === undefined) || (e.key === 'End' && max === undefined)) return;\n    e.preventDefault();\n    f();\n  };\n  const atMin = current !== null && min !== undefined && current <= min;\n  const atMax = current !== null && max !== undefined && current >= max;\n  return (\n    <InputGroup invalid={invalid} disabled={disabled} className={cn('max-w-48', className)}>\n      <InputGroupButton aria-label={decreaseLabel} aria-controls={nid} tabIndex={-1} disabled={disabled || atMin} onClick={() => bump(-step)} className=\"border-l-0 border-r\">\n        <Minus aria-hidden />\n      </InputGroupButton>\n      <InputGroupInput\n        ref={ref}\n        id={nid}\n        role=\"spinbutton\"\n        type=\"text\"\n        inputMode={dec > 0 || (min !== undefined && min < 0) ? 'decimal' : 'numeric'}\n        autoComplete=\"off\"\n        value={text}\n        aria-valuenow={current ?? undefined}\n        aria-valuemin={min}\n        aria-valuemax={max}\n        aria-valuetext={current === null ? undefined : `${fmt(current)}${unit ? ` ${unit}` : ''}`}\n        aria-invalid={invalid || undefined}\n        disabled={disabled}\n        onChange={(e) => setText(e.target.value)}\n        onBlur={settle}\n        onKeyDown={onKey}\n        className=\"text-center tabular-nums\"\n        {...aria}\n      />\n      {unit && <InputGroupAddon align=\"end\">{unit}</InputGroupAddon>}\n      <InputGroupButton aria-label={increaseLabel} aria-controls={nid} tabIndex={-1} disabled={disabled || atMax} onClick={() => bump(step)}>\n        <Plus aria-hidden />\n      </InputGroupButton>\n      {name && <input type=\"hidden\" name={name} value={current ?? ''} />}\n    </InputGroup>\n  );\n});\n"
    }
  ]
}
