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 = {
65 isInvalid: PropTypes.bool,
66 isValid: PropTypes.bool,
67 name: PropTypes.string,
68 offLabel: PropTypes.string,
69 onBlur: PropTypes.func,
70 onChange: PropTypes.func,
71 onLabel: PropTypes.string,
72 readonly: PropTypes.bool,
73 title: PropTypes.string,
74 value: PropTypes.bool,
77 ToggleSwitch.defaultProps = {
91 export default ToggleSwitch;