import { forwardRef, MouseEvent, type ReactElement, ReactNode, Ref, RefAttributes, useState, } from 'react' import TextFieldElement, {TextFieldElementProps} from './TextFieldElement' import {IconButton, IconButtonProps, InputAdornment} from '@mui/material' import Visibility from '@mui/icons-material/Visibility' import VisibilityOff from '@mui/icons-material/VisibilityOff' import {FieldPath, FieldValues} from 'react-hook-form' export type PasswordElementProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue = unknown, > = TextFieldElementProps & { iconColor?: IconButtonProps['color'] renderIcon?: (password: boolean) => ReactNode } type PasswordElementComponent = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, >( props: PasswordElementProps & RefAttributes ) => ReactElement const PasswordElement = forwardRef(function PasswordEl< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue = unknown, >( props: PasswordElementProps, ref: Ref ) { const { iconColor, renderIcon = (password) => (password ? : ), slotProps, ...rest } = props const [password, setPassword] = useState(true) const endAdornment = ( ) => e.preventDefault()} onClick={() => setPassword(!password)} tabIndex={-1} color={iconColor ?? 'default'} > {renderIcon(password)} ) const inputSlot = slotProps?.input const inputSlotPlain = inputSlot && typeof inputSlot === 'object' && !('call' in inputSlot) ? (inputSlot as {endAdornment?: ReactNode}) : undefined return ( {endAdornment} {inputSlotPlain?.endAdornment} ), }, }} /> ) }) PasswordElement.displayName = 'PasswordElement' export default PasswordElement as PasswordElementComponent