import { forwardRef, useState, useEffect, useRef } from "react" import { Eye, EyeOff, ExternalLink } from "lucide-react" import { cn } from "@/lib/utils" interface ExternalInputProps extends Omit, 'type'> { /** The type of input - either 'text' or 'password' */ type: 'text' | 'password'; /** URL to open when the external link button is clicked. If not provided, button won't be shown. */ url?: string; /** Title/label for the external link button. If not provided, button won't be shown. */ urlTitle?: string; } const ExternalInput = forwardRef( ({ className, type, url, urlTitle, value, ...props }, ref) => { const [showPassword, setShowPassword] = useState(false) const [isFocused, setIsFocused] = useState(false) const [buttonWidth, setButtonWidth] = useState(0) const buttonRef = useRef(null) // Determine if we have a value (works for both controlled and uncontrolled) const hasValue = Boolean(value || (props.defaultValue && !value)) const isPasswordType = type === 'password' const showEyeToggle = isPasswordType && hasValue const showExternalButton = url && urlTitle && !hasValue // Measure the button width when it's rendered useEffect(() => { if (showExternalButton && buttonRef.current) { const width = buttonRef.current.offsetWidth setButtonWidth(width) } else { setButtonWidth(0) } }, [showExternalButton, urlTitle]) // Calculate padding-right based on what's showing const paddingRight = showEyeToggle ? '2.5rem' // 40px for eye toggle : showExternalButton && buttonWidth > 0 ? `${buttonWidth + 8}px` // button width + 8px spacing : undefined return (
{ setIsFocused(true) props.onFocus?.(e) }} onBlur={(e) => { setIsFocused(false) props.onBlur?.(e) }} {...props} /> {showEyeToggle && ( )} {showExternalButton && ( )}
) } ) ExternalInput.displayName = "ExternalInput" export { ExternalInput }