|
| 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 LABEL_HEIGHT = 24; |
| 14 | +const PADDING = 16; |
| 15 | + |
| 16 | +export default class Sae extends BaseInput { |
| 17 | + |
| 18 | + static propTypes = { |
| 19 | + height: PropTypes.number, |
| 20 | + /* |
| 21 | + * This is the icon component you are importing from react-native-vector-icons. |
| 22 | + * import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome'; |
| 23 | + * iconClass={FontAwesomeIcon} |
| 24 | + */ |
| 25 | + iconClass: PropTypes.func.isRequired, |
| 26 | + /* |
| 27 | + * Passed to react-native-vector-icons library as name prop |
| 28 | + */ |
| 29 | + iconName: PropTypes.string, |
| 30 | + /* |
| 31 | + * Passed to react-native-vector-icons library as color prop. |
| 32 | + * This is also used as border color. |
| 33 | + */ |
| 34 | + iconColor: PropTypes.string, |
| 35 | + }; |
| 36 | + |
| 37 | + static defaultProps = { |
| 38 | + iconColor: 'white', |
| 39 | + height: 48, |
| 40 | + animationDuration: 300, |
| 41 | + iconName: 'pencil', |
| 42 | + }; |
| 43 | + |
| 44 | + render() { |
| 45 | + const { |
| 46 | + iconClass, |
| 47 | + iconColor, |
| 48 | + iconName, |
| 49 | + label, |
| 50 | + style: containerStyle, |
| 51 | + height: inputHeight, |
| 52 | + inputStyle, |
| 53 | + labelStyle, |
| 54 | + } = this.props; |
| 55 | + const { |
| 56 | + width, |
| 57 | + focusedAnim, |
| 58 | + value, |
| 59 | + } = this.state; |
| 60 | + const AnimatedIcon = Animated.createAnimatedComponent(iconClass); |
| 61 | + |
| 62 | + return ( |
| 63 | + <View |
| 64 | + style={[containerStyle, styles.container, { |
| 65 | + height: inputHeight + PADDING, |
| 66 | + }]} |
| 67 | + onLayout={this._onLayout} |
| 68 | + > |
| 69 | + <TouchableWithoutFeedback onPress={this._focus}> |
| 70 | + <Animated.View style={{ |
| 71 | + position: 'absolute', |
| 72 | + bottom: focusedAnim.interpolate({ |
| 73 | + inputRange: [0, 1], |
| 74 | + outputRange: [0, LABEL_HEIGHT + PADDING], |
| 75 | + }), |
| 76 | + }}> |
| 77 | + <Animated.Text style={[styles.label, labelStyle, { |
| 78 | + fontSize: focusedAnim.interpolate({ |
| 79 | + inputRange: [0, 1], |
| 80 | + outputRange: [18, 12], |
| 81 | + }), |
| 82 | + }]}> |
| 83 | + {label} |
| 84 | + </Animated.Text> |
| 85 | + </Animated.View> |
| 86 | + </TouchableWithoutFeedback> |
| 87 | + <TextInput |
| 88 | + ref="input" |
| 89 | + {...this.props} |
| 90 | + style={[styles.textInput, inputStyle, { |
| 91 | + width, |
| 92 | + height: inputHeight, |
| 93 | + }]} |
| 94 | + value={value} |
| 95 | + onBlur={this._onBlur} |
| 96 | + onChange={this._onChange} |
| 97 | + onFocus={this._onFocus} |
| 98 | + /> |
| 99 | + <AnimatedIcon |
| 100 | + name={iconName} |
| 101 | + color={iconColor} |
| 102 | + style={{ |
| 103 | + position: 'absolute', |
| 104 | + bottom: 0, |
| 105 | + right: focusedAnim.interpolate({ |
| 106 | + inputRange: [0, 1], |
| 107 | + outputRange: [0, width + PADDING], |
| 108 | + }), |
| 109 | + transform: [{ |
| 110 | + rotate: focusedAnim.interpolate({ |
| 111 | + inputRange: [0, 1], |
| 112 | + outputRange: ['0deg', '-90deg'], |
| 113 | + }), |
| 114 | + }], |
| 115 | + fontSize: 20, |
| 116 | + backgroundColor: 'transparent', |
| 117 | + }} |
| 118 | + /> |
| 119 | + {/* bottom border */} |
| 120 | + <Animated.View |
| 121 | + style={{ |
| 122 | + position: 'absolute', |
| 123 | + bottom: 0, |
| 124 | + right: 0, |
| 125 | + height: 2, |
| 126 | + width: focusedAnim.interpolate({ |
| 127 | + inputRange: [0, 1], |
| 128 | + outputRange: [0, width], |
| 129 | + }), |
| 130 | + backgroundColor: iconColor, |
| 131 | + }} |
| 132 | + /> |
| 133 | + </View> |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +const styles = StyleSheet.create({ |
| 139 | + container: { |
| 140 | + overflow: 'hidden', |
| 141 | + marginHorizontal: PADDING, |
| 142 | + }, |
| 143 | + label: { |
| 144 | + backgroundColor: 'transparent', |
| 145 | + fontFamily: 'Arial', |
| 146 | + fontWeight: 'bold', |
| 147 | + color: '#7771ab', |
| 148 | + }, |
| 149 | + textInput: { |
| 150 | + position: 'absolute', |
| 151 | + bottom: 0, |
| 152 | + left: 0, |
| 153 | + color: 'white', |
| 154 | + fontSize: 18, |
| 155 | + }, |
| 156 | +}); |
0 commit comments