UNPKG

2.45 kBJavaScriptView Raw
1import React, { PropTypes, Component } from 'react';
2import {
3 Animated,
4 Easing,
5 Text,
6 TextInput,
7 TouchableWithoutFeedback,
8 View,
9 StyleSheet,
10} from 'react-native';
11
12import BaseInput from './BaseInput';
13
14const PADDING = 16;
15
16export default class Kaede extends BaseInput {
17
18 static propTypes = {
19 easing: PropTypes.func,
20 height: PropTypes.number,
21 };
22
23 static defaultProps = {
24 easing: Easing.bezier(0.2, 1, 0.3, 1),
25 height: 60,
26 };
27
28 render() {
29 const {
30 label,
31 style: containerStyle,
32 inputStyle,
33 labelStyle,
34 height: inputHeight,
35 } = this.props;
36 const { width, focusedAnim, value } = this.state;
37 const inputWidth = width * 0.6;
38
39 const flatLabelStyle = StyleSheet.flatten(labelStyle);
40 let labelBackgroundColor = '#EBEAEA';
41 if (flatLabelStyle && flatLabelStyle.backgroundColor) {
42 labelBackgroundColor = flatLabelStyle.backgroundColor;
43 }
44
45 return (
46 <View style={containerStyle} onLayout={this._onLayout}>
47 <Animated.View style={{
48 width: inputWidth,
49 marginLeft: focusedAnim.interpolate({
50 inputRange: [0, 1],
51 outputRange: [inputWidth * -1, 0],
52 }),
53 }}>
54 <TextInput
55 ref="input"
56 {...this.props}
57 style={[styles.textInput, inputStyle, { height: inputHeight }]}
58 value={value}
59 onBlur={this._onBlur}
60 onFocus={this._onFocus}
61 onChange={this._onChange}
62 />
63 </Animated.View>
64 <TouchableWithoutFeedback onPress={this._focus}>
65 <Animated.View style={{
66 position: 'absolute',
67 justifyContent: 'center',
68 top: 0,
69 height: inputHeight,
70 width,
71 left: focusedAnim.interpolate({
72 inputRange: [0, 1],
73 outputRange: [0, inputWidth],
74 }),
75 backgroundColor: labelBackgroundColor,
76 }}>
77 <Text style={[styles.label, labelStyle]}>
78 {label}
79 </Text>
80 </Animated.View>
81 </TouchableWithoutFeedback>
82 </View>
83 );
84 }
85}
86
87const styles = StyleSheet.create({
88 label: {
89 marginHorizontal: PADDING,
90 fontSize: 16,
91 fontWeight: 'bold',
92 color: '#6a7989',
93 },
94 textInput: {
95 padding: PADDING,
96 backgroundColor: 'white',
97 color: 'black',
98 fontSize: 16,
99 fontWeight: 'bold',
100 },
101});