This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
forked from aaronshaf/react-toggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (93 loc) · 3.08 KB
/
index.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use strict";
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var React = _interopRequire(require("react"));
var classNames = _interopRequire(require("classnames"));
var Check = _interopRequire(require("./check"));
var X = _interopRequire(require("./x"));
var PureRenderMixin = _interopRequire(require("react-addons-pure-render-mixin"));
module.exports = React.createClass({
mixins: [PureRenderMixin],
displayName: "Toggle",
propTypes: {
checked: React.PropTypes.bool,
defaultChecked: React.PropTypes.bool,
onChange: React.PropTypes.func,
name: React.PropTypes.string,
value: React.PropTypes.string,
id: React.PropTypes.string,
"aria-labelledby": React.PropTypes.string,
"aria-label": React.PropTypes.string
},
getInitialState: function getInitialState() {
var checked = false;
if ("checked" in this.props) {
checked = this.props.checked;
} else if ("defaultChecked" in this.props) {
checked = this.props.defaultChecked;
}
return {
checked: !!checked,
hasFocus: false
};
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
if ("checked" in nextProps) {
this.setState({ checked: !!nextProps.checked });
}
},
handleClick: function handleClick(event) {
var checkbox = this.input;
if (event.target !== checkbox) {
event.preventDefault();
checkbox.focus();
checkbox.click();
return;
}
if (!("checked" in this.props)) {
this.setState({ checked: checkbox.checked });
}
},
handleFocus: function handleFocus() {
this.setState({ hasFocus: true });
},
handleBlur: function handleBlur() {
this.setState({ hasFocus: false });
},
setRef: function(ref) {
this.input = ref;
},
render: function render() {
var classes = classNames("react-toggle", {
"react-toggle--checked": this.state.checked,
"react-toggle--focus": this.state.hasFocus,
"react-toggle--disabled": this.props.disabled
});
return React.createElement(
"div",
{ className: classes, onClick: this.handleClick },
React.createElement(
"div",
{ className: "react-toggle-track" },
React.createElement(
"div",
{ className: "react-toggle-track-check" },
React.createElement(Check, null)
),
React.createElement(
"div",
{ className: "react-toggle-track-x" },
React.createElement(X, null)
)
),
React.createElement("div", { className: "react-toggle-thumb" }),
React.createElement("input", _extends({
ref: this.setRef,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
className: "react-toggle-screenreader-only",
type: "checkbox"
}, this.props))
);
}
});