UNPKG

4.59 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import {
4 Animated,
5 Easing,
6 TextInput,
7 TouchableWithoutFeedback,
8 View,
9 StyleSheet,
10} from 'react-native';
11
12import BaseInput from './BaseInput';
13
14export default class Isao extends BaseInput {
15 static propTypes = {
16 /*
17 * this is applied as passive border and label color
18 */
19 passiveColor: PropTypes.string,
20 /*
21 * this is applied as active border and label color
22 */
23 activeColor: PropTypes.string,
24 /*
25 * active border height
26 */
27 borderHeight: PropTypes.number,
28 labelHeight: PropTypes.number,
29 inputPadding: PropTypes.number,
30 height: PropTypes.number,
31 };
32
33 static defaultProps = {
34 activeColor: 'red',
35 passiveColor: 'white',
36 height: 48,
37 labelHeight: 24,
38 inputPadding: 16,
39 borderHeight: 8,
40 easing: Easing.bezier(0.2, 1, 0.3, 1),
41 };
42
43 render() {
44 const {
45 label,
46 style: containerStyle,
47 height: inputHeight,
48 borderHeight,
49 inputPadding,
50 labelHeight,
51 inputStyle,
52 labelStyle,
53 activeColor,
54 passiveColor,
55 } = this.props;
56 const {
57 width,
58 focusedAnim,
59 value,
60 } = this.state;
61
62 return (
63 <View
64 style={[
65 containerStyle,
66 {
67 height: inputHeight + labelHeight,
68 },
69 ]}
70 onLayout={this._onLayout}
71 >
72 <View
73 style={[
74 styles.inputContainer,
75 {
76 height: inputHeight,
77 borderBottomColor: passiveColor,
78 },
79 ]}
80 >
81 <TextInput
82 ref={this.input}
83 {...this.props}
84 style={[
85 styles.textInput,
86 inputStyle,
87 {
88 width,
89 height: inputHeight,
90 paddingHorizontal: inputPadding,
91 },
92 ]}
93 value={value}
94 onBlur={this._onBlur}
95 onChange={this._onChange}
96 onFocus={this._onFocus}
97 underlineColorAndroid={'transparent'}
98 />
99 </View>
100 <TouchableWithoutFeedback onPress={this.focus}>
101 <View
102 style={{
103 height: labelHeight,
104 width,
105 }}
106 >
107 {/* passive label */}
108 <Animated.Text
109 style={[
110 styles.label,
111 labelStyle,
112 {
113 width,
114 opacity: focusedAnim.interpolate({
115 inputRange: [0, 1],
116 outputRange: [1, 0],
117 }),
118 bottom: focusedAnim.interpolate({
119 inputRange: [0, 1],
120 outputRange: [0, labelHeight],
121 }),
122 color: passiveColor,
123 height: labelHeight,
124 left: inputPadding,
125 },
126 ]}
127 >
128 {label}
129 </Animated.Text>
130 {/* active label */}
131 <Animated.Text
132 style={[
133 styles.label,
134 labelStyle,
135 {
136 opacity: focusedAnim.interpolate({
137 inputRange: [0, 1],
138 outputRange: [0, 1],
139 }),
140 bottom: focusedAnim.interpolate({
141 inputRange: [0, 1],
142 outputRange: [labelHeight * -1, 0],
143 }),
144 color: activeColor,
145 height: labelHeight,
146 left: inputPadding,
147 },
148 ]}
149 >
150 {label}
151 </Animated.Text>
152 </View>
153 </TouchableWithoutFeedback>
154 <Animated.View
155 style={[
156 styles.border,
157 {
158 height: focusedAnim.interpolate({
159 inputRange: [0, 1],
160 outputRange: [0, borderHeight],
161 }),
162 width,
163 backgroundColor: activeColor,
164 bottom: labelHeight,
165 },
166 ]}
167 />
168 </View>
169 );
170 }
171}
172
173const styles = StyleSheet.create({
174 inputContainer: {
175 borderBottomWidth: 2,
176 },
177 label: {
178 position: 'absolute',
179 paddingTop: 4,
180 backgroundColor: 'transparent',
181 fontSize: 14,
182 fontWeight: 'bold',
183 },
184 textInput: {
185 color: '#afb3b8',
186 fontSize: 18,
187 fontWeight: 'bold',
188 padding: 0,
189 },
190 border: {
191 position: 'absolute',
192 left: 0,
193 right: 0,
194 },
195});