UNPKG

zarm-mobile

Version:
151 lines (121 loc) 5.87 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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) : subClass.__proto__ = superClass; } function getCheckedValue(children) { var checkedValue = []; _react2.default.Children.forEach(children, function (checkbox) { if (checkbox.props && checkbox.props.checked) { checkedValue.push(checkbox.props.value); } }); return checkedValue; } var CheckboxGroup = function (_PureComponent) { _inherits(CheckboxGroup, _PureComponent); function CheckboxGroup(props) { _classCallCheck(this, CheckboxGroup); var _this = _possibleConstructorReturn(this, (CheckboxGroup.__proto__ || Object.getPrototypeOf(CheckboxGroup)).call(this, props)); _this.state = { value: props.value || props.defaultValue || getCheckedValue(props.children) }; return _this; } _createClass(CheckboxGroup, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps || getCheckedValue(nextProps.children)) { this.setState({ value: nextProps.value || getCheckedValue(nextProps.children) }); } } }, { key: 'onCheckboxChange', value: function onCheckboxChange(value) { var values = this.state.value; var index = values.indexOf(value); if (index < 0) { values.push(value); } else { values.splice(index, 1); } this.setState({ value: values }); this.props.onChange(values); } }, { key: 'render', value: function render() { var _this2 = this, _classnames; var _props = this.props, prefixCls = _props.prefixCls, className = _props.className, theme = _props.theme, shape = _props.shape, type = _props.type, block = _props.block, disabled = _props.disabled, gather = _props.gather, children = _props.children; var items = _react2.default.Children.map(children, function (element, index) { return (0, _react.cloneElement)(element, { key: index, type: type, theme: theme, block: block || element.props.block, disabled: disabled || element.props.disabled, onChange: function onChange() { return _this2.onCheckboxChange(element.props.value); }, // use '==' because the result will fail when the value's typeof is Number checked: _this2.state.value.indexOf(element.props.value) > -1 }); }); var cls = (0, _classnames3.default)((_classnames = {}, _defineProperty(_classnames, '' + prefixCls, true), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, 'shape-' + shape, !!shape), _defineProperty(_classnames, 'is-gather', gather), _defineProperty(_classnames, 'block', block), _defineProperty(_classnames, 'disabled', disabled), _classnames)); return _react2.default.createElement( 'div', { className: cls }, items ); } }]); return CheckboxGroup; }(_react.PureComponent); CheckboxGroup.propTypes = { prefixCls: _propTypes2.default.string, className: _propTypes2.default.string, theme: _propTypes2.default.oneOf(['default', 'primary', 'info', 'success', 'warning', 'error']), type: _propTypes2.default.oneOf(['button', 'cell']), shape: _propTypes2.default.oneOf(['radius', 'round']), block: _propTypes2.default.bool, disabled: _propTypes2.default.bool, gather: _propTypes2.default.bool, onChange: _propTypes2.default.func }; CheckboxGroup.defaultProps = { prefixCls: 'za-checkbox-group', className: null, theme: 'primary', type: null, shape: null, block: false, disabled: false, gather: false, onChange: function onChange() {} }; exports.default = CheckboxGroup;