-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
TextInputOutlined.js
335 lines (303 loc) · 8.58 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
/* @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 InputLabel from './InputLabel';
import type { RenderProps, ChildTextInputProps } from './types';
import {
MAXIMIZED_LABEL_FONT_SIZE,
MINIMIZED_LABEL_FONT_SIZE,
LABEL_WIGGLE_X_OFFSET,
LABEL_PADDING_HORIZONTAL,
} from './constants';
import {
calculateLabelTopPosition,
calculateInputHeight,
calculatePadding,
adjustPaddingOut,
} from './helpers';
const AnimatedText = Animated.createAnimatedComponent(Text);
const OUTLINE_MINIMIZED_LABEL_Y_OFFSET = -6;
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 labelScale = MINIMIZED_LABEL_FONT_SIZE / fontSize;
const fontScale = MAXIMIZED_LABEL_FONT_SIZE / fontSize;
const labelWidth = parentState.labelLayout.width;
const labelHeight = parentState.labelLayout.height;
const labelHalfWidth = labelWidth / 2;
const labelHalfHeight = labelHeight / 2;
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 = 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: StyleSheet.flatten(
dense ? styles.inputOutlinedDense : styles.inputOutlined
),
};
// $FlowFixMe
const pad = calculatePadding(paddingSettings);
// $FlowFixMe
const paddingOut = adjustPaddingOut({ ...paddingSettings, pad });
const baseLabelTranslateY =
-labelHalfHeight - (topPosition + OUTLINE_MINIMIZED_LABEL_Y_OFFSET);
const placeholderOpacity = hasActiveOutline ? parentState.labeled : 1;
const labelProps = {
label,
onLayoutAnimatedText,
placeholderOpacity,
error,
placeholderStyle: styles.placeholder,
baseLabelTranslateY,
baseLabelTranslateX,
font,
fontSize,
labelScale,
wiggleOffsetX: LABEL_WIGGLE_X_OFFSET,
topPosition,
hasActiveOutline,
activeColor,
placeholderColor,
};
const minHeight = height || (dense ? MIN_DENSE_HEIGHT : MIN_HEIGHT);
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>
<Outline
theme={theme}
hasActiveOutline={hasActiveOutline}
activeColor={activeColor}
outlineColor={outlineColor}
/>
<View
style={{
paddingTop: LABEL_PADDING_TOP,
paddingBottom: 0,
minHeight,
}}
>
<OutlinedLabel
parentState={parentState}
label={label}
backgroundColor={backgroundColor}
font={font}
/>
<InputLabel parentState={parentState} labelProps={labelProps} />
{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 Outline = ({ theme, hasActiveOutline, activeColor, outlineColor }) => (
<View
pointerEvents="none"
style={[
styles.outline,
{
borderRadius: theme.roundness,
borderWidth: hasActiveOutline ? 2 : 1,
borderColor: hasActiveOutline ? activeColor : outlineColor,
},
]}
/>
);
const OutlinedLabel = ({ parentState, label, backgroundColor, font }) =>
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;
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,
},
});