Skip to content

Commit 1521618

Browse files
authored
Flattened react fragments to include nested fragments (#728)
1 parent 05eb8c6 commit 1521618

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/core/src/__tests__/FragmentWrapped.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ describe("Type checked components wrapped in a fragment tests", () => {
7474
<View testID="5" />
7575
<View testID="6" />
7676
</>,
77+
<>
78+
<>
79+
<View testID="7" />
80+
</>
81+
</>,
82+
<>
83+
<>
84+
<>
85+
<View testID="8" />
86+
</>
87+
<View testID="9" />
88+
</>
89+
</>,
7790
];
7891

7992
const result = flattenReactFragments(components);
@@ -97,6 +110,15 @@ describe("Type checked components wrapped in a fragment tests", () => {
97110
<View
98111
testID="6"
99112
/>,
113+
<View
114+
testID="7"
115+
/>,
116+
<View
117+
testID="8"
118+
/>,
119+
<View
120+
testID="9"
121+
/>,
100122
]
101123
`);
102124
});

packages/core/src/utilities.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function getValueForRadioButton(value: string | number) {
231231
}
232232

233233
/**
234-
* Flattens array of components to remove any top level React.Fragment's (<> </>) and returns the fragment's children in its place
234+
* Flattens array of components to remove any React.Fragment's (<> </>) and returns the fragment's children in its place
235235
* This is useful for operations that depend on a particular child type that would otherwise not match when wrapped in a fragment
236236
*/
237237
export function flattenReactFragments(
@@ -244,7 +244,9 @@ export function flattenReactFragments(
244244
component.props?.children
245245
) as React.ReactElement[];
246246

247-
flattened.push(...children);
247+
for (const child of children) {
248+
flattened.push(...flattenReactFragments([child]));
249+
}
248250
} else {
249251
flattened.push(component);
250252
}

0 commit comments

Comments
 (0)