-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.tsx
201 lines (183 loc) · 5.41 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {
Animated,
StyleProp,
StyleSheet,
Text,
TextStyle,
TouchableOpacity,
View,
ViewStyle,
} from 'react-native';
import React, {useEffect, useRef, useState} from 'react';
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
},
});
interface Props {
testID?: string;
switchOn: boolean;
onPress: () => void;
containerStyle?: ViewStyle;
circleStyle?: ViewStyle;
backgroundColorOn?: string;
backgroundColorOff?: string;
backgroundImageOn?: React.ReactElement;
backgroundImageOff?: React.ReactElement;
circleColorOff?: string;
circleColorOn?: string;
duration?: number;
type?: number;
buttonText?: string;
backTextRight?: string;
backTextLeft?: string;
buttonTextStyle?: StyleProp<TextStyle>;
textRightStyle?: StyleProp<TextStyle>;
textLeftStyle?: StyleProp<TextStyle>;
buttonStyle?: StyleProp<ViewStyle>;
// limitation: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/12202
buttonContainerStyle?: StyleProp<ViewStyle>;
rightContainerStyle?: StyleProp<ViewStyle>;
leftContainerStyle?: StyleProp<ViewStyle>;
RTL?: boolean;
}
function SwitchToggle(props: Props): React.ReactElement {
const {
backgroundColorOn = 'black',
backgroundColorOff = '#C4C4C4',
circleColorOn = 'white',
circleColorOff = '#6D6D6D',
duration = 300,
backgroundImageOn,
backgroundImageOff,
containerStyle = {
marginTop: 16,
width: 80,
height: 40,
borderRadius: 25,
padding: 5,
},
circleStyle = {
width: 32,
height: 32,
borderRadius: 16,
},
} = props;
const [animXValue] = useState(new Animated.Value(props.switchOn ? 1 : 0));
const getStart = (): number | Record<string, unknown> | undefined => {
return props.type === undefined
? 0
: props.type === 0
? 0
: props.containerStyle && props.containerStyle.padding
? (props.containerStyle.padding as number) * 2
: {};
};
// eslint-disable-next-line react-hooks/exhaustive-deps
const runAnimation = (): void => {
const animValue = {
fromValue: props.switchOn ? 0 : 1,
toValue: props.switchOn ? 1 : 0,
duration,
useNativeDriver: false,
};
Animated.timing(animXValue, animValue).start();
};
const endPos =
props.containerStyle && props.circleStyle
? (props.containerStyle.width as number) -
((props.circleStyle.width as number) +
((props.containerStyle.padding as number) || 0) * 2)
: 0;
const circlePosXEnd = props.RTL ? -endPos : endPos;
const [circlePosXStart] = useState(getStart());
const prevSwitchOnRef = useRef<boolean>();
const prevSwitchOn = !!prevSwitchOnRef.current;
useEffect(() => {
prevSwitchOnRef.current = props.switchOn;
if (prevSwitchOn !== props.switchOn) runAnimation();
}, [prevSwitchOn, props.switchOn, runAnimation]);
const generateRightText = (): React.ReactElement => {
return (
<Animated.View style={props.rightContainerStyle}>
<Text style={props.textRightStyle}>{props.backTextRight}</Text>
</Animated.View>
);
};
const generateLeftText = (): React.ReactElement => {
return (
<Animated.View style={props.leftContainerStyle}>
<Text style={props.textLeftStyle}>{props.backTextLeft}</Text>
</Animated.View>
);
};
const generateLeftIcon = (): React.ReactElement => {
return (
<View style={{position: 'absolute', left: 5}}>{backgroundImageOn}</View>
);
};
const generateRightIcon = (): React.ReactElement => {
return (
<View style={{position: 'absolute', right: 5}}>{backgroundImageOff}</View>
);
};
return (
<TouchableOpacity
testID={props.testID}
onPress={props.onPress}
activeOpacity={0.8}>
<Animated.View
style={[
styles.container,
props.containerStyle,
{
backgroundColor: animXValue.interpolate({
inputRange: [0, 1],
outputRange: [
backgroundColorOff as string | number,
backgroundColorOn as string | number,
] as string[] | number[],
}),
},
]}>
{generateLeftText()}
{props.switchOn && generateLeftIcon()}
<Animated.View
style={[
props.circleStyle,
{
backgroundColor: animXValue.interpolate({
inputRange: [0, 1],
outputRange: [
circleColorOff as string | number,
circleColorOn as string | number,
] as string[] | number[],
}),
},
{
transform: [
{
translateX: animXValue.interpolate({
inputRange: [0, 1],
outputRange: [
circlePosXStart as string | number,
circlePosXEnd as string | number,
] as string[] | number[],
}),
},
],
},
props.buttonStyle,
]}>
<Animated.View style={props.buttonContainerStyle}>
<Text style={props.buttonTextStyle}>{props.buttonText}</Text>
</Animated.View>
</Animated.View>
{generateRightText()}
{!props.switchOn && generateRightIcon()}
</Animated.View>
</TouchableOpacity>
);
}
export default SwitchToggle;