UNPKG

cpui-components

Version:

220 lines (196 loc) 9.56 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.BorderBox = undefined; 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 _Checkbox = require('../Checkbox/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; } /** * Flexible component for designing box models. */ var BorderBox = exports.BorderBox = function (_Component) { _inherits(BorderBox, _Component); function BorderBox(props) { _classCallCheck(this, BorderBox); var _this = _possibleConstructorReturn(this, (BorderBox.__proto__ || Object.getPrototypeOf(BorderBox)).call(this, props)); _this.handleOnChange = function (e) { var sides = _this.state.sides; var corners = _this.state.corners; switch (e.target.name) { case "top": case "bottom": case "left": case "right": sides[e.target.name] = e.target.checked; _this.setState({ sides: sides }); break; case "topLeft": case "topRight": case "bottomLeft": case "bottomRight": corners[e.target.name] = e.target.checked; _this.setState({ corners: corners }); break; default: break; } if (_this.props.onChange && typeof _this.props.onChange === 'function') { _this.props.onChange(e, sides, corners); } }; _this.render = function () { var classes = { "cp-borderBox-displayedBox ": true, "cp-borderBox-displayedBox--top ": _this.state.sides.top, "cp-borderBox-displayedBox--right ": _this.state.sides.right, "cp-borderBox-displayedBox--bottom ": _this.state.sides.bottom, "cp-borderBox-displayedBox--left ": _this.state.sides.left, "cp-borderBox-displayedBox--topLeft ": _this.state.corners.topLeft, "cp-borderBox-displayedBox--topRight ": _this.state.corners.topRight, "cp-borderBox-displayedBox--bottomLeft ": _this.state.corners.bottomLeft, "cp-borderBox-displayedBox--bottomRight ": _this.state.corners.bottomRight }; var boxClasses = ""; for (var cssClass in classes) { if (classes.hasOwnProperty(cssClass) && classes[cssClass] === true) { boxClasses += cssClass; } } var cornerVisibility = !_this.state.displayCorners ? 'hidden' : 'visible'; var sideVisiblity = !_this.state.displaySides ? 'hidden' : 'visible'; return _react2.default.createElement( 'table', { className: 'cp-borderBox' }, _react2.default.createElement( 'tbody', null, _react2.default.createElement( 'tr', null, _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--topLeft' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: cornerVisibility }, checked: _this.state.corners.topLeft, name: 'topLeft', label: 'Top Left', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--top' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: sideVisiblity }, checked: _this.state.sides.top, name: 'top', label: 'Top', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--topRight' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: cornerVisibility }, checked: _this.state.corners.topRight, name: 'topRight', label: 'Top Right', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ) ), _react2.default.createElement( 'tr', null, _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--left' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: sideVisiblity }, checked: _this.state.sides.left, name: 'left', label: 'Left', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-contentBox' }, _react2.default.createElement('div', { className: boxClasses }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--right' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: sideVisiblity }, checked: _this.state.sides.right, name: 'right', label: 'Right', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ) ), _react2.default.createElement( 'tr', null, _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--bottomLeft' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: cornerVisibility }, checked: _this.state.corners.bottomLeft, name: 'bottomLeft', label: 'Bottom Left', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--bottom' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: sideVisiblity }, checked: _this.state.sides.bottom, name: 'bottom', label: 'Bottom', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ), _react2.default.createElement( 'td', { className: 'cp-borderBox-selection cp-borderBox-selection--bottomRight' }, _react2.default.createElement(_Checkbox.Checkbox, { style: { visibility: cornerVisibility }, checked: _this.state.corners.bottomRight, name: 'bottomRight', label: 'Bottom Right', hiddenLabel: true, onChange: function onChange(e) { _this.handleOnChange(e); } }) ) ) ) ); }; _this.state = { displayCorners: _this.props.corners || false, displaySides: _this.props.sides || false, sides: { top: _this.props.values ? _this.props.values.top : false, bottom: _this.props.values ? _this.props.values.bottom : false, left: _this.props.values ? _this.props.values.left : false, right: _this.props.values ? _this.props.values.right : false }, corners: { topLeft: _this.props.values ? _this.props.values.topLeft : false, topRight: _this.props.values ? _this.props.values.topRight : false, bottomLeft: _this.props.values ? _this.props.values.bottomLeft : false, bottomRight: _this.props.values ? _this.props.values.bottomRight : false } }; return _this; } _createClass(BorderBox, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (this.props.values !== nextProps.values) { if (nextProps.values != null) { this.setState({ sides: { top: nextProps.values.top != null ? nextProps.values.top : this.state.sides.top, bottom: nextProps.values.bottom != null ? nextProps.values.bottom : this.state.sides.bottom, left: nextProps.values.left != null ? nextProps.values.left : this.state.sides.left, right: nextProps.values.right != null ? nextProps.values.right : this.state.sides.right }, corners: { topLeft: nextProps.values.topLeft != null ? nextProps.values.topLeft : this.state.corners.topLeft, topRight: nextProps.values.topRight != null ? nextProps.values.topRight : this.state.corners.topRight, bottomLeft: nextProps.values.bottomLeft != null ? nextProps.values.bottomLeft : this.state.corners.bottomLeft, bottomRight: nextProps.values.bottomRight != null ? nextProps.values.bottomRight : this.state.corners.bottomRight } }); } } } }]); return BorderBox; }(_react.Component); exports.default = BorderBox;