UNPKG

hy-checkbox

Version:

checkbox & radio ui components for react

143 lines (115 loc) 6.94 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 _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); var _checkBox = require('./checkBox'); var _checkBox2 = _interopRequireDefault(_checkBox); require('./checkBox.less'); 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; } /* * @Author: SiMeiyu * @Date: 2017-07-04 09:59:05 */ function handleChecked(checkedItems, item) { if (checkedItems.length) { var targIndex = checkedItems.findIndex(function (value, index, arr) { return value === item; }); return targIndex > -1; } } 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.state = { checkedItems: _this.props.defaultValue || [] }; return _this; } _createClass(CheckboxGroup, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ("defaultValue" in nextProps) { this.setState({ checkedItems: nextProps.defaultValue }); } } }, { key: 'handleChange', value: function handleChange(value) { var values = this.state.checkedItems; var decide = handleChecked(values, value); var newValue = []; if (decide) { newValue = values.filter(function (item) { return item != value; }); } else { newValue = values.concat(value); } this.setState({ checkedItems: newValue }); if (this.props.onChange) { this.props.onChange(newValue); } } }, { key: 'render', value: function render() { var _classnames, _this2 = this; var _props = this.props, className = _props.className, prefixCls = _props.prefixCls, name = _props.name, items = _props.items, inline = _props.inline, btnStyle = _props.btnStyle; var classes = (0, _classnames3.default)((_classnames = {}, _defineProperty(_classnames, prefixCls + '-checkbox-group', !btnStyle), _defineProperty(_classnames, prefixCls + '-btn-group', btnStyle && inline), _defineProperty(_classnames, prefixCls + '-btn-group-vertical', btnStyle && !inline), _classnames), className); return _react2.default.createElement( 'div', { className: classes }, items.map(function (item, index) { return _react2.default.createElement(_checkBox2.default, { key: index, name: name, value: item.value, title: item.title, disabled: item.disabled || false, checked: handleChecked(_this2.state.checkedItems, item.value) || false, onChange: _this2.handleChange.bind(_this2), inline: inline || false, btnStyle: btnStyle }); }) ); } }]); return CheckboxGroup; }(_react2.default.Component); CheckboxGroup.propTypes = { items: _react2.default.PropTypes.array.isRequired, className: _react2.default.PropTypes.string, defaultValue: _react2.default.PropTypes.array, // value: React.PropTypes.array, prefixCls: _react2.default.PropTypes.string, onChange: _react2.default.PropTypes.func, type: _react2.default.PropTypes.oneOf(['button', 'normal']) }; CheckboxGroup.defaultProps = { name: 'radioGroup', prefixCls: "ult" }; exports.default = CheckboxGroup;