myslide.tsx 1.5 KB
Newer Older
백승민's avatar
백승민 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useRef, useState } from "react";

type num = {
    total: number,
    page: number,
    setPage: Function,
    style: string,
    setStyle: Function,
    // slides : any[],
}

export function MySlide({ total, page, setPage, style, setStyle}: num) {

    const numPages = Math.ceil(total / 15);

    const firstLeftClick = useRef(true);
    const firstRightClick = useRef(true);
    const slide = useRef(1);

    const leftClick = () => {
        if (firstLeftClick.current) {
            firstLeftClick.current = false;
            firstRightClick.current = true;
        } else {
            setPage(page - 1)
        }
        slide.current -= 1;
        setStyle("-translate-x-full animate-slidetoright");
    };

    const rightClick = () => {
        if (firstRightClick.current) {
            firstLeftClick.current = true;
            firstRightClick.current = false;
        } else {
            setPage(page + 1)
        }
        slide.current += 1;
        setStyle("animate-slidetoleft");
    };

    return (
        <div>
            <button onClick={leftClick} disabled={slide.current === 1}>
                &lt;{slide.current}
            </button>
            <button onClick={rightClick} disabled={slide.current === numPages}>
                &gt;
            </button>
        </div>
        

    );
};




{/* {Array(numPages)
        .fill(1)
        .map((_, i) => (
            <button key={i + 1} onClick={() => setPage(i + 1)}>
                {i + 1}
            </button>
        ))} */}