1 | import React, { PropTypes, Component } from 'react';
|
2 | import {
|
3 | Animated,
|
4 | Text,
|
5 | TextInput,
|
6 | TouchableWithoutFeedback,
|
7 | View,
|
8 | StyleSheet,
|
9 | } from 'react-native';
|
10 |
|
11 | import BaseInput from './BaseInput';
|
12 |
|
13 | const PADDING = 16;
|
14 |
|
15 | export default class Hoshi extends BaseInput {
|
16 |
|
17 | static propTypes = {
|
18 | borderColor: PropTypes.string,
|
19 |
|
20 | |
21 |
|
22 |
|
23 |
|
24 | backgroundColor: PropTypes.string,
|
25 | height: PropTypes.number,
|
26 | };
|
27 |
|
28 | static defaultProps = {
|
29 | borderColor: 'red',
|
30 | height: 48,
|
31 | };
|
32 |
|
33 | render() {
|
34 | const {
|
35 | label,
|
36 | style: containerStyle,
|
37 | inputStyle,
|
38 | labelStyle,
|
39 | backgroundColor: maskColor,
|
40 | borderColor,
|
41 | height: inputHeight,
|
42 | } = this.props;
|
43 | const {
|
44 | width,
|
45 | focusedAnim,
|
46 | value,
|
47 | } = this.state;
|
48 |
|
49 | return (
|
50 | <View
|
51 | style={[containerStyle, styles.container, {
|
52 | height: inputHeight + PADDING,
|
53 | width,
|
54 | }]}
|
55 | onLayout={this._onLayout}
|
56 | >
|
57 | <TextInput
|
58 | ref="input"
|
59 | {...this.props}
|
60 | style={[styles.textInput, inputStyle, {
|
61 | width,
|
62 | height: inputHeight,
|
63 | }]}
|
64 | value={value}
|
65 | onBlur={this._onBlur}
|
66 | onChange={this._onChange}
|
67 | onFocus={this._onFocus}
|
68 | underlineColorAndroid={'transparent'}
|
69 | />
|
70 | <TouchableWithoutFeedback onPress={this._focus}>
|
71 | <Animated.View style={[styles.labelContainer, {
|
72 | opacity: focusedAnim.interpolate({
|
73 | inputRange: [0, 0.5, 1],
|
74 | outputRange: [1, 0, 1],
|
75 | }),
|
76 | top: focusedAnim.interpolate({
|
77 | inputRange: [0, 0.5, 0.51, 1],
|
78 | outputRange: [24, 24, 0, 0],
|
79 | }),
|
80 | left: focusedAnim.interpolate({
|
81 | inputRange: [0, 0.5, 0.51, 1],
|
82 | outputRange: [PADDING, 2 * PADDING, 0, PADDING],
|
83 | }),
|
84 | }]}>
|
85 | <Text style={[styles.label, labelStyle]}>
|
86 | {label}
|
87 | </Text>
|
88 | </Animated.View>
|
89 | </TouchableWithoutFeedback>
|
90 | <View style={[styles.labelMask, { backgroundColor: maskColor }]} />
|
91 | <Animated.View
|
92 | style={[styles.border, {
|
93 | width: focusedAnim.interpolate({
|
94 | inputRange: [0, 1],
|
95 | outputRange: [0, width],
|
96 | }),
|
97 | backgroundColor: borderColor,
|
98 | }]}
|
99 | />
|
100 | </View>
|
101 | );
|
102 | }
|
103 | }
|
104 |
|
105 | const styles = StyleSheet.create({
|
106 | container: {
|
107 | borderBottomWidth: 2,
|
108 | borderBottomColor: '#b9c1ca',
|
109 | },
|
110 | labelContainer: {
|
111 | position: 'absolute',
|
112 | },
|
113 | label: {
|
114 | fontSize: 16,
|
115 | color: '#6a7989',
|
116 | },
|
117 | textInput: {
|
118 | position: 'absolute',
|
119 | bottom: 2,
|
120 | left: PADDING,
|
121 | padding: 0,
|
122 | color: '#6a7989',
|
123 | fontSize: 18,
|
124 | fontWeight: 'bold',
|
125 | },
|
126 | labelMask: {
|
127 | height: 24,
|
128 | width: PADDING,
|
129 | },
|
130 | border: {
|
131 | position: 'absolute',
|
132 | bottom: 0,
|
133 | left: 0,
|
134 | right: 0,
|
135 | height: 3,
|
136 | },
|
137 | });
|