From c27809ccad34b4b7353423157e55a57d9578bef2 Mon Sep 17 00:00:00 2001 From: jomcarvajal Date: Wed, 22 Jan 2025 16:33:45 -0600 Subject: [PATCH] resolve comments, fix bug with disabled arrows --- src/components/Carousel.stories.tsx | 4 +-- src/components/Carousel/index.tsx | 28 ++++++++++++++++--- src/components/Carousel/styles.ts | 6 ++-- src/components/ToggleButtonGroup.stories.tsx | 10 +++---- src/components/ToggleButtonGroup/index.tsx | 6 ++-- .../__snapshots__/Carousel.spec.tsx.snap | 16 +++++------ 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/components/Carousel.stories.tsx b/src/components/Carousel.stories.tsx index 964f7b37..2a2bf378 100644 --- a/src/components/Carousel.stories.tsx +++ b/src/components/Carousel.stories.tsx @@ -40,7 +40,7 @@ export const WithToggleButtonGroups = () => { const FirstToggleGroup = {firstSection} @@ -48,7 +48,7 @@ export const WithToggleButtonGroups = () => { const SecondToggleGroup = {secondSection} diff --git a/src/components/Carousel/index.tsx b/src/components/Carousel/index.tsx index 732af6d8..3b64763e 100644 --- a/src/components/Carousel/index.tsx +++ b/src/components/Carousel/index.tsx @@ -12,17 +12,28 @@ export const Carousel = ({ children, customWidth }: CarouselProps) => { const [dragX, setDragX] = React.useState(0); const [dragging, setDragging] = React.useState(false); const wrapperRef = React.useRef(null); + const [isLeftArrowDisabled, setIsLeftArrowDisabled] = React.useState(false); + const [isRightArrowDisabled, setIsRightArrowDisabled] = React.useState(false); const scrollSpeed = 100; - const handlePrev = () => { + React.useEffect(() => { if (wrapperRef.current) { + const isDisabled = (currentIndex >= ((wrapperRef.current.scrollWidth || 0) - (wrapperRef.current.clientWidth || 0))); + setIsRightArrowDisabled(isDisabled); + setIsLeftArrowDisabled((currentIndex) === 0); + + } + }, [currentIndex, wrapperRef]); + + const handlePrev = () => { + if (wrapperRef.current && !isLeftArrowDisabled) { const itemWidth = (wrapperRef.current.clientWidth / children.length); setCurrentIndex((prevIndex) => Math.max(prevIndex - itemWidth - scrollSpeed, 0)); } }; const handleNext = () => { - if (wrapperRef.current) { + if (wrapperRef.current && !isRightArrowDisabled) { const itemWidth = (wrapperRef.current.clientWidth / children.length); const maxIndex = (wrapperRef.current.scrollWidth - wrapperRef.current.clientWidth); setCurrentIndex((prevIndex) => Math.min(prevIndex + itemWidth + scrollSpeed, maxIndex)); @@ -61,7 +72,12 @@ export const Carousel = ({ children, customWidth }: CarouselProps) => { return ( - + { })} - = ((wrapperRef.current?.scrollWidth) || 0) - (wrapperRef.current?.clientWidth || 0)} /> + ); diff --git a/src/components/Carousel/styles.ts b/src/components/Carousel/styles.ts index 2c870e4c..3344cf56 100644 --- a/src/components/Carousel/styles.ts +++ b/src/components/Carousel/styles.ts @@ -4,7 +4,7 @@ import { RightArrow } from "../svgs/RightArrow"; export const CarouselContainer = styled.div<{customWidth?: string}>` position: relative; - width: ${({customWidth}) => customWidth ? customWidth : '30rem'}; + width: ${({customWidth}) => customWidth ? customWidth : '100%'}; margin: 0 4rem; `; @@ -27,7 +27,7 @@ export const CarouselItem = styled.div` export const StyledLeftArrow = styled(LeftArrow)<{ disabled: boolean }>` position: absolute; top: 50%; - left: -2rem; + left: -3rem; transform: translateY(-50%); cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')}; opacity: ${({ disabled }) => (disabled ? 0.5 : 1)}; @@ -36,7 +36,7 @@ export const StyledLeftArrow = styled(LeftArrow)<{ disabled: boolean }>` export const StyledRightArrow = styled(RightArrow)<{ disabled: boolean }>` position: absolute; top: 50%; - right: -2rem; + right: -3rem; transform: translateY(-50%); cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')}; opacity: ${({ disabled }) => (disabled ? 0.5 : 1)}; diff --git a/src/components/ToggleButtonGroup.stories.tsx b/src/components/ToggleButtonGroup.stories.tsx index ce28c2f2..cd163627 100644 --- a/src/components/ToggleButtonGroup.stories.tsx +++ b/src/components/ToggleButtonGroup.stories.tsx @@ -18,7 +18,7 @@ export const MultipleSelection = () => { {childrenListWithKeys} @@ -29,16 +29,16 @@ export const MultipleSelection = () => { }; export const SingleSelection = () => { - const [selectedIetms, setSelectedItems] = React.useState(new Set([])); + const [selectedItems, setSelectedItems] = React.useState(new Set([])); return ( <> {childrenListWithKeys} -

Current selections: {[...selectedIetms].join(', ')}

+

Current selections: {[...selectedItems].join(', ')}

); diff --git a/src/components/ToggleButtonGroup/index.tsx b/src/components/ToggleButtonGroup/index.tsx index 5739f342..d24ec1ce 100644 --- a/src/components/ToggleButtonGroup/index.tsx +++ b/src/components/ToggleButtonGroup/index.tsx @@ -5,7 +5,7 @@ import { Key } from "react-aria-components"; export interface ToggleButtonGroupProps { children: { key: string, value: string }[]; selectedItems: Set; - handlerSelectedItems?: React.Dispatch>>; + onSelectionChange?: React.Dispatch>>; selectionMode?: 'single' | 'multiple' } @@ -13,7 +13,7 @@ export const ToggleButtonGroup = ( { children, selectedItems, - handlerSelectedItems, + onSelectionChange, selectionMode = 'single', }: ToggleButtonGroupProps) => { @@ -21,7 +21,7 @@ export const ToggleButtonGroup = ( {children.map((child) =>