Skip to content

Commit def4442

Browse files
committed
sync
1 parent b06da16 commit def4442

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-input-switch",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "React toggle switch component",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",

src/switch.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ const Switch = ({
99
value,
1010
onChange,
1111
name,
12+
disabled,
1213
...props
1314
}) => {
1415
const checked = value === on;
15-
const styles = makeStyles(customStyles, { checked });
16+
const styles = makeStyles(customStyles);
17+
18+
function handleClick() {
19+
if (onChange) {
20+
onChange(checked ? off : on);
21+
}
22+
}
1623

1724
return (
1825
<label
1926
{...props}
20-
css={styles.container}
21-
onClick={() => onChange && onChange(checked ? off : on)}
27+
css={[styles.container, disabled && styles.containerDisabled]}
28+
onClick={disabled ? null : handleClick}
2229
>
2330
<input type="hidden" name={name} value={value} />
24-
<span css={styles.track} />
25-
<span css={styles.button} />
31+
<span css={[styles.track, checked && styles.trackChecked]} />
32+
<span css={[styles.button, checked && styles.buttonChecked]} />
2633
</label>
2734
);
2835
};
@@ -31,6 +38,7 @@ Switch.defaultProps = {
3138
value: 1,
3239
on: 1,
3340
off: 0,
41+
disabled: false,
3442
styles: {}
3543
};
3644

src/utils.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
export const makeStyles = (customStyles, { checked }) => {
1+
export const makeStyles = customStyles => {
22
return {
3-
container: [defaultStyles.container, customStyles.container],
4-
track: [
5-
{ ...defaultStyles.track, ...customStyles.track },
6-
checked && { ...defaultStyles.trackChecked, ...customStyles.trackChecked }
7-
],
8-
button: [
9-
{ ...defaultStyles.button, ...customStyles.button },
10-
checked && {
11-
...defaultStyles.buttonChecked,
12-
...customStyles.buttonChecked
13-
}
14-
]
3+
container: { ...defaultStyles.container, ...customStyles.container },
4+
containerDisabled: {
5+
...defaultStyles.containerDisabled,
6+
...customStyles.containerDisabled
7+
},
8+
track: { ...defaultStyles.track, ...customStyles.track },
9+
trackChecked: {
10+
...defaultStyles.trackChecked,
11+
...customStyles.trackChecked
12+
},
13+
button: { ...defaultStyles.button, ...customStyles.button },
14+
buttonChecked: {
15+
...defaultStyles.buttonChecked,
16+
...customStyles.buttonChecked
17+
}
1518
};
1619
};
1720

@@ -26,6 +29,11 @@ export const defaultStyles = {
2629
userSelect: 'none'
2730
},
2831

32+
containerDisabled: {
33+
opacity: 0.7,
34+
cursor: 'not-allowed'
35+
},
36+
2937
track: {
3038
position: 'absolute',
3139
top: 0,

0 commit comments

Comments
 (0)