forked from rajun/ar-test
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { useState } from 'react';
|
|
import { Lock } from 'lucide-react';
|
|
import { Button } from '~/components/ui/button';
|
|
import { IconInput } from '~/components/ui/icon-input';
|
|
import { InlineMessage } from '~/components/ui/inline-message';
|
|
import type { InputProps } from '~/components/ui/input';
|
|
|
|
export function PasswordInput(props: Omit<InputProps, 'type'>) {
|
|
const [visible, setVisible] = useState(false);
|
|
const [capsLock, setCapsLock] = useState(false);
|
|
|
|
return (
|
|
<>
|
|
<IconInput
|
|
icon={Lock}
|
|
type={visible ? 'text' : 'password'}
|
|
onKeyUp={(e) => setCapsLock(e.getModifierState('CapsLock'))}
|
|
trailing={
|
|
<Button
|
|
type='button'
|
|
variant='ghost'
|
|
size='sm'
|
|
aria-label={visible ? 'Hide password' : 'Show password'}
|
|
aria-pressed={visible}
|
|
onClick={() => setVisible((v) => !v)}
|
|
>
|
|
{visible ? 'Hide' : 'Show'}
|
|
</Button>
|
|
}
|
|
{...props}
|
|
onBlur={(e) => {
|
|
setCapsLock(false);
|
|
props.onBlur?.(e);
|
|
}}
|
|
/>
|
|
{capsLock && <InlineMessage tone='warning'>Caps Lock is on</InlineMessage>}
|
|
</>
|
|
);
|
|
}
|