@@ -10,46 +10,53 @@ import Reanimated, {
1010 useSharedValue ,
1111 withSpring ,
1212 runOnJS ,
13+ withTiming ,
1314} from 'react-native-reanimated' ;
1415import type { SwitchProps } from '../types' ;
1516
1617const spring = (
1718 _value : any ,
18- config : any = { damping : 20 , stiffness : 120 } ,
19+ config : any = { damping : 15 , stiffness : 120 } ,
1920 callback ?: AnimationCallback
2021) => withSpring ( _value , config , callback ) ;
2122
23+ const timing = (
24+ _value : any ,
25+ config : any = { duration : 350 } ,
26+ callback ?: AnimationCallback
27+ ) => withTiming ( _value , config , callback ) ;
28+
2229const PADDINGHORIZONTAL = 2 ;
2330
24- const isNumbre = ( value : any , defaultValue = 0 ) => {
31+ const isNumber = ( value : any , defaultValue = 0 ) => {
2532 value = Number ( value ) ;
2633 if ( typeof value === 'number' && ! isNaN ( value ) && value !== null ) {
2734 return value ;
2835 }
2936 return defaultValue ;
3037} ;
3138
32- const Switch = ( IProps : SwitchProps ) : JSX . Element => {
39+ const Switch = ( IProps : SwitchProps ) : React . JSX . Element => {
3340 const {
34- value,
35- activeText,
36- inActiveText,
37- backgroundActive,
38- backgroundInActive,
39- circleActiveColor,
40- circleInActiveColor,
41- circleSize,
42- width,
43- onValueChange,
44- switchBorderRadius,
41+ value = false ,
42+ activeText = 'ON' ,
43+ inActiveText = 'OFF' ,
44+ backgroundActive = '#249c00' ,
45+ backgroundInActive = '#333' ,
46+ circleActiveColor = '#fff' ,
47+ circleInActiveColor = '#fff' ,
48+ circleSize = 30 ,
49+ width = 100 ,
50+ onValueChange = undefined ,
51+ switchBorderRadius = 30 ,
4552 textStyle,
46- disabled,
47- switchPaddingRight,
48- switchPaddingLeft,
53+ disabled = false ,
54+ switchPaddingRight = PADDINGHORIZONTAL ,
55+ switchPaddingLeft = PADDINGHORIZONTAL ,
4956 switchStyle,
5057 circleChildrenActive,
5158 circleChildrenInActive,
52- onAnimationEnd,
59+ onAnimationEnd = undefined ,
5360 } = IProps ;
5461
5562 const { isRTL } = I18nManager ;
@@ -62,17 +69,17 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
6269 const circleColor = useSharedValue < string | undefined > ( circleInActiveColor ) ;
6370
6471 const [ defaultWidth , setDefaultWidth ] = useState < number > (
65- isNumbre ( width , 100 )
72+ isNumber ( width , 100 )
6673 ) ;
6774 const [ defaultCircleSize , setDefaultCircleSize ] = useState < number > (
68- isNumbre ( circleSize , 30 )
75+ isNumber ( circleSize , 30 )
6976 ) ;
7077 const [ defaultPadding , setDefaultPadding ] = useState < {
7178 paddingLeft : number ;
7279 paddingRight : number ;
7380 } > ( {
74- paddingLeft : isNumbre ( switchPaddingLeft , PADDINGHORIZONTAL ) ,
75- paddingRight : isNumbre ( switchPaddingRight , PADDINGHORIZONTAL ) ,
81+ paddingLeft : isNumber ( switchPaddingLeft , PADDINGHORIZONTAL ) ,
82+ paddingRight : isNumber ( switchPaddingRight , PADDINGHORIZONTAL ) ,
7683 } ) ;
7784
7885 const circleStyle = useAnimatedStyle ( ( ) => {
@@ -127,8 +134,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
127134 const translateXSpringConfig = { damping : 15 , stiffness : 300 } ;
128135
129136 const onActive = ( size : number , factory : number ) => {
130- textTranslateXActive . value = spring ( 0 ) ;
131- textTranslateXInActive . value = spring ( factory * defaultWidth ) ;
137+ textTranslateXActive . value = timing ( 0 ) ;
138+ textTranslateXInActive . value = timing ( factory * defaultWidth ) ;
132139 if ( circleActiveColor ) {
133140 circleColor . value = spring ( circleActiveColor , {
134141 damping : 20 ,
@@ -150,8 +157,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
150157 } ;
151158
152159 const onInActive = ( _ : number , factory : number ) => {
153- textTranslateXActive . value = spring ( - ( defaultWidth * factory ) ) ;
154- textTranslateXInActive . value = spring ( 0 ) ;
160+ textTranslateXActive . value = timing ( - ( defaultWidth * factory ) ) ;
161+ textTranslateXInActive . value = timing ( 0 ) ;
155162 if ( circleInActiveColor ) {
156163 circleColor . value = spring ( circleInActiveColor , {
157164 damping : 20 ,
@@ -173,18 +180,18 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
173180 } ;
174181
175182 useEffect ( ( ) => {
176- setDefaultWidth ( isNumbre ( width , 100 ) ) ;
183+ setDefaultWidth ( isNumber ( width , 100 ) ) ;
177184 } , [ width ] ) ;
178185
179186 useEffect ( ( ) => {
180187 setDefaultPadding ( {
181- paddingLeft : isNumbre ( switchPaddingLeft , PADDINGHORIZONTAL ) ,
182- paddingRight : isNumbre ( switchPaddingRight , PADDINGHORIZONTAL ) ,
188+ paddingLeft : isNumber ( switchPaddingLeft , PADDINGHORIZONTAL ) ,
189+ paddingRight : isNumber ( switchPaddingRight , PADDINGHORIZONTAL ) ,
183190 } ) ;
184191 } , [ switchPaddingLeft , switchPaddingRight ] ) ;
185192
186193 useEffect ( ( ) => {
187- setDefaultCircleSize ( isNumbre ( circleSize , 30 ) ) ;
194+ setDefaultCircleSize ( isNumber ( circleSize , 30 ) ) ;
188195 } , [ circleSize ] ) ;
189196
190197 useEffect ( ( ) => {
@@ -199,6 +206,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
199206 } else {
200207 onInActive ( size , factory ) ;
201208 }
209+ // eslint-disable-next-line react-hooks/exhaustive-deps
202210 } , [ value , defaultWidth , defaultCircleSize , defaultPadding , isRTL ] ) ;
203211
204212 useEffect ( ( ) => {
@@ -207,6 +215,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
207215 } else {
208216 opacity . value = spring ( 1 ) ;
209217 }
218+ // eslint-disable-next-line react-hooks/exhaustive-deps
210219 } , [ disabled ] ) ;
211220
212221 return (
@@ -221,7 +230,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
221230 style = { [
222231 styles . switch ,
223232 {
224- borderRadius : isNumbre ( switchBorderRadius , 30 ) ,
233+ borderRadius : isNumber ( switchBorderRadius , 30 ) ,
225234 width : defaultWidth ,
226235 } ,
227236 switchStyle ,
@@ -279,7 +288,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
279288 {
280289 width : defaultCircleSize ,
281290 height : defaultCircleSize ,
282- borderRadius : isNumbre ( switchBorderRadius , 30 ) ,
291+ borderRadius : isNumber ( switchBorderRadius , 30 ) ,
283292 } ,
284293 circleStyle ,
285294 ] }
@@ -308,23 +317,6 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
308317 ) ;
309318} ;
310319
311- Switch . defaultProps = {
312- disabled : false ,
313- value : false ,
314- onValueChange : undefined ,
315- onAnimationEnd : undefined ,
316- activeText : 'ON' ,
317- inActiveText : 'OFF' ,
318- backgroundActive : '#249c00' ,
319- backgroundInActive : '#333' ,
320- circleActiveColor : '#fff' ,
321- circleInActiveColor : '#fff' ,
322- circleSize : 30 ,
323- switchBorderRadius : 30 ,
324- width : 100 ,
325- switchPaddingRight : PADDINGHORIZONTAL ,
326- switchPaddingLeft : PADDINGHORIZONTAL ,
327- } ;
328320
329321const styles = StyleSheet . create ( {
330322 switch : {
0 commit comments