1 import PropTypes from 'prop-types';
2 import React from 'react';
4 import Icon from './Icon';
6 const ToggleSwitch = ({
18 const toggle = () => {
20 if (onChange) onChange({ target: { name, value: !value } });
23 const handleClick = event => {
24 event.stopPropagation();
28 const handleKey = event => {
29 if ([13, 32].includes(event.which)) {
31 event.preventDefault();
32 event.stopPropagation();
36 const classNames = ['form-control', 'custom-toggle'];
37 if (value) classNames.push('is-toggled');
38 if (isInvalid) classNames.push('is-invalid');
39 if (isValid) classNames.push('is-valid');
40 if (readonly) classNames.push('readonly');
43 className={classNames.join(' ')}
48 onBlur={onBlur ? () => onBlur({ target: { name, value } }) : null}
52 <div className="handle">
53 <span className="handle-label">
55 ? onLabel || <Icon name="check" />
56 : offLabel || <Icon name="times" />
63 ToggleSwitch.propTypes = {
64 isInvalid: PropTypes.bool,
65 isValid: PropTypes.bool,
66 name: PropTypes.string,
67 offLabel: PropTypes.string,
68 onBlur: PropTypes.func,
69 onChange: PropTypes.func,
70 onLabel: PropTypes.string,
71 readonly: PropTypes.bool,
72 title: PropTypes.string,
73 value: PropTypes.bool,
76 export default ToggleSwitch;