UNPKG

zent

Version:

一套前端设计语言和基于React的实现

130 lines (100 loc) 5.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; var _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; }; 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 _class, _temp2; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('zent-utils/classnames'); var _classnames2 = _interopRequireDefault(_classnames); 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 findIndex(array, predicate) { for (var i = 0; i < array.length; i++) { if (predicate(array[i])) { return i; } } return -1; } var Group = (_temp2 = _class = function (_Component) { _inherits(Group, _Component); function Group() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Group); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Group.__proto__ || Object.getPrototypeOf(Group)).call.apply(_ref, [this].concat(args))), _this), _this.onCheckboxChange = function (e) { var changedValue = e.target.value; var groupValue = _this.props.value.slice(); var isValueEqual = _this.props.isValueEqual; var index = findIndex(groupValue, function (val) { return isValueEqual(val, changedValue); }); if (index !== -1) { groupValue.splice(index, 1); } else { groupValue.push(changedValue); } _this.props.onChange(groupValue); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Group, [{ key: 'render', value: function render() { var _this2 = this, _classNames; var _props = this.props, className = _props.className, prefix = _props.prefix, style = _props.style, isValueEqual = _props.isValueEqual, value = _props.value; var children = _react2['default'].Children.map(this.props.children, function (checkbox) { if (checkbox && checkbox.props) { return _react2['default'].cloneElement(checkbox, _extends({}, checkbox.props, { onChange: _this2.onCheckboxChange, checked: findIndex(value, function (val) { return isValueEqual(val, checkbox.props.value); }) !== -1, disabled: checkbox.props.disabled !== void 0 ? checkbox.props.disabled : _this2.props.disabled, readOnly: checkbox.props.readOnly !== void 0 ? checkbox.props.readOnly : _this2.props.readOnly })); } }); var classString = (0, _classnames2['default'])((_classNames = {}, _defineProperty(_classNames, prefix + '-checkbox-group', true), _defineProperty(_classNames, className, !!className), _classNames)); return _react2['default'].createElement( 'div', { className: classString, style: style }, children ); } }]); return Group; }(_react.Component), _class.propTypes = { value: _react.PropTypes.array, isValueEqual: _react.PropTypes.func, onChange: _react.PropTypes.func, className: _react.PropTypes.string, style: _react.PropTypes.object, prefix: _react.PropTypes.string }, _class.defaultProps = { value: [], prefix: 'zent', className: '', style: {}, onChange: function onChange() {}, isValueEqual: function isValueEqual(a, b) { return a === b; } }, _temp2); exports['default'] = Group; module.exports = exports['default'];