Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (120 loc) · 2.88 KB
/
Copy pathindex.js
File metadata and controls
139 lines (120 loc) · 2.88 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React, { useState, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
// Custom Interval hook.
const useInterval = (callback, delay) => {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
useEffect(() => {
const tick = () => savedCallback.current();
if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
};
const Dot = ({
color,
radius = 5,
}) => (
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: radius * 2,
height: radius * 2,
backgroundColor: color,
borderRadius: radius,
borderStyle: 'none',
}}
/>
);
Dot.propTypes = {
color: PropTypes.string,
radius: PropTypes.number,
};
Dot.defaultProps = {
color: '#6E6E6E',
radius: 5,
};
const Carousel = ({
pages,
advanceButton = false,
automatic = false,
clickable = false,
finishText = 'finish',
interval = 1000,
onFinish = null,
}) => {
const [index, setIndex] = useState(0);
const [resetBit, setResetBit] = useState(false);
const lastPage = pages.length - 1;
const nextPage = () => {
if (index < lastPage) {
return setIndex(index + 1);
}
if (onFinish) {
return onFinish();
}
return setIndex(0);
};
useInterval(() => {
if (automatic) nextPage();
}, resetBit ? null : interval);
const clickPage = (newIndex) => {
setIndex(newIndex);
setResetBit(true);
setTimeout(() => setResetBit(false), interval);
};
return (
<React.Fragment>
{pages[index]}
{advanceButton && <button type="button" onClick={nextPage}>{onFinish && index === lastPage ? finishText : 'next'}</button>}
<ProgressContainer>
{pages.map((_, i) => (
<DotContainer
onClick={clickable ? () => clickPage(i) : null}
clickable={clickable}
>
<Dot
radius={5}
color={index === i ? '#CBCBCB' : '#6E6E6E'}
/>
</DotContainer>
))}
</ProgressContainer>
</React.Fragment>
);
};
Carousel.propTypes = {
pages: PropTypes.array.isRequired,
advanceButton: PropTypes.bool,
automatic: PropTypes.bool,
clickable: PropTypes.bool,
finishText: PropTypes.string,
interval: PropTypes.number,
onFinish: PropTypes.func,
};
Carousel.defaultProps = {
advanceButton: false,
automatic: false,
clickable: false,
finishText: 'finish',
interval: 1000,
onFinish: null,
};
const ProgressContainer = styled.div`
display: flex;
justify-content: center;
padding: 20px 0px;
`;
const DotContainer = styled.div`
padding: 3px;
${props => (props.clickable ? 'cursor: pointer;' : null)}
`;
export default Carousel;