Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Insertion of Dynamic Child in Carousel crashes the Page/App #1475

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/ui/src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

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

const items = useMemo(
() =>
Children.map(children as ReactElement[], (child: ReactElement) =>
cloneElement(child, {
className: twMerge(theme.item.base, child.props.className),
}),
),
Children.map(children as ReactElement[], (child: ReactElement<CarouselProps>) => {
if (isValidElement(child)) {
return cloneElement(child as ReactElement, {
className: twMerge(theme.item.base, child.props.className || ""),
});
}
// If child is not a valid React element, return it as-is or handle it accordingly
return child;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we also want to include the twMerge for the child.className here? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but here, on this specific line there won't be any child.className because of the dynamic child, child.props & child.props.className is throwing Error @rluders

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rluders & @SutuSebastian --- your inputs please

}),
[children, theme.item.base],
);

Expand Down
Loading