-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Input.js
183 lines (163 loc) · 3.79 KB
/
Input.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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
Dimensions,
Animated,
Easing,
Platform,
} from 'react-native';
import ViewPropTypes from '../config/ViewPropTypes';
import fonts from '../config/fonts';
import colors from '../config/colors';
const SCREEN_WIDTH = Dimensions.get('window').width;
class Input extends Component {
componentWillMount() {
this.shake = this.shake.bind(this);
this.shakeAnimationValue = new Animated.Value(0);
this.props.shake && this.shake();
}
focus() {
this.input.focus();
}
blur() {
this.input.blur();
}
clear() {
this.input.clear();
}
shake() {
const { shakeAnimationValue } = this;
shakeAnimationValue.setValue(0);
// Animation duration based on Material Design
// https://material.io/guidelines/motion/duration-easing.html#duration-easing-common-durations
Animated.timing(shakeAnimationValue, {
duration: 375,
toValue: 3,
ease: Easing.bounce,
}).start();
}
_inputRef = input => (this.input = input)
render() {
const {
containerStyle,
leftIcon,
leftIconContainerStyle,
rightIcon,
rightIconContainerStyle,
inputStyle,
errorStyle,
errorMessage,
labelStyle,
label,
...attributes
} = this.props;
const translateX = this.shakeAnimationValue.interpolate({
inputRange: [0, 0.5, 1, 1.5, 2, 2.5, 3],
outputRange: [0, -15, 0, 15, 0, -15, 0],
});
return (
<View>
{label && (
<Text style={[styles.label, labelStyle]}>
{label}
</Text>
)}
<Animated.View
style={[
styles.container,
{ width: SCREEN_WIDTH - 100, height: 40 },
containerStyle,
{ transform: [{ translateX }] },
]}
>
{leftIcon && (
<View
style={[
styles.iconContainer,
{ marginLeft: 15 },
leftIconContainerStyle,
]}
>
{leftIcon}
</View>
)}
<TextInput
{...attributes}
ref={this._inputRef}
underlineColorAndroid="transparent"
style={[
styles.input,
inputStyle,
]}
/>
{rightIcon && (
<View style={[styles.iconContainer, rightIconContainerStyle]}>
{rightIcon}
</View>
)}
</Animated.View>
{errorMessage && (
<Text style={[styles.error, errorStyle && errorStyle]}>
{errorMessage}
</Text>
)}
</View>
);
}
}
Input.propTypes = {
containerStyle: ViewPropTypes.style,
leftIcon: PropTypes.node,
leftIconContainerStyle: ViewPropTypes.style,
rightIcon: PropTypes.node,
rightIconContainerStyle: ViewPropTypes.style,
inputStyle: Text.propTypes.style,
shake: PropTypes.any,
errorStyle: Text.propTypes.style,
errorMessage: PropTypes.string,
label: PropTypes.string,
labelStyle: Text.propTypes.style,
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
borderBottomWidth: 1,
borderColor: colors.grey3,
alignItems: 'center',
},
iconContainer: {
height: 40,
justifyContent: 'center',
alignItems: 'center',
},
input: {
alignSelf: 'center',
color: 'black',
fontSize: 18,
marginLeft: 10,
flex: 1,
height: 40,
},
error: {
color: '#FF2D00',
margin: 5,
fontSize: 12,
},
label: {
color: colors.grey3,
fontSize: 16,
...Platform.select({
ios: {
fontWeight: 'bold',
},
android: {
...fonts.android.bold,
},
}),
},
});
export default Input;