-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: TypeError when returning fragments or arrays from render prop (#…
- Loading branch information
Showing
5 changed files
with
199 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@react-pdf/layout': patch | ||
'@react-pdf/renderer': patch | ||
--- | ||
|
||
fix: TypeError when returning fragments or arrays from render prop |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { castArray } from '@react-pdf/fns'; | ||
import { TextInstance } from '@react-pdf/primitives'; | ||
|
||
const isString = value => typeof value === 'string'; | ||
|
||
const isNumber = value => typeof value === 'number'; | ||
|
||
const isFragment = value => | ||
value && value.type === Symbol.for('react.fragment'); | ||
|
||
/** | ||
* Transforms a react element instance to internal element format. | ||
* | ||
* Can return multiple instances in the case of arrays or fragments. | ||
* | ||
* @param {Object} React element | ||
* @returns {Array} parsed react elements | ||
*/ | ||
const createInstances = element => { | ||
if (!element) return []; | ||
|
||
if (isString(element) || isNumber(element)) { | ||
return [{ type: TextInstance, value: `${element}` }]; | ||
} | ||
|
||
if (isFragment(element)) { | ||
return createInstances(element.props.children); | ||
} | ||
|
||
if (Array.isArray(element)) { | ||
return element.reduce((acc, el) => acc.concat(createInstances(el)), []); | ||
} | ||
|
||
if (!isString(element.type)) { | ||
return createInstances(element.type(element.props)); | ||
} | ||
|
||
const { | ||
type, | ||
props: { style = {}, children = [], ...props }, | ||
} = element; | ||
|
||
const nextChildren = castArray(children).reduce( | ||
(acc, child) => acc.concat(createInstances(child)), | ||
[], | ||
); | ||
|
||
return [ | ||
{ | ||
type, | ||
style, | ||
props, | ||
box: {}, | ||
children: nextChildren, | ||
}, | ||
]; | ||
}; | ||
|
||
export default createInstances; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters