UNPKG

wix-style-react

Version:
290 lines (266 loc) • 9.79 kB
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import classNames from 'classnames'; import React from 'react'; import PropTypes from 'prop-types'; import styles from './Carousel.scss'; import ChevronLeftLarge from '../new-icons/ChevronLeftLarge'; import ChevronRightLarge from '../new-icons/ChevronRightLarge'; import IconButton from '../IconButton/IconButton'; import Pagination from './Pagination'; import Loader from '../Loader'; import Proportion from '../Proportion'; // because lodash throttle is not compatible with jest timeout mocks function throttle(callback, time) { var pause = void 0; return function () { if (!pause) { pause = true; setTimeout(function () { pause = false; }, time); callback.apply(undefined, arguments); } }; } var Carousel = function (_React$Component) { _inherits(Carousel, _React$Component); function Carousel(props) { _classCallCheck(this, Carousel); var _this = _possibleConstructorReturn(this, (Carousel.__proto__ || Object.getPrototypeOf(Carousel)).call(this, props)); _this.state = { activeIndex: 0, loadedImageCount: 0 }; _this._slide = throttle(_this._slide.bind(_this), 2000); return _this; } _createClass(Carousel, [{ key: 'componentDidMount', value: function componentDidMount() { if (this.props.autoplay) { this._autoplay(); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.props.autoplay) { this._haltAutoplay(); } } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { if (prevProps.autoplay && !this.props.autoplay) { this._haltAutoplay(); } if (!prevProps.autoplay && this.props.autoplay) { this._autoplay(); } } }, { key: '_autoplay', value: function _autoplay() { var _this2 = this; var intervalToken = setInterval(function () { return _this2._slide(_this2._getNextIndex()); }, 4000); this._haltAutoplay = function () { return clearInterval(intervalToken); }; } }, { key: '_stopSlideshow', value: function _stopSlideshow() { this.props.autoplay && this._haltAutoplay(); } }, { key: '_continueSlideshow', value: function _continueSlideshow() { this.props.autoplay && this._autoplay(); } }, { key: '_isLastImage', value: function _isLastImage() { return this.state.activeIndex === this.props.images.length - 1; } }, { key: '_slide', value: function _slide(index) { this.setState({ activeIndex: index }); } }, { key: '_prev', value: function _prev() { if (this.state.activeIndex === 0 && !this.props.infinite) { return; } this._slide(this._getPrevIndex()); } }, { key: '_next', value: function _next() { if (this._isLastImage() && !this.props.infinite) { return; } this._slide(this._getNextIndex()); } }, { key: '_getNextIndex', value: function _getNextIndex() { return this.state.activeIndex === this.props.images.length - 1 ? 0 : this.state.activeIndex + 1; } }, { key: '_getPrevIndex', value: function _getPrevIndex() { return this.state.activeIndex === 0 ? this.props.images.length - 1 : this.state.activeIndex - 1; } }, { key: '_onImageLoad', value: function _onImageLoad() { this.setState(function (state) { var loadedImageCount = state.loadedImageCount + 1; return { loadedImageCount: loadedImageCount }; }); } }, { key: '_isLoading', value: function _isLoading() { return this.state.loadedImageCount < this.props.images.length; } }, { key: '_getActivePage', value: function _getActivePage() { var activeIndex = this.state.activeIndex; var originalImageCount = this.props.images.length; return activeIndex % originalImageCount; } }, { key: 'render', value: function render() { var _this3 = this; var prevButton = React.createElement( 'div', { className: styles.buttonContainer }, React.createElement( IconButton, { dataHook: 'prev-button', priority: 'secondary', onClick: function onClick() { return _this3._prev(); } }, React.createElement(ChevronLeftLarge, null) ) ); var nextButton = React.createElement( 'div', { className: styles.buttonContainer }, React.createElement( IconButton, { dataHook: 'next-button', priority: 'secondary', onClick: function onClick() { return _this3._next(); } }, React.createElement(ChevronRightLarge, null) ) ); return React.createElement( 'div', { className: styles.carousel, 'data-hook': this.props.dataHook, 'data-ready': !this._isLoading() }, React.createElement( 'div', { className: styles.imagesAndButtonsContainer }, React.createElement( 'div', { className: styles.gallery }, prevButton, React.createElement( Proportion, { aspectRatio: Proportion.PREDEFINED_RATIOS.landscape, className: styles.imagesContainerLayout }, React.createElement( 'div', { 'data-hook': 'images-container', className: styles.imagesContainer, 'data-is-loading': this._isLoading(), onMouseOver: function onMouseOver() { return _this3._stopSlideshow(); }, onMouseOut: function onMouseOut() { return _this3._continueSlideshow(); } }, this.props.images.map(function (image, currentIndex) { var _classNames; return React.createElement( 'div', { key: currentIndex, className: classNames(styles.imageContainer, (_classNames = {}, _defineProperty(_classNames, styles.active, currentIndex === _this3.state.activeIndex), _defineProperty(_classNames, styles.prev, currentIndex === _this3._getPrevIndex()), _defineProperty(_classNames, styles.next, currentIndex === _this3._getNextIndex()), _classNames)) }, React.createElement('img', { className: styles.image, 'data-hook': 'carousel-img', src: image.src, onLoad: function onLoad() { return _this3._onImageLoad(); } }) ); }) ), this._isLoading() ? React.createElement( 'div', { className: styles.loader }, React.createElement(Loader, { dataHook: 'loader', size: 'small' }) ) : null ), nextButton ), React.createElement(Pagination, { className: styles.paginationLayout, totalPages: this.props.images.length, currentPage: this._getActivePage() }) ) ); } }]); return Carousel; }(React.Component); //update images on imageUpdate Carousel.propTypes = { dataHook: PropTypes.string, /** Array of strings where each string is a src of an image (in \<img src="your_src" /\>) */ images: PropTypes.array.isRequired, /** Images loop endlessly */ infinite: PropTypes.bool, /** Auto-playing of images */ autoplay: PropTypes.bool }; Carousel.defaultProps = { infinite: true, images: [] }; Carousel.displayName = 'Carousel'; export default Carousel;