File size: 1,661 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Button } from '@wordpress/components';
import { seen, unseen } from '@wordpress/icons';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { createRef, Component } from 'react';
import FormTextInput from 'calypso/components/forms/form-text-input';

import './style.scss';

class FormPasswordInput extends Component {
	static displayName = 'FormPasswordInput';

	textFieldRef = createRef();

	constructor( props ) {
		super( props );
		this.state = { hidePassword: true };
	}

	togglePasswordVisibility = () => {
		this.setState( { hidePassword: ! this.state.hidePassword } );
	};

	getIsValueHidden() {
		if ( this.props.hideToggle ) {
			return true;
		}

		return this.props.submitting || this.state.hidePassword;
	}

	focus = () => {
		this.textFieldRef.current.focus();
	};

	render() {
		const { hideToggle, submitting, isHidden, locale, translate, ...rest } = this.props;
		const isValueHidden = this.getIsValueHidden();

		return (
			<div className="form-password-input">
				<FormTextInput
					autoComplete="off"
					{ ...rest }
					ref={ this.textFieldRef }
					type={ isValueHidden ? 'password' : 'text' }
				/>

				<Button
					className={ clsx( {
						'form-password-input__toggle': true,
						'form-password-input__toggle-visibility': ! hideToggle,
					} ) }
					onClick={ this.togglePasswordVisibility }
					aria-hidden={ isHidden }
					tabIndex={ isHidden ? -1 : undefined }
					size="small"
					icon={ isValueHidden ? unseen : seen }
					label={ isValueHidden ? translate( 'Show password' ) : translate( 'Hide password' ) }
				/>
			</div>
		);
	}
}

export default localize( FormPasswordInput );