UNPKG

@etsoo/materialui

Version:

TypeScript Material-UI Implementation

239 lines (238 loc) 9.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewPageSize = void 0; exports.ViewPageGridItem = ViewPageGridItem; exports.ViewContainer = ViewContainer; const react_1 = require("react"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_2 = __importDefault(require("react")); const MUGlobal_1 = require("./MUGlobal"); const useCurrentBreakpoint_1 = require("./useCurrentBreakpoint"); const GridDataFormat_1 = require("./GridDataFormat"); const ReactApp_1 = require("./app/ReactApp"); const Grid_1 = __importDefault(require("@mui/material/Grid")); const Typography_1 = __importDefault(require("@mui/material/Typography")); function formatItemData(app, fieldData) { if (fieldData == null) return undefined; if (typeof fieldData === "string") return fieldData; if (fieldData instanceof Date) return app.formatDate(fieldData, "d"); if (Array.isArray(fieldData)) return fieldData.join(", "); return `${fieldData}`; } function getResp(singleRow) { const size = typeof singleRow === "object" ? singleRow : singleRow === "medium" ? ViewPageSize.medium : singleRow === "large" ? ViewPageSize.large : singleRow === true ? ViewPageSize.line : singleRow === false ? ViewPageSize.smallLine : ViewPageSize.small; return size; } function addLabelEnd(label) { return label + ":"; } function getItemField(app, field, data) { // Item data and label let itemData, itemLabel, gridProps = {}, size, isHorizontal; if (Array.isArray(field)) { const [fieldData, fieldType, renderProps, singleRow = "small"] = field; itemData = (0, GridDataFormat_1.GridDataFormat)(data[fieldData], fieldType, renderProps); itemLabel = addLabelEnd(app.get(fieldData) ?? fieldData); size = getResp(singleRow); gridProps = { size }; } else if (typeof field === "object") { // Destruct const { data: fieldData, dataType, label: fieldLabel, renderProps, singleRow = "default", horizontal, ...rest } = field; // Horizontal or not isHorizontal = horizontal; // Size size = getResp(singleRow); gridProps = { ...rest, size }; // Field data if (typeof fieldData === "function") itemData = fieldData(data); else if (dataType == null) itemData = formatItemData(app, data[fieldData]); else itemData = (0, GridDataFormat_1.GridDataFormat)(data[fieldData], dataType, renderProps); // Field label itemLabel = fieldLabel === "" ? undefined : fieldLabel == null && typeof fieldData === "string" ? addLabelEnd(app.get(fieldData) ?? fieldData) : typeof fieldLabel === "function" ? fieldLabel(data) : fieldLabel != null ? addLabelEnd(app.get(fieldLabel) ?? fieldLabel) : undefined; } else { // Single field format itemData = formatItemData(app, data[field]); itemLabel = addLabelEnd(app.get(field) ?? field); size = ViewPageSize.small; gridProps = { size }; } return [itemData, itemLabel, gridProps, size, isHorizontal]; } function getItemSize(bp, size) { const v = size[bp]; if (v != null) return v; const index = breakpoints.indexOf(bp); for (let i = index; i >= 0; i--) { const v = size[breakpoints[i]]; if (v != null) return v; } return 12; } const breakpoints = ["xs", "sm", "md", "lg", "xl"]; /** * View page grid item size */ var ViewPageSize; (function (ViewPageSize) { ViewPageSize.large = { xs: 12, sm: 12, md: 9, lg: 6, xl: 4 }; ViewPageSize.medium = { xs: 12, sm: 12, md: 6, lg: 4, xl: 3 }; ViewPageSize.line = { xs: 12, sm: 12, md: 12, lg: 12, xl: 12 }; ViewPageSize.small = { xs: 6, sm: 6, md: 4, lg: 3, xl: 2 }; ViewPageSize.smallLine = { xs: 12, sm: 6, md: 4, lg: 3, xl: 2 }; function matchSize(size) { return Object.fromEntries(Object.entries(size).map(([key, value]) => [ key, value == null ? undefined : value === 12 ? 12 : 12 - value ])); } ViewPageSize.matchSize = matchSize; })(ViewPageSize || (exports.ViewPageSize = ViewPageSize = {})); /** * View page grid item * @param props Props * @returns Result */ function ViewPageGridItem(props) { // Destruct const { data, label, singleRow, horizontal = false, ...gridProps } = props; // Default options let options = {}; if (gridProps.size == null) { options = getResp(singleRow ?? "small"); } else if (singleRow != null) { options = getResp(singleRow ?? "small"); } // Layout return ((0, jsx_runtime_1.jsxs)(Grid_1.default, { ...gridProps, ...options, children: [label != null && ((0, jsx_runtime_1.jsx)(Typography_1.default, { variant: "caption", component: horizontal ? "span" : "div", children: label })), typeof data === "object" ? (data) : horizontal ? ((0, jsx_runtime_1.jsx)(Typography_1.default, { variant: "subtitle2", component: "span", marginLeft: 0.5, children: data })) : ((0, jsx_runtime_1.jsx)(Typography_1.default, { variant: "subtitle2", children: data }))] })); } function ViewContainer(props) { // Global app const app = (0, ReactApp_1.useRequiredAppContext)(); // Destruct const { data, fields, gridRef, leftContainer, leftContainerLines = 3, leftContainerProps = {}, refresh, spacing = MUGlobal_1.MUGlobal.half(MUGlobal_1.MUGlobal.pagePaddings) } = props; // Left container const { size = ViewPageSize.smallLine, ...leftContainerPropsRest } = leftContainerProps; // Current breakpoint const bp = (0, useCurrentBreakpoint_1.useCurrentBreakpoint)(); // Create fields const fieldIndexRef = react_2.default.useRef(0); const createFields = react_2.default.useCallback((data, maxItems = 0) => { let validItems = 0; const items = []; let i = fieldIndexRef.current; for (; i < fields.length; i++) { const field = fields[i]; let oneSize; let oneItem; if (typeof field === "function") { // Most flexible way, do whatever you want const createdResult = field(data, refresh); if (createdResult == null || createdResult === "") continue; if (Array.isArray(createdResult)) { const [created, size] = createdResult; oneSize = size; oneItem = created; } else { oneSize = ViewPageSize.line; oneItem = createdResult; } } else { const [itemData, itemLabel, gridProps, size, horizontal] = getItemField(app, field, data); // Some callback function may return '' instead of undefined if (itemData == null || itemData === "") continue; oneSize = size; oneItem = ((0, react_1.createElement)(ViewPageGridItem, { ...gridProps, key: i, data: itemData, label: itemLabel, horizontal: horizontal })); } // Max lines if (maxItems > 0) { const itemSize = getItemSize(bp, oneSize); if (maxItems < validItems + itemSize) { fieldIndexRef.current = i; break; } else { items.push(oneItem); validItems += itemSize; } } else { items.push(oneItem); } } if (maxItems === 0) { fieldIndexRef.current = 0; } else { fieldIndexRef.current = i; } return items; }, [app, fields, data, bp]); let leftResult; // Layout return ((0, jsx_runtime_1.jsxs)(Grid_1.default, { container: true, justifyContent: "left", className: "ET-ViewContainer", ref: gridRef, spacing: spacing, children: [leftContainer && (leftResult = leftContainer(data)) != null && ((0, jsx_runtime_1.jsxs)(react_2.default.Fragment, { children: [(0, jsx_runtime_1.jsx)(Grid_1.default, { container: true, className: "ET-ViewPage-LeftContainer", spacing: spacing, size: size, ...leftContainerPropsRest, children: leftResult }), (0, jsx_runtime_1.jsx)(Grid_1.default, { container: true, className: "ET-ViewPage-LeftOthers", spacing: spacing, size: ViewPageSize.matchSize(size), children: createFields(data, leftContainerLines * (12 - getItemSize(bp, size))) })] })), createFields(data)] })); }