123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
- import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
- import classNames from 'classnames';
- import styles from 'dom-helpers/style';
- import transition from 'dom-helpers/transition';
- import React, { cloneElement } from 'react';
- import { uncontrollable } from 'uncontrollable';
- import CarouselCaption from './CarouselCaption';
- import CarouselItem from './CarouselItem';
- import SafeAnchor from './SafeAnchor';
- import { map, forEach } from './utils/ElementChildren';
- import triggerBrowserReflow from './utils/triggerBrowserReflow';
- import { createBootstrapComponent } from './ThemeProvider';
-
- var countChildren = function countChildren(c) {
- return React.Children.toArray(c).filter(React.isValidElement).length;
- };
-
- var SWIPE_THRESHOLD = 40; // TODO: `slide` should be `animate`.
-
- var defaultProps = {
- slide: true,
- fade: false,
- interval: 5000,
- keyboard: true,
- pauseOnHover: true,
- wrap: true,
- indicators: true,
- controls: true,
- activeIndex: 0,
- prevIcon: React.createElement("span", {
- "aria-hidden": "true",
- className: "carousel-control-prev-icon"
- }),
- prevLabel: 'Previous',
- nextIcon: React.createElement("span", {
- "aria-hidden": "true",
- className: "carousel-control-next-icon"
- }),
- nextLabel: 'Next',
- touch: true
- };
-
- var Carousel =
- /*#__PURE__*/
- function (_React$Component) {
- _inheritsLoose(Carousel, _React$Component);
-
- function Carousel() {
- var _this;
-
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
-
- _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
- _this.state = {
- prevClasses: '',
- currentClasses: 'active',
- touchStartX: 0
- };
- _this.isUnmounted = false;
- _this.carousel = React.createRef();
-
- _this.handleTouchStart = function (e) {
- _this.setState({
- touchStartX: e.changedTouches[0].screenX
- });
- };
-
- _this.handleTouchEnd = function (e) {
- // If the swipe is under the threshold, don't do anything.
- if (Math.abs(e.changedTouches[0].screenX - _this.state.touchStartX) < SWIPE_THRESHOLD) return;
-
- if (e.changedTouches[0].screenX < _this.state.touchStartX) {
- // Swiping left to navigate to next item.
- _this.handleNext(e);
- } else {
- // Swiping right to navigate to previous item.
- _this.handlePrev(e);
- }
- };
-
- _this.handleSlideEnd = function () {
- var pendingIndex = _this._pendingIndex;
- _this._isSliding = false;
- _this._pendingIndex = null;
- if (pendingIndex != null) _this.to(pendingIndex);else _this.cycle();
- };
-
- _this.handleMouseOut = function () {
- _this.cycle();
- };
-
- _this.handleMouseOver = function () {
- if (_this.props.pauseOnHover) _this.pause();
- };
-
- _this.handleKeyDown = function (event) {
- if (/input|textarea/i.test(event.target.tagName)) return;
-
- switch (event.key) {
- case 'ArrowLeft':
- event.preventDefault();
-
- _this.handlePrev(event);
-
- break;
-
- case 'ArrowRight':
- event.preventDefault();
-
- _this.handleNext(event);
-
- break;
-
- default:
- break;
- }
- };
-
- _this.handleNextWhenVisible = function () {
- if (!_this.isUnmounted && !document.hidden && styles(_this.carousel.current, 'visibility') !== 'hidden') {
- _this.handleNext();
- }
- };
-
- _this.handleNext = function (e) {
- if (_this._isSliding) return;
- var _this$props = _this.props,
- wrap = _this$props.wrap,
- activeIndex = _this$props.activeIndex;
- var index = activeIndex + 1;
- var count = countChildren(_this.props.children);
-
- if (index > count - 1) {
- if (!wrap) return;
- index = 0;
- }
-
- _this.select(index, e, 'next');
- };
-
- _this.handlePrev = function (e) {
- if (_this._isSliding) return;
- var _this$props2 = _this.props,
- wrap = _this$props2.wrap,
- activeIndex = _this$props2.activeIndex;
- var index = activeIndex - 1;
-
- if (index < 0) {
- if (!wrap) return;
- index = countChildren(_this.props.children) - 1;
- }
-
- _this.select(index, e, 'prev');
- };
-
- return _this;
- }
-
- var _proto = Carousel.prototype;
-
- _proto.componentDidMount = function componentDidMount() {
- this.cycle();
- };
-
- Carousel.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, _ref) {
- var previousActiveIndex = _ref.activeIndex;
-
- if (nextProps.activeIndex !== previousActiveIndex) {
- var lastPossibleIndex = countChildren(nextProps.children) - 1;
- var nextIndex = Math.max(0, Math.min(nextProps.activeIndex, lastPossibleIndex));
- var direction;
-
- if (nextIndex === 0 && previousActiveIndex >= lastPossibleIndex || previousActiveIndex <= nextIndex) {
- direction = 'next';
- } else {
- direction = 'prev';
- }
-
- return {
- direction: direction,
- previousActiveIndex: previousActiveIndex,
- activeIndex: nextIndex
- };
- }
-
- return null;
- };
-
- _proto.componentDidUpdate = function componentDidUpdate(_, prevState) {
- var _this2 = this;
-
- var _this$props3 = this.props,
- bsPrefix = _this$props3.bsPrefix,
- slide = _this$props3.slide,
- onSlideEnd = _this$props3.onSlideEnd;
- if (!slide || this.state.activeIndex === prevState.activeIndex || this._isSliding) return;
- var _this$state = this.state,
- activeIndex = _this$state.activeIndex,
- direction = _this$state.direction;
- var orderClassName, directionalClassName;
-
- if (direction === 'next') {
- orderClassName = bsPrefix + "-item-next";
- directionalClassName = bsPrefix + "-item-left";
- } else if (direction === 'prev') {
- orderClassName = bsPrefix + "-item-prev";
- directionalClassName = bsPrefix + "-item-right";
- }
-
- this._isSliding = true;
- this.pause(); // eslint-disable-next-line react/no-did-update-set-state
-
- this.safeSetState({
- prevClasses: 'active',
- currentClasses: orderClassName
- }, function () {
- var items = _this2.carousel.current.children;
- var nextElement = items[activeIndex];
- triggerBrowserReflow(nextElement);
-
- _this2.safeSetState({
- prevClasses: classNames('active', directionalClassName),
- currentClasses: classNames(orderClassName, directionalClassName)
- }, function () {
- return transition.end(nextElement, function () {
- _this2.safeSetState({
- prevClasses: '',
- currentClasses: 'active'
- }, _this2.handleSlideEnd);
-
- if (onSlideEnd) {
- onSlideEnd();
- }
- });
- });
- });
- };
-
- _proto.componentWillUnmount = function componentWillUnmount() {
- clearTimeout(this.timeout);
- this.isUnmounted = true;
- };
-
- _proto.safeSetState = function safeSetState(state, cb) {
- var _this3 = this;
-
- if (this.isUnmounted) return;
- this.setState(state, function () {
- return !_this3.isUnmounted && cb();
- });
- } // This might be a public API.
- ;
-
- _proto.pause = function pause() {
- this._isPaused = true;
- clearInterval(this._interval);
- this._interval = null;
- };
-
- _proto.cycle = function cycle() {
- this._isPaused = false;
- clearInterval(this._interval);
- this._interval = null;
-
- if (this.props.interval && !this._isPaused) {
- this._interval = setInterval(document.visibilityState ? this.handleNextWhenVisible : this.handleNext, this.props.interval);
- }
- };
-
- _proto.to = function to(index, event) {
- var children = this.props.children;
-
- if (index < 0 || index > countChildren(children) - 1) {
- return;
- }
-
- if (this._isSliding) {
- this._pendingIndex = index;
- return;
- }
-
- this.select(index, event);
- };
-
- _proto.select = function select(index, event, direction) {
- var _this4 = this;
-
- clearTimeout(this.selectThrottle);
- if (event && event.persist) event.persist(); // The timeout throttles fast clicks, in order to give any pending state
- // a chance to update and propagate back through props
-
- this.selectThrottle = setTimeout(function () {
- clearTimeout(_this4.timeout);
- var _this4$props = _this4.props,
- activeIndex = _this4$props.activeIndex,
- onSelect = _this4$props.onSelect;
- if (index === activeIndex || _this4._isSliding || _this4.isUnmounted) return;
- onSelect(index, direction || (index < activeIndex ? 'prev' : 'next'), event);
- }, 50);
- };
-
- _proto.renderControls = function renderControls(properties) {
- var bsPrefix = this.props.bsPrefix;
- var wrap = properties.wrap,
- children = properties.children,
- activeIndex = properties.activeIndex,
- prevIcon = properties.prevIcon,
- nextIcon = properties.nextIcon,
- prevLabel = properties.prevLabel,
- nextLabel = properties.nextLabel;
- var count = countChildren(children);
- return [(wrap || activeIndex !== 0) && React.createElement(SafeAnchor, {
- key: "prev",
- className: bsPrefix + "-control-prev",
- onClick: this.handlePrev
- }, prevIcon, prevLabel && React.createElement("span", {
- className: "sr-only"
- }, prevLabel)), (wrap || activeIndex !== count - 1) && React.createElement(SafeAnchor, {
- key: "next",
- className: bsPrefix + "-control-next",
- onClick: this.handleNext
- }, nextIcon, nextLabel && React.createElement("span", {
- className: "sr-only"
- }, nextLabel))];
- };
-
- _proto.renderIndicators = function renderIndicators(children, activeIndex) {
- var _this5 = this;
-
- var bsPrefix = this.props.bsPrefix;
- var indicators = [];
- forEach(children, function (child, index) {
- indicators.push(React.createElement("li", {
- key: index,
- className: index === activeIndex ? 'active' : null,
- onClick: function onClick(e) {
- return _this5.to(index, e);
- }
- }), // Force whitespace between indicator elements. Bootstrap requires
- // this for correct spacing of elements.
- ' ');
- });
- return React.createElement("ol", {
- className: bsPrefix + "-indicators"
- }, indicators);
- };
-
- _proto.render = function render() {
- var _this$props4 = this.props,
- _this$props4$as = _this$props4.as,
- Component = _this$props4$as === void 0 ? 'div' : _this$props4$as,
- bsPrefix = _this$props4.bsPrefix,
- slide = _this$props4.slide,
- fade = _this$props4.fade,
- indicators = _this$props4.indicators,
- controls = _this$props4.controls,
- wrap = _this$props4.wrap,
- touch = _this$props4.touch,
- prevIcon = _this$props4.prevIcon,
- prevLabel = _this$props4.prevLabel,
- nextIcon = _this$props4.nextIcon,
- nextLabel = _this$props4.nextLabel,
- className = _this$props4.className,
- children = _this$props4.children,
- keyboard = _this$props4.keyboard,
- _5 = _this$props4.activeIndex,
- _4 = _this$props4.pauseOnHover,
- _3 = _this$props4.interval,
- _2 = _this$props4.onSelect,
- _1 = _this$props4.onSlideEnd,
- props = _objectWithoutPropertiesLoose(_this$props4, ["as", "bsPrefix", "slide", "fade", "indicators", "controls", "wrap", "touch", "prevIcon", "prevLabel", "nextIcon", "nextLabel", "className", "children", "keyboard", "activeIndex", "pauseOnHover", "interval", "onSelect", "onSlideEnd"]);
-
- var _this$state2 = this.state,
- activeIndex = _this$state2.activeIndex,
- previousActiveIndex = _this$state2.previousActiveIndex,
- prevClasses = _this$state2.prevClasses,
- currentClasses = _this$state2.currentClasses;
- return (// eslint-disable-next-line jsx-a11y/no-static-element-interactions
- React.createElement(Component, _extends({
- onTouchStart: touch ? this.handleTouchStart : undefined,
- onTouchEnd: touch ? this.handleTouchEnd : undefined
- }, props, {
- className: classNames(className, bsPrefix, slide && 'slide', fade && bsPrefix + "-fade"),
- onKeyDown: keyboard ? this.handleKeyDown : undefined,
- onMouseOver: this.handleMouseOver,
- onMouseOut: this.handleMouseOut
- }), indicators && this.renderIndicators(children, activeIndex), React.createElement("div", {
- className: bsPrefix + "-inner",
- ref: this.carousel
- }, map(children, function (child, index) {
- var current = index === activeIndex;
- var previous = index === previousActiveIndex;
- return cloneElement(child, {
- className: classNames(child.props.className, bsPrefix + "-item", current && currentClasses, previous && prevClasses)
- });
- })), controls && this.renderControls({
- wrap: wrap,
- children: children,
- activeIndex: activeIndex,
- prevIcon: prevIcon,
- prevLabel: prevLabel,
- nextIcon: nextIcon,
- nextLabel: nextLabel
- }))
- );
- };
-
- return Carousel;
- }(React.Component);
-
- Carousel.defaultProps = defaultProps;
- var DecoratedCarousel = createBootstrapComponent(uncontrollable(Carousel, {
- activeIndex: 'onSelect'
- }), 'carousel');
- DecoratedCarousel.Caption = CarouselCaption;
- DecoratedCarousel.Item = CarouselItem;
- export default DecoratedCarousel;
|