Skip to content

Commit 93cf868

Browse files
committed
sync
1 parent f1f4442 commit 93cf868

File tree

6 files changed

+1124
-744
lines changed

6 files changed

+1124
-744
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ npm install react-input-switch --save
1515
yarn add react-input-switch
1616
```
1717

18+
### Demo
19+
20+
[https://swiftcarrot.dev/react-input-switch/](https://swiftcarrot.dev/react-input-switch/)
21+
1822
### Custom styles
1923

2024
```javascript

babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['zkit']
2+
presets: ['swiftcarrot']
33
};

package.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-input-switch",
3-
"version": "2.1.18",
3+
"version": "2.1.21",
44
"description": "React toggle switch component",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",
@@ -31,25 +31,25 @@
3131
},
3232
"homepage": "https://swiftcarrot.dev/react-input-switch",
3333
"dependencies": {
34-
"@babel/runtime": "^7.3.4",
35-
"@emotion/core": "^10.0.9"
34+
"@babel/runtime": "^7.4.4",
35+
"@emotion/core": "^10.0.10"
3636
},
3737
"peerDependencies": {
3838
"react": "^16.6.3",
3939
"react-dom": "^16.6.3"
4040
},
4141
"devDependencies": {
42-
"babel-jest": "^24.5.0",
43-
"babel-preset-zkit": "^0.4.0",
42+
"babel-jest": "^24.8.0",
43+
"babel-preset-swiftcarrot": "^0.6.1",
4444
"enzyme": "^3.9.0",
45-
"enzyme-adapter-react-16": "^1.11.2",
46-
"jest": "^24.5.0",
47-
"react": "^16.8.4",
48-
"react-dom": "^16.8.4",
49-
"rollup": "^1.6.0",
45+
"enzyme-adapter-react-16": "^1.12.1",
46+
"jest": "^24.8.0",
47+
"react": "^16.8.6",
48+
"react-dom": "^16.8.6",
49+
"rollup": "^1.11.3",
5050
"rollup-plugin-babel": "^4.3.2",
51-
"rollup-plugin-commonjs": "^9.2.1",
52-
"rollup-plugin-node-resolve": "^4.0.1"
51+
"rollup-plugin-commonjs": "^9.3.4",
52+
"rollup-plugin-node-resolve": "^4.2.3"
5353
},
5454
"jest": {
5555
"collectCoverage": true,

src/__tests__/switch.js

+33-36
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import renderer from 'react-test-renderer';
44
import Switch from '../switch';
55

66
test('render', () => {
7-
const component = renderer.create(<Switch />);
8-
expect(component.toJSON()).toMatchInlineSnapshot(`
9-
<label
10-
className="css-1m7f95s-Switch"
11-
onClick={[Function]}
12-
>
13-
<input
14-
type="hidden"
15-
value={1}
16-
/>
17-
<span
18-
className="css-5qfbhd-Switch"
19-
/>
20-
<span
21-
className="css-1x4cdny-Switch"
22-
/>
23-
</label>
24-
`);
7+
expect(renderer.create(<Switch />).toJSON()).toMatchInlineSnapshot(`
8+
<label
9+
className="css-1m7f95s-Switch"
10+
onClick={[Function]}
11+
>
12+
<input
13+
type="hidden"
14+
value={1}
15+
/>
16+
<span
17+
className="css-1lmr6io-Switch"
18+
/>
19+
<span
20+
className="css-1xb86df-Switch"
21+
/>
22+
</label>
23+
`);
2524
});
2625

2726
test('value', () => {
@@ -31,7 +30,6 @@ test('value', () => {
3130
render() {
3231
return (
3332
<Switch
34-
theme={{ primaryColor: 'blue' }}
3533
value={this.state.value}
3634
onChange={value => this.setState({ value })}
3735
/>
@@ -59,7 +57,6 @@ test('custom on/off', () => {
5957
render() {
6058
return (
6159
<Switch
62-
theme={{ primaryColor: 'blue' }}
6360
on="yes"
6461
off="no"
6562
value={this.state.value}
@@ -85,20 +82,20 @@ test('custom on/off', () => {
8582
test('className', () => {
8683
const component = renderer.create(<Switch className="test" />);
8784
expect(component.toJSON()).toMatchInlineSnapshot(`
88-
<label
89-
className="test css-1m7f95s-Switch"
90-
onClick={[Function]}
91-
>
92-
<input
93-
type="hidden"
94-
value={1}
95-
/>
96-
<span
97-
className="test-track css-5qfbhd-Switch"
98-
/>
99-
<span
100-
className="test-button css-1x4cdny-Switch"
101-
/>
102-
</label>
103-
`);
85+
<label
86+
className="test css-1m7f95s-Switch"
87+
onClick={[Function]}
88+
>
89+
<input
90+
type="hidden"
91+
value={1}
92+
/>
93+
<span
94+
className="test-track css-1lmr6io-Switch"
95+
/>
96+
<span
97+
className="test-button css-1xb86df-Switch"
98+
/>
99+
</label>
100+
`);
104101
});

src/switch.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ const Switch = ({
1313
}) => {
1414
const checked = value === on;
1515
const styles = {
16-
container: [
17-
defaultStyles.container(checked),
18-
customStyles.container && customStyles.container(checked)
19-
],
16+
container: [defaultStyles.container, customStyles.container],
2017
track: [
21-
defaultStyles.track(checked),
22-
customStyles.track && customStyles.track(checked)
18+
{ ...defaultStyles.track, ...customStyles.track },
19+
checked && { ...defaultStyles.trackChecked, ...customStyles.trackChecked }
2320
],
2421
button: [
25-
defaultStyles.button(checked),
26-
customStyles.button && customStyles.button(checked)
22+
{ ...defaultStyles.button, ...customStyles.button },
23+
checked && {
24+
...defaultStyles.buttonChecked,
25+
...customStyles.buttonChecked
26+
}
2727
]
2828
};
2929

@@ -52,36 +52,45 @@ Switch.defaultProps = {
5252
};
5353

5454
const defaultStyles = {
55-
container: () => ({
55+
container: {
5656
position: 'relative',
5757
display: 'inline-block',
5858
width: 24,
5959
height: 14,
6060
verticalAlign: 'middle',
6161
cursor: 'pointer',
6262
userSelect: 'none'
63-
}),
63+
},
6464

65-
track: checked => ({
65+
track: {
6666
position: 'absolute',
6767
top: 0,
6868
left: 0,
6969
right: 0,
7070
bottom: 0,
7171
borderRadius: 7,
72-
backgroundColor: checked ? 'blue' : '#cccccc'
73-
}),
72+
backgroundColor: '#cccccc'
73+
},
74+
75+
trackChecked: {
76+
backgroundColor: '#5e72e4'
77+
},
7478

75-
button: checked => ({
79+
button: {
7680
position: 'absolute',
7781
top: 2,
7882
bottom: 2,
79-
right: checked ? 2 : 11,
80-
left: checked ? 11 : 3,
83+
right: 11,
84+
left: 2,
8185
backgroundColor: '#fff',
8286
borderRadius: 9,
8387
transition: 'all 100ms ease'
84-
}),
88+
},
89+
90+
buttonChecked: {
91+
right: 2,
92+
left: 11
93+
},
8594

8695
input: {
8796
opacity: 0,

0 commit comments

Comments
 (0)