{"version":3,"file":"ChipInput.cjs","names":[],"sources":["../../../src/components/ChipInput/ChipInput.tsx"],"sourcesContent":["/**\n * @tempest-limits props-count — the eight or so props that repeat across every input\n * in the SDK are one contract, not a bag: label, helperText and error are the three\n * states of the same message slot, and dropping any of them pushes the app back to\n * building the label/help/error scaffolding by hand around a bare control. Beyond\n * those, ChipInput adds only commitKeys and normalize, which are what make it a chip\n * input rather than a text field.\n */\nimport { useId, useRef, useState } from \"react\";\nimport type { KeyboardEvent } from \"react\";\nimport { cn } from \"@/utils/cn\";\nimport styles from \"./ChipInput.module.css\";\n\nexport interface ChipInputProps {\n    value: string[];\n    onChange: (next: string[]) => void;\n    label?: string;\n    placeholder?: string;\n    helperText?: string;\n    error?: string;\n    /** Id for the inner input. Generated when omitted. */\n    id?: string;\n    /**\n     * Accessible name for the inner input when there is no visible `label`.\n     * Without either, screen readers announce an unnamed text field.\n     */\n    \"aria-label\"?: string;\n    /** Keys that commit the current draft as a chip. Default: Enter, comma, Tab. */\n    commitKeys?: string[];\n    /** Lowercase + trim each chip + dedupe. Default: true. */\n    normalize?: boolean;\n    className?: string;\n}\n\n/**\n * Multi-value input. Type a value and press Enter (or comma / Tab) to push a\n * chip. Backspace on empty input removes the last chip.\n *\n * Name the field with `label` (associated through `htmlFor`) or, when the label\n * lives elsewhere in the layout, with `aria-label`. The placeholder is not an\n * accessible name — it disappears as soon as the first chip is added.\n */\nexport function ChipInput({\n    value,\n    onChange,\n    label,\n    placeholder = \"Adicionar e pressionar Enter…\",\n    helperText,\n    error,\n    commitKeys = [\"Enter\", \",\", \"Tab\"],\n    normalize = true,\n    className,\n    id,\n    \"aria-label\": ariaLabel,\n}: ChipInputProps) {\n    const [draft, setDraft] = useState<string>(\"\");\n    const inputRef = useRef<HTMLInputElement>(null);\n    const generatedId = useId();\n    const inputId = id ?? generatedId;\n    const describedById = error ? `${inputId}-error` : helperText ? `${inputId}-helper` : undefined;\n\n    function commit(): void {\n        const next = normalize ? draft.trim().toLowerCase() : draft.trim();\n        if (!next) return;\n        if (value.includes(next)) {\n            setDraft(\"\");\n            return;\n        }\n        onChange([...value, next]);\n        setDraft(\"\");\n    }\n\n    function handleKeyDown(event: KeyboardEvent<HTMLInputElement>): void {\n        if (commitKeys.includes(event.key)) {\n            event.preventDefault();\n            commit();\n            return;\n        }\n        if (event.key === \"Backspace\" && !draft && value.length > 0) {\n            onChange(value.slice(0, -1));\n        }\n    }\n\n    function remove(index: number): void {\n        const next = value.slice();\n        next.splice(index, 1);\n        onChange(next);\n    }\n\n    return (\n        <div className={cn(styles.wrapper, error && styles.error, className)}>\n            {label && (\n                <label htmlFor={inputId} className={styles.label}>\n                    {label}\n                </label>\n            )}\n            <div className={styles.field} onClick={() => inputRef.current?.focus()}>\n                {value.map((chip, index) => (\n                    <span key={`${chip}-${index}`} className={styles.chip}>\n                        {chip}\n                        <button\n                            type=\"button\"\n                            className={styles.remove}\n                            aria-label={`Remover ${chip}`}\n                            onClick={() => remove(index)}\n                        >\n                            ×\n                        </button>\n                    </span>\n                ))}\n                <input\n                    ref={inputRef}\n                    id={inputId}\n                    className={styles.input}\n                    value={draft}\n                    placeholder={value.length === 0 ? placeholder : \"\"}\n                    aria-label={label ? undefined : ariaLabel}\n                    aria-invalid={!!error}\n                    aria-describedby={describedById}\n                    onChange={(event) => setDraft(event.target.value)}\n                    onKeyDown={handleKeyDown}\n                    onBlur={commit}\n                />\n            </div>\n            {error ? (\n                <span id={`${inputId}-error`} className={styles.errorText}>\n                    {error}\n                </span>\n            ) : helperText ? (\n                <span id={`${inputId}-helper`} className={styles.helper}>\n                    {helperText}\n                </span>\n            ) : null}\n        </div>\n    );\n}\n"],"mappings":"gIA0CA,SAAgB,EAAU,CACtB,QACA,WACA,QACA,cAAc,gCACd,aACA,QACA,aAAa,CAAC,QAAS,IAAK,KAAK,EACjC,YAAY,GACZ,YACA,KACA,aAAc,GACC,CACf,GAAM,CAAC,EAAO,IAAA,EAAY,EAAA,SAAA,CAAiB,EAAE,EACvC,G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