-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathTextInput.tsx
576 lines (535 loc) · 16.5 KB
/
TextInput.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import * as React from 'react';
import {
Animated,
LayoutChangeEvent,
StyleProp,
TextInput as NativeTextInput,
TextStyle,
ViewStyle,
NativeSyntheticEvent,
TextLayoutEventData,
} from 'react-native';
import TextInputAffix, {
Props as TextInputAffixProps,
} from './Adornment/TextInputAffix';
import TextInputIcon, {
Props as TextInputIconProps,
} from './Adornment/TextInputIcon';
import TextInputFlat from './TextInputFlat';
import TextInputOutlined from './TextInputOutlined';
import type { RenderProps, TextInputLabelProp } from './types';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
import { roundLayoutSize } from '../../utils/roundLayoutSize';
const BLUR_ANIMATION_DURATION = 180;
const FOCUS_ANIMATION_DURATION = 150;
export type Props = React.ComponentPropsWithRef<typeof NativeTextInput> & {
/**
* Mode of the TextInput.
* - `flat` - flat input with an underline.
* - `outlined` - input with an outline.
*
* In `outlined` mode, the background color of the label is derived from `colors?.background` in theme or the `backgroundColor` style.
* This component render TextInputOutlined or TextInputFlat based on that props
*/
mode?: 'flat' | 'outlined';
left?: React.ReactNode;
right?: React.ReactNode;
/**
* If true, user won't be able to interact with the component.
*/
disabled?: boolean;
/**
* The text or component to use for the floating label.
*/
label?: TextInputLabelProp;
/**
* Placeholder for the input.
*/
placeholder?: string;
/**
* Whether to style the TextInput with error style.
*/
error?: boolean;
/**
* Callback that is called when the text input's text changes. Changed text is passed as an argument to the callback handler.
*/
onChangeText?: Function;
/**
* Selection color of the input. On iOS, it sets both the selection color and cursor color.
* On Android, it sets only the selection color.
*/
selectionColor?: string;
/**
* @platform Android only
* Cursor (or "caret") color of the input on Android.
* This property has no effect on iOS.
*/
cursorColor?: string;
/**
* Inactive underline color of the input.
*/
underlineColor?: string;
/**
* Active underline color of the input.
*/
activeUnderlineColor?: string;
/**
* Inactive outline color of the input.
*/
outlineColor?: string;
/**
* Active outline color of the input.
*/
activeOutlineColor?: string;
/**
* Color of the text in the input.
*/
textColor?: string;
/**
* Sets min height with densed layout. For `TextInput` in `flat` mode
* height is `64dp` or in dense layout - `52dp` with label or `40dp` without label.
* For `TextInput` in `outlined` mode
* height is `56dp` or in dense layout - `40dp` regardless of label.
* When you apply `height` prop in style the `dense` prop affects only `paddingVertical` inside `TextInput`
*/
dense?: boolean;
/**
* Whether the input can have multiple lines.
*/
multiline?: boolean;
/**
* @platform Android only
* The number of lines to show in the input (Android only).
*/
numberOfLines?: number;
/**
* Callback that is called when the text input is focused.
*/
onFocus?: (args: any) => void;
/**
* Callback that is called when the text input is blurred.
*/
onBlur?: (args: any) => void;
/**
*
* Callback to render a custom input component such as `react-native-text-input-mask`
* instead of the default `TextInput` component from `react-native`.
*
* Example:
* ```js
* <TextInput
* label="Phone number"
* render={props =>
* <TextInputMask
* {...props}
* mask="+[00] [000] [000] [000]"
* />
* }
* />
* ```
*/
render?: (props: RenderProps) => React.ReactNode;
/**
* Value of the text input.
*/
value?: string;
/**
* Pass `fontSize` prop to modify the font size inside `TextInput`.
* Pass `height` prop to set `TextInput` height. When `height` is passed,
* `dense` prop will affect only input's `paddingVertical`.
* Pass `paddingHorizontal` to modify horizontal padding.
* This can be used to get MD Guidelines v1 TextInput look.
*/
style?: StyleProp<TextStyle>;
/**
* @optional
*/
theme?: ThemeProp;
/**
* testID to be used on tests.
*/
testID?: string;
/**
* Pass custom style directly to the input itself.
* Overrides input style
* Example: `paddingLeft`, `backgroundColor`
*/
contentStyle?: StyleProp<TextStyle>;
/**
* Pass style to override the default style of outlined wrapper.
* Overrides style when mode is set to `outlined`
* Example: `borderRadius`, `borderColor`
*/
outlineStyle?: StyleProp<ViewStyle>;
/**
* Pass style to override the default style of underlined wrapper.
* Overrides style when mode is set to `flat`
* Example: `borderRadius`, `borderColor`
*/
underlineStyle?: StyleProp<ViewStyle>;
};
interface CompoundedComponent
extends React.ForwardRefExoticComponent<
Props & React.RefAttributes<TextInputHandles>
> {
Icon: React.FunctionComponent<TextInputIconProps>;
Affix: React.FunctionComponent<Partial<TextInputAffixProps>>;
}
type TextInputHandles = Pick<
NativeTextInput,
'focus' | 'clear' | 'blur' | 'isFocused' | 'setNativeProps'
>;
const DefaultRenderer = (props: RenderProps) => <NativeTextInput {...props} />;
/**
* A component to allow users to input text.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { TextInput } from 'react-native-paper';
*
* const MyComponent = () => {
* const [text, setText] = React.useState("");
*
* return (
* <TextInput
* label="Email"
* value={text}
* onChangeText={text => setText(text)}
* />
* );
* };
*
* export default MyComponent;
* ```
*
* @extends TextInput props https://reactnative.dev/docs/textinput#props
*/
const TextInput = forwardRef<TextInputHandles, Props>(
(
{
mode = 'flat',
dense = false,
disabled = false,
error: errorProp = false,
multiline = false,
editable = true,
contentStyle,
render = DefaultRenderer,
theme: themeOverrides,
...rest
}: Props,
ref
) => {
const theme = useInternalTheme(themeOverrides);
const isControlled = rest.value !== undefined;
const validInputValue = isControlled ? rest.value : rest.defaultValue;
const { current: labeled } = React.useRef<Animated.Value>(
new Animated.Value(validInputValue ? 0 : 1)
);
const { current: error } = React.useRef<Animated.Value>(
new Animated.Value(errorProp ? 1 : 0)
);
const [focused, setFocused] = React.useState<boolean>(false);
const [displayPlaceholder, setDisplayPlaceholder] =
React.useState<boolean>(false);
const [uncontrolledValue, setUncontrolledValue] = React.useState<
string | undefined
>(validInputValue);
// Use value from props instead of local state when input is controlled
const value = isControlled ? rest.value : uncontrolledValue;
const [labelTextLayout, setLabelTextLayout] = React.useState({
width: 33,
});
const [inputContainerLayout, setInputContainerLayout] = React.useState({
width: 65,
});
const [labelLayout, setLabelLayout] = React.useState<{
measured: boolean;
width: number;
height: number;
}>({
measured: false,
width: 0,
height: 0,
});
const [leftLayout, setLeftLayout] = React.useState<{
height: number | null;
width: number | null;
}>({
width: null,
height: null,
});
const [rightLayout, setRightLayout] = React.useState<{
height: number | null;
width: number | null;
}>({
width: null,
height: null,
});
const timer = React.useRef<NodeJS.Timeout | undefined>();
const root = React.useRef<NativeTextInput | undefined | null>();
const { scale } = theme.animation;
React.useImperativeHandle(ref, () => ({
focus: () => root.current?.focus(),
clear: () => root.current?.clear(),
setNativeProps: (args: { text?: string } & Object) => {
if ('text' in args) {
// Update uncontrolledValue when text is set via setNativeProps
setUncontrolledValue(args.text as string);
}
root.current?.setNativeProps(args);
},
isFocused: () => root.current?.isFocused() || false,
blur: () => root.current?.blur(),
forceFocus: () => root.current?.focus(),
}));
React.useEffect(() => {
// When the input has an error, we wiggle the label and apply error styles
if (errorProp) {
// show error
Animated.timing(error, {
toValue: 1,
duration: FOCUS_ANIMATION_DURATION * scale,
// To prevent this - https://github.com/callstack/react-native-paper/issues/941
useNativeDriver: true,
}).start();
} else {
// hide error
{
Animated.timing(error, {
toValue: 0,
duration: BLUR_ANIMATION_DURATION * scale,
// To prevent this - https://github.com/callstack/react-native-paper/issues/941
useNativeDriver: true,
}).start();
}
}
}, [errorProp, scale, error]);
React.useEffect(() => {
// Show placeholder text only if the input is focused, or there's no label
// We don't show placeholder if there's a label because the label acts as placeholder
// When focused, the label moves up, so we can show a placeholder
if (focused || !rest.label) {
// If the user wants to use the contextMenu, when changing the placeholder, the contextMenu is closed
// This is a workaround to mitigate this behavior in scenarios where the placeholder is not specified.
if (rest.placeholder) {
// Display placeholder in a delay to offset the label animation
// If we show it immediately, they'll overlap and look ugly
timer.current = setTimeout(
() => setDisplayPlaceholder(true),
50
) as unknown as NodeJS.Timeout;
}
} else {
// hidePlaceholder
setDisplayPlaceholder(false);
}
return () => {
if (timer.current) {
clearTimeout(timer.current);
}
};
}, [focused, rest.label, rest.placeholder]);
React.useEffect(() => {
labeled.stopAnimation();
// The label should be minimized if the text input is focused, or has text
// In minimized mode, the label moves up and becomes small
// workaround for animated regression for react native > 0.61
// https://github.com/callstack/react-native-paper/pull/1440
if (value || focused) {
// minimize label
Animated.timing(labeled, {
toValue: 0,
duration: BLUR_ANIMATION_DURATION * scale,
// To prevent this - https://github.com/callstack/react-native-paper/issues/941
useNativeDriver: true,
}).start();
} else {
// restore label
Animated.timing(labeled, {
toValue: 1,
duration: FOCUS_ANIMATION_DURATION * scale,
// To prevent this - https://github.com/callstack/react-native-paper/issues/941
useNativeDriver: true,
}).start();
}
}, [focused, value, labeled, scale]);
const onLeftAffixLayoutChange = React.useCallback(
(event: LayoutChangeEvent) => {
const height = roundLayoutSize(event.nativeEvent.layout.height);
const width = roundLayoutSize(event.nativeEvent.layout.width);
if (width !== leftLayout.width || height !== leftLayout.height) {
setLeftLayout({
width,
height,
});
}
},
[leftLayout.height, leftLayout.width]
);
const onRightAffixLayoutChange = React.useCallback(
(event: LayoutChangeEvent) => {
const width = roundLayoutSize(event.nativeEvent.layout.width);
const height = roundLayoutSize(event.nativeEvent.layout.height);
if (width !== rightLayout.width || height !== rightLayout.height) {
setRightLayout({
width,
height,
});
}
},
[rightLayout.height, rightLayout.width]
);
const handleFocus = (args: any) => {
if (disabled || !editable) {
return;
}
setFocused(true);
rest.onFocus?.(args);
};
const handleBlur = (args: Object) => {
if (!editable) {
return;
}
setFocused(false);
rest.onBlur?.(args);
};
const handleChangeText = (value: string) => {
if (!editable || disabled) {
return;
}
if (!isControlled) {
// Keep track of value in local state when input is not controlled
setUncontrolledValue(value);
}
rest.onChangeText?.(value);
};
const handleLayoutAnimatedText = React.useCallback(
(e: LayoutChangeEvent) => {
const width = roundLayoutSize(e.nativeEvent.layout.width);
const height = roundLayoutSize(e.nativeEvent.layout.height);
if (width !== labelLayout.width || height !== labelLayout.height) {
setLabelLayout({
width,
height,
measured: true,
});
}
},
[labelLayout.height, labelLayout.width]
);
const handleLabelTextLayout = React.useCallback(
({ nativeEvent }: NativeSyntheticEvent<TextLayoutEventData>) => {
setLabelTextLayout({
width: nativeEvent.lines.reduce(
(acc, line) => acc + Math.ceil(line.width),
0
),
});
},
[]
);
const handleInputContainerLayout = React.useCallback(
({ nativeEvent: { layout } }: LayoutChangeEvent) => {
setInputContainerLayout({
width: layout.width,
});
},
[]
);
const forceFocus = React.useCallback(() => root.current?.focus(), []);
const { maxFontSizeMultiplier = 1.5 } = rest;
const scaledLabel = !!(value || focused);
if (mode === 'outlined') {
return (
<TextInputOutlined
dense={dense}
disabled={disabled}
error={errorProp}
multiline={multiline}
editable={editable}
render={render}
{...rest}
theme={theme}
value={value}
parentState={{
labeled,
error,
focused,
displayPlaceholder,
value,
labelTextLayout,
labelLayout,
leftLayout,
rightLayout,
inputContainerLayout,
}}
innerRef={(ref) => {
root.current = ref;
}}
onFocus={handleFocus}
forceFocus={forceFocus}
onBlur={handleBlur}
onChangeText={handleChangeText}
onLayoutAnimatedText={handleLayoutAnimatedText}
onInputLayout={handleInputContainerLayout}
onLabelTextLayout={handleLabelTextLayout}
onLeftAffixLayoutChange={onLeftAffixLayoutChange}
onRightAffixLayoutChange={onRightAffixLayoutChange}
maxFontSizeMultiplier={maxFontSizeMultiplier}
contentStyle={contentStyle}
scaledLabel={scaledLabel}
/>
);
}
return (
<TextInputFlat
dense={dense}
disabled={disabled}
error={errorProp}
multiline={multiline}
editable={editable}
render={render}
{...rest}
theme={theme}
value={value}
parentState={{
labeled,
error,
focused,
displayPlaceholder,
value,
labelTextLayout,
labelLayout,
leftLayout,
rightLayout,
inputContainerLayout,
}}
innerRef={(ref) => {
root.current = ref;
}}
onFocus={handleFocus}
forceFocus={forceFocus}
onBlur={handleBlur}
onInputLayout={handleInputContainerLayout}
onChangeText={handleChangeText}
onLayoutAnimatedText={handleLayoutAnimatedText}
onLabelTextLayout={handleLabelTextLayout}
onLeftAffixLayoutChange={onLeftAffixLayoutChange}
onRightAffixLayoutChange={onRightAffixLayoutChange}
maxFontSizeMultiplier={maxFontSizeMultiplier}
contentStyle={contentStyle}
scaledLabel={scaledLabel}
/>
);
}
) as CompoundedComponent;
// @component ./Adornment/TextInputIcon.tsx
TextInput.Icon = TextInputIcon;
// @component ./Adornment/TextInputAffix.tsx
// @ts-ignore Types of property 'theme' are incompatible.
TextInput.Affix = TextInputAffix;
export default TextInput;