forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridList.tsx
More file actions
62 lines (58 loc) · 1.76 KB
/
GridList.tsx
File metadata and controls
62 lines (58 loc) · 1.76 KB
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
import React, { cloneElement, Children, useState } from 'react';
import { Group } from 'reakit/Group';
import { useRoverState, Rover } from 'reakit/Rover';
import {
Container,
Arrow,
CarrouselWrapper,
Carrousel,
ArrowButton,
} from './elements';
const chunk = (arr: any[], size: number): any[] =>
Array.from({ length: Math.ceil(arr.length / size) }, (_, i) =>
arr.slice(i * size, i * size + size)
);
export const GridList = ({ children, ...props }) => {
const [numberOfClick, setNumberOfClick] = useState(0);
const rover = useRoverState();
const templates = chunk(children, 3);
const move = (n: number) => {
setNumberOfClick(numberOfClicks => numberOfClicks + n);
};
return (
<Group as={Container} {...props}>
{numberOfClick > 0 ? (
<ArrowButton
type="button"
aria-label="Next Templates"
onClick={() => move(-1)}
>
<Arrow />
</ArrowButton>
) : null}
<Carrousel number={numberOfClick}>
{templates.map((kids: any[], i: number) => (
// eslint-disable-next-line react/no-array-index-key
<CarrouselWrapper key={i}>
{kids.map((child: any[], idx: number) => (
// eslint-disable-next-line react/no-array-index-key
<Rover key={idx} {...rover}>
{itemProps => cloneElement(Children.only(child), itemProps)}
</Rover>
))}
</CarrouselWrapper>
))}
</Carrousel>
{templates.length > 2 && templates.length - 2 !== numberOfClick && (
<ArrowButton
type="button"
aria-label="Next Templates"
onClick={() => move(1)}
next
>
<Arrow next />
</ArrowButton>
)}
</Group>
);
};