UNPKG

4.56 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
4import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
5import React, { Component } from 'react';
6import PropTypes from 'prop-types';
7import Carousel from './Carousel';
8import CarouselItem from './CarouselItem';
9import CarouselControl from './CarouselControl';
10import CarouselIndicators from './CarouselIndicators';
11import CarouselCaption from './CarouselCaption';
12var propTypes = {
13 items: PropTypes.array.isRequired,
14 indicators: PropTypes.bool,
15 controls: PropTypes.bool,
16 autoPlay: PropTypes.bool,
17 defaultActiveIndex: PropTypes.number,
18 activeIndex: PropTypes.number,
19 next: PropTypes.func,
20 previous: PropTypes.func,
21 goToIndex: PropTypes.func
22};
23
24var UncontrolledCarousel = /*#__PURE__*/function (_Component) {
25 _inheritsLoose(UncontrolledCarousel, _Component);
26
27 function UncontrolledCarousel(props) {
28 var _this;
29
30 _this = _Component.call(this, props) || this;
31 _this.animating = false;
32 _this.state = {
33 activeIndex: props.defaultActiveIndex || 0
34 };
35 _this.next = _this.next.bind(_assertThisInitialized(_this));
36 _this.previous = _this.previous.bind(_assertThisInitialized(_this));
37 _this.goToIndex = _this.goToIndex.bind(_assertThisInitialized(_this));
38 _this.onExiting = _this.onExiting.bind(_assertThisInitialized(_this));
39 _this.onExited = _this.onExited.bind(_assertThisInitialized(_this));
40 return _this;
41 }
42
43 var _proto = UncontrolledCarousel.prototype;
44
45 _proto.onExiting = function onExiting() {
46 this.animating = true;
47 };
48
49 _proto.onExited = function onExited() {
50 this.animating = false;
51 };
52
53 _proto.next = function next() {
54 if (this.animating) return;
55 var nextIndex = this.state.activeIndex === this.props.items.length - 1 ? 0 : this.state.activeIndex + 1;
56 this.setState({
57 activeIndex: nextIndex
58 });
59 };
60
61 _proto.previous = function previous() {
62 if (this.animating) return;
63 var nextIndex = this.state.activeIndex === 0 ? this.props.items.length - 1 : this.state.activeIndex - 1;
64 this.setState({
65 activeIndex: nextIndex
66 });
67 };
68
69 _proto.goToIndex = function goToIndex(newIndex) {
70 if (this.animating) return;
71 this.setState({
72 activeIndex: newIndex
73 });
74 };
75
76 _proto.render = function render() {
77 var _this2 = this;
78
79 var _this$props = this.props,
80 defaultActiveIndex = _this$props.defaultActiveIndex,
81 autoPlay = _this$props.autoPlay,
82 indicators = _this$props.indicators,
83 controls = _this$props.controls,
84 items = _this$props.items,
85 goToIndex = _this$props.goToIndex,
86 props = _objectWithoutPropertiesLoose(_this$props, ["defaultActiveIndex", "autoPlay", "indicators", "controls", "items", "goToIndex"]);
87
88 var activeIndex = this.state.activeIndex;
89 var slides = items.map(function (item) {
90 var key = item.key || item.src;
91 return /*#__PURE__*/React.createElement(CarouselItem, {
92 onExiting: _this2.onExiting,
93 onExited: _this2.onExited,
94 key: key
95 }, /*#__PURE__*/React.createElement("img", {
96 className: "d-block w-100",
97 src: item.src,
98 alt: item.altText
99 }), /*#__PURE__*/React.createElement(CarouselCaption, {
100 captionText: item.caption,
101 captionHeader: item.header || item.caption
102 }));
103 });
104 return /*#__PURE__*/React.createElement(Carousel, _extends({
105 activeIndex: activeIndex,
106 next: this.next,
107 previous: this.previous,
108 ride: autoPlay ? 'carousel' : undefined
109 }, props), indicators && /*#__PURE__*/React.createElement(CarouselIndicators, {
110 items: items,
111 activeIndex: props.activeIndex || activeIndex,
112 onClickHandler: goToIndex || this.goToIndex
113 }), slides, controls && /*#__PURE__*/React.createElement(CarouselControl, {
114 direction: "prev",
115 directionText: "Previous",
116 onClickHandler: props.previous || this.previous
117 }), controls && /*#__PURE__*/React.createElement(CarouselControl, {
118 direction: "next",
119 directionText: "Next",
120 onClickHandler: props.next || this.next
121 }));
122 };
123
124 return UncontrolledCarousel;
125}(Component);
126
127UncontrolledCarousel.propTypes = propTypes;
128UncontrolledCarousel.defaultProps = {
129 controls: true,
130 indicators: true,
131 autoPlay: true
132};
133export default UncontrolledCarousel;
\No newline at end of file