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