|
1 | | -import React from "react"; |
| 1 | +import * as React from "react"; |
2 | 2 | import { |
3 | | - View, |
4 | | - Dimensions, |
5 | 3 | StyleProp, |
6 | 4 | TextInput, |
7 | 5 | TextInputProps, |
8 | 6 | ViewStyle, |
| 7 | + Animated, |
| 8 | + Image, |
| 9 | + TouchableOpacity, |
| 10 | + View, |
| 11 | + TextStyle, |
| 12 | + ImageStyle, |
| 13 | + ImageSourcePropType, |
9 | 14 | } from "react-native"; |
10 | 15 | /** |
11 | 16 | * ? Local Imports |
12 | 17 | */ |
13 | | -import styles from "./InteractiveTextInput.style"; |
| 18 | +import styles, { _textInputStyle } from "./InteractiveTextInput.style"; |
14 | 19 |
|
15 | | -const { width: ScreenWidth, height: ScreenHeight } = Dimensions.get("screen"); |
| 20 | +const AnimatedTextInput = Animated.createAnimatedComponent(TextInput); |
| 21 | + |
| 22 | +const MAIN_COLOR = "#008FEB"; |
| 23 | +const ORIGINAL_COLOR = "transparent"; |
| 24 | +const PLACEHOLDER_COLOR = "#757575"; |
| 25 | +const ORIGINAL_VALUE = 0; |
| 26 | +const ANIMATED_VALUE = 1; |
16 | 27 |
|
17 | 28 | type CustomStyleProp = StyleProp<ViewStyle> | Array<StyleProp<ViewStyle>>; |
| 29 | +type CustomTextStyleProp = StyleProp<TextStyle> | Array<StyleProp<TextStyle>>; |
| 30 | +type CustomImageStyleProp = |
| 31 | + | StyleProp<ImageStyle> |
| 32 | + | Array<StyleProp<ImageStyle>>; |
18 | 33 |
|
19 | 34 | interface IInteractiveTextInputProps extends TextInputProps { |
20 | 35 | style?: CustomStyleProp; |
| 36 | + textInputStyle?: CustomTextStyleProp; |
| 37 | + iconContainerStyle?: CustomStyleProp; |
| 38 | + iconImageStyle?: CustomImageStyleProp; |
| 39 | + iconImageSource?: ImageSourcePropType; |
| 40 | + ImageComponent?: any; |
| 41 | + IconComponent?: any; |
| 42 | + enableIcon?: boolean; |
| 43 | + mainColor?: string; |
| 44 | + originalColor?: string; |
| 45 | + animatedPlaceholderTextColor?: string; |
| 46 | + onFocus?: () => void; |
| 47 | + onBlur?: () => void; |
| 48 | + onIconPress?: () => void; |
21 | 49 | } |
22 | 50 |
|
23 | | -const InteractiveTextInput: React.FC<IInteractiveTextInputProps> = ({ |
24 | | - style, |
25 | | -}) => { |
26 | | - return ( |
27 | | - <TextInput |
28 | | - style={{ |
29 | | - height: 50, |
30 | | - width: ScreenWidth * 0.9, |
31 | | - backgroundColor: "#f5f6f8", |
32 | | - paddingLeft: 8, |
33 | | - paddingRight: 8, |
34 | | - borderRadius: 8, |
35 | | - justifyContent: "center", |
36 | | - }} |
37 | | - placeholder="Email" |
38 | | - /> |
39 | | - ); |
40 | | -}; |
41 | | - |
42 | | -export default InteractiveTextInput; |
| 51 | +interface IState {} |
| 52 | + |
| 53 | +export default class InteractiveTextInput extends React.Component< |
| 54 | + IInteractiveTextInputProps, |
| 55 | + IState |
| 56 | +> { |
| 57 | + interpolatedColor: Animated.Value; |
| 58 | + |
| 59 | + constructor(props: IInteractiveTextInputProps) { |
| 60 | + super(props); |
| 61 | + this.interpolatedColor = new Animated.Value(ORIGINAL_VALUE); |
| 62 | + this.state = {}; |
| 63 | + } |
| 64 | + |
| 65 | + showOriginColor = () => { |
| 66 | + Animated.timing(this.interpolatedColor, { |
| 67 | + duration: 350, |
| 68 | + toValue: ORIGINAL_VALUE, |
| 69 | + useNativeDriver: false, |
| 70 | + }).start(); |
| 71 | + }; |
| 72 | + |
| 73 | + showFocusColor = () => { |
| 74 | + Animated.timing(this.interpolatedColor, { |
| 75 | + duration: 450, |
| 76 | + toValue: ANIMATED_VALUE, |
| 77 | + useNativeDriver: false, |
| 78 | + }).start(); |
| 79 | + }; |
| 80 | + |
| 81 | + /* -------------------------------------------------------------------------- */ |
| 82 | + /* Render Methods */ |
| 83 | + /* -------------------------------------------------------------------------- */ |
| 84 | + |
| 85 | + renderIcon = () => { |
| 86 | + const { |
| 87 | + enableIcon, |
| 88 | + iconImageStyle, |
| 89 | + iconContainerStyle, |
| 90 | + iconImageSource, |
| 91 | + onIconPress, |
| 92 | + ImageComponent = Image, |
| 93 | + IconComponent = TouchableOpacity, |
| 94 | + } = this.props; |
| 95 | + |
| 96 | + return ( |
| 97 | + enableIcon && ( |
| 98 | + <IconComponent |
| 99 | + style={[styles.iconContainerStyle, iconContainerStyle]} |
| 100 | + onPress={onIconPress} |
| 101 | + > |
| 102 | + <ImageComponent |
| 103 | + resizeMode="contain" |
| 104 | + source={iconImageSource} |
| 105 | + style={[styles.iconImageStyle, iconImageStyle]} |
| 106 | + /> |
| 107 | + </IconComponent> |
| 108 | + ) |
| 109 | + ); |
| 110 | + }; |
| 111 | + |
| 112 | + renderAnimatedTextInput = () => { |
| 113 | + const mainColor = this.props.mainColor || MAIN_COLOR; |
| 114 | + const originalColor = this.props.originalColor || ORIGINAL_COLOR; |
| 115 | + const animatedPlaceholderTextColor = |
| 116 | + this.props.animatedPlaceholderTextColor || PLACEHOLDER_COLOR; |
| 117 | + |
| 118 | + let borderColor = this.interpolatedColor.interpolate({ |
| 119 | + inputRange: [ORIGINAL_VALUE, ANIMATED_VALUE], |
| 120 | + outputRange: [originalColor, mainColor], |
| 121 | + }); |
| 122 | + let placeholderTextColor = this.interpolatedColor.interpolate({ |
| 123 | + inputRange: [ORIGINAL_VALUE, ANIMATED_VALUE], |
| 124 | + outputRange: [animatedPlaceholderTextColor, mainColor], |
| 125 | + }); |
| 126 | + return ( |
| 127 | + <AnimatedTextInput |
| 128 | + placeholderTextColor={placeholderTextColor} |
| 129 | + placeholder="Email" |
| 130 | + {...this.props} |
| 131 | + style={[_textInputStyle(borderColor), this.props.textInputStyle]} |
| 132 | + onFocus={() => { |
| 133 | + this.showFocusColor(); |
| 134 | + this.props.onFocus && this.props.onFocus(); |
| 135 | + }} |
| 136 | + onBlur={() => { |
| 137 | + this.showOriginColor(); |
| 138 | + this.props.onBlur && this.props.onBlur(); |
| 139 | + }} |
| 140 | + /> |
| 141 | + ); |
| 142 | + }; |
| 143 | + |
| 144 | + render() { |
| 145 | + return ( |
| 146 | + <View style={styles.container}> |
| 147 | + {this.renderAnimatedTextInput()} |
| 148 | + {this.renderIcon()} |
| 149 | + </View> |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments