forked from react-toolbox/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.js
More file actions
93 lines (84 loc) · 2.5 KB
/
Switch.js
File metadata and controls
93 lines (84 loc) · 2.5 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { themr } from 'react-css-themr';
import { SWITCH } from '../identifiers';
import rippleFactory from '../ripple/Ripple';
import thumbFactory from './Thumb';
const factory = (Thumb) => {
class Switch extends Component {
static propTypes = {
checked: PropTypes.bool,
className: PropTypes.string,
disabled: PropTypes.bool,
label: PropTypes.string,
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
ripple: PropTypes.bool,
theme: PropTypes.shape({
disabled: PropTypes.string,
field: PropTypes.string,
input: PropTypes.string,
off: PropTypes.string,
on: PropTypes.string,
ripple: PropTypes.string,
text: PropTypes.string,
thumb: PropTypes.string,
}),
};
static defaultProps = {
checked: false,
className: '',
disabled: false,
};
handleToggle = (event) => {
if (event.pageX !== 0 && event.pageY !== 0) this.blur();
if (!this.props.disabled && this.props.onChange) {
this.props.onChange(!this.props.checked, event);
}
};
blur() {
this.inputNode.blur();
}
focus() {
this.inputNode.focus();
}
render() {
const {
checked,
className,
disabled,
onChange, // eslint-disable-line no-unused-vars
ripple,
theme,
...others
} = this.props;
const _className = classnames(theme[disabled ? 'disabled' : 'field'], className);
return (
<label data-react-toolbox="switch" className={_className}>
<input
{...others}
checked={this.props.checked}
className={theme.input}
onClick={this.handleToggle}
readOnly
ref={(node) => { this.inputNode = node; }}
type="checkbox"
/>
<span className={theme[checked ? 'on' : 'off']}>
<Thumb disabled={this.props.disabled} theme={theme} ripple={ripple} />
</span>
{this.props.label ? <span className={theme.text}>{this.props.label}</span> : null}
</label>
);
}
}
return Switch;
};
const Thumb = thumbFactory(rippleFactory({ centered: true, spread: 2.6 }));
const Switch = factory(Thumb);
export default themr(SWITCH)(Switch);
export { factory as switchFactory };
export { Switch };