1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; };
|
8 |
|
9 | var _react = require("react");
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _reactDom = require("react-dom");
|
14 |
|
15 | var _reactDom2 = _interopRequireDefault(_reactDom);
|
16 |
|
17 | var _classnames = require("classnames");
|
18 |
|
19 | var _classnames2 = _interopRequireDefault(_classnames);
|
20 |
|
21 | var _propTypes = require("prop-types");
|
22 |
|
23 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
26 |
|
27 | function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
|
28 |
|
29 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
34 |
|
35 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
|
36 |
|
37 | var propTypes = {
|
38 | clsPrefix: _propTypes2["default"].string,
|
39 | disabled: _propTypes2["default"].bool,
|
40 | checkedChildren: _propTypes2["default"].any,
|
41 | unCheckedChildren: _propTypes2["default"].any,
|
42 | onChangeHandler: _propTypes2["default"].func,
|
43 | onChange: _propTypes2["default"].func
|
44 | };
|
45 | var defaultProps = {
|
46 | clsPrefix: "u-switch",
|
47 | checkedChildren: null,
|
48 | unCheckedChildren: null,
|
49 | defaultChecked: false,
|
50 | size: "",
|
51 | disabled: false,
|
52 | onChangeHandler: function onChangeHandler() {},
|
53 | onChange: function onChange() {}
|
54 | };
|
55 |
|
56 | var Switch = function (_Component) {
|
57 | _inherits(Switch, _Component);
|
58 |
|
59 | function Switch(props) {
|
60 | _classCallCheck(this, Switch);
|
61 |
|
62 | var _this = _possibleConstructorReturn(this, _Component.call(this, props));
|
63 |
|
64 | _initialiseProps.call(_this);
|
65 |
|
66 | var checked = false;
|
67 | if ('checked' in props) {
|
68 | checked = !!props.checked;
|
69 | } else if ('defaultValue' in props) {
|
70 | checked = !!props.defaultValue;
|
71 | } else {
|
72 | checked = !!props.defaultChecked;
|
73 | }
|
74 | _this.state = { checked: checked };
|
75 | return _this;
|
76 | }
|
77 |
|
78 | Switch.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps, nextState) {
|
79 | if ("checked" in nextProps) {
|
80 | this.setState({ checked: !!nextProps.checked });
|
81 | }
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | Switch.prototype.render = function render() {
|
89 | var _props = this.props,
|
90 | checkedChildren = _props.checkedChildren,
|
91 | unCheckedChildren = _props.unCheckedChildren,
|
92 | onChangeHandler = _props.onChangeHandler,
|
93 | size = _props.size,
|
94 | className = _props.className,
|
95 | clsPrefix = _props.clsPrefix,
|
96 | disabled = _props.disabled,
|
97 | colors = _props.colors,
|
98 | others = _objectWithoutProperties(_props, ["checkedChildren", "unCheckedChildren", "onChangeHandler", "size", "className", "clsPrefix", "disabled", "colors"]);
|
99 |
|
100 |
|
101 |
|
102 | var checked = this.state.checked;
|
103 | var classes = {
|
104 | "is-checked": checked
|
105 | };
|
106 | if (size) {
|
107 | classes[clsPrefix + "-" + size] = true;
|
108 | }
|
109 | if (colors) {
|
110 | classes[clsPrefix + "-" + colors] = true;
|
111 | }
|
112 | classes[[clsPrefix + "-disabled"]] = disabled;
|
113 |
|
114 | var classNames = (0, _classnames2["default"])(clsPrefix, classes);
|
115 |
|
116 | return _react2["default"].createElement(
|
117 | "span",
|
118 | _extends({}, others, {
|
119 | ref: this.saveNode,
|
120 | onClick: this.clickHandler,
|
121 | onKeyDown: this.handleKeyDown,
|
122 | onMouseUp: this.handleMouseUp,
|
123 | className: (0, _classnames2["default"])(className, classNames),
|
124 | tabIndex: disabled ? -1 : 0
|
125 | }),
|
126 | _react2["default"].createElement(
|
127 | "span",
|
128 | { className: clsPrefix + "-inner" },
|
129 | checked ? checkedChildren : unCheckedChildren
|
130 | )
|
131 | );
|
132 | };
|
133 |
|
134 | return Switch;
|
135 | }(_react.Component);
|
136 |
|
137 | var _initialiseProps = function _initialiseProps() {
|
138 | var _this2 = this;
|
139 |
|
140 | this.setChecked = function (checked) {
|
141 | if (_this2.props.disabled) {
|
142 | return;
|
143 | }
|
144 | if (!('checked' in _this2.props)) {
|
145 | _this2.setState({
|
146 | checked: checked
|
147 | });
|
148 | }
|
149 | _this2.props.onChangeHandler(checked);
|
150 | _this2.props.onChange(checked);
|
151 | };
|
152 |
|
153 | this.clickHandler = function () {
|
154 | var checked = !_this2.state.checked;
|
155 | _this2.setChecked(checked);
|
156 | };
|
157 |
|
158 | this.handleKeyDown = function (e) {
|
159 | if (e.keyCode === 37) {
|
160 |
|
161 | _this2.setChecked(false);
|
162 | } else if (e.keyCode === 39) {
|
163 |
|
164 | _this2.setChecked(true);
|
165 | } else if (e.keyCode === 32 || e.keyCode === 13) {
|
166 |
|
167 | _this2.clickHandler();
|
168 | }
|
169 | };
|
170 |
|
171 | this.handleMouseUp = function (e) {
|
172 | if (_this2.node) {
|
173 | _this2.node.blur();
|
174 | }
|
175 | if (_this2.props.onMouseUp) {
|
176 | _this2.props.onMouseUp(e);
|
177 | }
|
178 | };
|
179 |
|
180 | this.saveNode = function (node) {
|
181 | _this2.node = node;
|
182 | };
|
183 | };
|
184 |
|
185 | Switch.propTypes = propTypes;
|
186 | Switch.defaultProps = defaultProps;
|
187 | exports["default"] = Switch;
|
188 | module.exports = exports["default"]; |
\ | No newline at end of file |