{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-text-input",
  "type": "registry:ui",
  "title": "react-tw/text-input.tsx",
  "description": "Carbon `TextInput` as a React + Tailwind component: label, the field, one message line Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "class-variance-authority@0.7.1",
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/tw-affix",
    "@kanok-ui/tw-field",
    "@kanok-ui/tw-skeleton",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-tooltip",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/text-input.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/text-input.tsx",
      "content": "import { useRef, type ChangeEvent, type ComponentProps, type CSSProperties, type MouseEvent, type ReactNode } from 'react';\nimport { cva } from 'class-variance-authority';\nimport { TextInput as TextInputPrimitive } from '@purpurabog/ui/headless';\nimport { FieldCounter, FieldLabel, FieldMessageLine, StatusIcon, fieldBoxClasses, fieldStateEdges, fluidBoxClasses, fluidControlClasses, useFluid, useTextField, useFieldDisabled, fieldInvalidRing, fieldTextSize } from './field.tsx';\nimport { cn } from './lib/utils.ts';\nimport { Affix, useAffixes } from './affix.tsx';\nimport { FieldSkeleton, type FieldSkeletonProps } from './skeleton.tsx';\nimport { Tooltip } from './tooltip.tsx';\n\n/**\n * Carbon `TextInput` as a React + Tailwind component: label, the field, one message line\n * (error > warning > helper), the in-field status icon, a show / hide toggle for passwords and a\n * soft `maxCount` counter — the look of `<ui-text-input>`. Validation is the app's (Carbon React):\n * set `invalid` + `invalidText`; going over `maxCount` is an error by itself. `fluid` (or a\n * `FluidForm` around it) is Carbon `FluidTextInput`: the label inside a 64px box. `prefixText` /\n * `suffixText` (a currency, a unit) and `mask` (format as you type) have no Carbon counterpart;\n * they follow USWDS, as `<ui-text-input>` does. The password toggle (Carbon PasswordInput) is named by its icon\n * tooltip, `showPasswordLabel` / `hidePasswordLabel`; `inline` puts the label before the field and the message\n * after it (Carbon `inline`). The field's behaviour (password toggle, mask) is the headless TextInput.\n */\nexport const textInputVariants = cva(\n    [\n        'box-border m-0 w-full py-0 ps-4 pe-(--end-pad)',\n        fieldBoxClasses,\n        'text-ink type-body-sm',\n        fieldTextSize,\n        // Carbon text-input: background-color and outline at fast-01\n        'outline-[length:var(--ui-field-focus-width,2px)] [outline-offset:calc(-1*var(--ui-field-focus-width,2px))] outline-transparent transition-[background-color,outline-color] duration-(--ui-duration-fast) ease-carbon',\n        'placeholder:text-ink-placeholder placeholder:opacity-100 focus:outline-focus focus:[box-shadow:var(--ui-field-focus-halo,none)]',\n        'disabled:cursor-not-allowed disabled:text-ink-disabled disabled:placeholder:text-ink-disabled',\n        'read-only:not-disabled:bg-[var(--ui-field-readonly-background,transparent)]',\n        fieldStateEdges,\n        'forced-colors:border forced-colors:border-[FieldText] forced-colors:focus:outline-[Highlight]',\n    ],\n    {\n        variants: {\n            size: { sm: 'min-h-field-sm', md: 'min-h-field-md', lg: 'min-h-field-lg' },\n            invalid: { true: fieldInvalidRing, false: '' },\n        },\n        defaultVariants: { size: 'md', invalid: false },\n    },\n);\n\nconst eyeIcon = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\">\n        <path d=\"M15.5 7.8C14.3 4.7 11.3 2.5 8 2.5S1.7 4.7.5 7.8v.4c1.2 3.1 4.2 5.3 7.5 5.3s6.3-2.2 7.5-5.3v-.4ZM8 12.5c-2.7 0-5.4-1.9-6.5-4.5C2.6 5.4 5.3 3.5 8 3.5s5.4 1.9 6.5 4.5c-1.1 2.6-3.8 4.5-6.5 4.5Z\" />\n        <path d=\"M8 5a3 3 0 1 0 0 6 3 3 0 0 0 0-6Zm0 5a2 2 0 1 1 0-4 2 2 0 0 1 0 4Z\" />\n    </svg>\n);\n\nconst eyeOffIcon = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\">\n        <path d=\"m2.6 11.3.7-.7C2.5 9.9 1.9 9 1.5 8c1.1-2.6 3.8-4.5 6.5-4.5.9 0 1.7.2 2.5.5l.8-.8c-1-.4-2.1-.7-3.3-.7C4.7 2.5 1.7 4.7.5 7.8v.4c.5 1.2 1.2 2.3 2.1 3.1ZM6 7.9A2 2 0 0 1 7.9 6l.9-.9A3 3 0 0 0 5.1 8.8l.9-.9ZM15.5 7.8c-.6-1.5-1.5-2.8-2.7-3.8L15 1.7 14.3 1 1 14.3l.7.7 2.5-2.5c1.1.6 2.4 1 3.8 1 3.3 0 6.3-2.2 7.5-5.3v-.4ZM10 8a2 2 0 0 1-2 2c-.3 0-.6-.1-.9-.2l2.7-2.7c.1.3.2.6.2.9Zm-2 4.5c-1.1 0-2.1-.3-3-.8L6.4 10.3c.5.4 1 .7 1.6.7a3 3 0 0 0 3-3c0-.6-.3-1.2-.7-1.6L12 4.7c1.1.9 2 2 2.5 3.3-1.1 2.6-3.8 4.5-6.5 4.5Z\" />\n    </svg>\n);\n\nexport interface TextInputProps extends Omit<ComponentProps<'input'>, 'size' | 'onChange'> {\n    /** Visible label (required for a name; `hideLabel` keeps it for screen readers only). */\n    label: ReactNode;\n    hideLabel?: boolean;\n    /** Heights: sm 32px · md 40px (Carbon default) · lg 48px. */\n    size?: 'sm' | 'md' | 'lg';\n    helperText?: ReactNode;\n    /** Error state, set by the app's validation. */\n    invalid?: boolean;\n    invalidText?: ReactNode;\n    /** Non-blocking warning, shown when there is no error. */\n    warn?: boolean;\n    warnText?: ReactNode;\n    /** Soft limit with a visible counter: typing past it isn't blocked, it becomes an error. */\n    maxCount?: number;\n    /** Error over `maxCount`; `{max}` is the limit (default: the translation's). */\n    tooLongText?: string;\n    /** Announced once within the last 10% of `maxCount` (default: the translation's). */\n    nearLimitText?: string;\n    /** The password toggle's name and tooltip while the password is hidden (Carbon; default: the translation's \"Show password\"). */\n    showPasswordLabel?: string;\n    /** The password toggle's name and tooltip while the password shows (Carbon; default: the translation's \"Hide password\"). */\n    hidePasswordLabel?: string;\n    /** Each press of the password toggle (Carbon `onTogglePasswordVisibility`); `visible` is the new state. */\n    onTogglePasswordVisibility?: (event: MouseEvent<HTMLButtonElement>, visible: boolean) => void;\n    /**\n     * Carbon `inline`: the label before the field and the helper / error text after it, in one row centred; each side\n     * at most 142px, 8px from the field (ignored while fluid).\n     */\n    inline?: boolean;\n    /** Language of the built-in strings; defaults to `<html lang>`. */\n    lang?: string;\n    /** Carbon `FluidTextInput`: the label inside a 64px box, an error / warning inside it (also set by `FluidForm`). */\n    fluid?: boolean;\n    /** Text inside the field before the value, e.g. a currency (`฿`). Part of the field's description for screen readers. */\n    prefixText?: ReactNode;\n    /** Text inside the field after the value, e.g. a unit (`kg`). Part of the field's description for screen readers. */\n    suffixText?: ReactNode;\n    /**\n     * Formats the value as the user types: `0` a digit, `a` a letter, `*` either; any other\n     * character is a separator added for the user (`\\` makes a pattern character one).\n     * E.g. `0-0000-00000-00-0` (Thai citizen ID), `000-000-0000`. The value is the formatted\n     * text; `onChange` also gets what was typed. Put the format in the helper text too (Carbon).\n     */\n    mask?: string;\n    /** The input's change; `raw` is the value without the mask's separators (the value itself when there is no mask). */\n    onChange?: (event: ChangeEvent<HTMLInputElement>, raw: string) => void;\n}\n\n/** The value 8px clear of the prefix / suffix (the password toggle brings no gap of its own). */\nfunction affixPadding(hasPrefix: boolean, hasSuffix: boolean, password: boolean) {\n    return cn(\n        hasPrefix && 'ps-[calc(16px_+_var(--prefix-width,0px)_+_8px)]',\n        hasSuffix && (password ? 'pe-[calc(var(--end-pad)_+_var(--suffix-width,0px)_+_16px)]' : 'pe-[calc(var(--end-pad)_+_var(--suffix-width,0px)_+_8px)]'),\n    );\n}\n\n/** Where the prefix (absolute, at the start) and the suffix (first in the end adornments) sit. */\nfunction affixPlace(kind: 'prefix' | 'suffix', fluid: boolean) {\n    if (kind === 'prefix') return cn('pointer-events-none absolute inset-y-0 start-4 flex items-center [input:disabled~&]:text-ink-disabled', fluid && 'top-8 bottom-auto h-[18px]');\n    return cn('[input:disabled~*_&]:text-ink-disabled', fluid && 'mt-2 self-start');\n}\n\n/**\n * Carbon PasswordInput's toggle: named by an icon tooltip below it, lined up with its end (tooltipPosition bottom,\n * tooltipAlignment end); the name says what a press does next, so no aria-pressed.\n */\nfunction PasswordToggle({ labels }: Readonly<{ labels: { show: string; hide: string } }>) {\n    const { visible } = TextInputPrimitive.useTextInputContext();\n    const label = visible ? labels.hide : labels.show;\n    return (\n        <Tooltip text={label} asLabel placement=\"bottom-end\">\n            <TextInputPrimitive.PasswordToggle\n                className=\"pointer-events-auto grid size-10 cursor-pointer place-items-center rounded-[var(--ui-icon-control-radius,0px)] border-0 bg-transparent p-0 text-ink outline-[length:var(--ui-focus-width,2px)] -outline-offset-2 outline-transparent transition-[outline-color] duration-(--ui-duration-fast) ease-carbon focus-visible:outline-focus disabled:cursor-not-allowed disabled:bg-transparent disabled:text-ink-disabled [&_svg]:size-4 [&_svg]:fill-current\"\n                aria-label={label}\n            >\n                {visible ? eyeOffIcon : eyeIcon}\n            </TextInputPrimitive.PasswordToggle>\n        </Tooltip>\n    );\n}\n\n/**\n * Carbon inline (text-input-wrapper--inline): label, field and message in one row, centred; the label and the message\n * each at most 142px and 8px from the field, which takes the rest (flex: 8), their own margins gone.\n */\nconst INLINE = { root: 'flex items-center', top: 'me-2 max-w-[142px] flex-[0_1_auto] [overflow-wrap:break-word]', label: 'm-0 [word-break:break-word]', row: 'flex-[8]', message: 'm-0' };\nconst STACKED = { root: '', top: '', label: undefined, row: '', message: undefined };\n\n/** Inline, the message sits after the field in a box of its own, kept (8px) when there is none, as Carbon's. */\nfunction MessageBox({ inline, children }: Readonly<{ inline: boolean; children: ReactNode }>) {\n    return inline ? <div className=\"ms-2 max-w-[142px] flex-[0_1_auto] [overflow-wrap:break-word]\">{children}</div> : children;\n}\n\nexport function TextInput({ type = 'text', value, defaultValue, mask, disabled: ownDisabled, onTogglePasswordVisibility, ...rest }: Readonly<TextInputProps>) {\n    const disabled = useFieldDisabled(ownDisabled);\n    return (\n        <TextInputPrimitive.Root\n            type={type}\n            value={value}\n            defaultValue={defaultValue}\n            mask={mask}\n            disabled={disabled}\n            onVisibleChange={(visible, event) => onTogglePasswordVisibility?.(event, visible)}\n        >\n            <TextInputField {...rest} type={type} />\n        </TextInputPrimitive.Root>\n    );\n}\n\ntype TextInputFieldProps = Omit<TextInputProps, 'value' | 'defaultValue' | 'mask' | 'disabled' | 'onTogglePasswordVisibility'>;\n\n/** The label, counter, field, affixes, status icon, password toggle and message, inside the primitive (they follow its value and state). */\nfunction TextInputField({\n    label,\n    hideLabel,\n    size,\n    helperText,\n    invalid,\n    invalidText,\n    warn,\n    warnText,\n    maxCount,\n    tooLongText,\n    nearLimitText,\n    showPasswordLabel,\n    hidePasswordLabel,\n    inline: inlineProp,\n    lang,\n    fluid: fluidProp,\n    prefixText,\n    suffixText,\n    className,\n    id,\n    type = 'text',\n    onChange,\n    readOnly,\n    ...rest\n}: Readonly<TextInputFieldProps>) {\n    const { value: shownValue, defaultValue: shownDefault, disabled } = TextInputPrimitive.useTextInputContext();\n    const row = useRef<HTMLDivElement>(null);\n    const field = useTextField({ id, value: shownValue, defaultValue: shownDefault, maxCount, tooLongText, nearLimitText, invalid, invalidText, warn, warnText, helperText, disabled, readOnly, lang });\n    const { message } = field;\n    const { hasPrefix, hasSuffix, describedBy } = useAffixes(row, field.id, prefixText, suffixText, field.messageId);\n    const fluid = useFluid(fluidProp);\n    const inline = !!inlineProp && !fluid;\n    const layout = inline ? INLINE : STACKED;\n    const status = message?.kind === 'helper' ? undefined : message?.kind;\n    // room for the status icon (16px + 8px gap) and the password toggle (40px), else 16px (fluid: the icon sits in the message row)\n    const endPad = (status && !fluid ? 24 : 0) + (type === 'password' ? 40 : 16);\n\n    const onInputChange = (event: ChangeEvent<HTMLInputElement>, raw: string) => {\n        field.onTyped(event.target.value);\n        onChange?.(event, raw);\n    };\n\n    return (\n        <div className={cn('block font-sans', layout.root, fluid && fluidBoxClasses(status, readOnly), className)}>\n            <div className={cn(fluid ? 'contents' : 'flex justify-between gap-2', layout.top)}>\n                <FieldLabel htmlFor={field.id} hidden={hideLabel} disabled={disabled} fluid={fluid} className={layout.label}>\n                    {label}\n                </FieldLabel>\n                <FieldCounter length={field.length} max={maxCount} over={field.count.over} announcement={field.announcement} fluid={fluid} />\n            </div>\n            <div ref={row} className={cn('relative flex items-center', layout.row)} style={{ '--end-pad': `${endPad}px` } as CSSProperties}>\n                <TextInputPrimitive.Input\n                    {...rest}\n                    id={field.id}\n                    readOnly={readOnly}\n                    aria-invalid={field.showError}\n                    aria-describedby={describedBy}\n                    className={cn(\n                        textInputVariants({ size, invalid: field.showError }),\n                        affixPadding(hasPrefix, hasSuffix, type === 'password'),\n                        fluid && cn('h-16 min-h-16 pt-8 pb-[13px]', fluidControlClasses(status)),\n                    )}\n                    onChange={onInputChange}\n                />\n                <Affix id={field.id} kind=\"prefix\" className={affixPlace('prefix', fluid)}>\n                    {prefixText}\n                </Affix>\n                <span className={cn('pointer-events-none absolute inset-y-0 end-0 flex items-center gap-2 pe-4 has-[button]:pe-0', fluid && 'top-6 bottom-auto h-10')}>\n                    <Affix id={field.id} kind=\"suffix\" className={affixPlace('suffix', fluid)}>\n                        {suffixText}\n                    </Affix>\n                    <StatusIcon kind={message?.kind} className={fluid ? 'absolute end-4 top-[calc(100%+8px)]' : undefined} />\n                    {type === 'password' ? <PasswordToggle labels={{ show: showPasswordLabel ?? field.t.showPassword, hide: hidePasswordLabel ?? field.t.hidePassword }} /> : null}\n                </span>\n            </div>\n            <MessageBox inline={inline}>\n                <FieldMessageLine id={`${field.id}-message`} message={message} content={field.messageContent} disabled={disabled} fluid={fluid} className={layout.message} />\n            </MessageBox>\n        </div>\n    );\n}\n\n/** Carbon `TextInputSkeleton`: the shape of the field while its data loads (label bar, 8px, the field's bar). Hidden from assistive tech. */\nexport function TextInputSkeleton(props: Readonly<FieldSkeletonProps>) {\n    return <FieldSkeleton {...props} />;\n}\n"
    }
  ]
}
