UNPKG

cpui-components

Version:

134 lines (103 loc) 5.13 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Col = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _blacklist = require('blacklist'); var _blacklist2 = _interopRequireDefault(_blacklist); var _constants = require('./../../constants'); var _constants2 = _interopRequireDefault(_constants); 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 Col = exports.Col = function (_Component) { _inherits(Col, _Component); function Col() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Col); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Col.__proto__ || Object.getPrototypeOf(Col)).call.apply(_ref, [this].concat(args))), _this), _this.getInitialState = function () { return { windowWidth: typeof window !== 'undefined' ? window.innerWidth : 0 }; }, _this.componentDidMount = function () { if (typeof window !== 'undefined') window.addEventListener('resize', _this.handleResize); }, _this.componentWillUnmount = function () { if (typeof window !== 'undefined') window.removeEventListener('resize', _this.handleResize); }, _this.handleResize = function () { _this.setState({ windowWidth: typeof window !== 'undefined' ? window.innerWidth : 0 }); }, _this.render = function () { var _this$props = _this.props, basis = _this$props.basis, gutter = _this$props.gutter, xs = _this$props.xs, sm = _this$props.sm, md = _this$props.md, lg = _this$props.lg; var windowWidth = _this.state.windowWidth; var columnStyle = { minHeight: 1, paddingLeft: gutter / 2, paddingRight: gutter / 2 }; // if no width control is provided fill available space if (!basis && !xs && !sm && !md && !lg) { columnStyle.flex = '1 1 auto'; columnStyle.msFlex = '1 1 auto'; columnStyle.WebkitFlex = '1 1 auto'; } // set widths / flex-basis if (basis) { columnStyle.flex = '1 0 ' + basis; columnStyle.msFlex = '1 0 ' + basis; columnStyle.WebkitFlex = '1 0 ' + basis; } else if (windowWidth < _constants2.default.breakpoint.xs) { columnStyle.width = xs; } else if (windowWidth < _constants2.default.breakpoint.sm) { columnStyle.width = sm || xs; } else if (windowWidth < _constants2.default.breakpoint.md) { columnStyle.width = md || sm || xs; } else { columnStyle.width = lg || md || sm || xs; } if (!columnStyle.width) { columnStyle.width = '100%'; } if (columnStyle.width in _constants2.default.fractions) { columnStyle.width = _constants2.default.fractions[columnStyle.width]; } var props = (0, _blacklist2.default)(_this.props, 'basis', 'gutter', 'style', 'xs', 'sm', 'md', 'lg'); return _react2.default.createElement('div', _extends({ style: Object.assign(columnStyle, _this.props.style) }, props)); }, _temp), _possibleConstructorReturn(_this, _ret); } /* eslint-enable */ return Col; }(_react.Component); Col.propTypes = { /* eslint-disable react/jsx-sort-prop-types */ basis: _propTypes2.default.oneOfType([_propTypes2.default.number, // allow pixels _propTypes2.default.string // allow percentage ]), children: _propTypes2.default.node, gutter: _propTypes2.default.number, style: _propTypes2.default.object, lg: _propTypes2.default.string, // width as a percentage or fraction md: _propTypes2.default.string, // width as a percentage or fraction sm: _propTypes2.default.string, // width as a percentage or fraction xs: _propTypes2.default.string // width as a percentage or fraction }; Col.defaultProps = { gutter: _constants2.default.width.gutter }; exports.default = Col;