1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
+ import { injectIntl } from 'react-intl' ;
3
4
import requiredIf from 'react-required-if' ;
4
5
import Constraints from '../../constraints' ;
5
6
import Spacings from '../../spacings' ;
6
7
import FieldLabel from '../../field-label' ;
7
8
import PasswordInput from '../../inputs/password-input' ;
9
+ import FlatButton from '../../buttons/flat-button' ;
10
+ import { EyeIcon , EyeCrossedIcon } from '../../icons' ;
8
11
import getFieldId from '../../../utils/get-field-id' ;
9
12
import createSequentialId from '../../../utils/create-sequential-id' ;
10
13
import FieldErrors from '../../field-errors' ;
11
14
import filterDataAttributes from '../../../utils/filter-data-attributes' ;
15
+ import messages from './messages' ;
12
16
13
17
const sequentialId = createSequentialId ( 'password-field-' ) ;
14
18
@@ -27,6 +31,9 @@ class PasswordField extends React.Component {
27
31
renderError : PropTypes . func ,
28
32
isRequired : PropTypes . bool ,
29
33
touched : PropTypes . bool ,
34
+ intl : PropTypes . shape ( {
35
+ formatMessage : PropTypes . func . isRequired ,
36
+ } ) . isRequired ,
30
37
31
38
// PasswordInput
32
39
name : PropTypes . string ,
@@ -35,7 +42,6 @@ class PasswordField extends React.Component {
35
42
onBlur : PropTypes . func ,
36
43
onFocus : PropTypes . func ,
37
44
isAutofocussed : PropTypes . bool ,
38
- isPasswordVisible : PropTypes . bool ,
39
45
isDisabled : PropTypes . bool ,
40
46
isReadOnly : PropTypes . bool ,
41
47
placeholder : PropTypes . string ,
@@ -61,6 +67,7 @@ class PasswordField extends React.Component {
61
67
// We generate an id in case no id is provided by the parent to attach the
62
68
// label to the input component.
63
69
id : this . props . id ,
70
+ isPasswordVisible : false ,
64
71
} ;
65
72
66
73
static getDerivedStateFromProps = ( props , state ) => ( {
@@ -72,16 +79,40 @@ class PasswordField extends React.Component {
72
79
return (
73
80
< Constraints . Horizontal constraint = { this . props . horizontalConstraint } >
74
81
< Spacings . Stack scale = "xs" >
75
- < FieldLabel
76
- title = { this . props . title }
77
- hint = { this . props . hint }
78
- description = { this . props . description }
79
- onInfoButtonClick = { this . props . onInfoButtonClick }
80
- hintIcon = { this . props . hintIcon }
81
- badge = { this . props . badge }
82
- hasRequiredIndicator = { this . props . isRequired }
83
- htmlFor = { this . state . id }
84
- />
82
+ < Spacings . Inline alignItems = "center" justifyContent = "space-between" >
83
+ < FieldLabel
84
+ hint = { this . props . hint }
85
+ title = { this . props . title }
86
+ badge = { this . props . badge }
87
+ htmlFor = { this . state . id }
88
+ hintIcon = { this . props . hintIcon }
89
+ description = { this . props . description }
90
+ onInfoButtonClick = { this . props . onInfoButtonClick }
91
+ hasRequiredIndicator = { this . props . isRequired }
92
+ />
93
+ { ! this . props . isDisabled && ! this . props . isReadOnly && (
94
+ < FlatButton
95
+ icon = {
96
+ this . state . isPasswordVisible ? (
97
+ < EyeCrossedIcon />
98
+ ) : (
99
+ < EyeIcon />
100
+ )
101
+ }
102
+ label = {
103
+ this . state . isPasswordVisible
104
+ ? this . props . intl . formatMessage ( messages . hide )
105
+ : this . props . intl . formatMessage ( messages . show )
106
+ }
107
+ onClick = { ( ) =>
108
+ this . setState ( prevState => ( {
109
+ isPasswordVisible : ! prevState . isPasswordVisible ,
110
+ } ) )
111
+ }
112
+ isDisabled = { ! this . props . value }
113
+ />
114
+ ) }
115
+ </ Spacings . Inline >
85
116
< PasswordInput
86
117
id = { this . state . id }
87
118
name = { this . props . name }
@@ -90,7 +121,7 @@ class PasswordField extends React.Component {
90
121
onBlur = { this . props . onBlur }
91
122
onFocus = { this . props . onFocus }
92
123
isAutofocussed = { this . props . isAutofocussed }
93
- isPasswordVisible = { this . props . isPasswordVisible }
124
+ isPasswordVisible = { this . state . isPasswordVisible }
94
125
isDisabled = { this . props . isDisabled }
95
126
isReadOnly = { this . props . isReadOnly }
96
127
hasError = { hasError }
@@ -110,4 +141,4 @@ class PasswordField extends React.Component {
110
141
}
111
142
}
112
143
113
- export default PasswordField ;
144
+ export default injectIntl ( PasswordField ) ;
0 commit comments