-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathCCarousel.js
125 lines (105 loc) · 2.94 KB
/
CCarousel.js
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
import React, {useState, useRef} from 'react';
import PropTypes from 'prop-types';
import CCarouselCustom from './CCarouselCustom';
import CCarouselItem from './CCarouselItem';
import CCarouselControl from './CCarouselControl';
import CCarouselIndicators from './CCarouselIndicators';
import CCarouselCaption from './CCarouselCaption';
//component - CoreUI / CCarousel
const CCarousel = props=>{
const {
//
custom,
autoPlay,
indicators,
controls,
items,
goToIndex
} = props;
const [activeIndex, setActiveIndex] = useState(props.defaultOpen || false);
const fields = useRef({animating: false}).current;
if (!custom){
const onExiting = ()=>{
fields.animating = true;
}
const onExited = ()=>{
fields.animating = false;
}
const next = ()=>{
if (fields.animating) return;
const nextIndex = activeIndex === props.items.length - 1 ? 0 : activeIndex + 1;
setActiveIndex(nextIndex);
}
const previous = ()=>{
if (fields.animating) return;
const nextIndex = activeIndex === 0 ? props.items.length - 1 : activeIndex - 1;
setActiveIndex(nextIndex);
}
const toIndex = newIndex=>{
if (fields.animating) return;
setActiveIndex(newIndex);
}
//render
const slides = items.map((item) => {
return (
<CCarouselItem
onExiting={onExiting}
onExited={onExited}
key={item.src}
>
<img className="d-block w-100" src={item.src} alt={item.altText} />
<CCarouselCaption captionText={item.caption} captionHeader={item.header || item.caption} />
</CCarouselItem>
);
});
return (
<CCarouselCustom
activeIndex={activeIndex}
next={next}
previous={previous}
ride={autoPlay ? 'carousel' : undefined}
{...props}
>
{indicators && <CCarouselIndicators
items={items}
activeIndex={props.activeIndex || activeIndex}
onClickHandler={goToIndex || toIndex}
/>}
{slides}
{controls && <CCarouselControl
direction="prev"
directionText="Previous"
onClickHandler={props.previous || previous}
/>}
{controls && <CCarouselControl
direction="next"
directionText="Next"
onClickHandler={props.next || next}
/>}
</CCarouselCustom>
);
}
return (
<CCarouselCustom {...props}/>
);
}
CCarousel.propTypes = {
custom: PropTypes.bool,
//
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
items: PropTypes.array,
indicators: PropTypes.bool,
defaultOpen: PropTypes.bool,
controls: PropTypes.bool,
autoPlay: PropTypes.bool,
activeIndex: PropTypes.number,
next: PropTypes.func,
previous: PropTypes.func,
goToIndex: PropTypes.func
};
CCarousel.defaultProps = {
controls: true,
indicators: true,
autoPlay: true,
};
export default CCarousel;