1+ import React , { JSXElementConstructor } from 'react' ;
2+ import { View , StyleSheet , Pressable } from 'react-native' ;
3+
4+
5+ interface ToggleProps {
6+
7+ /**
8+ * Color of the switch
9+ */
10+ color ?: string ,
11+
12+ /**
13+ * Size of the switch
14+ */
15+ size ?: number ,
16+
17+ /**
18+ * Pass a true value if you want to use a filled color switch
19+ */
20+ filled ?: boolean
21+
22+
23+ /**
24+ * Color of the circle in switch
25+ */
26+ circleColor ?: string ,
27+
28+
29+ /**
30+ * toggle status of the switch
31+ */
32+ toggle : boolean
33+
34+
35+ /**
36+ * Function to set the status of toggle switch which will be stored in your local state
37+ */
38+ setToggle : React . Dispatch < React . SetStateAction < undefined > > ,
39+
40+ }
41+
42+
43+ const Toggle : React . FC < ToggleProps > = ( { color, size, filled, circleColor, toggle, setToggle} ) => {
44+ const _color = color ?? "#4C956C" ;
45+ const _circle = circleColor ?? _color ;
46+ const _filled = filled ?? false ;
47+ const _size = size ?? 25 ;
48+
49+
50+ function toggleContainerStyles ( ) {
51+ if ( ! _filled ) {
52+ return [ styles ( _color , _size , _circle ) . toggleContainer , ( ! toggle ) ? { borderColor : "#AAAAAA" } : null ]
53+ } else {
54+ return [ styles ( _color , _size , _circle ) . toggleContainer , styles ( _color , _size , _circle ) . toggleContainerFilled , ( ! toggle ) ? { backgroundColor : "#AAAAAA" , borderColor : "#AAAAAA" } : null ]
55+ }
56+ }
57+
58+ function toggleCircleStyles ( ) {
59+ if ( ! _filled ) {
60+ return [ styles ( _color , _size , _circle ) . circle , ( toggle ) ? { alignSelf : "flex-end" } : { backgroundColor : "#AAAAAA" } ] ;
61+ } else {
62+ return [ styles ( _color , _size , _circle ) . circle , styles ( _color , _size , _circle ) . circleFilled , ( toggle ) ? { alignSelf : "flex-end" } : { backgroundColor : "#FEFEFE" } ]
63+ }
64+ }
65+
66+ return (
67+ < View >
68+ < Pressable style = { toggleContainerStyles ( ) } onPress = { ( ) => setToggle ( ! toggle ) } >
69+ < View style = { toggleCircleStyles ( ) } > </ View >
70+ </ Pressable >
71+ </ View >
72+ )
73+
74+
75+ }
76+
77+ export default Toggle ;
78+
79+
80+
81+ const styles = ( color :string , size :number , circleColor :string ) => {
82+ return StyleSheet . create ( {
83+ toggleContainer : { padding : size / 10 , width : 2.3 * size , borderRadius : size , borderColor : color , borderWidth : 2.5 } ,
84+ toggleContainerFilled : { backgroundColor : color , borderColor : color } ,
85+ circle : { width : size , height : size , borderRadius : size / 2 , backgroundColor : circleColor } ,
86+ circleFilled : { backgroundColor : circleColor } ,
87+ } ) ;
88+ }
0 commit comments