{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-field",
  "title": "Number Field",
  "description": "A number with its unit or currency, formatted when the field loses focus, kept between its minimum and maximum, with arrow keys and optional minus and plus buttons.",
  "registryDependencies": [
    "input-group",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/number-field.tsx",
      "content": "'use client'\n\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport {\n  formatNumber,\n  getCurrencyDigits,\n  getCurrencySymbol,\n} from '@/registry/lib/dashboardblocks/format'\nimport { type KeyboardEvent, type ReactNode, useState } from 'react'\n\nimport {\n  InputGroup,\n  InputGroupAddon,\n  InputGroupButton,\n  InputGroupInput,\n  InputGroupText,\n} from '@/components/ui/input-group'\n\nimport { cn } from '@/lib/utils'\n\ninterface NumberFieldProps {\n  'aria-describedby'?: string\n  'aria-invalid'?: boolean\n  'aria-label'?: string\n  'aria-labelledby'?: string\n  /** Set the width here, such as `@md:w-48`: a `Field` makes it full width. */\n  className?: string\n  /** A currency code, such as `EUR`: its symbol goes before the value, and the value keeps its cents. */\n  currency?: string\n  /** Names the minus button. @default 'Decrease' */\n  decrementLabel?: string\n  disabled?: boolean\n  /** The id of the form the field belongs to when it sits outside it, so Enter submits that form. */\n  form?: string\n  id?: string\n  /** Names the plus button. @default 'Increase' */\n  incrementLabel?: string\n  max?: number\n  min?: number\n  name?: string\n  /** Called as the person types, with `null` while the field is empty. A value outside `min` and `max` is brought back inside it when the field loses focus. */\n  onValueChange: (value: number | null) => void\n  placeholder?: string\n  /** With a `currency`: `exact` keeps its cents (`1,250.00`), `whole` rounds to whole units (`1,250`). @default 'exact' */\n  precision?: 'exact' | 'whole'\n  readOnly?: boolean\n  /** The step of the arrow keys and the minus and plus buttons, which also sets the decimals kept. @default 1 */\n  step?: number\n  /** Shows minus and plus buttons, such as for a number of seats. */\n  stepper?: boolean\n  /** After the value, such as `seats` or `a month`. */\n  unit?: ReactNode\n  value: number | null\n}\n\nfunction decimalsOf(step: number) {\n  const [, decimals = ''] = String(step).split('.')\n  return decimals.length\n}\n\nfunction parse(text: string) {\n  const cleaned = text.replace(/[^\\d.-]/g, '')\n  if (!/\\d/.test(cleaned)) return null\n  const value = Number(cleaned)\n  return Number.isFinite(value) ? value : null\n}\n\nfunction NumberField({\n  className,\n  currency,\n  decrementLabel = 'Decrease',\n  disabled,\n  incrementLabel = 'Increase',\n  max,\n  min,\n  onValueChange,\n  precision = 'exact',\n  readOnly,\n  step = 1,\n  stepper = false,\n  unit,\n  value,\n  ...props\n}: NumberFieldProps) {\n  const [text, setText] = useState<string | null>(null)\n  const decimals =\n    currency && precision === 'exact' ? getCurrencyDigits(currency) : decimalsOf(step)\n\n  function format(next: number) {\n    return formatNumber(next, {\n      maximumFractionDigits: decimals,\n      minimumFractionDigits: currency && precision === 'exact' ? decimals : 0,\n    })\n  }\n\n  function fit(next: number) {\n    const rounded = Number(next.toFixed(decimals))\n    return Math.min(max ?? Infinity, Math.max(min ?? -Infinity, rounded))\n  }\n\n  function stepBy(direction: 1 | -1) {\n    const next = fit((value ?? min ?? 0) + direction * step)\n    setText(null)\n    onValueChange(next)\n  }\n\n  function onKeyDown(event: KeyboardEvent<HTMLInputElement>) {\n    if (readOnly) return\n    if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return\n    event.preventDefault()\n    stepBy(event.key === 'ArrowUp' ? 1 : -1)\n  }\n\n  const shown = text ?? (value === null ? '' : format(value))\n  const atMin = min !== undefined && value !== null && value <= min\n  const atMax = max !== undefined && value !== null && value >= max\n\n  return (\n    <InputGroup data-slot='number-field' className={cn('w-full', className)}>\n      {currency && (\n        <InputGroupAddon>\n          <InputGroupText>{getCurrencySymbol(currency)}</InputGroupText>\n        </InputGroupAddon>\n      )}\n      <InputGroupInput\n        {...props}\n        autoComplete='off'\n        disabled={disabled}\n        readOnly={readOnly}\n        inputMode={decimals > 0 ? 'decimal' : 'numeric'}\n        value={shown}\n        onChange={(event) => {\n          const typed = event.target.value.replace(/[^\\d.,-]/g, '')\n          setText(typed)\n          const parsed = parse(typed)\n          onValueChange(parsed === null ? null : Number(parsed.toFixed(decimals)))\n        }}\n        onBlur={() => {\n          setText(null)\n          if (value !== null && fit(value) !== value) onValueChange(fit(value))\n        }}\n        onKeyDown={onKeyDown}\n        className='tabular-nums'\n      />\n      {(unit || stepper) && (\n        <InputGroupAddon align='inline-end'>\n          {unit && <InputGroupText>{unit}</InputGroupText>}\n          {stepper && (\n            <>\n              <InputGroupButton\n                size='icon-xs'\n                aria-label={decrementLabel}\n                aria-disabled={atMin || undefined}\n                disabled={disabled || readOnly}\n                className='aria-disabled:opacity-50'\n                onClick={() => {\n                  if (!atMin) stepBy(-1)\n                }}\n              >\n                <IconPlaceholder\n                  lucide='MinusIcon'\n                  tabler='IconMinus'\n                  hugeicons='MinusSignIcon'\n                  phosphor='MinusIcon'\n                  remixicon='RiSubtractLine'\n                />\n              </InputGroupButton>\n              <InputGroupButton\n                size='icon-xs'\n                aria-label={incrementLabel}\n                aria-disabled={atMax || undefined}\n                disabled={disabled || readOnly}\n                className='aria-disabled:opacity-50'\n                onClick={() => {\n                  if (!atMax) stepBy(1)\n                }}\n              >\n                <IconPlaceholder\n                  lucide='PlusIcon'\n                  tabler='IconPlus'\n                  hugeicons='PlusSignIcon'\n                  phosphor='PlusIcon'\n                  remixicon='RiAddLine'\n                />\n              </InputGroupButton>\n            </>\n          )}\n        </InputGroupAddon>\n      )}\n    </InputGroup>\n  )\n}\n\nexport { NumberField }\n\nexport type { NumberFieldProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/number-field.tsx"
    }
  ],
  "type": "registry:component"
}
