Skip to content

Commit f92c83c

Browse files
committed
refactor: exclude non-CoreUI components from cloneElement
1 parent c0b468c commit f92c83c

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

packages/coreui-react/src/components/sidebar/CSidebarNav.tsx

+12-11
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@ export const CNavContext = createContext({} as ContextProps)
3434

3535
const recursiveClone = (children: ReactNode, id?: string, updateId?: boolean): ReactNode => {
3636
return React.Children.map(children, (child: ReactNode, index: number) => {
37-
if (!React.isValidElement(child)) {
37+
if (
38+
!React.isValidElement(child) ||
39+
// @ts-expect-error the `children` exist in each component. TODO: resolve
40+
(child.type.displayName !== 'CNavGroup' &&
41+
// @ts-expect-error the `children` exist in each component. TODO: resolve
42+
child.type.displayName !== 'CNavLink' &&
43+
// @ts-expect-error the `children` exist in each component. TODO: resolve
44+
child.type.displayName !== 'CNavItem')
45+
) {
3846
return child
3947
}
4048

4149
const _id = id ? (updateId ? `${id}.${index}` : `${id}`) : `${index}`
4250

4351
if (child.props && child.props.children) {
44-
child = React.cloneElement(child, {
45-
// @ts-expect-error the `displayName` exist in each component. TODO: resolve
46-
...((child.type.displayName === 'CNavGroup' || child.type.displayName === 'CNavLink') && {
47-
idx: _id,
48-
}),
49-
// @ts-expect-error the `children` exist in each component. TODO: resolve
52+
return React.cloneElement(child as ReactElement<any>, {
53+
idx: _id,
5054
children: recursiveClone(
5155
child.props.children,
5256
_id,
@@ -57,10 +61,7 @@ const recursiveClone = (children: ReactNode, id?: string, updateId?: boolean): R
5761
}
5862

5963
return React.cloneElement(child as ReactElement<any>, {
60-
// @ts-expect-error the `displayName` exist in each component. TODO: resolve
61-
...((child.type.displayName === 'CNavGroup' || child.type.displayName === 'CNavLink') && {
62-
idx: _id,
63-
}),
64+
idx: _id,
6465
})
6566
})
6667
}

0 commit comments

Comments
 (0)