{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ink/masked-input",
  "title": "Masked Input",
  "description": "Masked input for phone, date, credit card formats",
  "dependencies": [
    "ink"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/ink/interaction.json",
    "https://termcn.dev/r/ink/terminal-style.json",
    "https://termcn.dev/r/ink/terminal-symbols.json",
    "https://termcn.dev/r/ink/use-theme.json",
    "https://termcn.dev/r/ink/use-unicode.json"
  ],
  "files": [
    {
      "path": "registry/ui/masked-input.tsx",
      "content": "import { Box, Text } from \"ink\";\nimport React, { useState } from \"react\";\n\nimport { useInteraction } from \"@/hooks/use-interaction\";\nimport { useTheme } from \"@/hooks/use-theme\";\nimport { useUnicode } from \"@/hooks/use-unicode\";\nimport { resolveBorderStyle } from \"@/lib/terminal-style\";\nimport { resolveTerminalSymbol } from \"@/lib/terminal-symbols\";\n\nexport interface MaskedInputProps {\n  mask: string;\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  onChange?: (value: string) => void;\n  onSubmit?: (value: string) => void;\n  label?: string;\n  placeholder?: string;\n  autoFocus?: boolean;\n  id?: string;\n  width?: number;\n  isActive?: boolean;\n  disabled?: boolean;\n  readOnly?: boolean;\n  required?: boolean;\n  \"aria-label\"?: string;\n}\n\n/**\n * Returns true if the literal at `pos` \"closes\" a digit group, meaning there\n * exists a literal at some position j < pos such that every character strictly\n * between j and pos is '#'.  e.g. ')' at position 4 in '(###) ###-####'.\n */\nconst isClosingLiteral = (mask: string, pos: number): boolean => {\n  for (let j = pos - 1; j >= 0; j -= 1) {\n    if (mask[j] === \"#\") {\n      continue;\n    }\n    const between = mask.slice(j + 1, pos);\n    return between.length > 0 && [...between].every((c) => c === \"#\");\n  }\n  return false;\n};\n\n/**\n * Apply a mask to raw digits. '#' in the mask is a digit placeholder.\n * Returns the formatted display string.\n */\nconst applyMask = (raw: string, mask: string): string => {\n  let rawIdx = 0;\n  let result = \"\";\n\n  for (let i = 0; i < mask.length; i += 1) {\n    const maskChar = mask[i];\n\n    if (rawIdx >= raw.length) {\n      if (maskChar !== \"#\" && isClosingLiteral(mask, i)) {\n        result += maskChar;\n      }\n      break;\n    }\n\n    if (maskChar === \"#\") {\n      result += raw[rawIdx];\n      rawIdx += 1;\n    } else if (mask.includes(\"#\", i + 1)) {\n      result += maskChar;\n    }\n  }\n\n  return result;\n};\n\nconst maxDigits = (mask: string): number =>\n  [...mask].filter((c) => c === \"#\").length;\n\nexport const MaskedInput = ({\n  mask,\n  value: controlledValue,\n  defaultValue = \"\",\n  onValueChange,\n  onChange,\n  onSubmit,\n  label,\n  placeholder,\n  autoFocus = false,\n  id,\n  width = 40,\n  isActive = true,\n  disabled = false,\n  readOnly = false,\n  required = false,\n  \"aria-label\": ariaLabel,\n}: MaskedInputProps) => {\n  const unicode = useUnicode();\n  const cursor = resolveTerminalSymbol(unicode, \"█\", \"|\");\n  const [internalValue, setInternalValue] = useState(defaultValue);\n  const theme = useTheme();\n\n  const raw = controlledValue ?? internalValue;\n  const max = maxDigits(mask);\n  const applyChange = (nextValue: string) => {\n    const changeHandler = onValueChange ?? onChange;\n    if (changeHandler) {\n      changeHandler(nextValue);\n    } else {\n      setInternalValue(nextValue);\n    }\n  };\n\n  const { isFocused } = useInteraction(\n    (input, key) => {\n      if (key.return) {\n        onSubmit?.(raw);\n        return;\n      }\n\n      if (readOnly) {\n        return;\n      }\n\n      if (key.backspace) {\n        applyChange(raw.slice(0, -1));\n        return;\n      }\n\n      if (key.escape || key.upArrow || key.downArrow || key.tab) {\n        return;\n      }\n\n      const insertedDigits = input.replaceAll(/\\D/g, \"\");\n      if (insertedDigits && raw.length < max) {\n        applyChange((raw + insertedDigits).slice(0, max));\n      }\n    },\n    { autoFocus, disabled, id, isActive }\n  );\n\n  const display = raw.length > 0 ? applyMask(raw, mask) : \"\";\n  const borderColor = isFocused ? theme.colors.focusRing : theme.colors.border;\n\n  const remainingMask = mask.slice(display.length);\n\n  const displayPlaceholder = placeholder ? (\n    <Text aria-hidden color={theme.colors.mutedForeground}>\n      {placeholder}\n    </Text>\n  ) : null;\n\n  return (\n    <Box flexDirection=\"column\">\n      {label && <Text bold>{label}</Text>}\n      <Box\n        aria-label={\n          ariaLabel ?? `${label ?? \"Masked input\"}: ${display || \"empty\"}`\n        }\n        aria-role=\"textbox\"\n        aria-state={{ disabled, readonly: readOnly, required }}\n        borderStyle={resolveBorderStyle(\"round\", unicode)}\n        borderColor={borderColor}\n        width={width}\n        paddingX={1}\n      >\n        {display.length > 0 ? (\n          <Text aria-hidden color={theme.colors.foreground}>\n            {display}\n          </Text>\n        ) : (\n          displayPlaceholder\n        )}\n        {isFocused && display.length < mask.length && (\n          <Text aria-hidden color={theme.colors.focusRing}>\n            {cursor}\n          </Text>\n        )}\n        {display.length > 0 && display.length < mask.length && (\n          <Text aria-hidden color={theme.colors.mutedForeground} dimColor>\n            {remainingMask}\n          </Text>\n        )}\n      </Box>\n    </Box>\n  );\n};\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "input"
  ],
  "type": "registry:ui"
}