UNPKG

wix-style-react

Version:
327 lines (281 loc) • 11.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _Carousel = require('./Carousel.scss'); var _Carousel2 = _interopRequireDefault(_Carousel); var _ChevronLeftLarge = require('../new-icons/ChevronLeftLarge'); var _ChevronLeftLarge2 = _interopRequireDefault(_ChevronLeftLarge); var _ChevronRightLarge = require('../new-icons/ChevronRightLarge'); var _ChevronRightLarge2 = _interopRequireDefault(_ChevronRightLarge); var _IconButton = require('../IconButton/IconButton'); var _IconButton2 = _interopRequireDefault(_IconButton); var _Pagination = require('./Pagination'); var _Pagination2 = _interopRequireDefault(_Pagination); var _Loader = require('../Loader'); var _Loader2 = _interopRequireDefault(_Loader); var _Proportion = require('../Proportion'); var _Proportion2 = _interopRequireDefault(_Proportion); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } // 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 = _react2.default.createElement( 'div', { className: _Carousel2.default.buttonContainer }, _react2.default.createElement( _IconButton2.default, { dataHook: 'prev-button', priority: 'secondary', onClick: function onClick() { return _this3._prev(); } }, _react2.default.createElement(_ChevronLeftLarge2.default, null) ) ); var nextButton = _react2.default.createElement( 'div', { className: _Carousel2.default.buttonContainer }, _react2.default.createElement( _IconButton2.default, { dataHook: 'next-button', priority: 'secondary', onClick: function onClick() { return _this3._next(); } }, _react2.default.createElement(_ChevronRightLarge2.default, null) ) ); return _react2.default.createElement( 'div', { className: _Carousel2.default.carousel, 'data-hook': this.props.dataHook, 'data-ready': !this._isLoading() }, _react2.default.createElement( 'div', { className: _Carousel2.default.imagesAndButtonsContainer }, _react2.default.createElement( 'div', { className: _Carousel2.default.gallery }, prevButton, _react2.default.createElement( _Proportion2.default, { aspectRatio: _Proportion2.default.PREDEFINED_RATIOS.landscape, className: _Carousel2.default.imagesContainerLayout }, _react2.default.createElement( 'div', { 'data-hook': 'images-container', className: _Carousel2.default.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 _react2.default.createElement( 'div', { key: currentIndex, className: (0, _classnames2.default)(_Carousel2.default.imageContainer, (_classNames = {}, _defineProperty(_classNames, _Carousel2.default.active, currentIndex === _this3.state.activeIndex), _defineProperty(_classNames, _Carousel2.default.prev, currentIndex === _this3._getPrevIndex()), _defineProperty(_classNames, _Carousel2.default.next, currentIndex === _this3._getNextIndex()), _classNames)) }, _react2.default.createElement('img', { className: _Carousel2.default.image, 'data-hook': 'carousel-img', src: image.src, onLoad: function onLoad() { return _this3._onImageLoad(); } }) ); }) ), this._isLoading() ? _react2.default.createElement( 'div', { className: _Carousel2.default.loader }, _react2.default.createElement(_Loader2.default, { dataHook: 'loader', size: 'small' }) ) : null ), nextButton ), _react2.default.createElement(_Pagination2.default, { className: _Carousel2.default.paginationLayout, totalPages: this.props.images.length, currentPage: this._getActivePage() }) ) ); } }]); return Carousel; }(_react2.default.Component); //update images on imageUpdate Carousel.propTypes = { dataHook: _propTypes2.default.string, /** Array of strings where each string is a src of an image (in \<img src="your_src" /\>) */ images: _propTypes2.default.array.isRequired, /** Images loop endlessly */ infinite: _propTypes2.default.bool, /** Auto-playing of images */ autoplay: _propTypes2.default.bool }; Carousel.defaultProps = { infinite: true, images: [] }; Carousel.displayName = 'Carousel'; exports.default = Carousel;