Skip to content

Commit bd540cc

Browse files
authored
Release 2.1.0 (#47)
* Update default styles and simplified readme * Update tests
1 parent 72d2262 commit bd540cc

File tree

5 files changed

+285
-288
lines changed

5 files changed

+285
-288
lines changed

README.md

Lines changed: 44 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# react-native-switch-toggle
22

33
> Simple switch toggle component for react-native. This component supports horizontal switch toggle with animation with several options like start/end background colors, start/end circle colors, and duration for animation.
4-
<img src="https://firebasestorage.googleapis.com/v0/b/bookoo-89f6c.appspot.com/o/switch-toggle.gif?alt=media&token=a9dc36e0-3c25-45dc-bbb7-8b095a716dc8"/>
54
65
[![Npm Version](http://img.shields.io/npm/v/react-native-switch-toggle.svg?style=flat-square)](https://npmjs.org/package/react-native-switch-toggle)
76
[![Downloads](http://img.shields.io/npm/dm/react-native-switch-toggle.svg?style=flat-square)](https://npmjs.org/package/react-native-switch-toggle)
@@ -15,6 +14,9 @@
1514
[![supports web](https://img.shields.io/badge/web-4630EB.svg?style=flat-square&logo=GOOGLE-CHROME&labelColor=4285F4&logoColor=fff)](https://docs.expo.io/workflow/web/)
1615
[![runs with expo](https://img.shields.io/badge/Runs%20with%20Expo-000.svg?style=flat&logo=EXPO&labelColor=ffffff&logoColor=000)](https://github.com/expo/expo)
1716

17+
18+
<img src="https://user-images.githubusercontent.com/27461460/120913849-32f23580-c6d5-11eb-90bc-be0a97125427.gif" height=400 />
19+
1820
## News
1921

2022
Current package is fully redesigned in `2.0.0`. We recommend you to use this package version `>=2.0.0`.
@@ -25,13 +27,13 @@ Current package is fully redesigned in `2.0.0`. We recommend you to use this pac
2527
| -------------------- | --------- | ------- | -------------------------------------------------------- |
2628
| switchOn || boolean | false |
2729
| onPress || func | () => {} |
28-
| containerStyle | | styles | { width: 72, height: 36, borderRadius: 18 ... } |
29-
| circleStyle | | styles | { width: 30, height: 30, borderRadius: 15 ... } |
30-
| backgroundColorOn | | string | 'rgb(227,227,227)' |
31-
| backgroundColorOff | | string | 'rgb(215,215,215)' |
32-
| circleColorOn | | string | 'white' |
33-
| circleColorOff | | string | 'rgb(102,134,205)' |
34-
| duration | | number | 300 |
30+
| containerStyle | | styles | { width: 80, height: 40, borderRadius: 25 ... } |
31+
| circleStyle | | styles | { width: 32, height: 32, borderRadius: 16 ... } |
32+
| backgroundColorOn | | string | 'black' |
33+
| backgroundColorOff | | string | '#C4C4C4' |
34+
| circleColorOn | | string | 'white' |
35+
| circleColorOff | | string | '#C4C4C4' |
36+
| duration | | number | 300 |
3537
| type | | number | 0 - Normal switch, 1 - Switch with a text |
3638
| buttonText | | string | Text on-top of the button |
3739
| backTextRight | | string | Text to appear in right side when button toggled to left |
@@ -49,147 +51,42 @@ Current package is fully redesigned in `2.0.0`. We recommend you to use this pac
4951
`$ npm install react-native-switch-toggle --save`
5052

5153
```javascript
52-
import React, { Component } from "react";
53-
import { Platform, StyleSheet, Text, View } from "react-native";
54-
5554
import SwitchToggle from "react-native-switch-toggle";
55+
```
5656

57-
export default class App extends Component<{}> {
58-
constructor(props) {
59-
super(props);
60-
this.state = {
61-
switchOn1: false,
62-
switchOn2: false,
63-
switchOn4: false
64-
};
65-
}
66-
67-
getButtonText() {
68-
return this.state.switchOn4 ? "Hour" : "Day";
69-
}
70-
71-
getRightText() {
72-
return this.state.switchOn4 ? "" : "Hour";
73-
}
74-
75-
getLeftText() {
76-
return this.state.switchOn4 ? "Day" : "";
77-
}
57+
#### Basic Style
58+
```jsx
59+
<SwitchToggle switchOn={on} onPress={() => off(!on)} />
60+
```
7861

79-
render() {
80-
return (
81-
<View style={styles.container}>
82-
<SwitchToggle switchOn={this.state.switchOn1} onPress={this.onPress1} />
83-
<SwitchToggle
84-
containerStyle={{
85-
marginTop: 16,
86-
width: 108,
87-
height: 48,
88-
borderRadius: 25,
89-
backgroundColor: "#ccc",
90-
padding: 5
91-
}}
92-
circleStyle={{
93-
width: 38,
94-
height: 38,
95-
borderRadius: 19,
96-
backgroundColor: "white" // rgb(102,134,205)
97-
}}
98-
switchOn={this.state.switchOn2}
99-
onPress={this.onPress2}
100-
circleColorOff="white"
101-
circleColorOn="red"
102-
duration={500}
103-
/>
104-
<SwitchToggle
105-
containerStyle={{
106-
marginTop: 16,
107-
width: 160,
108-
height: 65,
109-
borderRadius: 30,
110-
padding: 5
111-
}}
112-
backgroundColorOn="#a0e1e5"
113-
backgroundColorOff="#e5e1e0"
114-
circleStyle={{
115-
width: 55,
116-
height: 55,
117-
borderRadius: 27.5,
118-
backgroundColor: "blue" // rgb(102,134,205)
119-
}}
120-
switchOn={this.state.switchOn3}
121-
onPress={this.onPress3}
122-
circleColorOff="#ff11ff"
123-
circleColorOn="green"
124-
duration={500}
125-
/>
126-
<SwitchToggle
127-
buttonText={this.getButtonText()}
128-
backTextRight={this.getRightText()}
129-
backTextLeft={this.getLeftText()}
130-
type={1}
131-
buttonStyle={{
132-
alignItems: "center",
133-
justifyContent: "center",
134-
position: "absolute"
135-
}}
136-
rightContainerStyle={{
137-
flex: 1,
138-
alignItems: "center",
139-
justifyContent: "center"
140-
}}
141-
leftContainerStyle={{
142-
flex: 1,
143-
alignItems: "center",
144-
justifyContent: "flex-start"
145-
}}
146-
buttonTextStyle={{ fontSize: 20 }}
147-
textRightStyle={{ fontSize: 20 }}
148-
textLeftStyle={{ fontSize: 20 }}
149-
containerStyle={{
150-
marginTop: 16,
151-
width: 160,
152-
height: 65,
153-
borderRadius: 30,
154-
padding: 5
155-
}}
156-
backgroundColorOn="#fff"
157-
backgroundColorOff="#fff"
158-
circleStyle={{
159-
width: 80,
160-
height: 55,
161-
borderRadius: 27.5,
162-
backgroundColor: "blue" // rgb(102,134,205)
163-
}}
164-
switchOn={this.state.switchOn4}
165-
onPress={this.onPress4}
166-
circleColorOff="#e5e1e0"
167-
circleColorOn="#e5e1e0"
168-
duration={500}
169-
/>
170-
</View>
171-
);
172-
}
173-
onPress1 = () => {
174-
this.setState({ switchOn1: !this.state.switchOn1 });
175-
};
176-
onPress2 = () => {
177-
this.setState({ switchOn2: !this.state.switchOn2 });
178-
};
179-
onPress3 = () => {
180-
this.setState({ switchOn3: !this.state.switchOn3 });
181-
};
182-
onPress4 = () => {
183-
this.setState({ switchOn4: !this.state.switchOn4 });
184-
};
185-
}
62+
#### Custom Color
63+
```jsx
64+
<SwitchToggle
65+
switchOn={on}
66+
onPress={() => off(!on)}
67+
circleColorOff='#C4C4C4'
68+
circleColorOn='#00D9D5'
69+
backgroundColorOn='#6D6D6D'
70+
backgroundColorOff='#C4C4C4'
71+
/>
72+
```
18673

187-
const styles = StyleSheet.create({
188-
container: {
189-
flex: 1,
190-
justifyContent: "center",
191-
alignItems: "center",
192-
backgroundColor: "#F5FCFF"
193-
}
194-
});
74+
### Custom Size
75+
```jsx
76+
<SwitchToggle
77+
switchOn={on}
78+
onPress={() => off(!on)}
79+
containerStyle={{
80+
marginTop: 16,
81+
width: 106,
82+
height: 48,
83+
borderRadius: 25,
84+
padding: 5,
85+
}}
86+
circleStyle={{
87+
width: 40,
88+
height: 40,
89+
borderRadius: 20,
90+
}}
91+
/>
19592
```

__tests__/SwitchToggle.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,22 @@ describe('[SwitchToggle]', (): void => {
279279
expect(props.onPress).toHaveBeenCalled();
280280
});
281281
});
282+
283+
it('should render custom `colors` and `duration`', () => {
284+
props = {
285+
...props,
286+
backgroundColorOn: 'red',
287+
backgroundColorOff: 'red',
288+
circleColorOn: 'red',
289+
circleColorOff: 'red',
290+
duration: 500,
291+
};
292+
293+
component = <SwitchToggle {...props} />;
294+
295+
const rendered = renderer.create(component);
296+
297+
expect(rendered).toMatchSnapshot();
298+
expect(rendered).toBeTruthy();
299+
});
282300
});

0 commit comments

Comments
 (0)