Skip to content

Commit 13baabe

Browse files
committed
fix: Insertion of Dynamic Child in Carousel crashes
1 parent e204ee4 commit 13baabe

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

packages/ui/src/components/Carousel/Carousel.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import type { ComponentProps, FC, ReactElement, ReactNode } from "react";
4-
import { Children, cloneElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
4+
import { Children, cloneElement, isValidElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
55
import { HiOutlineChevronLeft, HiOutlineChevronRight } from "react-icons/hi";
66
import { twMerge } from "tailwind-merge";
77
import ScrollContainer from "../../helpers/drag-scroll";
@@ -88,11 +88,15 @@ export const Carousel: FC<CarouselProps> = ({
8888

8989
const items = useMemo(
9090
() =>
91-
Children.map(children as ReactElement[], (child: ReactElement) =>
92-
cloneElement(child, {
93-
className: twMerge(theme.item.base, child.props.className),
94-
}),
95-
),
91+
Children.map(children as ReactElement[], (child: ReactElement<CarouselProps>) => {
92+
if (isValidElement(child)) {
93+
return cloneElement(child as ReactElement, {
94+
className: twMerge(theme.item.base, child.props.className || ""),
95+
});
96+
}
97+
// If child is not a valid React element, return it as-is or handle it accordingly
98+
return child;
99+
}),
96100
[children, theme.item.base],
97101
);
98102

0 commit comments

Comments
 (0)