Files
jellyseerr/src/components/Common/SensitiveInput/index.tsx
renovate[bot] dd48d59b20 fix(deps): update dependency @heroicons/react to v2 (#2970)
* fix(deps): update dependency @heroicons/react to v2

* fix: update imports and fix icon name changes for heroicons

* fix: also update MiniStatusBadge to use new check icon

* fix: update last place with old import

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: sct <ryan@sct.dev>
2023-01-04 01:06:02 +00:00

53 lines
1.3 KiB
TypeScript

import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/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 ? <EyeSlashIcon /> : <EyeIcon />}
</button>
</>
);
};
export default SensitiveInput;