11import React from "react" ;
2- import { View , StyleProp , Image , Text , Animated , ViewStyle , ImageSourcePropType } from "react-native" ;
3- import RNBounceable from "@freakycoder/react-native-bounceable"
2+ import {
3+ View ,
4+ StyleProp ,
5+ Image ,
6+ Text ,
7+ Animated ,
8+ ViewStyle ,
9+ ImageSourcePropType ,
10+ TextStyle ,
11+ ImageStyle ,
12+ } from "react-native" ;
13+ import RNBounceable from "@freakycoder/react-native-bounceable" ;
414/**
515 * ? Local Imports
616 */
7- import styles , { _containerStyle } from "./SwitchButton.style" ;
17+ import styles , { _containerStyle , _imageStyle } from "./SwitchButton.style" ;
818
919const AnimatedRNBounceable = Animated . createAnimatedComponent ( RNBounceable ) ;
1020
11-
1221const MAIN_COLOR = "#f1bb7b" ;
1322const ORIGINAL_COLOR = "#fff" ;
1423const TINT_COLOR = "#f1bb7b" ;
1524const ORIGINAL_VALUE = 0 ;
1625const ANIMATED_VALUE = 1 ;
1726
1827type CustomStyleProp = StyleProp < ViewStyle > | Array < StyleProp < ViewStyle > > ;
28+ type CustomTextStyleProp = StyleProp < TextStyle > | Array < StyleProp < TextStyle > > ;
29+ type CustomImageStyleProp =
30+ | StyleProp < ImageStyle >
31+ | Array < StyleProp < ImageStyle > > ;
1932
2033interface ISwitchButtonProps {
2134 style ?: CustomStyleProp ;
35+ textStyle ?: CustomTextStyleProp ;
36+ imageStyle ?: CustomImageStyleProp ;
37+ textContainerStyle ?: CustomTextStyleProp ;
2238 activeImageSource : ImageSourcePropType ;
2339 inactiveImageSource : ImageSourcePropType ;
40+ text ?: string ;
2441 mainColor ?: string ;
25- originalColor ?: string ;
2642 tintColor ?: string ;
43+ disableText ?: boolean ;
44+ originalColor ?: string ;
45+ onPress : ( isActive : boolean ) => void ;
2746}
2847
2948interface IState {
30- isActive : boolean ;
49+ isActive : boolean ;
3150}
3251
33-
3452export default class SwitchButton extends React . Component <
3553 ISwitchButtonProps ,
3654 IState
3755> {
3856 interpolatedColor : Animated . Value ;
3957
40-
4158 constructor ( props : ISwitchButtonProps ) {
4259 super ( props ) ;
4360 this . interpolatedColor = new Animated . Value ( ORIGINAL_VALUE ) ;
4461 this . state = {
45- isActive : false
62+ isActive : false ,
4663 } ;
4764 }
4865
49-
5066 showOriginColor = ( ) => {
5167 Animated . timing ( this . interpolatedColor , {
5268 duration : 350 ,
@@ -63,7 +79,24 @@ export default class SwitchButton extends React.Component<
6379 } ) . start ( ) ;
6480 } ;
6581
66- render ( ) {
82+ handlePress = ( ) => {
83+ this . setState ( { isActive : ! this . state . isActive } , ( ) => {
84+ this . state . isActive ? this . showFocusColor ( ) : this . showOriginColor ( ) ;
85+ this . props . onPress && this . props . onPress ( this . state . isActive ) ;
86+ } ) ;
87+ } ;
88+
89+ /* -------------------------------------------------------------------------- */
90+ /* Render Methods */
91+ /* -------------------------------------------------------------------------- */
92+
93+ renderBounceableButton = ( ) => {
94+ const {
95+ style,
96+ imageStyle,
97+ activeImageSource,
98+ inactiveImageSource,
99+ } = this . props ;
67100 const mainColor = this . props . mainColor || MAIN_COLOR ;
68101 const originalColor = this . props . originalColor || ORIGINAL_COLOR ;
69102 const tintColor = this . props . tintColor || TINT_COLOR ;
@@ -75,21 +108,36 @@ export default class SwitchButton extends React.Component<
75108 inputRange : [ ORIGINAL_VALUE , ANIMATED_VALUE ] ,
76109 outputRange : [ tintColor , originalColor ] ,
77110 } ) ;
111+ return (
112+ < AnimatedRNBounceable
113+ style = { [ _containerStyle ( animatedBackgroundColor ) , style ] }
114+ onPress = { this . handlePress }
115+ >
116+ < Animated . Image
117+ source = { this . state . isActive ? activeImageSource : inactiveImageSource }
118+ style = { [ _imageStyle ( animatedTintColor ) , imageStyle ] }
119+ />
120+ </ AnimatedRNBounceable >
121+ ) ;
122+ } ;
78123
79- const { style, activeImageSource, inactiveImageSource } = this . props ;
124+ renderText = ( ) => {
125+ const { text, textStyle, textContainerStyle } = this . props ;
80126 return (
81- < View style = { { alignItems : "center" ,
82- } } >
83- < AnimatedRNBounceable style = { [ _containerStyle ( animatedBackgroundColor ) , style ] } onPress = { ( ) => { this . setState ( { isActive : ! this . state . isActive } , ( ) => {
84- this . state . isActive ? this . showFocusColor ( ) : this . showOriginColor ( )
85- } ) } } >
86- < Animated . Image source = { this . state . isActive ? activeImageSource : inactiveImageSource } style = { { height : 30 , width : 30 , tintColor : animatedTintColor } } />
87- </ AnimatedRNBounceable >
88- < View style = { { marginTop : 8 } } >
89- < Text > Notification</ Text >
127+ ! this . props . disableText && (
128+ < View style = { [ styles . textContainerStyle , textContainerStyle ] } >
129+ < Text style = { textStyle } > { text } </ Text >
90130 </ View >
131+ )
132+ ) ;
133+ } ;
134+
135+ render ( ) {
136+ return (
137+ < View style = { styles . container } >
138+ { this . renderBounceableButton ( ) }
139+ { this . renderText ( ) }
91140 </ View >
92141 ) ;
93142 }
94- } ;
95-
143+ }
0 commit comments