-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.native.tsx
449 lines (412 loc) · 21.1 KB
/
index.native.tsx
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import Str from 'expensify-common/lib/str';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInput, TextInputFocusEventData, ViewStyle} from 'react-native';
import {ActivityIndicator, Animated, StyleSheet, View} from 'react-native';
import Checkbox from '@components/Checkbox';
import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import type {AnimatedTextInputRef} from '@components/RNTextInput';
import RNTextInput from '@components/RNTextInput';
import Text from '@components/Text';
import * as styleConst from '@components/TextInput/styleConst';
import TextInputClearButton from '@components/TextInput/TextInputClearButton';
import TextInputLabel from '@components/TextInput/TextInputLabel';
import useLocalize from '@hooks/useLocalize';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import getSecureEntryKeyboardType from '@libs/getSecureEntryKeyboardType';
import isInputAutoFilled from '@libs/isInputAutoFilled';
import useNativeDriver from '@libs/useNativeDriver';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {BaseTextInputProps, BaseTextInputRef} from './types';
function BaseTextInput(
{
label = '',
/**
* To be able to function as either controlled or uncontrolled component we should not
* assign a default prop value for `value` or `defaultValue` props
*/
value = undefined,
defaultValue = undefined,
placeholder = '',
errorText = '',
iconLeft = null,
icon = null,
textInputContainerStyles,
touchableInputWrapperStyle,
containerStyles,
inputStyle,
forceActiveLabel = false,
autoFocus = false,
disableKeyboard = false,
autoGrow = false,
autoGrowHeight = false,
maxAutoGrowHeight,
hideFocusedState = false,
maxLength = undefined,
hint = '',
onInputChange = () => {},
shouldDelayFocus = false,
multiline = false,
autoCorrect = true,
prefixCharacter = '',
inputID,
isMarkdownEnabled = false,
shouldShowClearButton = false,
prefixContainerStyle = [],
prefixStyle = [],
...props
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput;
const inputProps = {shouldSaveDraft: false, shouldUseDefaultValue: false, ...props};
const theme = useTheme();
const styles = useThemeStyles();
const markdownStyle = useMarkdownStyle();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const {hasError = false} = inputProps;
// Disabling this line for safeness as nullish coalescing works only if the value is undefined or null
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const initialValue = value || defaultValue || '';
const initialActiveLabel = !!forceActiveLabel || initialValue.length > 0 || !!prefixCharacter;
const isMultiline = multiline || autoGrowHeight;
const [isFocused, setIsFocused] = useState(false);
const [passwordHidden, setPasswordHidden] = useState(inputProps.secureTextEntry);
const [textInputWidth, setTextInputWidth] = useState(0);
const [textInputHeight, setTextInputHeight] = useState(0);
const [height, setHeight] = useState<number>(variables.componentSizeLarge);
const [width, setWidth] = useState<number | null>(null);
const labelScale = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE)).current;
const labelTranslateY = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y)).current;
const input = useRef<TextInput | null>(null);
const isLabelActive = useRef(initialActiveLabel);
// AutoFocus which only works on mount:
useEffect(() => {
// We are manually managing focus to prevent this issue: https://github.com/Expensify/App/issues/4514
if (!autoFocus || !input.current) {
return;
}
if (shouldDelayFocus) {
const focusTimeout = setTimeout(() => input.current?.focus(), CONST.ANIMATED_TRANSITION);
return () => clearTimeout(focusTimeout);
}
input.current.focus();
// We only want this to run on mount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const animateLabel = useCallback(
(translateY: number, scale: number) => {
Animated.parallel([
Animated.spring(labelTranslateY, {
toValue: translateY,
useNativeDriver,
}),
Animated.spring(labelScale, {
toValue: scale,
useNativeDriver,
}),
]).start();
},
[labelScale, labelTranslateY],
);
const activateLabel = useCallback(() => {
const inputValue = value ?? '';
if (inputValue.length < 0 || isLabelActive.current) {
return;
}
animateLabel(styleConst.ACTIVE_LABEL_TRANSLATE_Y, styleConst.ACTIVE_LABEL_SCALE);
isLabelActive.current = true;
}, [animateLabel, value]);
const deactivateLabel = useCallback(() => {
const inputValue = value ?? '';
if (!!forceActiveLabel || inputValue.length !== 0 || prefixCharacter) {
return;
}
animateLabel(styleConst.INACTIVE_LABEL_TRANSLATE_Y, styleConst.INACTIVE_LABEL_SCALE);
isLabelActive.current = false;
}, [animateLabel, forceActiveLabel, prefixCharacter, value]);
const onFocus = (event: NativeSyntheticEvent<TextInputFocusEventData>) => {
inputProps.onFocus?.(event);
setIsFocused(true);
};
const onBlur = (event: NativeSyntheticEvent<TextInputFocusEventData>) => {
inputProps.onBlur?.(event);
setIsFocused(false);
};
const onPress = (event?: GestureResponderEvent | KeyboardEvent) => {
if (!!inputProps.disabled || !event) {
return;
}
inputProps.onPress?.(event);
if ('isDefaultPrevented' in event && !event?.isDefaultPrevented()) {
input.current?.focus();
}
};
const onLayout = useCallback(
(event: LayoutChangeEvent) => {
if (!autoGrowHeight && multiline) {
return;
}
const layout = event.nativeEvent.layout;
setWidth((prevWidth: number | null) => (autoGrowHeight ? layout.width : prevWidth));
setHeight((prevHeight: number) => (!multiline ? layout.height : prevHeight));
},
[autoGrowHeight, multiline],
);
// The ref is needed when the component is uncontrolled and we don't have a value prop
const hasValueRef = useRef(initialValue.length > 0);
const inputValue = value ?? '';
const hasValue = inputValue.length > 0 || hasValueRef.current;
// Activate or deactivate the label when either focus changes, or for controlled
// components when the value prop changes:
useEffect(() => {
if (
hasValue ||
isFocused ||
// 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, keep the label activated.
isInputAutoFilled(input.current)
) {
activateLabel();
} else {
deactivateLabel();
}
}, [activateLabel, deactivateLabel, hasValue, isFocused]);
// When the value prop gets cleared externally, we need to keep the ref in sync:
useEffect(() => {
// Return early when component uncontrolled, or we still have a value
if (value === undefined || value) {
return;
}
hasValueRef.current = false;
}, [value]);
/**
* Set Value & activateLabel
*/
const setValue = (newValue: string) => {
const formattedValue = isMultiline ? newValue : newValue.replace(/\n/g, ' ');
onInputChange?.(formattedValue);
if (inputProps.onChangeText) {
Str.result(inputProps.onChangeText, formattedValue);
}
if (formattedValue && formattedValue.length > 0) {
hasValueRef.current = true;
// When the component is uncontrolled, we need to manually activate the label:
if (value === undefined) {
activateLabel();
}
} else {
hasValueRef.current = false;
}
};
const togglePasswordVisibility = useCallback(() => {
setPasswordHidden((prevPasswordHidden) => !prevPasswordHidden);
}, []);
const hasLabel = Boolean(label?.length);
const isReadOnly = inputProps.readOnly ?? inputProps.disabled;
// Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and errorText can be an empty string
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const inputHelpText = errorText || hint;
const placeholderValue = !!prefixCharacter || isFocused || !hasLabel || (hasLabel && forceActiveLabel) ? placeholder : undefined;
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
autoGrow && StyleUtils.getWidthStyle(textInputWidth),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
]);
return (
<>
<View style={[containerStyles]}>
<PressableWithoutFeedback
role={CONST.ROLE.PRESENTATION}
onPress={onPress}
tabIndex={-1}
// 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={onLayout}
accessibilityLabel={label}
style={[
autoGrowHeight && styles.autoGrowHeightInputContainer(textInputHeight, variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0),
!isMultiline && styles.componentHeightLarge,
touchableInputWrapperStyle,
]}
>
<View
style={[
newTextInputContainerStyles,
// When autoGrow is on and minWidth is not supplied, add a minWidth to allow the input to be focusable.
autoGrow && !newTextInputContainerStyles?.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, styles.pointerEventsNone]} />}
<TextInputLabel
isLabelActive={isLabelActive.current}
label={label}
labelTranslateY={labelTranslateY}
labelScale={labelScale}
for={inputProps.nativeID}
/>
</>
) : null}
<View style={[styles.textInputAndIconContainer, isMultiline && hasLabel && styles.textInputMultilineContainer, styles.pointerEventsBoxNone]}>
{iconLeft && (
<View style={styles.textInputLeftIconContainer}>
<Icon
src={iconLeft}
fill={theme.icon}
height={20}
width={20}
/>
</View>
)}
{!!prefixCharacter && (
<View style={[styles.textInputPrefixWrapper, prefixContainerStyle]}>
<Text
tabIndex={-1}
style={[styles.textInputPrefix, !hasLabel && styles.pv0, styles.pointerEventsNone, prefixStyle]}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
>
{prefixCharacter}
</Text>
</View>
)}
<InputComponent
ref={(element: AnimatedTextInputRef | AnimatedMarkdownTextInputRef | null): void => {
const baseTextInputRef = element as BaseTextInputRef | null;
if (typeof ref === 'function') {
ref(baseTextInputRef);
} else if (ref && 'current' in ref) {
// eslint-disable-next-line no-param-reassign
ref.current = baseTextInputRef;
}
input.current = element;
}}
// eslint-disable-next-line
{...inputProps}
autoCorrect={inputProps.secureTextEntry ? false : autoCorrect}
placeholder={placeholderValue}
placeholderTextColor={theme.placeholderText}
underlineColorAndroid="transparent"
style={[
styles.flex1,
styles.w100,
inputStyle,
(!hasLabel || isMultiline) && styles.pv0,
!!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft),
inputProps.secureTextEntry && styles.secureInput,
!isMultiline && {height, lineHeight: undefined},
// Stop scrollbar flashing when breaking lines with autoGrowHeight enabled.
...(autoGrowHeight
? [StyleUtils.getAutoGrowHeightInputStyle(textInputHeight, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0), styles.verticalAlignTop]
: []),
// Add disabled color theme when field is not editable.
inputProps.disabled && styles.textInputDisabled,
styles.pointerEventsAuto,
]}
multiline={isMultiline}
maxLength={maxLength}
onFocus={onFocus}
onBlur={onBlur}
onChangeText={setValue}
secureTextEntry={passwordHidden}
onPressOut={inputProps.onPress}
showSoftInputOnFocus={!disableKeyboard}
keyboardType={getSecureEntryKeyboardType(inputProps.keyboardType, inputProps.secureTextEntry ?? false, passwordHidden ?? false)}
inputMode={!disableKeyboard ? inputProps.inputMode : CONST.INPUT_MODE.NONE}
value={value}
selection={inputProps.selection}
readOnly={isReadOnly}
defaultValue={defaultValue}
markdownStyle={markdownStyle}
/>
{isFocused && !isReadOnly && shouldShowClearButton && value && <TextInputClearButton onPressButton={() => setValue('')} />}
{inputProps.isLoading && (
<ActivityIndicator
size="small"
color={theme.iconSuccessFill}
style={[styles.mt4, styles.ml1]}
/>
)}
{!!inputProps.secureTextEntry && (
<Checkbox
style={[styles.flex1, styles.textInputIconContainer]}
onPress={togglePasswordVisibility}
onMouseDown={(event) => {
event.preventDefault();
}}
accessibilityLabel={translate('common.visible')}
>
<Icon
src={passwordHidden ? Expensicons.Eye : Expensicons.EyeDisabled}
fill={theme.icon}
/>
</Checkbox>
)}
{!inputProps.secureTextEntry && icon && (
<View style={[styles.textInputIconContainer, !isReadOnly ? styles.cursorPointer : styles.pointerEventsNone]}>
<Icon
src={icon}
fill={theme.icon}
/>
</View>
)}
</View>
</View>
</PressableWithoutFeedback>
{!!inputHelpText && (
<FormHelpMessage
isError={!!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.
*/}
{(!!autoGrow || autoGrowHeight) && (
// Add +2 to width on Safari browsers so that text is not cut off due to the cursor or when changing the value
// https://github.com/Expensify/App/issues/8158
// https://github.com/Expensify/App/issues/26628
<Text
style={[
inputStyle,
autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined),
styles.hiddenElementOutsideOfWindow,
styles.visibilityHidden,
]}
onLayout={(e) => {
if (e.nativeEvent.layout.width === 0 && e.nativeEvent.layout.height === 0) {
return;
}
setTextInputWidth(e.nativeEvent.layout.width);
setTextInputHeight(e.nativeEvent.layout.height);
}}
>
{/* \u200B added to solve the issue of not expanding the text input enough when the value ends with '\n' (https://github.com/Expensify/App/issues/21271) */}
{value ? `${value}${value.endsWith('\n') ? '\u200B' : ''}` : placeholder}
</Text>
)}
</>
);
}
BaseTextInput.displayName = 'BaseTextInput';
export default forwardRef(BaseTextInput);