-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.tsx
534 lines (493 loc) · 26.1 KB
/
index.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import {Str} from 'expensify-common';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, 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 SwipeInterceptPanResponder from '@components/SwipeInterceptPanResponder';
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 * as Browser from '@libs/Browser';
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 = '',
icon = null,
iconLeft = 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,
shouldInterceptSwipe = false,
autoCorrect = true,
prefixCharacter = '',
suffixCharacter = '',
inputID,
isMarkdownEnabled = false,
excludedMarkdownStyles = [],
shouldShowClearButton = false,
shouldUseDisabledStyles = true,
prefixContainerStyle = [],
prefixStyle = [],
suffixContainerStyle = [],
suffixStyle = [],
contentWidth,
loadingSpinnerStyle,
...inputProps
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
) {
const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput;
const isAutoGrowHeightMarkdown = isMarkdownEnabled && autoGrowHeight;
const theme = useTheme();
const styles = useThemeStyles();
const markdownStyle = useMarkdownStyle(undefined, excludedMarkdownStyles);
const {hasError = false} = inputProps;
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
// Disabling this line for saftiness as nullish coalescing works only if 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 || !!suffixCharacter;
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<HTMLInputElement | 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-compiler/react-compiler, 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 newValue = value ?? '';
if (newValue.length < 0 || isLabelActive.current) {
return;
}
animateLabel(styleConst.ACTIVE_LABEL_TRANSLATE_Y, styleConst.ACTIVE_LABEL_SCALE);
isLabelActive.current = true;
}, [animateLabel, value]);
const deactivateLabel = useCallback(() => {
const newValue = value ?? '';
if (!!forceActiveLabel || newValue.length !== 0 || prefixCharacter || suffixCharacter) {
return;
}
animateLabel(styleConst.INACTIVE_LABEL_TRANSLATE_Y, styleConst.INACTIVE_LABEL_SCALE);
isLabelActive.current = false;
}, [animateLabel, forceActiveLabel, prefixCharacter, suffixCharacter, 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) => {
onInputChange?.(newValue);
if (inputProps.onChangeText) {
Str.result(inputProps.onChangeText, newValue);
}
if (newValue && newValue.length > 0) {
hasValueRef.current = true;
// When the componment is uncontrolled, we need to manually activate the label:
if (value === undefined) {
activateLabel();
}
} else {
hasValueRef.current = false;
}
};
const togglePasswordVisibility = useCallback(() => {
setPasswordHidden((prevPasswordHidden: boolean | undefined) => !prevPasswordHidden);
}, []);
const hasLabel = !!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 newPlaceholder = !!prefixCharacter || !!suffixCharacter || isFocused || !hasLabel || (hasLabel && forceActiveLabel) ? placeholder : undefined;
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
(autoGrow || !!contentWidth) && StyleUtils.getWidthStyle(textInputWidth),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
isAutoGrowHeightMarkdown && styles.pb2,
]);
const isMultiline = multiline || autoGrowHeight;
/**
* To prevent text jumping caused by virtual DOM calculations on Safari and mobile Chrome,
* make sure to include the `lineHeight`.
* Reference: https://github.com/Expensify/App/issues/26735
* For other platforms, explicitly remove `lineHeight` from single-line inputs
* to prevent long text from disappearing once it exceeds the input space.
* See https://github.com/Expensify/App/issues/13802
*/
const lineHeight = useMemo(() => {
if (Browser.isSafari() || Browser.isMobileChrome()) {
const lineHeightValue = StyleSheet.flatten(inputStyle).lineHeight;
if (lineHeightValue !== undefined) {
return lineHeightValue;
}
}
return undefined;
}, [inputStyle]);
const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft);
const inputPaddingRight = !!suffixCharacter && StyleUtils.getPaddingRight(StyleUtils.getCharacterPadding(suffixCharacter) + styles.pr1.paddingRight);
return (
<>
<View
style={[containerStyles]}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(shouldInterceptSwipe && SwipeInterceptPanResponder.panHandlers)}
>
<PressableWithoutFeedback
role={CONST.ROLE.PRESENTATION}
onPress={onPress}
tabIndex={-1}
accessibilityLabel={label}
// 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}
style={[
autoGrowHeight &&
!isAutoGrowHeightMarkdown &&
styles.autoGrowHeightInputContainer(textInputHeight, variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0),
isAutoGrowHeightMarkdown && {minHeight: variables.componentSizeLarge},
!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
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, !isReadOnly ? styles.cursorPointer : styles.pointerEventsNone]}>
<Icon
src={iconLeft}
fill={theme.icon}
height={variables.iconSizeNormal}
width={variables.iconSizeNormal}
/>
</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 as HTMLInputElement | null;
}}
// eslint-disable-next-line
{...inputProps}
autoCorrect={inputProps.secureTextEntry ? false : autoCorrect}
placeholder={newPlaceholder}
placeholderTextColor={theme.placeholderText}
underlineColorAndroid="transparent"
style={[
styles.flex1,
styles.w100,
inputStyle,
(!hasLabel || isMultiline) && styles.pv0,
inputPaddingLeft,
inputPaddingRight,
inputProps.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, lineHeight},
// Explicitly change boxSizing attribute for mobile chrome in order to apply line-height
// for the issue mentioned here https://github.com/Expensify/App/issues/26735
// Set overflow property to enable the parent flexbox to shrink its size
// (See https://github.com/Expensify/App/issues/41766)
!isMultiline && Browser.isMobileChrome() && {boxSizing: 'content-box', height: undefined, ...styles.overflowAuto},
// Stop scrollbar flashing when breaking lines with autoGrowHeight enabled.
...(autoGrowHeight && !isAutoGrowHeightMarkdown
? [StyleUtils.getAutoGrowHeightInputStyle(textInputHeight, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0), styles.verticalAlignTop]
: []),
isAutoGrowHeightMarkdown ? [StyleUtils.getMarkdownMaxHeight(maxAutoGrowHeight), styles.verticalAlignTop] : undefined,
// Add disabled color theme when field is not editable.
inputProps.disabled && shouldUseDisabledStyles && styles.textInputDisabled,
styles.pointerEventsAuto,
]}
multiline={isMultiline}
maxLength={maxLength}
onFocus={onFocus}
onBlur={onBlur}
onChangeText={setValue}
secureTextEntry={passwordHidden}
onPressOut={inputProps.onPress}
showSoftInputOnFocus={!disableKeyboard}
inputMode={inputProps.inputMode}
value={value}
selection={inputProps.selection}
readOnly={isReadOnly}
defaultValue={defaultValue}
markdownStyle={markdownStyle}
/>
{!!suffixCharacter && (
<View style={[styles.textInputSuffixWrapper, suffixContainerStyle]}>
<Text
tabIndex={-1}
style={[styles.textInputSuffix, !hasLabel && styles.pv0, styles.pointerEventsNone, suffixStyle]}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
>
{suffixCharacter}
</Text>
</View>
)}
{isFocused && !isReadOnly && shouldShowClearButton && !!value && <TextInputClearButton onPressButton={() => setValue('')} />}
{!!inputProps.isLoading && (
<ActivityIndicator
size="small"
color={theme.iconSuccessFill}
style={[styles.mt4, styles.ml1, loadingSpinnerStyle]}
/>
)}
{!!inputProps.secureTextEntry && (
<Checkbox
style={[styles.flex1, styles.textInputIconContainer]}
onPress={togglePasswordVisibility}
onMouseDown={(e) => {
e.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>
{!!contentWidth && (
<View
style={[inputStyle as ViewStyle, styles.hiddenElementOutsideOfWindow, styles.visibilityHidden, styles.wAuto, inputPaddingLeft]}
onLayout={(e) => {
if (e.nativeEvent.layout.width === 0 && e.nativeEvent.layout.height === 0) {
return;
}
setTextInputWidth(e.nativeEvent.layout.width);
setTextInputHeight(e.nativeEvent.layout.height);
}}
>
<Text
style={[
inputStyle,
autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined),
{width: contentWidth},
]}
>
{/* \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>
</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) && !isAutoGrowHeightMarkdown && (
// Add +2 to width on Safari browsers so that text is not cut off due to the cursor or when changing the value
// Reference: https://github.com/Expensify/App/issues/8158, https://github.com/Expensify/App/issues/26628
// For mobile Chrome, ensure proper display of the text selection handle (blue bubble down).
// Reference: https://github.com/Expensify/App/issues/34921
<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;
}
let additionalWidth = 0;
if (Browser.isMobileSafari() || Browser.isSafari() || Browser.isMobileChrome()) {
additionalWidth = 2;
}
setTextInputWidth(e.nativeEvent.layout.width + additionalWidth);
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);