UNPKG

1.83 kBJavaScriptView Raw
1import React, {PureComponent} from 'react';
2import PropTypes from 'prop-types';
3import classNames from 'classnames';
4
5import Theme from '../global/theme';
6import dataTests from '../global/data-tests';
7
8import styles from './toggle.css';
9
10/**
11 * @name Toggle
12 */
13
14export default class Toggle extends PureComponent {
15 static propTypes = {
16 children: PropTypes.node,
17 name: PropTypes.string,
18 className: PropTypes.string,
19 title: PropTypes.string,
20 leftLabel: PropTypes.node,
21 defaultChecked: PropTypes.bool,
22 checked: PropTypes.bool,
23 disabled: PropTypes.bool,
24 pale: PropTypes.bool,
25 onChange: PropTypes.func,
26 onTransitionEnd: PropTypes.func,
27 theme: PropTypes.string,
28 'data-test': PropTypes.string
29 };
30
31 static defaultProps = {
32 theme: Theme.LIGHT
33 };
34
35 render() {
36 const {className, children, disabled, pale, title, leftLabel, theme, 'data-test': dataTest,
37 onTransitionEnd, ...restProps} = this.props;
38
39 const classes = classNames(
40 className,
41 styles.toggle,
42 styles[theme],
43 disabled && styles.disabled
44 );
45
46 return (
47 <label
48 className={classes}
49 title={title}
50 data-test={dataTests('ring-toggle', dataTest)}
51 >
52 {leftLabel && <span className={styles.leftLabel}>{leftLabel}</span>}
53
54 <span className={styles.switchWrapper}>
55 <input
56 data-test="ring-toggle-input"
57 {...restProps}
58 type="checkbox"
59 disabled={disabled}
60 className={styles.input}
61 />
62
63 <span
64 className={classNames(styles.switch, pale && styles.paleSwitch)}
65 onTransitionEnd={onTransitionEnd}
66 />
67 </span>
68
69 {children && <span className={styles.label}>{children}</span>}
70 </label>
71 );
72 }
73}