@formepdf/react 0.7.10 → 0.7.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/serialize.d.ts +1 -1
- package/dist/serialize.js +20 -1
- package/package.json +1 -1
package/dist/serialize.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type ReactElement } from 'react';
|
|
|
2
2
|
import type { Style, Edges, Corners, FormeDocument, FormeStyle, FormeEdges, FormeDimension, FormeColor, FormeCornerValues } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Serialize a React element tree into a Forme JSON document object.
|
|
5
|
-
* The top-level element must be a <Document
|
|
5
|
+
* The top-level element must be a <Document> (or a component that returns one).
|
|
6
6
|
*/
|
|
7
7
|
export declare function serialize(element: ReactElement): FormeDocument;
|
|
8
8
|
export declare function mapStyle(style?: Style): FormeStyle;
|
package/dist/serialize.js
CHANGED
|
@@ -40,12 +40,30 @@ function extractSourceLocation(element) {
|
|
|
40
40
|
}
|
|
41
41
|
return undefined;
|
|
42
42
|
}
|
|
43
|
+
// ─── Component resolution ────────────────────────────────────────────
|
|
44
|
+
/**
|
|
45
|
+
* Resolve wrapper function components (e.g. `<MyReport data={...} />`) by
|
|
46
|
+
* calling them until we reach a Forme primitive like `<Document>`. This lets
|
|
47
|
+
* users pass custom components directly to `renderDocument()` /
|
|
48
|
+
* `serialize()` without manually invoking them first.
|
|
49
|
+
*/
|
|
50
|
+
function resolveElement(element) {
|
|
51
|
+
let resolved = element;
|
|
52
|
+
for (let i = 0; i < 10 && typeof resolved.type === 'function' && resolved.type !== Document; i++) {
|
|
53
|
+
const result = resolved.type(resolved.props);
|
|
54
|
+
if (!isValidElement(result))
|
|
55
|
+
break;
|
|
56
|
+
resolved = result;
|
|
57
|
+
}
|
|
58
|
+
return resolved;
|
|
59
|
+
}
|
|
43
60
|
// ─── Public API ──────────────────────────────────────────────────────
|
|
44
61
|
/**
|
|
45
62
|
* Serialize a React element tree into a Forme JSON document object.
|
|
46
|
-
* The top-level element must be a <Document
|
|
63
|
+
* The top-level element must be a <Document> (or a component that returns one).
|
|
47
64
|
*/
|
|
48
65
|
export function serialize(element) {
|
|
66
|
+
element = resolveElement(element);
|
|
49
67
|
if (element.type !== Document) {
|
|
50
68
|
throw new Error('Top-level element must be <Document>');
|
|
51
69
|
}
|
|
@@ -1141,6 +1159,7 @@ function mergeFonts(globalFonts, docFonts) {
|
|
|
1141
1159
|
* Like `serialize()` but with expression marker detection for template compilation.
|
|
1142
1160
|
*/
|
|
1143
1161
|
export function serializeTemplate(element) {
|
|
1162
|
+
element = resolveElement(element);
|
|
1144
1163
|
if (element.type !== Document) {
|
|
1145
1164
|
throw new Error('Top-level element must be <Document>');
|
|
1146
1165
|
}
|