Skip to content

Commit

Permalink
fix: carousel: add controls prop to scroll menu (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkilgore-eightfold authored Aug 31, 2023
1 parent 3404bb1 commit 0044f69
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ export const Carousel: FC<CarouselProps> = React.forwardRef(
{type === 'scroll' && (
<ResizeObserver onResize={updateScrollMode}>
<ScrollMenu
controls={controls}
nextButton={() => autoScrollButton('next')}
onWheel={handleOnWheel}
previousButton={() => autoScrollButton('previous')}
Expand Down
5 changes: 5 additions & 0 deletions src/components/Carousel/Carousel.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export interface ScrollMenuProps
* @default 0
*/
containerPadding?: number;
/**
* Whether to display the previous and next controls.
* @default true
*/
controls?: boolean;
/**
* The pixel gap between items.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/components/Carousel/ScrollMenu/ScrollMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const ScrollMenu: FC<ScrollMenuProps> = forwardRef(
apiRef = { current: {} as autoScrollApiType },
children,
containerPadding = 0,
controls = true,
gap = 8,
itemClassNames,
nextButton: _rightArrow,
Expand Down Expand Up @@ -163,7 +164,7 @@ export const ScrollMenu: FC<ScrollMenuProps> = forwardRef(
>
<VisibilityContext.Provider value={context}>
<div className={innerWrapperClassName}>
{!context?.isFirstItemVisible && LeftArrow}
{!context?.isFirstItemVisible && controls && LeftArrow}
<ScrollContainer
classNames={scrollContainerClassNames}
containerPadding={containerPadding}
Expand All @@ -181,7 +182,7 @@ export const ScrollMenu: FC<ScrollMenuProps> = forwardRef(
{children}
</MenuItems>
</ScrollContainer>
{!context?.isLastItemVisible && RightArrow}
{!context?.isLastItemVisible && controls && RightArrow}
</div>
</VisibilityContext.Provider>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/components/Carousel/Tests/ScrollMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ describe('ScrollMenu', () => {

expect(container).toMatchSnapshot();
});

test('Hides LeftArrow, RightArrow', () => {
(useIntersectionObserver as jest.Mock).mockReturnValue({
visibleElementsWithSeparators: defaultItemsWithSeparators,
});

const { container } = setup({
previousButton: PreviousButton,
nextButton: NextButton,
controls: false,
});

expect(container.querySelector('.left-arrow')).toBeFalsy();
expect(container.querySelector('.right-arrow')).toBeFalsy();

expect(container).toMatchSnapshot();
});
});

describe('Events', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ScrollMenu Children and buttons Hides LeftArrow, RightArrow 1`] = `
<div>
<div
class="carousel-auto-scroll"
>
<div
class="carousel-auto-scroll-inner-wrapper"
>
<ul
class="carousel-auto-scroll-container"
style="margin: 0px; padding: 0px;"
>
<li
class="carousel-scroll-menu-item"
data-index="0"
data-key="test1"
style="margin-left: 0px; margin-right: 4px;"
>
<div
data-testid="test1"
>
{"itemId":"test1","isFirstItemVisible":false,"isLastItemVisible":false,"visibleElements":["test1","test2"],"visibleElementsWithSeparators":["test1","item1-separator","test2"],"visibleItems":["test1","item1-separator","test2"],"visibleItemsWithoutSeparators":["test1","test2"],"initComplete":true}
</div>
</li>
<div
aria-hidden="true"
class="carousel-scroll-menu-separator"
data-index="0.1"
data-key="test1-separator"
style="margin-left: 0px; margin-right: 4px;"
/>
<li
class="carousel-scroll-menu-item"
data-index="1"
data-key="test2"
style="margin-left: 0px; margin-right: 4px;"
>
<div
data-testid="test2"
>
{"itemId":"test2","isFirstItemVisible":false,"isLastItemVisible":false,"visibleElements":["test1","test2"],"visibleElementsWithSeparators":["test1","item1-separator","test2"],"visibleItems":["test1","item1-separator","test2"],"visibleItemsWithoutSeparators":["test1","test2"],"initComplete":true}
</div>
</li>
</ul>
</div>
</div>
</div>
`;

exports[`ScrollMenu Children and buttons LeftArrow, ScrollContainer, MenuItems, RightArrow 1`] = `
<div>
<div
Expand Down

0 comments on commit 0044f69

Please sign in to comment.