UNPKG

dm-web-react

Version:

The DM web client with React.

82 lines 3.29 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import * as React from "react"; import Checkbox from "./Checkbox"; var CheckboxGroup = /** @class */ (function (_super) { __extends(CheckboxGroup, _super); function CheckboxGroup(props) { var _this = _super.call(this, props) || this; _this.toggleOption = function (option) { var optionIndex = _this.state.value.indexOf(option.value); var value = _this.state.value.slice(); if (optionIndex === -1) { value.push(option.value); } else { value.splice(optionIndex, 1); } if (!("value" in _this.props)) { _this.setState({ value: value }); } var onChange = _this.props.onChange; if (onChange) { onChange(value); } }; _this.state = { value: props.value || props.defaultValue || [], }; return _this; } CheckboxGroup.prototype.componentWillReceiveProps = function (nextProps) { if ("value" in nextProps) { this.setState({ value: nextProps.value || [], }); } }; CheckboxGroup.prototype.render = function () { var _this = this; var _a = this.props, type = _a.type, checkboxStyle = _a.checkboxStyle; var value = this.state.value; return this.getOptions().map(function (option, index) { return (React.createElement(Checkbox, { key: index, type: type, checked: value.indexOf(option.value) > -1, onChange: function () { return _this.toggleOption(option); }, style: __assign({ margin: "1px" }, checkboxStyle) }, option.label)); }); }; CheckboxGroup.prototype.getOptions = function () { var options = this.props.options; // https://github.com/Microsoft/TypeScript/issues/7960 return options.map(function (option) { if (typeof option === "string") { return { label: option, value: option, }; } return option; }); }; return CheckboxGroup; }(React.PureComponent)); export default CheckboxGroup; //# sourceMappingURL=CheckboxGroup.js.map