UNPKG

wix-style-react

Version:
94 lines (76 loc) 3.68 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import chunk from 'lodash/chunk'; import { st, classes } from './AnalyticsLayout.st.css'; import Cell from './Cell/Cell'; var MIN_ITEMS_PER_ROW = 2; var MAX_ITEMS_PER_ROW = 3; function _splitItemsToRows(items, min, max) { var mod = items.length % max; if (mod < min && mod !== 0) { var numberOfItemsForFillsMaxRows = max * (Math.floor(items.length / max) - 1); var ItemsForFillsMaxRows = items.splice(0, numberOfItemsForFillsMaxRows); return [].concat(_toConsumableArray(chunk(ItemsForFillsMaxRows, max)), _toConsumableArray(chunk(items, min))); } else { return chunk(items, max); } } /** AnalyticsLayout */ var AnalyticsLayout = /*#__PURE__*/function (_React$PureComponent) { _inherits(AnalyticsLayout, _React$PureComponent); var _super = _createSuper(AnalyticsLayout); function AnalyticsLayout() { _classCallCheck(this, AnalyticsLayout); return _super.apply(this, arguments); } _createClass(AnalyticsLayout, [{ key: "render", value: function render() { var _this$props = this.props, dataHook = _this$props.dataHook, items = _this$props.items, className = _this$props.className, children = _this$props.children; var itemsSplitToRows = _splitItemsToRows(items, MIN_ITEMS_PER_ROW, MAX_ITEMS_PER_ROW); return /*#__PURE__*/React.createElement("div", { className: st(classes.root, className), "data-hook": dataHook }, itemsSplitToRows.map(function (row, indexKey) { return /*#__PURE__*/React.createElement("div", { key: indexKey, className: st(classes.row), style: { gridTemplateColumns: "repeat(".concat(row.length, ", 1fr)") } }, row.map(function (item, index) { return children(item, index, row.length); })); })); } }]); return AnalyticsLayout; }(React.PureComponent); AnalyticsLayout.displayName = 'AnalyticsLayout'; AnalyticsLayout.propTypes = { /** Applied as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** A css class to be applied to the component's root element */ className: PropTypes.string, /** Items that will be rendered */ items: PropTypes.any.isRequired, /** Children */ children: PropTypes.func.isRequired }; AnalyticsLayout.defaultProps = { items: [] }; AnalyticsLayout.Cell = Cell; export default AnalyticsLayout;