import React, { useImperativeHandle, useMemo, useRef, useState } from 'react' import withDefaults from '../utils/with-defaults' import { Props, defaultProps } from './input-props' import PasswordIcon from './password-icon' import Input from './input' interface PasswordProps extends Props { hideToggle?: boolean } const passwordDefaultProps = { ...defaultProps, hideToggle: false } type NativeAttrs = Omit, keyof PasswordProps> export type InputPasswordProps = PasswordProps & typeof passwordDefaultProps & NativeAttrs const InputPassword = React.forwardRef< HTMLInputElement, React.PropsWithChildren >(({ hideToggle, children, ...props }, ref: React.Ref) => { const inputRef = useRef(null) const [visible, setVisible] = useState(false) useImperativeHandle(ref, () => inputRef.current) const iconClickHandler = () => { setVisible((v) => !v) /* istanbul ignore next */ if (inputRef && inputRef.current) { inputRef.current.focus() } } const inputProps = useMemo( () => ({ ...props, ref: inputRef, iconClickable: true, onIconClick: iconClickHandler, type: visible ? 'text' : 'password' }), [props, iconClickHandler, visible, inputRef] ) const icon = useMemo(() => { if (hideToggle) return null return }, [hideToggle, visible]) return ( {children} ) }) export default withDefaults(InputPassword, passwordDefaultProps)