-
Notifications
You must be signed in to change notification settings - Fork 3k
/
NewPasswordForm.js
98 lines (83 loc) · 2.9 KB
/
NewPasswordForm.js
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
import React from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import Text from '../../components/Text';
import withLocalize, {
withLocalizePropTypes,
} from '../../components/withLocalize';
import CONST from '../../CONST';
import styles from '../../styles/styles';
import TextInput from '../../components/TextInput';
const propTypes = {
/** String to control the first password box in the form */
password: PropTypes.string.isRequired,
/** Function to update the first password box in the form */
updatePassword: PropTypes.func.isRequired,
/** Callback function called with boolean value for if the password form is valid */
updateIsFormValid: PropTypes.func.isRequired,
/** Callback function for when form is submitted */
onSubmitEditing: PropTypes.func.isRequired,
...withLocalizePropTypes,
};
class NewPasswordForm extends React.Component {
constructor(props) {
super(props);
this.state = {
passwordHintError: false,
};
}
componentDidUpdate(prevProps) {
const passwordChanged = (this.props.password !== prevProps.password);
if (passwordChanged) {
this.props.updateIsFormValid(this.isValidForm());
}
}
onBlurNewPassword() {
if (this.state.passwordHintError) {
return;
}
if (this.props.password && !this.isValidPassword()) {
this.setState({passwordHintError: true});
}
}
isValidPassword() {
return this.props.password.match(CONST.PASSWORD_COMPLEXITY_REGEX_STRING);
}
/**
* checks if the password invalid
* @returns {Boolean}
*/
isInvalidPassword() {
return this.state.passwordHintError && this.props.password && !this.isValidPassword();
}
isValidForm() {
return this.isValidPassword();
}
render() {
return (
<View style={styles.mb6}>
<TextInput
label={`${this.props.translate('setPasswordPage.enterPassword')}`}
secureTextEntry
autoCompleteType="password"
textContentType="password"
value={this.props.password}
onChangeText={password => this.props.updatePassword(password)}
onBlur={() => this.onBlurNewPassword()}
onSubmitEditing={() => this.props.onSubmitEditing()}
/>
<Text
style={[
styles.formHelp,
styles.mt1,
this.isInvalidPassword() && styles.formError,
]}
>
{this.props.translate('setPasswordPage.newPasswordPrompt')}
</Text>
</View>
);
}
}
NewPasswordForm.propTypes = propTypes;
export default withLocalize(NewPasswordForm);