UNPKG

@pisell/layout

Version:

基于 Fusion 设计系统的自然布局体系

91 lines (89 loc) 5.07 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _context = _interopRequireDefault(require("./common/context")); var _utils = require("./utils"); var _useFlexClassNames = _interopRequireDefault(require("./hooks/use-flex-class-names")); var _excluded = ["className", "children", "style", "align", "verAlign", "rowGap", "colGap", "renderItem", "rows", "cols", "width", "minWidth", "maxWidth"]; function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * 网格布局 * @param props * @param ref */ var Grid = function Grid(props, ref) { var _classNames; var className = props.className, children = props.children, style = props.style, align = props.align, verAlign = props.verAlign, rowGapProp = props.rowGap, colGapProp = props.colGap, renderItem = props.renderItem, rows = props.rows, cols = props.cols, width = props.width, minWidth = props.minWidth, maxWidth = props.maxWidth, others = (0, _objectWithoutPropertiesLoose2["default"])(props, _excluded); var _useContext = (0, _react.useContext)(_context["default"]), prefix = _useContext.prefix, gridGap = _useContext.gridGap; var clsPrefix = prefix + "grid"; var rowGap = (0, _utils.getGapVal)(gridGap, rowGapProp); var colGap = (0, _utils.getGapVal)(gridGap, colGapProp); var validWidth = width || (style === null || style === void 0 ? void 0 : style.width) || minWidth; var memorizedNewStyle = (0, _react.useMemo)(function () { var gtc = "repeat(" + cols + ", 1fr)"; if (cols && cols > 1) { gtc = "repeat(" + cols + ", calc( (100% - " + (colGap || "var(--page-grid-gap)") + " * " + (cols - 1) + ")/" + cols + "))"; } else if (minWidth && maxWidth) { gtc = "repeat(auto-fill, minmax(" + (0, _utils.wrapUnit)(minWidth) + ", " + (0, _utils.wrapUnit)(maxWidth) + "))"; } else if (minWidth && !maxWidth) { gtc = "repeat(auto-fit, minmax(" + (0, _utils.wrapUnit)(minWidth) + ", auto))"; } else if (!minWidth && maxWidth) { gtc = "repeat(auto-fill, minmax(auto, " + (0, _utils.wrapUnit)(maxWidth) + "))"; } return (0, _extends2["default"])({ display: 'grid', gridTemplateColumns: gtc, gridTemplateRows: "repeat(" + rows + ", 1fr)" }, rowGap ? { gridRowGap: (0, _utils.wrapUnit)(rowGap) } : null, colGap ? { gridColumnGap: (0, _utils.wrapUnit)(colGap) } : null, validWidth ? { flexBasis: (0, _utils.wrapUnit)(validWidth) } : null, style); }, [cols, colGap, minWidth, maxWidth, rows, rowGap, style, validWidth]); // 优先行渲染 var renderChildren = function renderChildren() { return Array.from(new Array(rows)).map(function (_, row) { return Array.from(new Array(cols)).map(function (__, col) { return renderItem ? renderItem(row, col) : null; }); }); }; var flexClassNames = (0, _useFlexClassNames["default"])(props); return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, others, { ref: ref, className: (0, _classnames["default"])(className, clsPrefix, flexClassNames, (_classNames = {}, _classNames[clsPrefix + "-align--" + align] = align, _classNames[clsPrefix + "-valign--" + verAlign] = verAlign, _classNames)), style: memorizedNewStyle }), renderItem ? renderChildren() : children); }; var RefGrid = /*#__PURE__*/(0, _react.forwardRef)(Grid); RefGrid.displayName = 'Grid'; RefGrid.typeMark = 'Grid'; RefGrid.defaultProps = { rows: 1, cols: 1 }; var _default = RefGrid; exports["default"] = _default;