-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PDFPasswordForm.js
153 lines (135 loc) · 5.15 KB
/
PDFPasswordForm.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
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
import _ from 'underscore';
import React, {useState, useRef, useEffect, useMemo} from 'react';
import PropTypes from 'prop-types';
import {View, ScrollView} from 'react-native';
import Button from '../Button';
import Text from '../Text';
import TextInput from '../TextInput';
import styles from '../../styles/styles';
import PDFInfoMessage from './PDFInfoMessage';
import shouldDelayFocus from '../../libs/shouldDelayFocus';
import * as Browser from '../../libs/Browser';
import CONST from '../../CONST';
import useWindowDimensions from '../../hooks/useWindowDimensions';
import useLocalize from '../../hooks/useLocalize';
const propTypes = {
/** If the submitted password is invalid (show an error message) */
isPasswordInvalid: PropTypes.bool,
/** If loading indicator should be shown */
shouldShowLoadingIndicator: PropTypes.bool,
/** Notify parent that the password form has been submitted */
onSubmit: PropTypes.func,
/** Notify parent that the password has been updated/edited */
onPasswordUpdated: PropTypes.func,
/** Notify parent that a text field has been focused or blurred */
onPasswordFieldFocused: PropTypes.func,
/** Should focus to the password input */
isFocused: PropTypes.bool.isRequired,
};
const defaultProps = {
isPasswordInvalid: false,
shouldShowLoadingIndicator: false,
onSubmit: () => {},
onPasswordUpdated: () => {},
onPasswordFieldFocused: () => {},
};
function PDFPasswordForm({isFocused, isPasswordInvalid, shouldShowLoadingIndicator, onSubmit, onPasswordUpdated, onPasswordFieldFocused}) {
const {isSmallScreenWidth} = useWindowDimensions();
const {translate} = useLocalize();
const [password, setPassword] = useState('');
const [validationErrorText, setValidationErrorText] = useState('');
const [shouldShowForm, setShouldShowForm] = useState(false);
const textInputRef = useRef(null);
const errorText = useMemo(() => {
if (isPasswordInvalid) {
return translate('attachmentView.passwordIncorrect');
}
if (!_.isEmpty(validationErrorText)) {
return translate(validationErrorText);
}
return '';
}, [isPasswordInvalid, translate, validationErrorText]);
useEffect(() => {
if (!isFocused) {
return;
}
if (!textInputRef.current) {
return;
}
textInputRef.current.focus();
}, [isFocused]);
const updatePassword = (newPassword) => {
onPasswordUpdated(newPassword);
if (!_.isEmpty(newPassword) && validationErrorText) {
setValidationErrorText('');
}
setPassword(newPassword);
};
const validate = () => {
if (!isPasswordInvalid && !_.isEmpty(password)) {
return true;
}
if (_.isEmpty(password)) {
setValidationErrorText('attachmentView.passwordRequired');
}
return false;
};
const submitPassword = () => {
if (!validate()) {
return;
}
onSubmit(password);
};
const validateAndNotifyPasswordBlur = () => {
validate();
onPasswordFieldFocused(false);
};
return shouldShowForm ? (
<ScrollView
keyboardShouldPersistTaps="handled"
style={styles.getPDFPasswordFormStyle(isSmallScreenWidth)}
contentContainerStyle={styles.p5}
>
<View style={styles.mb4}>
<Text>{translate('attachmentView.pdfPasswordForm.formLabel')}</Text>
</View>
<TextInput
ref={textInputRef}
label={translate('common.password')}
accessibilityLabel={translate('common.password')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
/**
* This is a workaround to bypass Safari's autofill odd behaviour.
* This tricks the browser not to fill the username somewhere else and still fill the password correctly.
*/
autoComplete={Browser.getBrowser() === CONST.BROWSER.SAFARI ? 'username' : 'off'}
autoCorrect={false}
textContentType="password"
onChangeText={updatePassword}
returnKeyType="done"
onSubmitEditing={submitPassword}
errorText={errorText}
onFocus={() => onPasswordFieldFocused(true)}
onBlur={validateAndNotifyPasswordBlur}
autoFocus
shouldDelayFocus={shouldDelayFocus}
secureTextEntry
/>
<Button
text={translate('common.confirm')}
onPress={submitPassword}
style={styles.mt4}
isLoading={shouldShowLoadingIndicator}
pressOnEnter
/>
</ScrollView>
) : (
<View style={[styles.flex1, styles.justifyContentCenter]}>
<PDFInfoMessage onShowForm={() => setShouldShowForm(true)} />
</View>
);
}
PDFPasswordForm.propTypes = propTypes;
PDFPasswordForm.defaultProps = defaultProps;
PDFPasswordForm.displayName = 'PDFPasswordForm';
export default PDFPasswordForm;