antd
Version:
An enterprise-class UI design language and React-based implementation
101 lines (90 loc) • 4.03 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
}return t;
};
import * as React from 'react';
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
import RcCheckbox from 'rc-checkbox';
import shallowEqual from 'shallowequal';
var Checkbox = function (_React$Component) {
_inherits(Checkbox, _React$Component);
function Checkbox() {
_classCallCheck(this, Checkbox);
var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).apply(this, arguments));
_this.saveCheckbox = function (node) {
_this.rcCheckbox = node;
};
return _this;
}
_createClass(Checkbox, [{
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.context.checkboxGroup, nextContext.checkboxGroup);
}
}, {
key: 'focus',
value: function focus() {
this.rcCheckbox.focus();
}
}, {
key: 'blur',
value: function blur() {
this.rcCheckbox.blur();
}
}, {
key: 'render',
value: function render() {
var props = this.props,
context = this.context;
var prefixCls = props.prefixCls,
className = props.className,
children = props.children,
indeterminate = props.indeterminate,
style = props.style,
onMouseEnter = props.onMouseEnter,
onMouseLeave = props.onMouseLeave,
restProps = __rest(props, ["prefixCls", "className", "children", "indeterminate", "style", "onMouseEnter", "onMouseLeave"]);
var checkboxGroup = context.checkboxGroup;
var checkboxProps = _extends({}, restProps);
if (checkboxGroup) {
checkboxProps.onChange = function () {
return checkboxGroup.toggleOption({ label: children, value: props.value });
};
checkboxProps.checked = checkboxGroup.value.indexOf(props.value) !== -1;
checkboxProps.disabled = props.disabled || checkboxGroup.disabled;
}
var classString = classNames(className, _defineProperty({}, prefixCls + '-wrapper', true));
var checkboxClass = classNames(_defineProperty({}, prefixCls + '-indeterminate', indeterminate));
return React.createElement(
'label',
{ className: classString, style: style, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave },
React.createElement(RcCheckbox, _extends({}, checkboxProps, { prefixCls: prefixCls, className: checkboxClass, ref: this.saveCheckbox })),
children !== undefined ? React.createElement(
'span',
null,
children
) : null
);
}
}]);
return Checkbox;
}(React.Component);
export default Checkbox;
Checkbox.defaultProps = {
prefixCls: 'ant-checkbox',
indeterminate: false
};
Checkbox.contextTypes = {
checkboxGroup: PropTypes.any
};