-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToggleSwitch.jsx
42 lines (36 loc) · 1.04 KB
/
ToggleSwitch.jsx
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
import PropTypes from "prop-types";
/**
* @callback onChange
* @param {boolean} checked - New state of the toggle switch
*/
/**
* @typedef {object} ToggleProps
* @property {string} checked - Whether the toggle is checked
* @property {Function} onChange - Callback fired on input change
* @property {string} labelFalse - Label displayed to the left of the button ("off" state)
* @property {string} labelTrue - Label displayed to the right of the button ("on" state)
*/
/**
* @param {ToggleProps} props - Props of the component
*/
const ToggleSwitch = ({ checked, onChange, labelFalse, labelTrue }) => (
<label className="toggleSwitch">
{labelFalse}
<span className="inputWrapper">
<input
type="checkbox"
checked={checked}
onChange={() => onChange(!checked)}
/>
<span></span>
</span>
{labelTrue}
</label>
);
ToggleSwitch.propTypes = {
checked: PropTypes.bool.isRequired,
onChange: PropTypes.func.isRequired,
labelFalse: PropTypes.string.isRequired,
labelTrue: PropTypes.string.isRequired
};
export default ToggleSwitch;