{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "digit-input",
  "type": "registry:ui",
  "title": "Digit input",
  "description": "One-time code boxes that read Western, Arabic-Indic and Persian digits, typed or pasted.",
  "dependencies": [
    "@dualkit/format"
  ],
  "registryDependencies": [
    "https://dualkit.dev/r/base.json"
  ],
  "files": [
    {
      "path": "components/DigitInput.tsx",
      "type": "registry:ui",
      "target": "components/dualkit/DigitInput.tsx",
      "content": "import { toArabicDigits, toWesternDigits } from '@dualkit/format'\nimport { useId, useRef, useState, type ClipboardEvent, type KeyboardEvent, type ReactNode } from 'react'\nimport { cx } from './direction'\n\nexport type DigitInputProps = {\n  label: ReactNode\n  /** The page language. Arabic shows Arabic-Indic digits; the value is always Western digits. */\n  locale?: 'en' | 'ar'\n  length?: number\n  value?: string\n  defaultValue?: string\n  onChange?: (value: string) => void\n  onComplete?: (value: string) => void\n  help?: ReactNode\n  error?: ReactNode\n  className?: string\n}\n\n/** Every digit system people type, reduced to Western digits and nothing else. */\nconst digitsIn = (text: string) => toWesternDigits(text).replace(/\\D/g, '')\nconst boxLabel = (locale: 'en' | 'ar', k: number, n: number) =>\n  locale === 'ar' ? `الخانة ${toArabicDigits(k + 1)} من ${toArabicDigits(n)}` : `Digit ${k + 1} of ${n}`\n\n/** A one-time code. The boxes stay left to right in both languages, because codes are read that way. */\nexport function DigitInput({ label, locale = 'en', length = 6, value, defaultValue = '', onChange, onComplete, help, error, className }: DigitInputProps) {\n  const [own, setOwn] = useState(() => digitsIn(defaultValue).slice(0, length))\n  const code = value !== undefined ? digitsIn(value).slice(0, length) : own\n  const boxes = useRef<Array<HTMLInputElement | null>>([])\n  const labelId = useId()\n  const noteId = useId()\n  const note = error ?? help\n\n  const focus = (k: number) => {\n    const el = boxes.current[Math.max(0, Math.min(length - 1, k))]\n    el?.focus()\n    el?.select()\n  }\n\n  const commit = (next: string) => {\n    if (next === code) return\n    if (value === undefined) setOwn(next)\n    onChange?.(next)\n    if (next.length === length) onComplete?.(next)\n  }\n\n  /** Write digits over the boxes from k on, keep whatever comes after them, and move to the next box. */\n  const writeFrom = (k: number, incoming: string) => {\n    const next = (code.slice(0, k) + incoming + code.slice(k + incoming.length)).slice(0, length)\n    commit(next)\n    focus(k + incoming.length)\n  }\n\n  /** Take out the digit in box k and close the gap, so the code stays one unbroken string. */\n  const removeAt = (k: number) => commit(code.slice(0, k) + code.slice(k + 1))\n\n  const onInput = (k: number, raw: string) => {\n    const typed = digitsIn(raw)\n    if (!typed) {\n      if (raw === '') removeAt(k)\n      return\n    }\n    // A box that already held a digit sees both; keep the new one.\n    const previous = code[k]\n    const incoming = previous && typed.length === 2 ? (typed.startsWith(previous) ? typed.slice(1) : typed.slice(0, 1)) : typed\n    writeFrom(Math.min(k, code.length), incoming)\n  }\n\n  const onKeyDown = (k: number, e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === 'Backspace' && !code[k] && k > 0) {\n      e.preventDefault()\n      removeAt(k - 1)\n      focus(k - 1)\n    } else if (e.key === 'ArrowLeft') {\n      e.preventDefault()\n      focus(k - 1)\n    } else if (e.key === 'ArrowRight') {\n      e.preventDefault()\n      focus(k + 1)\n    }\n  }\n\n  const onPaste = (k: number, e: ClipboardEvent<HTMLInputElement>) => {\n    e.preventDefault()\n    const pasted = digitsIn(e.clipboardData.getData('text'))\n    if (pasted) writeFrom(Math.min(k, code.length), pasted)\n  }\n\n  const shown = (d: string | undefined) => (d ? (locale === 'ar' ? toArabicDigits(d) : d) : '')\n\n  return (\n    <div className={cx('dk-digits', locale === 'ar' && 'dk-digits--arab', error != null && 'dk-digits--error', className)}>\n      <span id={labelId} className=\"dk-label\">{label}</span>\n      <div className=\"dk-digits__boxes\" dir=\"ltr\" role=\"group\" aria-labelledby={labelId} aria-describedby={note != null ? noteId : undefined}>\n        {Array.from({ length }, (_, k) => (\n          <input key={k} ref={(el) => { boxes.current[k] = el }} className=\"dk-digits__box\"\n            type=\"text\" inputMode=\"numeric\" autoComplete={k === 0 ? 'one-time-code' : 'off'}\n            aria-label={boxLabel(locale, k, length)} aria-invalid={error != null ? true : undefined}\n            value={shown(code[k])}\n            onFocus={(e) => e.currentTarget.select()}\n            onChange={(e) => onInput(k, e.currentTarget.value)}\n            onKeyDown={(e) => onKeyDown(k, e)}\n            onPaste={(e) => onPaste(k, e)} />\n        ))}\n      </div>\n      {note != null && <div id={noteId} className={cx('dk-caption', error != null ? 'dk-danger' : 'dk-secondary')}>{note}</div>}\n    </div>\n  )\n}\n"
    }
  ]
}
