import React, { useImperativeHandle, useRef } from 'react'; import { HiOutlineEye, HiOutlineEyeOff } from 'react-icons/hi'; import { IconButton, IconButtonProps, InputAdornment } from '@mui/material'; import { isFunction } from 'lodash'; import { useDefaultProps } from '../../hooks'; import { TextField, TextFieldProps } from '../text-field'; export type PasswordProps = Omit, 'select'> & { /** * position of show/hide password toggler button in input * @default 'end' */ showPasswordTogglerButtonPosition?: 'end' | 'start'; slots?: { showPasswordIcon?: React.ElementType; hidePasswordIcon?: React.ElementType; }; slotProps?: { showPasswordIcon?: React.SVGAttributes; hidePasswordIcon?: React.SVGAttributes; showPasswordTogglerButton?: Omit; }; }; function Password(inProps: PasswordProps) { const _inputRef = useRef(null); const { slotProps: inSlotProps, slots: inSlots, showPasswordTogglerButtonPosition = 'end', autoDirection = false, inputRef, ...props } = useDefaultProps({ name: 'ReevolvePassword', props: inProps }); useImperativeHandle(inputRef, () => _inputRef.current, []); const [showPassword, setShowPassword] = React.useState(false); const toggleShowPassword = () => { setShowPassword((show) => !show); _inputRef.current?.focus(); }; const { showPasswordIcon: showPasswordIconProps, hidePasswordIcon: hidePasswordIconProps, showPasswordTogglerButton: showPasswordTogglerButtonProps, ...slotProps } = inSlotProps ?? {}; const { showPasswordIcon, hidePasswordIcon, ...slots } = inSlots ?? {}; const ShowPasswordIcon = showPasswordIcon ?? HiOutlineEye; const HidePasswordIcon = hidePasswordIcon ?? HiOutlineEyeOff; const inputAddorment = ( ({ ...(showPasswordTogglerButtonPosition === 'end' ? { marginRight: -1.5 } : { marginLeft: -1.5 }), ...(isFunction(showPasswordTogglerButtonProps?.sx) ? (showPasswordTogglerButtonProps.sx as Function)(theme) : showPasswordTogglerButtonProps?.sx) })} > {showPassword ? ( ) : ( )} ); return ( ); } declare module '@mui/material' { interface Components { ReevolvePassword?: { defaultProps?: Partial; }; } } export default Password;