-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseTextInput.js
355 lines (315 loc) · 16 KB
/
BaseTextInput.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import _ from 'underscore';
import React, {Component} from 'react';
import {
Animated, View, TouchableWithoutFeedback, AppState, Keyboard,
} from 'react-native';
import Str from 'expensify-common/lib/str';
import RNTextInput from '../RNTextInput';
import TextInputLabel from './TextInputLabel';
import * as baseTextInputPropTypes from './baseTextInputPropTypes';
import themeColors from '../../styles/themes/default';
import styles from '../../styles/styles';
import Icon from '../Icon';
import * as Expensicons from '../Icon/Expensicons';
import Text from '../Text';
import * as styleConst from './styleConst';
import * as StyleUtils from '../../styles/StyleUtils';
import variables from '../../styles/variables';
import Checkbox from '../Checkbox';
import getSecureEntryKeyboardType from '../../libs/getSecureEntryKeyboardType';
import CONST from '../../CONST';
import FormHelpMessage from '../FormHelpMessage';
class BaseTextInput extends Component {
constructor(props) {
super(props);
const value = props.value || props.defaultValue || '';
const activeLabel = props.forceActiveLabel || value.length > 0 || props.prefixCharacter;
this.state = {
isFocused: false,
labelTranslateY: new Animated.Value(activeLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y),
labelScale: new Animated.Value(activeLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE),
passwordHidden: props.secureTextEntry,
textInputWidth: 0,
prefixWidth: 0,
selection: props.selection,
height: variables.componentSizeLarge,
// Value should be kept in state for the autoGrow feature to work - https://github.com/Expensify/App/pull/8232#issuecomment-1077282006
value,
};
this.input = null;
this.isLabelActive = activeLabel;
this.onPress = this.onPress.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
this.setValue = this.setValue.bind(this);
this.togglePasswordVisibility = this.togglePasswordVisibility.bind(this);
this.dismissKeyboardWhenBackgrounded = this.dismissKeyboardWhenBackgrounded.bind(this);
this.storePrefixLayoutDimensions = this.storePrefixLayoutDimensions.bind(this);
}
componentDidMount() {
if (this.props.disableKeyboard) {
this.appStateSubscription = AppState.addEventListener(
'change',
this.dismissKeyboardWhenBackgrounded,
);
}
// We are manually managing focus to prevent this issue: https://github.com/Expensify/App/issues/4514
if (!this.props.autoFocus || !this.input) {
return;
}
if (this.props.shouldDelayFocus) {
this.focusTimeout = setTimeout(() => this.input.focus(), CONST.ANIMATED_TRANSITION);
return;
}
this.input.focus();
}
componentDidUpdate(prevProps) {
// Activate or deactivate the label when value is changed programmatically from outside
const inputValue = _.isUndefined(this.props.value) ? this.input.value : this.props.value;
if ((_.isUndefined(inputValue) || this.state.value === inputValue) && _.isEqual(prevProps.selection, this.props.selection)) {
return;
}
// eslint-disable-next-line react/no-did-update-set-state
this.setState({value: inputValue, selection: this.props.selection});
// In some cases, When the value prop is empty, it is not properly updated on the TextInput due to its uncontrolled nature, thus manually clearing the TextInput.
if (inputValue === '') {
this.input.clear();
}
if (inputValue) {
this.activateLabel();
} else if (!this.state.isFocused) {
this.deactivateLabel();
}
}
componentWillUnmount() {
if (this.focusTimeout) {
clearTimeout(this.focusTimeout);
}
if (!this.props.disableKeyboard || !this.appStateSubscription) {
return;
}
this.appStateSubscription.remove();
}
onPress(event) {
if (this.props.disabled) {
return;
}
if (this.props.onPress) {
this.props.onPress(event);
}
if (!event.isDefaultPrevented()) {
this.input.focus();
}
}
onFocus(event) {
if (this.props.onFocus) { this.props.onFocus(event); }
this.setState({isFocused: true});
this.activateLabel();
}
onBlur(event) {
if (this.props.onBlur) { this.props.onBlur(event); }
this.setState({isFocused: false});
this.deactivateLabel();
}
/**
* Set Value & activateLabel
*
* @param {String} value
* @memberof BaseTextInput
*/
setValue(value) {
if (this.props.onInputChange) {
this.props.onInputChange(value);
}
this.setState({value});
Str.result(this.props.onChangeText, value);
this.activateLabel();
}
activateLabel() {
if (this.state.value.length < 0 || this.isLabelActive) {
return;
}
this.animateLabel(
styleConst.ACTIVE_LABEL_TRANSLATE_Y,
styleConst.ACTIVE_LABEL_SCALE,
);
this.isLabelActive = true;
}
deactivateLabel() {
if (this.props.forceActiveLabel || this.state.value.length !== 0 || this.props.prefixCharacter) {
return;
}
this.animateLabel(styleConst.INACTIVE_LABEL_TRANSLATE_Y, styleConst.INACTIVE_LABEL_SCALE);
this.isLabelActive = false;
}
dismissKeyboardWhenBackgrounded(nextAppState) {
if (!nextAppState.match(/inactive|background/)) {
return;
}
Keyboard.dismiss();
}
animateLabel(translateY, scale) {
Animated.parallel([
Animated.spring(this.state.labelTranslateY, {
toValue: translateY,
duration: 80,
useNativeDriver: true,
}),
Animated.spring(this.state.labelScale, {
toValue: scale,
duration: 80,
useNativeDriver: true,
}),
]).start();
}
togglePasswordVisibility() {
this.setState(prevState => ({passwordHidden: !prevState.passwordHidden}));
}
storePrefixLayoutDimensions(event) {
this.setState({prefixWidth: Math.abs(event.nativeEvent.layout.width)});
}
render() {
// eslint-disable-next-line react/forbid-foreign-prop-types
const inputProps = _.omit(this.props, _.keys(baseTextInputPropTypes.propTypes));
const hasLabel = Boolean(this.props.label.length);
const inputHelpText = this.props.errorText || this.props.hint;
const placeholder = (this.props.prefixCharacter || this.state.isFocused || !hasLabel || (hasLabel && this.props.forceActiveLabel)) ? this.props.placeholder : null;
const textInputContainerStyles = _.reduce([
styles.textInputContainer,
...this.props.textInputContainerStyles,
this.props.autoGrow && StyleUtils.getWidthStyle(this.state.textInputWidth),
!this.props.hideFocusedState && this.state.isFocused && styles.borderColorFocus,
(this.props.hasError || this.props.errorText) && styles.borderColorDanger,
], (finalStyles, s) => ({...finalStyles, ...s}), {});
return (
<>
<View>
<View
style={[
!this.props.multiline && styles.componentHeightLarge,
...this.props.containerStyles,
]}
>
<TouchableWithoutFeedback onPress={this.onPress} focusable={false}>
<View
// When multiline is not supplied, calculating textinput height using onLayout
onLayout={event => !this.props.multiline && this.setState({height: event.nativeEvent.layout.height})}
style={[
textInputContainerStyles,
// When autoGrow is on and minWidth is not supplied, add a minWidth to allow the input to be focusable.
this.props.autoGrow && !textInputContainerStyles.minWidth && styles.mnw2,
]}
>
{hasLabel ? (
<>
{/* Adding this background to the label only for multiline text input,
to prevent text overlapping with label when scrolling */}
{this.props.multiline && <View style={styles.textInputLabelBackground} pointerEvents="none" />}
<TextInputLabel
label={this.props.label}
labelTranslateY={this.state.labelTranslateY}
labelScale={this.state.labelScale}
for={this.props.nativeID}
/>
</>
) : null}
<View
style={[
styles.textInputAndIconContainer,
(this.props.multiline && hasLabel) && styles.textInputMultilineContainer,
]}
pointerEvents="box-none"
>
{Boolean(this.props.prefixCharacter) && (
<Text
pointerEvents="none"
selectable={false}
style={[
styles.textInputPrefix,
!hasLabel && styles.pv0,
]}
onLayout={this.storePrefixLayoutDimensions}
>
{this.props.prefixCharacter}
</Text>
)}
<RNTextInput
ref={(ref) => {
if (typeof this.props.innerRef === 'function') { this.props.innerRef(ref); }
this.input = ref;
}}
// eslint-disable-next-line
{...inputProps}
autoCorrect={this.props.secureTextEntry ? false : this.props.autoCorrect}
placeholder={placeholder}
placeholderTextColor={themeColors.placeholderText}
underlineColorAndroid="transparent"
style={[
styles.flex1,
styles.w100,
this.props.inputStyle,
(!hasLabel || this.props.multiline) && styles.pv0,
this.props.prefixCharacter && StyleUtils.getPaddingLeft(this.state.prefixWidth + styles.pl1.paddingLeft),
this.props.secureTextEntry && styles.secureInput,
// Explicitly remove `lineHeight` from single line inputs so that long text doesn't disappear
// once it exceeds the input space (See https://github.com/Expensify/App/issues/13802)
!this.props.multiline && {height: this.state.height, lineHeight: undefined},
]}
multiline={this.props.multiline}
maxLength={this.props.maxLength}
onFocus={this.onFocus}
onBlur={this.onBlur}
onChangeText={this.setValue}
secureTextEntry={this.state.passwordHidden}
onPressOut={this.props.onPress}
showSoftInputOnFocus={!this.props.disableKeyboard}
keyboardType={getSecureEntryKeyboardType(this.props.keyboardType, this.props.secureTextEntry, this.state.passwordHidden)}
value={this.state.value}
selection={this.state.selection}
editable={_.isUndefined(this.props.editable) ? !this.props.disabled : this.props.editable}
// FormSubmit Enter key handler does not have access to direct props.
// `dataset.submitOnEnter` is used to indicate that pressing Enter on this input should call the submit callback.
dataSet={{submitOnEnter: this.props.multiline && this.props.submitOnEnter}}
/>
{this.props.secureTextEntry && (
<Checkbox
style={styles.secureInputShowPasswordButton}
onPress={this.togglePasswordVisibility}
onMouseDown={e => e.preventDefault()}
>
<Icon
src={this.state.passwordHidden ? Expensicons.Eye : Expensicons.EyeDisabled}
fill={themeColors.icon}
/>
</Checkbox>
)}
</View>
</View>
</TouchableWithoutFeedback>
</View>
{!_.isEmpty(inputHelpText) && (
<FormHelpMessage isError={!_.isEmpty(this.props.errorText)} message={inputHelpText} />
)}
</View>
{/*
Text input component doesn't support auto grow by default.
We're using a hidden text input to achieve that.
This text view is used to calculate width of the input value given textStyle in this component.
This Text component is intentionally positioned out of the screen.
*/}
{this.props.autoGrow && (
// Add +2 to width so that the first digit of amount do not cut off on mWeb - https://github.com/Expensify/App/issues/8158.
<Text
style={[...this.props.inputStyle, styles.hiddenElementOutsideOfWindow, styles.visibilityHidden]}
onLayout={e => this.setState({textInputWidth: e.nativeEvent.layout.width + 2})}
>
{this.state.value || this.props.placeholder}
</Text>
)}
</>
);
}
}
BaseTextInput.propTypes = baseTextInputPropTypes.propTypes;
BaseTextInput.defaultProps = baseTextInputPropTypes.defaultProps;
export default BaseTextInput;