dragon-mobile-ui
Version:
UI for react.js
120 lines (92 loc) • 4.34 kB
JavaScript
;
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 _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _Checkbox = require('./Checkbox');
var _Checkbox2 = _interopRequireDefault(_Checkbox);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }
var CheckboxGroup = function (_Component) {
_inherits(CheckboxGroup, _Component);
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 || _this.getCheckedValue(props.children)
};
return _this;
}
_createClass(CheckboxGroup, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if ('value' in nextProps || this.getCheckedValue(nextProps.children)) {
this.setState({
value: nextProps.value || this.getCheckedValue(nextProps.children)
});
}
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = this.props;
var items = _react2.default.Children.map(props.children, function (element, index) {
return (0, _react.cloneElement)(element, {
key: index,
onChange: function onChange() {
return _this2.onCheckboxChange(element.props.value);
},
checked: !!(_this2.state.value.indexOf(element.props.value) > -1)
});
});
return _react2.default.createElement(
'div',
{ className: 'ui-checkbox-group' },
items
);
}
}, {
key: 'getCheckedValue',
value: 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;
}
}, {
key: 'onCheckboxChange',
value: function onCheckboxChange(value) {
var values = this.state.value,
index = values.indexOf(value);
if (index < 0) {
values.push(value);
} else {
values.splice(index, 1);
}
this.setState({
value: values
});
this.props.onChange(values);
}
}]);
return CheckboxGroup;
}(_react.Component);
CheckboxGroup.propTypes = {
onChange: _propTypes2.default.func
};
CheckboxGroup.defaultProps = {
onChange: function onChange() {}
};
exports.default = CheckboxGroup;