-
Notifications
You must be signed in to change notification settings - Fork 19
/
PasswordInput.jsx
156 lines (144 loc) · 3.92 KB
/
PasswordInput.jsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from "react";
import PropTypes from "prop-types";
import { ShallowComponent, Application } from "robe-react-commons";
import { InputGroup } from "react-bootstrap";
import zxcvbn from 'zxcvbn';
import Input from "./BaseInput";
/**
* An input element for passwords.
* Password wrapper for BaseInput
* @export
* @class PasswordInput
* @extends {ShallowComponent}
*/
export default class PasswordInput extends ShallowComponent {
/**
* Properties of the component
*
* @static
*/
static propTypes = {
/**
* Label for the form control.
*/
label: PropTypes.string,
/**
* name use as input field name
*/
name: PropTypes.string,
/**
* Value of the component
*/
value: PropTypes.any.isRequired,
/**
* onChange event for the component
*/
onChange: PropTypes.func,
/**
* Disable input
*/
disabled: PropTypes.bool,
/**
* it specifies that an input field is read-only
*/
readOnly: PropTypes.bool,
/**
* it specifies that an input field is hidden or visible
*/
hidden: PropTypes.bool,
/**
*Defines the display style of the Validation message.
*/
validationDisplay: PropTypes.oneOf(["overlay", "block"]),
/**
* Left Input Addon
*/
inputGroupLeft: PropTypes.any,
/**
* Right Input Addon
*/
inputGroupRight: PropTypes.any,
/**
* it specifies that a password strength is hidden or visible
*/
strength: PropTypes.bool
};
innerComponent;
strengthMessages;
constructor(props: Object) {
super(props);
this.strengthMessages = Application.i18n(PasswordInput, "inputs.PasswordInput", "strength");
}
/**
* defaultProps
* @static
*/
static defaultProps = {
disabled: false,
readOnly: false,
hidden: false,
strength: false,
autoFocus: false,
value: "",
validationDisplay: "block"
};
/**
* render
* @returns
*/
render(): Object {
let {strength, inputGroupRight, autoFocus, ...newProps} = this.props;
if (strength) {
let value = this.props.value;
let result = zxcvbn(value);
let resultScore = this.strengthMessages[result.score];
let inputGroupRightStrength = <InputGroup.Addon key="password-strength-addon">{resultScore}</InputGroup.Addon>;
if (inputGroupRight) {
inputGroupRight = [inputGroupRightStrength,inputGroupRight];
} else {
inputGroupRight = inputGroupRightStrength;
}
autoFocus = true;
}
return (
<Input
{...newProps}
autoFocus={autoFocus}
onChange={this.__onChange}
type="password"
ref={(component: Object) => { this.innerComponent = component }}
inputGroupRight={inputGroupRight}
/>
);
}
/**
* Returns validity of the component.
* @return true if it is valid.
*/
isValid(): boolean {
return this.innerComponent.isValid();
}
/**
* checks validation by current value
* isValid then return empty Array else return Array<String>
* isValid = Array.length != 0
* @param value
*/
validate(value: any): Array<string> {
return this.innerComponent.validate(value);
}
/**
* Internal onchange handler.
*/
__onChange(e: Object): boolean {
let result = true;
if (this.props.onChange) {
result = this.props.onChange(e);
}
if (!result) {
e.preventDefault();
e.stopPropagation();
}
return result;
}
}