"use client" import * as React from "react" import { EyeIcon, EyeOffIcon } from "lucide-react" import { InputDecorator } from "./decorator" export type PasswordInputProps = Omit< React.ComponentProps, "type" | "value" | "onChange" > & { value?: string | null onChange?: React.ChangeEventHandler onValueChange?: (value: string) => void visible?: boolean defaultVisible?: boolean onVisibleChange?: (visible: boolean) => void showToggle?: boolean showCapsLockWarning?: boolean capsLockLabel?: string wrapperClassName?: string inputClassName?: string showLabel?: string hideLabel?: string } const PasswordInput = React.forwardRef( ( { value, onChange, onValueChange, visible, defaultVisible = false, onVisibleChange, showToggle = true, showCapsLockWarning = true, capsLockLabel = "Caps Lock is on", showLabel = "Show password", hideLabel = "Hide password", disabled, autoComplete = "current-password", trailing, onKeyDown, onKeyUp, ...props }, ref ) => { const isControlled = visible !== undefined const [internalVisible, setInternalVisible] = React.useState(defaultVisible) const [capsLockOn, setCapsLockOn] = React.useState(false) const currentVisible = isControlled ? visible : internalVisible const setVisibleState = (nextVisible: boolean) => { if (!isControlled) { setInternalVisible(nextVisible) } onVisibleChange?.(nextVisible) } const handleChange: React.ChangeEventHandler = (event) => { onChange?.(event) onValueChange?.(event.target.value) } const updateCapsLock = (event: React.KeyboardEvent) => { if (!showCapsLockWarning) return setCapsLockOn(event.getModifierState("CapsLock")) } const handleKeyDown: React.KeyboardEventHandler = (event) => { updateCapsLock(event) onKeyDown?.(event) } const handleKeyUp: React.KeyboardEventHandler = (event) => { updateCapsLock(event) onKeyUp?.(event) } const trailingContent = ( <> {trailing} {showCapsLockWarning && capsLockOn ? ( {capsLockLabel} ) : null} {showToggle ? ( ) : null} ) return ( ) } ) PasswordInput.displayName = "PasswordInput" export { PasswordInput }