Skip to content

Commit 96b0de4

Browse files
authored
fix(withWebcomponent): correctly propagate/set key for slot components (#5230)
1 parent 6710b18 commit 96b0de4

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
- charts
2020
- cypress-commands
2121
- main/src/components
22+
- main/src/internal
2223
fail-fast: false
2324

2425
steps:
@@ -51,4 +52,4 @@ jobs:
5152
with:
5253
github-token: ${{ secrets.GITHUB_TOKEN }}
5354
parallel-finished: true
54-
carryforward: 'base,charts,cypress-commands,main/src/components'
55+
carryforward: 'base,charts,cypress-commands,main/src/components,main/src/internal'

packages/main/src/internal/withWebComponent.cy.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ describe('withWebComponent', () => {
7979
<Bar
8080
startContent={
8181
<>
82+
{true}
83+
not mounted
8284
{false && <span>not mounted</span>}
8385
<span>mounted</span>
8486
<>

packages/main/src/internal/withWebComponent.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { getEffectiveScopingSuffixForTag } from '@ui5/webcomponents-base/dist/CustomElementsScope.js';
44
import { useIsomorphicLayoutEffect, useSyncRef } from '@ui5/webcomponents-react-base';
5-
import type { ComponentType, ReactElement, Ref } from 'react';
6-
import React, { Children, cloneElement, forwardRef, Fragment, useEffect, useState } from 'react';
5+
import type { ComponentType, ReactElement, ReactNode, Ref } from 'react';
6+
import React, { cloneElement, forwardRef, Fragment, isValidElement, useEffect, useState } from 'react';
77
import type { CommonProps, Ui5DomRef } from '../interfaces/index.js';
88
import { useServerSideEffect } from './ssr.js';
99
import { camelToKebabCase, capitalizeFirstLetter, kebabToCamelCase } from './utils.js';
@@ -77,18 +77,25 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
7777

7878
const slottedChildren = [];
7979
let index = 0;
80-
const removeFragments = (element) => {
81-
if (!element) return;
80+
const removeFragments = (element: ReactNode) => {
81+
if (!isValidElement(element)) return;
8282
if (element.type === Fragment) {
83-
Children.toArray(element.props?.children)
84-
.filter(Boolean)
85-
.forEach((item) => {
86-
removeFragments(item);
83+
const elementChildren = element.props?.children;
84+
if (Array.isArray(elementChildren)) {
85+
elementChildren.forEach((item) => {
86+
if (Array.isArray(item)) {
87+
item.forEach(removeFragments);
88+
} else {
89+
removeFragments(item);
90+
}
8791
});
92+
} else {
93+
removeFragments(elementChildren);
94+
}
8895
} else {
8996
slottedChildren.push(
90-
cloneElement(element, {
91-
key: `${name}-${index}`,
97+
cloneElement<Partial<HTMLElement>>(element, {
98+
key: element.key ?? `${name}-${index}`,
9299
slot: name
93100
})
94101
);

0 commit comments

Comments
 (0)