forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextInputFlat.tsx
345 lines (312 loc) · 8.21 KB
/
TextInputFlat.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
import * as React from 'react';
import {
View,
Animated,
TextInput as NativeTextInput,
StyleSheet,
I18nManager,
Platform,
TextStyle,
} from 'react-native';
import color from 'color';
import InputLabel from './Label/InputLabel';
import { 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,
adjustPaddingFlat,
Padding,
interpolatePlaceholder,
} from './helpers';
const MINIMIZED_LABEL_Y_OFFSET = -18;
const LABEL_PADDING_TOP = 30;
const LABEL_PADDING_TOP_DENSE = 24;
const MIN_HEIGHT = 64;
const MIN_DENSE_HEIGHT_WL = 52;
const MIN_DENSE_HEIGHT = 40;
const INPUT_OFFSET = 8;
class TextInputFlat 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 {
fontSize: fontSizeStyle,
fontWeight,
height,
paddingHorizontal,
...viewStyle
} = (StyleSheet.flatten(style) || {}) as TextStyle;
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
const paddingOffset = (paddingHorizontal !== undefined &&
typeof paddingHorizontal === 'number'
? { paddingHorizontal }
: StyleSheet.flatten(styles.paddingOffset)) as {
paddingHorizontal: number;
};
let inputTextColor,
activeColor,
underlineColorCustom,
placeholderColor,
errorColor;
if (disabled) {
inputTextColor = activeColor = color(colors.text)
.alpha(0.54)
.rgb()
.string();
placeholderColor = colors.disabled;
underlineColorCustom = 'transparent';
} else {
inputTextColor = colors.text;
activeColor = error ? colors.error : colors.primary;
placeholderColor = colors.placeholder;
errorColor = colors.error;
underlineColorCustom = underlineColor || colors.disabled;
}
const containerStyle = {
backgroundColor: theme.dark
? color(colors.background)
.lighten(0.24)
.rgb()
.string()
: color(colors.background)
.darken(0.06)
.rgb()
.string(),
borderTopLeftRadius: theme.roundness,
borderTopRightRadius: theme.roundness,
};
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) +
(1 - labelScale) * paddingOffset.paddingHorizontal;
const minInputHeight = dense
? (label ? MIN_DENSE_HEIGHT_WL : MIN_DENSE_HEIGHT) -
LABEL_PADDING_TOP_DENSE
: MIN_HEIGHT - LABEL_PADDING_TOP;
const inputHeight = calculateInputHeight(
labelHeight,
height,
minInputHeight
);
const topPosition = calculateLabelTopPosition(
labelHeight,
inputHeight,
multiline && height ? 0 : !height ? minInputHeight / 2 : 0
);
if (height && typeof height !== 'number')
// eslint-disable-next-line
console.warn('Currently we support only numbers in height prop');
const paddingSettings = {
height: height ? +height : null,
labelHalfHeight,
offset: INPUT_OFFSET,
multiline: multiline ? multiline : null,
dense: dense ? dense : null,
topPosition,
fontSize,
label,
scale: fontScale,
isAndroid: Platform.OS === 'android',
styles: StyleSheet.flatten(
dense ? styles.inputFlatDense : styles.inputFlat
) as Padding,
};
const pad = calculatePadding(paddingSettings);
const paddingFlat = adjustPaddingFlat({
...paddingSettings,
pad,
});
const baseLabelTranslateY =
-labelHalfHeight - (topPosition + MINIMIZED_LABEL_Y_OFFSET);
const placeholderOpacity = hasActiveOutline
? interpolatePlaceholder(parentState.labeled, hasActiveOutline)
: parentState.labelLayout.measured
? 1
: 0;
const labelProps = {
label,
onLayoutAnimatedText,
placeholderOpacity,
error,
placeholderStyle: styles.placeholder,
baseLabelTranslateY,
baseLabelTranslateX,
font,
fontSize,
fontWeight,
labelScale,
wiggleOffsetX: LABEL_WIGGLE_X_OFFSET,
topPosition,
paddingOffset,
hasActiveOutline,
activeColor,
placeholderColor,
errorColor,
};
const minHeight =
height ||
(dense ? (label ? MIN_DENSE_HEIGHT_WL : MIN_DENSE_HEIGHT) : MIN_HEIGHT);
const flatHeight =
inputHeight +
(!height ? (dense ? LABEL_PADDING_TOP_DENSE : LABEL_PADDING_TOP) : 0);
return (
<View style={[containerStyle, viewStyle]}>
<Underline
parentState={parentState}
underlineColorCustom={underlineColorCustom}
error={error}
colors={colors}
activeColor={activeColor}
/>
<View
style={{
paddingTop: 0,
paddingBottom: 0,
minHeight,
}}
>
<InputLabel parentState={parentState} labelProps={labelProps} />
{render?.({
...rest,
ref: innerRef,
onChangeText,
// @ts-ignore
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,
paddingOffset,
!multiline || (multiline && height) ? { height: flatHeight } : {},
paddingFlat,
{
...font,
fontSize,
fontWeight,
color: inputTextColor,
textAlignVertical: multiline ? 'top' : 'center',
},
],
})}
</View>
</View>
);
}
}
export default TextInputFlat;
type UnderlineProps = {
parentState: {
focused: boolean;
};
error?: boolean;
colors: {
error: string;
};
activeColor: string;
underlineColorCustom?: string;
};
const Underline = ({
parentState,
error,
colors,
activeColor,
underlineColorCustom,
}: UnderlineProps) => {
let backgroundColor = parentState.focused
? activeColor
: underlineColorCustom;
if (error) backgroundColor = colors.error;
return (
<Animated.View
style={[
styles.underline,
{
backgroundColor,
// Underlines is thinner when input is not focused
transform: [{ scaleY: parentState.focused ? 1 : 0.5 }],
},
]}
/>
);
};
const styles = StyleSheet.create({
placeholder: {
position: 'absolute',
left: 0,
},
underline: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
height: 2,
},
input: {
flexGrow: 1,
margin: 0,
textAlign: I18nManager.isRTL ? 'right' : 'left',
zIndex: 1,
},
inputFlat: {
paddingTop: 24,
paddingBottom: 4,
},
inputFlatDense: {
paddingTop: 22,
paddingBottom: 2,
},
paddingOffset: {
paddingHorizontal: LABEL_PADDING_HORIZONTAL,
},
});