-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
TextInputOutlined.js
391 lines (363 loc) · 11.1 KB
/
TextInputOutlined.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
/* @flow */
import * as React from 'react';
import {
View,
Animated,
TextInput as NativeTextInput,
StyleSheet,
I18nManager,
Platform,
} from 'react-native';
import color from 'color';
import Text from '../Typography/Text';
import type { ChildTextInputProps, RenderProps } from './types';
import {
calculateLabelTopPosition,
calculateInputHeight,
calculatePadding,
adjustPaddingOut,
} from './helpers';
const AnimatedText = Animated.createAnimatedComponent(Text);
const OUTLINE_MINIMIZED_LABEL_Y_OFFSET = -6;
const MAXIMIZED_LABEL_FONT_SIZE = 16;
const MINIMIZED_LABEL_FONT_SIZE = 12;
const LABEL_WIGGLE_X_OFFSET = 4;
const LABEL_PADDING_HORIZONTAL = 12;
const LABEL_PADDING_TOP = 8;
const MIN_HEIGHT = 64;
const MIN_DENSE_HEIGHT = 48;
class TextInputOutlined extends React.Component<ChildTextInputProps, {}> {
static defaultProps = {
disabled: false,
error: false,
multiline: false,
editable: true,
render: (props: RenderProps) => <NativeTextInput {...props} />,
};
render() {
const {
disabled,
editable,
label,
error,
selectionColor,
underlineColor,
dense,
style,
theme,
render,
multiline,
parentState,
innerRef,
onFocus,
onBlur,
onChangeText,
onLayoutAnimatedText,
...rest
} = this.props;
const { colors, fonts } = theme;
const font = fonts.regular;
const hasActiveOutline = parentState.focused || error;
const { backgroundColor = colors.background } =
StyleSheet.flatten(style) || {};
const { fontSize: fontSizeStyle, height, ...viewStyle } =
StyleSheet.flatten(style) || {};
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
let inputTextColor,
activeColor,
outlineColor,
placeholderColor,
containerStyle;
if (disabled) {
inputTextColor = activeColor = color(colors.text)
.alpha(0.54)
.rgb()
.string();
placeholderColor = outlineColor = colors.disabled;
} else {
inputTextColor = colors.text;
activeColor = error ? colors.error : colors.primary;
placeholderColor = outlineColor = colors.placeholder;
}
const labelWidth = parentState.labelLayout.width;
const labelHeight = parentState.labelLayout.height;
const labelHalfWidth = labelWidth / 2;
const labelHalfHeight = labelHeight / 2;
const labelScale = MINIMIZED_LABEL_FONT_SIZE / fontSize;
const fontScale = MAXIMIZED_LABEL_FONT_SIZE / fontSize;
const baseLabelTranslateX =
(I18nManager.isRTL ? 1 : -1) *
(labelHalfWidth -
(labelScale * labelWidth) / 2 -
(fontSize - MINIMIZED_LABEL_FONT_SIZE) * labelScale);
const minInputHeight =
(dense ? MIN_DENSE_HEIGHT : MIN_HEIGHT) - LABEL_PADDING_TOP;
const inputHeight = calculateInputHeight(
labelHeight,
height,
minInputHeight
);
const topPosition =
multiline && height
? calculateLabelTopPosition(labelHeight, inputHeight, LABEL_PADDING_TOP) // 18
: calculateLabelTopPosition(
labelHeight,
inputHeight,
LABEL_PADDING_TOP
);
if (height && typeof height !== 'number')
// eslint-disable-next-line
console.warn('Currently we support only numbers in height prop');
const paddingSettings = {
height: +height || undefined,
labelHalfHeight,
offset: LABEL_PADDING_TOP,
multiline,
dense,
topPosition,
fontSize,
label,
scale: fontScale,
isAndroid: Platform.OS === 'android',
styles: dense ? styles.inputOutlinedDense : styles.inputOutlined,
};
const pad = calculatePadding(paddingSettings);
const paddingOut = adjustPaddingOut({ ...paddingSettings, pad });
const baseLabelTranslateY =
-labelHalfHeight - (topPosition + OUTLINE_MINIMIZED_LABEL_Y_OFFSET);
const labelTranslationX = {
transform: [
{
// Offset label scale since RN doesn't support transform origin
translateX: parentState.labeled.interpolate({
inputRange: [0, 1],
outputRange: [baseLabelTranslateX, 0],
}),
},
],
};
const labelStyle = {
...font,
fontSize,
transform: [
{
// Wiggle the label when there's an error
translateX: parentState.error.interpolate({
inputRange: [0, 0.5, 1],
outputRange: [
0,
parentState.value && error ? LABEL_WIGGLE_X_OFFSET : 0,
0,
],
}),
},
{
// Move label to top
translateY: parentState.labeled.interpolate({
inputRange: [0, 1],
outputRange: [baseLabelTranslateY, 0],
}),
},
{
// Make label smaller
scale: parentState.labeled.interpolate({
inputRange: [0, 1],
outputRange: [labelScale, 1],
}),
},
],
};
return (
<View style={[containerStyle, viewStyle]}>
{/*
Render the outline separately from the container
This is so that the label can overlap the outline
Otherwise the border will cut off the label on Android
*/}
<View>
<View
pointerEvents="none"
style={[
styles.outline,
{
borderRadius: theme.roundness,
borderWidth: hasActiveOutline ? 2 : 1,
borderColor: hasActiveOutline ? activeColor : outlineColor,
},
]}
/>
<View
style={{
paddingTop: LABEL_PADDING_TOP,
paddingBottom: 0,
minHeight: height || (dense ? MIN_DENSE_HEIGHT : MIN_HEIGHT),
}}
>
{label ? (
// The input label stays on top of the outline
// The background of the label covers the outline so it looks cut off
// To achieve the effect, we position the actual label with a background on top of it
// We set the color of the text to transparent so only the background is visible
<AnimatedText
pointerEvents="none"
style={[
styles.outlinedLabelBackground,
{
backgroundColor,
...font,
fontSize: MINIMIZED_LABEL_FONT_SIZE,
// Hide the background when scale will be 0
// There's a bug in RN which makes scale: 0 act weird
opacity: parentState.labeled.interpolate({
inputRange: [0, 0.9, 1],
outputRange: [1, 1, 0],
}),
transform: [
{
// Animate the scale when label is moved up
scaleX: parentState.labeled.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
}),
},
],
},
]}
numberOfLines={1}
>
{label}
</AnimatedText>
) : null}
{label ? (
// Position colored placeholder and gray placeholder on top of each other and crossfade them
// This gives the effect of animating the color, but allows us to use native driver
<Animated.View
pointerEvents="none"
style={[
StyleSheet.absoluteFill,
{
opacity:
// Hide the label in minimized state until we measure it's width
parentState.value || parentState.focused
? parentState.labelLayout.measured
? 1
: 0
: 1,
},
labelTranslationX,
]}
>
<AnimatedText
onLayout={onLayoutAnimatedText}
style={[
styles.placeholder,
{
top: topPosition,
},
labelStyle,
{
color: activeColor,
opacity: parentState.labeled.interpolate({
inputRange: [0, 1],
outputRange: [hasActiveOutline ? 1 : 0, 0],
}),
},
]}
numberOfLines={1}
>
{label}
</AnimatedText>
<AnimatedText
style={[
styles.placeholder,
{
top: topPosition,
},
labelStyle,
{
color: placeholderColor,
opacity: hasActiveOutline ? parentState.labeled : 1,
},
]}
numberOfLines={1}
>
{label}
</AnimatedText>
</Animated.View>
) : null}
{render(
({
...rest,
ref: innerRef,
onChangeText,
placeholder: label
? parentState.placeholder
: this.props.placeholder,
placeholderTextColor: placeholderColor,
editable: !disabled && editable,
selectionColor:
typeof selectionColor === 'undefined'
? activeColor
: selectionColor,
onFocus,
onBlur,
underlineColorAndroid: 'transparent',
multiline,
style: [
styles.input,
!multiline || (multiline && height)
? { height: inputHeight }
: {},
paddingOut,
{
fontSize,
color: inputTextColor,
...font,
textAlignVertical: multiline && height ? 'top' : 'center',
},
],
}: RenderProps)
)}
</View>
</View>
</View>
);
}
}
export default TextInputOutlined;
const styles = StyleSheet.create({
placeholder: {
position: 'absolute',
left: 0,
paddingHorizontal: LABEL_PADDING_HORIZONTAL,
},
outline: {
position: 'absolute',
left: 0,
right: 0,
top: 6,
bottom: 0,
},
outlinedLabelBackground: {
position: 'absolute',
top: 0,
left: 8,
paddingHorizontal: 4,
color: 'transparent',
},
input: {
flexGrow: 1,
paddingHorizontal: 12,
margin: 0,
textAlign: I18nManager.isRTL ? 'right' : 'left',
zIndex: 1,
},
inputOutlined: {
paddingTop: 8,
paddingBottom: 8,
},
inputOutlinedDense: {
paddingTop: 4,
paddingBottom: 4,
},
});