UNPKG

@bigfishtv/cockpit

Version:

79 lines (66 loc) 2.83 kB
var _class, _temp; 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; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import Checkbox from '../input/Checkbox'; import { isArray } from '../../utils/typeUtils'; var CheckboxGroup = (_temp = _class = function (_Component) { _inherits(CheckboxGroup, _Component); function CheckboxGroup() { _classCallCheck(this, CheckboxGroup); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } CheckboxGroup.prototype.handleChange = function handleChange(value, checked) { var _this2 = this; var selected = this.props.options.filter(function (option) { if (value !== option.value) { return _this2._contains(option.value); } else { return checked; } }).map(function (option) { return option.value; }); this.props.onChange(selected); }; CheckboxGroup.prototype._contains = function _contains(value) { if (!this.props.value || !isArray(this.props.value)) return false; return this.props.value.indexOf(value) !== -1; }; CheckboxGroup.prototype.render = function render() { var _this3 = this; return React.createElement( 'div', { className: this.props.className }, this.props.children, this.props.options.map(function (option, i) { return React.createElement(Checkbox, { key: i, value: _this3._contains(option.value), text: option.text, onChange: function onChange(checked) { return _this3.handleChange(option.value, checked); }, readOnly: _this3.props.disabled || _this3.props.readOnly, inline: _this3.props.inline }); }) ); }; return CheckboxGroup; }(Component), _class.propTypes = { value: PropTypes.array, options: PropTypes.array, onChange: PropTypes.func, inline: PropTypes.bool }, _class.defaultProps = { value: [], options: [], onChange: function onChange() {}, inline: false, disabled: false, readOnly: false }, _temp); export { CheckboxGroup as default };