|
2 | 2 |
|
3 | 3 | import { getEffectiveScopingSuffixForTag } from '@ui5/webcomponents-base/dist/CustomElementsScope.js';
|
4 | 4 | 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'; |
7 | 7 | import type { CommonProps, Ui5DomRef } from '../interfaces/index.js';
|
8 | 8 | import { useServerSideEffect } from './ssr.js';
|
9 | 9 | import { camelToKebabCase, capitalizeFirstLetter, kebabToCamelCase } from './utils.js';
|
@@ -77,18 +77,25 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
|
77 | 77 |
|
78 | 78 | const slottedChildren = [];
|
79 | 79 | let index = 0;
|
80 |
| - const removeFragments = (element) => { |
81 |
| - if (!element) return; |
| 80 | + const removeFragments = (element: ReactNode) => { |
| 81 | + if (!isValidElement(element)) return; |
82 | 82 | 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 | + } |
87 | 91 | });
|
| 92 | + } else { |
| 93 | + removeFragments(elementChildren); |
| 94 | + } |
88 | 95 | } else {
|
89 | 96 | slottedChildren.push(
|
90 |
| - cloneElement(element, { |
91 |
| - key: `${name}-${index}`, |
| 97 | + cloneElement<Partial<HTMLElement>>(element, { |
| 98 | + key: element.key ?? `${name}-${index}`, |
92 | 99 | slot: name
|
93 | 100 | })
|
94 | 101 | );
|
|
0 commit comments