UNPKG

react-pdf-builder

Version:
72 lines (71 loc) 3.65 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Card = void 0; const react_1 = __importDefault(require("react")); const ThemeBuilder_1 = require("../../themes/ThemeBuilder"); const sanitizeChildren_1 = require("../children/sanitizeChildren"); const Box_1 = require("../layout/Box"); const PDFThemeProvider_1 = require("../theme/PDFThemeProvider"); const util_1 = require("../../util/util"); /** * Read the [full documentation for Card](https://justinmahar.github.io/react-pdf-builder/?path=/docs/documentation-components-cards-card--docs) */ const Card = (_a) => { var { children, className, style } = _a, props = __rest(_a, ["children", "className", "style"]); const theme = (0, PDFThemeProvider_1.usePDFThemeContext)(); const themeProps = theme.cardProps; const mergedProps = Object.assign(Object.assign({}, themeProps), props); const styleInnate = { overflow: 'hidden', width: '100%', }; // Inject themed children with props from Card const themedChildren = (0, sanitizeChildren_1.sanitizeChildren)(children); const themedChildrenArray = (0, util_1.childrenAsArray)(themedChildren).flat(); const injectedChildArray = themedChildrenArray.map((c, i, arr) => { var _a, _b; const name = ((_a = c.type) === null || _a === void 0 ? void 0 : _a.displayName) || ((_b = c.type) === null || _b === void 0 ? void 0 : _b.name) || 'Unknown'; const isHeader = name === 'CardHeader'; const isBody = name === 'CardBody'; const additionalProps = {}; if (isHeader) { if (i === 0 && arr.length === 1) { additionalProps.noBody = true; } if (i > 0) { additionalProps.withHeader = true; } if (i === arr.length - 1) { additionalProps.noBody = true; } } else if (isBody) { if (i === 0) { additionalProps.noHeader = true; } if (i < arr.length - 1) { additionalProps.withFooter = true; } } return react_1.default.cloneElement(c, Object.assign(Object.assign({ key: `card-child-` + i, swatch: mergedProps.swatch, swatchOpacity: mergedProps.swatchOpacity }, additionalProps), c.props)); }); const themeClassName = themeProps.className; const themeClassNameStyles = ThemeBuilder_1.ThemeBuilder.getStylesForClassName(themeClassName, theme.classNames); const classNameStyles = ThemeBuilder_1.ThemeBuilder.getStylesForClassName(className, theme.classNames); return (react_1.default.createElement(Box_1.Box, Object.assign({ dir: "y" }, mergedProps, { style: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, styleInnate), themeClassNameStyles), themeProps === null || themeProps === void 0 ? void 0 : themeProps.style), classNameStyles), style) }), (0, sanitizeChildren_1.sanitizeChildren)(injectedChildArray))); }; exports.Card = Card;