-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSwitchButton.js
218 lines (180 loc) · 8.04 KB
/
SwitchButton.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
how to use:
1) import component
2) set activeSwitch=1 to state
3) use <MySwitchButton onValueChange={(val) => this.setState({ ttttt: val })} /> in your code
4) use { this.state.activeSwitch === 1 ? view1 : view2 }
small example: ...
constructor () {
super();
this.state = {
activeSwitch: 1
};
}
render () {
return (
<View>
<SwitchButton
onValueChange={(val) => this.setState({ activeSwitch: val })} // this is necessary for this component
text1 = 'ON' // optional: first text in switch button --- default ON
text2 = 'OFF' // optional: second text in switch button --- default OFF
switchWidth = {100} // optional: switch width --- default 44
switchHeight = {44} // optional: switch height --- default 100
switchdirection = 'rtl' // optional: switch button direction ( ltr and rtl ) --- default ltr
switchBorderRadius = {100} // optional: switch border radius --- default oval
switchSpeedChange = {500} // optional: button change speed --- default 100
switchBorderColor = '#d4d4d4' // optional: switch border color --- default #d4d4d4
switchBackgroundColor = '#fff' // optional: switch background color --- default #fff
btnBorderColor = '#00a4b9' // optional: button border color --- default #00a4b9
btnBackgroundColor = '#00bcd4' // optional: button background color --- default #00bcd4
fontColor = '#b1b1b1' // optional: text font color --- default #b1b1b1
activeFontColor = '#fff' // optional: active font color --- default #fff
/>
</View>
);
}
*/
import React, { Component } from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
Animated
} from 'react-native';
import PropTypes from 'prop-types';
export default class SwitchButton extends Component {
static propTypes = {
onValueChange: PropTypes.func
};
static defaultProps = {
onValueChange: () => null
};
constructor() {
super();
this.state = {
activeSwitch: 1,
sbWidth: 100,
sbHeight: 44,
direction: 'ltr',
offsetX: new Animated.Value(0)
};
this._switchDirection = this._switchDirection.bind(this);
}
_switchDirection(direction) {
let dir = 'row';
if (direction === 'rtl') {
dir = 'row-reverse';
}
else {
dir = 'row';
}
return dir;
}
_switchThump(direction) {
const { onValueChange, disabled } = this.props;
let dirsign = 1;
if (direction === 'rtl') {
dirsign = -1;
}
else {
dirsign = 1;
}
if (this.state.activeSwitch === 1){
this.setState({ activeSwitch: 2 }, () => onValueChange(this.state.activeSwitch));
Animated.timing(
this.state.offsetX,
{
toValue: (((this.props.switchWidth || this.state.sbWidth) / 2) - 6) * dirsign,
duration: this.props.switchSpeedChange || 100
}
).start();
}
else {
this.setState({ activeSwitch: 1 }, () => onValueChange(this.state.activeSwitch));
Animated.timing(
this.state.offsetX,
{
toValue: 0,
duration: this.props.switchSpeedChange || 100
}
).start();
}
}
render() {
return (
<View>
<TouchableOpacity activeOpacity={1} onPress={ () => { this._switchThump(this.props.switchdirection || this.state.direction) } }>
<View
style={[{
width: this.props.switchWidth || this.state.sbWidth,
height: this.props.switchHeight || this.state.sbHeight,
borderRadius: this.props.switchBorderRadius !== undefined ? this.props.switchBorderRadius : this.state.sbHeight / 2,
borderWidth: 1,
borderColor: this.props.switchBorderColor || "#d4d4d4",
backgroundColor: this.props.switchBackgroundColor || "#fff"
}]}
>
<View style={[{ flexDirection: this._switchDirection(this.props.switchdirection || this.state.direction) }]} >
<Animated.View style={{ transform: [{translateX: this.state.offsetX }] }}>
<View
style={[switchStyles.wayBtnActive,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
borderRadius: this.props.switchBorderRadius !== undefined ? this.props.switchBorderRadius : this.state.sbHeight / 2,
borderColor: this.props.btnBorderColor || "#00a4b9",
backgroundColor: this.props.btnBackgroundColor || "#00bcd4"
}]}
/>
</Animated.View>
<View style={[switchStyles.textPos,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
left: 0
}]}
>
<Text style={[ this.state.activeSwitch === 1 ? { color: this.props.activeFontColor || "#fff" } : { color: this.props.fontColor || "#b1b1b1" } ]}>
{ this.props.text1 || 'ON' }
</Text>
</View>
<View
style={[switchStyles.textPos,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
right:0
}]}
>
<Text style={[ this.state.activeSwitch === 2 ? { color: this.props.activeFontColor || "#fff" } : { color: this.props.fontColor || "#b1b1b1" } ]}>
{ this.props.text2 || 'OFF' }
</Text>
</View>
</View>
</View>
</TouchableOpacity>
{ this.props.children }
</View>
);
}
}
const switchStyles = StyleSheet.create({
textPos: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center'
},
rtl: {
flexDirection: 'row-reverse'
},
ltr: {
flexDirection: 'row'
},
wayBtnActive: {
borderWidth: 1,
marginTop: 2,
marginRight: 2,
marginLeft: 2
}
});