forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncontrolledCarousel.js
More file actions
111 lines (99 loc) · 2.95 KB
/
Copy pathUncontrolledCarousel.js
File metadata and controls
111 lines (99 loc) · 2.95 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Carousel from './Carousel';
import CarouselItem from './CarouselItem';
import CarouselControl from './CarouselControl';
import CarouselIndicators from './CarouselIndicators';
import CarouselCaption from './CarouselCaption';
const propTypes = {
items: PropTypes.array.isRequired,
indicators: PropTypes.bool,
controls: PropTypes.bool,
autoPlay: PropTypes.bool,
activeIndex: PropTypes.number,
next: PropTypes.func,
previous: PropTypes.func,
goToIndex: PropTypes.func,
};
class UncontrolledCarousel extends Component {
constructor(props) {
super(props);
this.animating = false;
this.state = { activeIndex: 0 };
this.next = this.next.bind(this);
this.previous = this.previous.bind(this);
this.goToIndex = this.goToIndex.bind(this);
this.onExiting = this.onExiting.bind(this);
this.onExited = this.onExited.bind(this);
}
onExiting() {
this.animating = true;
}
onExited() {
this.animating = false;
}
next() {
if (this.animating) return;
const nextIndex = this.state.activeIndex === this.props.items.length - 1 ? 0 : this.state.activeIndex + 1;
this.setState({ activeIndex: nextIndex });
}
previous() {
if (this.animating) return;
const nextIndex = this.state.activeIndex === 0 ? this.props.items.length - 1 : this.state.activeIndex - 1;
this.setState({ activeIndex: nextIndex });
}
goToIndex(newIndex) {
if (this.animating) return;
this.setState({ activeIndex: newIndex });
}
render() {
const { autoPlay, indicators, controls, items, goToIndex, ...props } = this.props;
const { activeIndex } = this.state;
const slides = items.map((item) => {
return (
<CarouselItem
onExiting={this.onExiting}
onExited={this.onExited}
key={item.src}
src={item.src}
altText={item.altText}
>
<CarouselCaption captionText={item.caption} captionHeader={item.caption} />
</CarouselItem>
);
});
return (
<Carousel
activeIndex={activeIndex}
next={this.next}
previous={this.previous}
ride={autoPlay && 'carousel'}
{...props}
>
{indicators && <CarouselIndicators
items={items}
activeIndex={props.activeIndex || activeIndex}
onClickHandler={goToIndex || this.goToIndex}
/>}
{slides}
{controls && <CarouselControl
direction="prev"
directionText="Previous"
onClickHandler={props.previous || this.previous}
/>}
{controls && <CarouselControl
direction="next"
directionText="Next"
onClickHandler={props.next || this.next}
/>}
</Carousel>
);
}
}
UncontrolledCarousel.propTypes = propTypes;
UncontrolledCarousel.defaultProps = {
controls: true,
indicators: true,
autoPlay: true,
};
export default UncontrolledCarousel;