UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

176 lines (143 loc) 6.13 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var React = _interopRequireWildcard(_react); var _propTypes = require('prop-types'); var PropTypes = _interopRequireWildcard(_propTypes); var _reactLifecyclesCompat = require('react-lifecycles-compat'); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _shallowequal = require('shallowequal'); var _shallowequal2 = _interopRequireDefault(_shallowequal); var _Checkbox = require('./Checkbox'); var _Checkbox2 = _interopRequireDefault(_Checkbox); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var CheckboxGroup = function (_React$Component) { (0, _inherits3['default'])(CheckboxGroup, _React$Component); function CheckboxGroup(props) { (0, _classCallCheck3['default'])(this, CheckboxGroup); var _this = (0, _possibleConstructorReturn3['default'])(this, (CheckboxGroup.__proto__ || Object.getPrototypeOf(CheckboxGroup)).call(this, props)); _this.toggleOption = function (option) { var optionIndex = _this.state.value.indexOf(option.value); var value = [].concat((0, _toConsumableArray3['default'])(_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; } (0, _createClass3['default'])(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 !(0, _shallowequal2['default'])(this.props, nextProps) || !(0, _shallowequal2['default'])(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( _Checkbox2['default'], { 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 = (0, _classnames2['default'])(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 }; (0, _reactLifecyclesCompat.polyfill)(CheckboxGroup); exports['default'] = CheckboxGroup; module.exports = exports['default'];