UNPKG

6.75 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9var _classnames = require('classnames');
10
11var _classnames2 = _interopRequireDefault(_classnames);
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17var _propTypes = require('prop-types');
18
19var _propTypes2 = _interopRequireDefault(_propTypes);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
23function _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; }
24
25function _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; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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; }
30
31function _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); }
32
33var propTypes = {
34
35 colors: _propTypes2["default"].oneOf(['', 'dark', 'success', 'info', 'warning', 'danger', 'primary']),
36
37 disabled: _propTypes2["default"].bool,
38
39 inverse: _propTypes2["default"].bool
40
41};
42
43var defaultProps = {
44 disabled: false,
45 inverse: false,
46 colors: 'primary',
47 clsPrefix: 'u-checkbox',
48 defaultChecked: false,
49 onClick: function onClick() {}
50};
51var clsPrefix = 'u-checkbox';
52
53var Checkbox = function (_React$Component) {
54 _inherits(Checkbox, _React$Component);
55
56 function Checkbox(props) {
57 _classCallCheck(this, Checkbox);
58
59 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
60
61 _initialiseProps.call(_this);
62
63 _this.state = {
64 checked: 'checked' in props ? props.checked : props.defaultChecked
65 };
66 _this.doubleClickFlag = null;
67 return _this;
68 }
69
70 Checkbox.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
71 if ('checked' in nextProps) {
72 this.setState({
73 checked: nextProps.checked
74 });
75 }
76 };
77
78 Checkbox.prototype.render = function render() {
79 var _props = this.props,
80 disabled = _props.disabled,
81 inverse = _props.inverse,
82 colors = _props.colors,
83 size = _props.size,
84 className = _props.className,
85 indeterminate = _props.indeterminate,
86 onClick = _props.onClick,
87 children = _props.children,
88 checked = _props.checked,
89 clsPrefix = _props.clsPrefix,
90 onDoubleClick = _props.onDoubleClick,
91 onChange = _props.onChange,
92 others = _objectWithoutProperties(_props, ['disabled', 'inverse', 'colors', 'size', 'className', 'indeterminate', 'onClick', 'children', 'checked', 'clsPrefix', 'onDoubleClick', 'onChange']);
93
94 var input = _react2["default"].createElement('input', _extends({}, others, {
95 type: 'checkbox',
96 disabled: this.props.disabled
97 }));
98
99 var classes = {
100 'is-checked': this.state.checked,
101 disabled: disabled
102 };
103
104 if (inverse) {
105 classes[clsPrefix + '-inverse'] = true;
106 }
107
108 if (colors) {
109 classes[clsPrefix + '-' + colors] = true;
110 }
111
112 if (size) {
113 classes[clsPrefix + '-' + size] = true;
114 }
115
116 if (!checked && indeterminate) {
117 classes[clsPrefix + '-indeterminate'] = true;
118 }
119
120 var classNames = (0, _classnames2["default"])(clsPrefix, classes);
121
122 return _react2["default"].createElement(
123 'label',
124 {
125 className: (0, _classnames2["default"])(classNames, className),
126 onDoubleClick: this.handledbClick,
127 onClick: this.changeState },
128 input,
129 _react2["default"].createElement(
130 'label',
131 { className: clsPrefix + '-label' },
132 children
133 )
134 );
135 };
136
137 return Checkbox;
138}(_react2["default"].Component);
139
140var _initialiseProps = function _initialiseProps() {
141 var _this2 = this;
142
143 this.changeState = function (e) {
144 var props = _this2.props;
145 var checked = _this2.state.checked;
146
147 clearTimeout(_this2.doubleClickFlag);
148 if (props.onClick instanceof Function) {
149 props.onClick(e);
150 }
151 if (props.onDoubleClick instanceof Function) {
152 _this2.doubleClickFlag = setTimeout(function () {
153 //do function在此处写单击事件要执行的代码
154 _this2.change(props, checked);
155 }, 300);
156 } else {
157 _this2.change(props, checked);
158 }
159 e.stopPropagation();
160 e.preventDefault();
161 //执行延时
162 };
163
164 this.change = function (props, checked) {
165 if (props.disabled) {
166 return;
167 }
168 if (!('checked' in props)) {
169 _this2.setState({
170 checked: !checked
171 });
172 }
173
174 if (props.onChange instanceof Function) {
175 props.onChange(!checked);
176 }
177 };
178
179 this.handledbClick = function (e) {
180 var onDoubleClick = _this2.props.onDoubleClick;
181
182 clearTimeout(_this2.doubleClickFlag);
183 onDoubleClick && onDoubleClick(_this2.state.checked, e);
184 };
185};
186
187Checkbox.propTypes = propTypes;
188Checkbox.defaultProps = defaultProps;
189
190exports["default"] = Checkbox;
191module.exports = exports['default'];
\No newline at end of file