2024-10-26 22:06:09 +02:00
|
|
|
import * as React from 'react';
|
2021-11-15 14:02:59 +02:00
|
|
|
import { useState, useCallback } from 'react';
|
|
|
|
import StyledInput from '../style/StyledInput';
|
2024-10-26 22:06:09 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import { ChangeEventHandler } from './types';
|
2021-11-15 14:02:59 +02:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
value: string;
|
2024-10-26 22:06:09 +02:00
|
|
|
inputId: string;
|
2021-11-15 14:02:59 +02:00
|
|
|
onChange: ChangeEventHandler;
|
2024-10-26 22:06:09 +02:00
|
|
|
|
|
|
|
'aria-invalid'?: boolean;
|
|
|
|
'aria-errormessage'?: string;
|
2021-11-15 14:02:59 +02:00
|
|
|
}
|
|
|
|
|
2024-10-26 22:06:09 +02:00
|
|
|
const PasswordInput = (props: Props) => {
|
2021-11-15 14:02:59 +02:00
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
|
|
|
const inputType = showPassword ? 'text' : 'password';
|
|
|
|
const icon = showPassword ? 'far fa-eye-slash' : 'far fa-eye';
|
2024-10-26 22:06:09 +02:00
|
|
|
const title = showPassword ? _('Hide password') : _('Show password');
|
2021-11-15 14:02:59 +02:00
|
|
|
|
|
|
|
const onShowPassword = useCallback(() => {
|
|
|
|
setShowPassword(current => !current);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="password-input">
|
2024-10-26 22:06:09 +02:00
|
|
|
<StyledInput
|
|
|
|
id={props.inputId}
|
|
|
|
aria-errormessage={props['aria-errormessage']}
|
|
|
|
aria-invalid={props['aria-invalid']}
|
|
|
|
className="field"
|
|
|
|
type={inputType}
|
|
|
|
value={props.value}
|
|
|
|
onChange={props.onChange}
|
|
|
|
/>
|
|
|
|
<button onClick={onShowPassword} className="showpasswordbutton">
|
|
|
|
<i className={icon} role='img' aria-label={title} title={title}></i>
|
|
|
|
</button>
|
2021-11-15 14:02:59 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2024-10-26 22:06:09 +02:00
|
|
|
|
|
|
|
export default PasswordInput;
|