antd
Version:
An enterprise-class UI design language and React-based implementation
136 lines (126 loc) • 4.79 kB
JavaScript
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
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';
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import Checkbox from './Checkbox';
var CheckboxGroup = function (_React$Component) {
_inherits(CheckboxGroup, _React$Component);
function CheckboxGroup(props) {
_classCallCheck(this, CheckboxGroup);
var _this = _possibleConstructorReturn(this, (CheckboxGroup.__proto__ || Object.getPrototypeOf(CheckboxGroup)).call(this, props));
_this.toggleOption = function (option) {
var optionIndex = _this.state.value.indexOf(option.value);
var value = [].concat(_toConsumableArray(_this.state.value));
if (optionIndex === -1) {
value.push(option.value);
} else {
value.splice(optionIndex, 1);
}
if (!('value' in _this.props)) {
_this.setState({ value: value });
}
var onChange = _this.props.onChange;
if (onChange) {
onChange(value);
}
};
_this.state = {
value: props.value || props.defaultValue || []
};
return _this;
}
_createClass(CheckboxGroup, [{
key: 'getChildContext',
value: function getChildContext() {
return {
checkboxGroup: {
toggleOption: this.toggleOption,
value: this.state.value,
disabled: this.props.disabled
}
};
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
}
}, {
key: 'getOptions',
value: function getOptions() {
var options = this.props.options;
// https://github.com/Microsoft/TypeScript/issues/7960
return options.map(function (option) {
if (typeof option === 'string') {
return {
label: option,
value: option
};
}
return option;
});
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = this.props,
state = this.state;
var prefixCls = props.prefixCls,
className = props.className,
style = props.style,
options = props.options;
var groupPrefixCls = prefixCls + '-group';
var children = props.children;
if (options && options.length > 0) {
children = this.getOptions().map(function (option) {
return React.createElement(
Checkbox,
{ prefixCls: prefixCls, key: option.value.toString(), disabled: 'disabled' in option ? option.disabled : props.disabled, value: option.value, checked: state.value.indexOf(option.value) !== -1, onChange: function onChange() {
return _this2.toggleOption(option);
}, className: groupPrefixCls + '-item' },
option.label
);
});
}
var classString = classNames(groupPrefixCls, className);
return React.createElement(
'div',
{ className: classString, style: style },
children
);
}
}], [{
key: 'getDerivedStateFromProps',
value: function getDerivedStateFromProps(nextProps) {
if ('value' in nextProps) {
return {
value: nextProps.value || []
};
}
return null;
}
}]);
return CheckboxGroup;
}(React.Component);
CheckboxGroup.defaultProps = {
options: [],
prefixCls: 'ant-checkbox'
};
CheckboxGroup.propTypes = {
defaultValue: PropTypes.array,
value: PropTypes.array,
options: PropTypes.array.isRequired,
onChange: PropTypes.func
};
CheckboxGroup.childContextTypes = {
checkboxGroup: PropTypes.any
};
polyfill(CheckboxGroup);
export default CheckboxGroup;