|
| 1 | +import * as React from "react"; |
| 2 | +import { |
| 3 | + View, |
| 4 | + Text, |
| 5 | + Animated, |
| 6 | + ViewStyle, |
| 7 | + StyleProp, |
| 8 | + TextStyle, |
| 9 | + ImageStyle, |
| 10 | + ImageSourcePropType, |
| 11 | +} from "react-native"; |
| 12 | +import RNBounceable from "@freakycoder/react-native-bounceable"; |
| 13 | +/** |
| 14 | + * ? Local Imports |
| 15 | + */ |
| 16 | +import styles, { _containerStyle, _imageStyle } from "./SwitchButton.style"; |
| 17 | + |
| 18 | +const AnimatedRNBounceable = Animated.createAnimatedComponent(RNBounceable); |
| 19 | + |
| 20 | +const MAIN_COLOR = "#f1bb7b"; |
| 21 | +const ORIGINAL_COLOR = "#fff"; |
| 22 | +const TINT_COLOR = "#f1bb7b"; |
| 23 | +const ORIGINAL_VALUE = 0; |
| 24 | +const ANIMATED_VALUE = 1; |
| 25 | + |
| 26 | +type CustomStyleProp = StyleProp<ViewStyle> | Array<StyleProp<ViewStyle>>; |
| 27 | +type CustomTextStyleProp = StyleProp<TextStyle> | Array<StyleProp<TextStyle>>; |
| 28 | +type CustomImageStyleProp = |
| 29 | + | StyleProp<ImageStyle> |
| 30 | + | Array<StyleProp<ImageStyle>>; |
| 31 | + |
| 32 | +interface ISwitchButtonProps { |
| 33 | + style?: CustomStyleProp; |
| 34 | + textStyle?: CustomTextStyleProp; |
| 35 | + imageStyle?: CustomImageStyleProp; |
| 36 | + textContainerStyle?: CustomTextStyleProp; |
| 37 | + activeImageSource: ImageSourcePropType; |
| 38 | + inactiveImageSource: ImageSourcePropType; |
| 39 | + text?: string; |
| 40 | + mainColor?: string; |
| 41 | + tintColor?: string; |
| 42 | + isActive?: boolean; |
| 43 | + disableText?: boolean; |
| 44 | + originalColor?: string; |
| 45 | + onPress: (isActive: boolean) => void; |
| 46 | +} |
| 47 | + |
| 48 | +interface IState { |
| 49 | + isActive: boolean; |
| 50 | +} |
| 51 | + |
| 52 | +export default class SwitchButton extends React.Component< |
| 53 | + ISwitchButtonProps, |
| 54 | + IState |
| 55 | +> { |
| 56 | + interpolatedColor: Animated.Value; |
| 57 | + |
| 58 | + constructor(props: ISwitchButtonProps) { |
| 59 | + super(props); |
| 60 | + this.interpolatedColor = new Animated.Value(ORIGINAL_VALUE); |
| 61 | + this.state = { |
| 62 | + isActive: false, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + componentDidMount() { |
| 67 | + this.handleActiveState(); |
| 68 | + } |
| 69 | + |
| 70 | + handleActiveState = () => { |
| 71 | + if (this.props.isActive) { |
| 72 | + this.setState({ isActive: !this.state.isActive }, () => { |
| 73 | + this.state.isActive ? this.showFocusColor() : this.showOriginColor(); |
| 74 | + }); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + showOriginColor = () => { |
| 79 | + Animated.timing(this.interpolatedColor, { |
| 80 | + duration: 350, |
| 81 | + toValue: ORIGINAL_VALUE, |
| 82 | + useNativeDriver: false, |
| 83 | + }).start(); |
| 84 | + }; |
| 85 | + |
| 86 | + showFocusColor = () => { |
| 87 | + Animated.timing(this.interpolatedColor, { |
| 88 | + duration: 450, |
| 89 | + toValue: ANIMATED_VALUE, |
| 90 | + useNativeDriver: false, |
| 91 | + }).start(); |
| 92 | + }; |
| 93 | + |
| 94 | + handlePress = () => { |
| 95 | + this.setState({ isActive: !this.state.isActive }, () => { |
| 96 | + this.state.isActive ? this.showFocusColor() : this.showOriginColor(); |
| 97 | + this.props.onPress && this.props.onPress(this.state.isActive); |
| 98 | + }); |
| 99 | + }; |
| 100 | + |
| 101 | + /* -------------------------------------------------------------------------- */ |
| 102 | + /* Render Methods */ |
| 103 | + /* -------------------------------------------------------------------------- */ |
| 104 | + |
| 105 | + renderBounceableButton = () => { |
| 106 | + const { |
| 107 | + style, |
| 108 | + imageStyle, |
| 109 | + activeImageSource, |
| 110 | + inactiveImageSource, |
| 111 | + } = this.props; |
| 112 | + const mainColor = this.props.mainColor || MAIN_COLOR; |
| 113 | + const originalColor = this.props.originalColor || ORIGINAL_COLOR; |
| 114 | + const tintColor = this.props.tintColor || TINT_COLOR; |
| 115 | + const _animatedBGColorOutputRange = this.state.isActive |
| 116 | + ? [mainColor, originalColor] |
| 117 | + : [originalColor, mainColor]; |
| 118 | + const _animatedTintColorOutputRange = this.state.isActive |
| 119 | + ? [originalColor, tintColor] |
| 120 | + : [tintColor, originalColor]; |
| 121 | + let animatedBackgroundColor = this.interpolatedColor.interpolate({ |
| 122 | + inputRange: [ORIGINAL_VALUE, ANIMATED_VALUE], |
| 123 | + outputRange: [originalColor, mainColor], |
| 124 | + }); |
| 125 | + let animatedTintColor = this.interpolatedColor.interpolate({ |
| 126 | + inputRange: [ORIGINAL_VALUE, ANIMATED_VALUE], |
| 127 | + outputRange: [tintColor, originalColor], |
| 128 | + }); |
| 129 | + return ( |
| 130 | + <AnimatedRNBounceable |
| 131 | + style={[_containerStyle(animatedBackgroundColor), style]} |
| 132 | + onPress={this.handlePress} |
| 133 | + > |
| 134 | + <Animated.Image |
| 135 | + source={this.state.isActive ? activeImageSource : inactiveImageSource} |
| 136 | + style={[_imageStyle(animatedTintColor), imageStyle]} |
| 137 | + /> |
| 138 | + </AnimatedRNBounceable> |
| 139 | + ); |
| 140 | + }; |
| 141 | + |
| 142 | + renderText = () => { |
| 143 | + const { text, textStyle, textContainerStyle } = this.props; |
| 144 | + return ( |
| 145 | + !this.props.disableText && ( |
| 146 | + <View style={[styles.textContainerStyle, textContainerStyle]}> |
| 147 | + <Text style={textStyle}>{text}</Text> |
| 148 | + </View> |
| 149 | + ) |
| 150 | + ); |
| 151 | + }; |
| 152 | + |
| 153 | + render() { |
| 154 | + return ( |
| 155 | + <View style={styles.container}> |
| 156 | + {this.renderBounceableButton()} |
| 157 | + {this.renderText()} |
| 158 | + </View> |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments