-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseTextInput.js
402 lines (362 loc) · 18.8 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import _ from 'underscore';
import React, {Component} from 'react';
import {Animated, View, AppState, Keyboard, StyleSheet} 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';
import isInputAutoFilled from '../../libs/isInputAutoFilled';
import * as Pressables from '../Pressable';
const PressableWithoutFeedback = Pressables.PressableWithoutFeedback;
class BaseTextInput extends Component {
constructor(props) {
super(props);
const value = props.value || props.defaultValue || '';
const activeLabel = props.forceActiveLabel || value.length > 0 || Boolean(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});
// If the text has been supplied by Chrome autofill, the value state is not synced with the value
// as Chrome doesn't trigger a change event. When there is autofill text, don't deactivate label.
if (!isInputAutoFilled(this.input)) {
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: styleConst.LABEL_ANIMATION_DURATION,
useNativeDriver: true,
}),
Animated.spring(this.state.labelScale, {
toValue: scale,
duration: styleConst.LABEL_ANIMATION_DURATION,
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 isEditable = _.isUndefined(this.props.editable) ? !this.props.disabled : this.props.editable;
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 maxHeight = StyleSheet.flatten(this.props.containerStyles).maxHeight;
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,
this.props.autoGrowHeight && {scrollPaddingTop: 2 * maxHeight},
],
(finalStyles, s) => ({...finalStyles, ...s}),
{},
);
const isMultiline = this.props.multiline || this.props.autoGrowHeight;
return (
<>
<View>
<PressableWithoutFeedback
onPress={this.onPress}
focusable={false}
accessibilityLabel={this.props.label}
style={[
this.props.autoGrowHeight && styles.autoGrowHeightInputContainer(this.state.textInputHeight, maxHeight),
!isMultiline && styles.componentHeightLarge,
...this.props.containerStyles,
]}
>
<View
// When autoGrowHeight is true we calculate the width for the textInput, so It will break lines properly
// or if multiline is not supplied we calculate the textinput height, using onLayout.
onLayout={(event) => {
if (!this.props.autoGrowHeight && this.props.multiline) {
return;
}
const layout = event.nativeEvent.layout;
this.setState((prevState) => ({
width: this.props.autoGrowHeight ? layout.width : prevState.width,
height: !isMultiline ? layout.height : prevState.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 */}
{isMultiline && (
<View
style={styles.textInputLabelBackground}
pointerEvents="none"
/>
)}
<TextInputLabel
isLabelActive={this.isLabelActive}
label={this.props.label}
labelTranslateY={this.state.labelTranslateY}
labelScale={this.state.labelScale}
for={this.props.nativeID}
/>
</>
) : null}
<View
style={[styles.textInputAndIconContainer, isMultiline && hasLabel && styles.textInputMultilineContainer]}
pointerEvents="box-none"
>
{Boolean(this.props.prefixCharacter) && (
<View style={styles.textInputPrefixWrapper}>
<Text
pointerEvents="none"
selectable={false}
style={[styles.textInputPrefix, !hasLabel && styles.pv0]}
onLayout={this.storePrefixLayoutDimensions}
>
{this.props.prefixCharacter}
</Text>
</View>
)}
<RNTextInput
ref={(ref) => {
if (typeof this.props.innerRef === 'function') {
this.props.innerRef(ref);
} else if (this.props.innerRef && _.has(this.props.innerRef, 'current')) {
this.props.innerRef.current = 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 || isMultiline) && 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)
!isMultiline && {height: this.state.height, lineHeight: undefined},
// Stop scrollbar flashing when breaking lines with autoGrowHeight enabled.
this.props.autoGrowHeight && StyleUtils.getAutoGrowHeightInputStyle(this.state.textInputHeight, maxHeight),
]}
multiline={isMultiline}
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={isEditable}
// 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: isMultiline && this.props.submitOnEnter}}
/>
{Boolean(this.props.secureTextEntry) && (
<Checkbox
style={styles.textInputIconContainer}
onPress={this.togglePasswordVisibility}
onMouseDown={(e) => e.preventDefault()}
>
<Icon
src={this.state.passwordHidden ? Expensicons.Eye : Expensicons.EyeDisabled}
fill={themeColors.icon}
/>
</Checkbox>
)}
{!this.props.secureTextEntry && Boolean(this.props.icon) && (
<View style={[styles.textInputIconContainer, isEditable ? styles.cursorPointer : styles.pointerEventsNone]}>
<Icon
src={this.props.icon}
fill={themeColors.icon}
/>
</View>
)}
</View>
</View>
</PressableWithoutFeedback>
{!_.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 or height of the input value given textStyle in this component.
This Text component is intentionally positioned out of the screen.
*/}
{(this.props.autoGrow || this.props.autoGrowHeight) && (
// 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,
this.props.autoGrowHeight && styles.autoGrowHeightHiddenInput(this.state.width, maxHeight),
styles.hiddenElementOutsideOfWindow,
styles.visibilityHidden,
]}
onLayout={(e) => this.setState({textInputWidth: e.nativeEvent.layout.width + 2, textInputHeight: e.nativeEvent.layout.height})}
>
{this.state.value || this.props.placeholder}
</Text>
)}
</>
);
}
}
BaseTextInput.propTypes = baseTextInputPropTypes.propTypes;
BaseTextInput.defaultProps = baseTextInputPropTypes.defaultProps;
export default BaseTextInput;