You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
54 lines
1.3 KiB
import { EyeIcon, EyeOffIcon } from '@heroicons/react/solid';
|
|
import { Field } from 'formik';
|
|
|
|
import { useState } from 'react';
|
|
|
|
interface CustomInputProps extends React.ComponentProps<'input'> {
|
|
as?: 'input';
|
|
}
|
|
|
|
interface CustomFieldProps extends React.ComponentProps<typeof Field> {
|
|
as?: 'field';
|
|
}
|
|
|
|
type SensitiveInputProps = CustomInputProps | CustomFieldProps;
|
|
|
|
const SensitiveInput = ({ as = 'input', ...props }: SensitiveInputProps) => {
|
|
const [isHidden, setHidden] = useState(true);
|
|
const Component = as === 'input' ? 'input' : Field;
|
|
const componentProps =
|
|
as === 'input'
|
|
? props
|
|
: {
|
|
...props,
|
|
as: props.type === 'textarea' && !isHidden ? 'textarea' : undefined,
|
|
};
|
|
return (
|
|
<>
|
|
<Component
|
|
{...componentProps}
|
|
className={`rounded-l-only ${componentProps.className ?? ''}`}
|
|
type={
|
|
isHidden
|
|
? 'password'
|
|
: props.type !== 'password'
|
|
? props.type ?? 'text'
|
|
: 'text'
|
|
}
|
|
/>
|
|
<button
|
|
onClick={(e) => {
|
|
e.preventDefault();
|
|
setHidden(!isHidden);
|
|
}}
|
|
type="button"
|
|
className="input-action"
|
|
>
|
|
{isHidden ? <EyeOffIcon /> : <EyeIcon />}
|
|
</button>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default SensitiveInput;
|