Skip to content
Closed
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions spec/components/Carousel/Carousel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,43 @@ describe('Carousel component', () => {
expect(carousel).toBeInTheDocument();
});
});

describe('itemCallbacks', () => {
test('calls onProductClick when product card is clicked', () => {
const onProductClick = vi.fn();
render(<CioCarousel items={mockProducts} itemCallbacks={{ onProductClick }} />);

const productCard = screen.getByText('Product 1').closest('.cio-product-card')!;
fireEvent.click(productCard);
expect(onProductClick).toHaveBeenCalledWith(mockProducts[0], 0);
});

test('calls onAddToCart when add to cart button is clicked', () => {
const onAddToCart = vi.fn();
render(<CioCarousel items={mockProducts} itemCallbacks={{ onAddToCart }} />);

const buttons = screen.getAllByText('Add to Cart');
fireEvent.click(buttons[0]);
expect(onAddToCart).toHaveBeenCalledWith(expect.any(Object), mockProducts[0], 0);
});

test('calls onAddToWishlist when wishlist button is clicked', () => {
const onAddToWishlist = vi.fn();
render(<CioCarousel items={mockProducts} itemCallbacks={{ onAddToWishlist }} />);

const wishlistButtons = screen.getAllByLabelText(/add to wishlist/i);
fireEvent.click(wishlistButtons[0]);
expect(onAddToWishlist).toHaveBeenCalledWith(expect.any(Object), mockProducts[0], 0);
});

test('does not throw when itemCallbacks is not provided', () => {
expect(() => {
render(<CioCarousel items={mockProducts} />);

// Without callbacks, clicking should not throw (buttons won't exist)
const productCard = screen.getByText('Product 1').closest('.cio-product-card')!;
fireEvent.click(productCard);
}).not.toThrow();
});
});
});
8 changes: 7 additions & 1 deletion src/components/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ function CarouselBase<T = Product>({
}

function Carousel<T = Product>(props: CarouselOpts<T>) {
const { children, items, componentOverrides, ...rest } = props;
const { children, items, componentOverrides, itemCallbacks, ...rest } = props;
const { onProductClick, onAddToCart, onAddToWishlist } = itemCallbacks || {};
const { autoPlay, slidesToScroll, orientation, loop, responsive } = rest;

const renderProps: CarouselRenderProps<T> = {
Expand Down Expand Up @@ -225,6 +226,11 @@ function Carousel<T = Product>(props: CarouselOpts<T>) {
<ProductCard
product={product}
className='w-full h-full'
onProductClick={onProductClick ? () => onProductClick(item, index) : undefined}
onAddToCart={onAddToCart ? (e) => onAddToCart(e, item, index) : undefined}
onAddToWishlist={
onAddToWishlist ? (e) => onAddToWishlist(e, item, index) : undefined
}
componentOverrides={componentOverrides?.item?.productCard}
/>
</CarouselItem>
Expand Down
7 changes: 7 additions & 0 deletions src/types/carouselTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ export type CarouselOverrides<T = Product> = ComponentOverrideProps<CarouselRend
next?: ComponentOverrideProps<CarouselRenderProps<T>>;
};

export type CarouselItemCallbacks<T = Product> = {
onProductClick?: (item: T, index: number) => void;
onAddToCart?: (e: React.MouseEvent, item: T, index: number) => void;
onAddToWishlist?: (e: React.MouseEvent, item: T, index: number) => void;
};

Comment on lines +76 to +81
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we move this into the types for ProductCard instead? This will be then be generically interpreted if the consumer uses the carousel for others types

export type CarouselOpts<T = Product> = CarouselRenderProps<T> &
IncludeComponentOverrides<CarouselOverrides<T>> &
Omit<ComponentProps<'div'>, 'children'> & {
children?: RenderPropsChildren<CarouselRenderProps<T>>;
itemCallbacks?: CarouselItemCallbacks<T>;
};
Loading