Skip to content

Commit 276b6bf

Browse files
committed
fix: undefined and null wrap prop values are rendered as undefined instead of true by default
1 parent ded9cdc commit 276b6bf

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

.changeset/fix-undefined-wrap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/layout': patch
3+
---
4+
5+
Treat an explicit `wrap={undefined}` like an omitted `wrap` prop (default `true`). `null` and `false` still disable wrapping.

packages/layout/src/node/getWrap.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const getWrap = (node: SafeNode) => {
88

99
if (!node.props) return true;
1010

11-
return 'wrap' in node.props ? node.props.wrap : true;
11+
return 'wrap' in node.props
12+
? node.props.wrap === undefined || node.props.wrap === true
13+
: true;
1214
};
1315

1416
export default getWrap;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, expect, test } from 'vitest';
2+
3+
import getWrap from '../../src/node/getWrap';
4+
5+
describe('node getWrap', () => {
6+
test('Should return false by default for non-wrap types', () => {
7+
const svgResult = getWrap({ type: 'SVG', props: {}, style: {} });
8+
expect(svgResult).toBe(false);
9+
10+
const noteResult = getWrap({ type: 'NOTE', props: {}, style: {} });
11+
expect(noteResult).toBe(false);
12+
13+
const imageResult = getWrap({
14+
type: 'IMAGE',
15+
props: { src: '' },
16+
style: {},
17+
});
18+
expect(imageResult).toBe(false);
19+
20+
const canvasResult = getWrap({
21+
type: 'CANVAS',
22+
props: { paint: () => null },
23+
style: {},
24+
});
25+
expect(canvasResult).toBe(false);
26+
});
27+
28+
test('Should return true by default for other types', () => {
29+
const viewResult = getWrap({ type: 'VIEW', props: {}, style: {} });
30+
expect(viewResult).toBe(true);
31+
32+
const textResult = getWrap({ type: 'TEXT', props: {}, style: {} });
33+
expect(textResult).toBe(true);
34+
});
35+
36+
test('Should return true if wrap value is undefined', () => {
37+
const undefinedResult = getWrap({
38+
type: 'VIEW',
39+
props: { wrap: undefined },
40+
style: {},
41+
});
42+
expect(undefinedResult).toBe(true);
43+
});
44+
45+
test('Should return false if wrap value is null', () => {
46+
// @ts-expect-error Deliberately testing an invalid case to ensure it's also handled gracefully
47+
const nullResult = getWrap({
48+
type: 'VIEW',
49+
props: { wrap: null },
50+
style: {},
51+
});
52+
expect(nullResult).toBe(false);
53+
});
54+
55+
test('Should return the custom wrap value if provided', () => {
56+
const trueResult = getWrap({
57+
type: 'VIEW',
58+
props: { wrap: true },
59+
style: {},
60+
});
61+
expect(trueResult).toBe(true);
62+
63+
const falseResult = getWrap({
64+
type: 'VIEW',
65+
props: { wrap: false },
66+
style: {},
67+
});
68+
expect(falseResult).toBe(false);
69+
});
70+
});

0 commit comments

Comments
 (0)