From beb9b2e47ced6448c27aeb379c85da7ea830e19a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 25 Aug 2021 18:46:37 +0700 Subject: [PATCH] :bug: bug: dot :%s --- pages/index.tsx | 2 +- .../CarouselCommon/CaroucelCommon.module.scss | 70 +++++++++++++------ .../common/CarouselCommon/CarouselCommon.tsx | 45 ++++++++++-- .../CarouselCommon/CustomDot/CustomDot.tsx | 21 ++++++ 4 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 src/components/common/CarouselCommon/CustomDot/CustomDot.tsx diff --git a/pages/index.tsx b/pages/index.tsx index 6872af8e1..e769be751 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -86,7 +86,7 @@ const dataTest = [{ export default function Home() { return ( <> - + ) } diff --git a/src/components/common/CarouselCommon/CaroucelCommon.module.scss b/src/components/common/CarouselCommon/CaroucelCommon.module.scss index f38db8f73..373d03f31 100644 --- a/src/components/common/CarouselCommon/CaroucelCommon.module.scss +++ b/src/components/common/CarouselCommon/CaroucelCommon.module.scss @@ -1,25 +1,51 @@ @import '../../../styles/utilities'; -.navigationWrapper{ - @apply relative; - min-height: theme("caroucel.arrow-height") ; - .isPadding{ - @apply spacing-horizontal; +.navigationWrapper { + @apply relative; + min-height: theme('caroucel.arrow-height'); + .isPadding { + @apply spacing-horizontal; + } + :global(.customArrow) { + width: 64px; + height: 64px; + &:focus { + outline: none; } - :global(.customArrow) { - width: 64px; - height: 64px; - &:focus{ - outline: none; - } - @apply absolute top-1/2 bg-background-arrow transform -translate-y-1/2 flex justify-center items-center transition duration-100; - &:global(.leftArrow){ - @apply left-0; - } - &:global(.rightArrow){ - @apply right-0; - } - &:global(.isDisabledArrow){ - @apply hidden ; - } + @apply absolute top-1/2 bg-background-arrow transform -translate-y-1/2 flex justify-center items-center transition duration-100; + &:global(.leftArrow) { + @apply left-0; } -} \ No newline at end of file + &:global(.rightArrow) { + @apply right-0; + } + &:global(.isDisabledArrow) { + @apply hidden; + } + } + :global { + .dots { + display: flex; + padding: 10px 0; + justify-content: center; + } + + .dot { + border: none; + width: 10px; + height: 10px; + background: #c5c5c5; + border-radius: 50%; + margin: 0 5px; + padding: 5px; + cursor: pointer; + } + + .dot:focus { + outline: none; + } + + .dot.active { + background: #000; + } + } +} diff --git a/src/components/common/CarouselCommon/CarouselCommon.tsx b/src/components/common/CarouselCommon/CarouselCommon.tsx index 2d5d467e2..ab9f688c7 100644 --- a/src/components/common/CarouselCommon/CarouselCommon.tsx +++ b/src/components/common/CarouselCommon/CarouselCommon.tsx @@ -1,14 +1,16 @@ import { useKeenSlider } from 'keen-slider/react' -import React from 'react' +import React, { useEffect } from 'react' import 'keen-slider/keen-slider.min.css' import { CustomCarouselArrow } from './CustomArrow/CustomCarouselArrow' import s from './CaroucelCommon.module.scss' import { TOptionsEvents } from 'keen-slider' import classNames from 'classnames' +import CustomDot from './CustomDot/CustomDot' export interface CarouselCommonProps { data: T[] Component: React.ComponentType isArrow?: Boolean + isDot?: Boolean itemKey: String option: TOptionsEvents keenClassname?: string @@ -19,12 +21,18 @@ const CarouselCommon = ({ data, Component, itemKey, - keenClassname,isPadding=false, - option: { slideChanged, ...sliderOption }, + keenClassname, + isPadding = false, + isArrow = true, + isDot = false, + option: { slideChanged,slidesPerView, ...sliderOption }, }: CarouselCommonProps) => { const [currentSlide, setCurrentSlide] = React.useState(0) + const [dotActive, setDotActive] = React.useState(0) + const [dotArr, setDotArr] = React.useState([]) const [sliderRef, slider] = useKeenSlider({ ...sliderOption, + slidesPerView, slideChanged(s) { setCurrentSlide(s.details().relativeSlide) }, @@ -33,19 +41,37 @@ const CarouselCommon = ({ slider.next() } + useEffect(() => { + if(isDot && slider){ + console.log('f',Math.ceil(data.length/(Number(slider.details().slidesPerView)||1))) + setDotArr([...Array(Math.ceil(data.length/(Number(slider.details().slidesPerView)||1))).keys()]) + } + }, [isDot,slider]) + const handleLeftArrowClick = () => { slider.prev() } + + const onDotClick = (index:number) => { + slider.moveToSlide(((Number(slider.details().slidesPerView)||1)*index)) + setDotActive(index) + + } return (
-
+
{data?.map((props, index) => (
))}
- {slider && ( + {slider && isArrow && ( <> ({ /> )} + {slider && isDot && ( +
+ {dotArr.map((index) => { + return ( + + ) + })} +
+ )}
) } diff --git a/src/components/common/CarouselCommon/CustomDot/CustomDot.tsx b/src/components/common/CarouselCommon/CustomDot/CustomDot.tsx new file mode 100644 index 000000000..7c3615c45 --- /dev/null +++ b/src/components/common/CarouselCommon/CustomDot/CustomDot.tsx @@ -0,0 +1,21 @@ +import React from 'react' + +interface Props { + index: number + dotActive:number + onClick: (index: number) => void +} + +const CustomDot = ({ index, onClick, dotActive }: Props) => { + const handleOnClick = () => { + onClick && onClick(index) + } + return ( +