"use client" import * as React from "react" import { PlusCircle, X } from "lucide-react" import { cn } from "../../utils/cn" import { Input } from "./input" import { Button } from "./button" import { Label } from "./label" interface AllowedDomainsInputProps { value: string[] onChange: (domains: string[]) => void onValidate?: (domain: string) => { valid: boolean; error?: string; cleanedDomain?: string } label?: string placeholder?: string disabled?: boolean error?: string | null helperText?: string className?: string } const AllowedDomainsInput = React.forwardRef( ( { value, onChange, onValidate, label = "Allowed Domains", placeholder = "example.com", disabled = false, error, helperText, className }, ref ) => { const [inputValue, setInputValue] = React.useState("") const [localError, setLocalError] = React.useState(null) const inputRef = React.useRef(null) const displayError = error || localError const addDomain = () => { const trimmedValue = inputValue.trim() if (!trimmedValue) return // Validate if validator provided if (onValidate) { const validation = onValidate(trimmedValue) if (!validation.valid) { setLocalError(validation.error || "Invalid domain") return } // Use cleaned domain if provided const domainToAdd = validation.cleanedDomain || trimmedValue if (!value.includes(domainToAdd)) { onChange([...value, domainToAdd]) setLocalError(null) } } else { // No validation, just add if not duplicate if (!value.includes(trimmedValue)) { onChange([...value, trimmedValue]) } } setInputValue("") } const removeDomain = (index: number) => { const newDomains = value.filter((_, i) => i !== index) onChange(newDomains) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault() addDomain() } } return (
{label && } {/* Existing domains */} {value.map((domain, index) => (
))} {/* Add new domain input */}
{ setInputValue(e.target.value) setLocalError(null) }} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={disabled} className="bg-ods-card border-ods-border rounded-[6px] flex-1" />
{/* Add Domain button */} {/* Error message */} {displayError && (

{displayError}

)} {/* Helper text */} {helperText && !displayError && (

{helperText}

)}
) } ) AllowedDomainsInput.displayName = "AllowedDomainsInput" export { AllowedDomainsInput } export type { AllowedDomainsInputProps }