UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

239 lines (204 loc) 6.75 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import React, { Component } from 'react'; import debounce from 'lodash/debounce'; import classNames from 'classnames'; import { matchMediaPolifill } from '../_util/mediaQueryListPolyfill'; import ConfigContext from '../config-provider/ConfigContext'; // matchMedia polyfill for // https://github.com/WickyNilliams/enquire.js/issues/82 if (typeof window !== 'undefined') { // const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => { // return { // media: mediaQuery, // matches: false, // addListener() { // }, // removeListener() { // }, // }; // }; window.matchMedia = window.matchMedia || matchMediaPolifill; } // Use require over import (will be lifted up) // make sure matchMedia polyfill run before require('react-slick') // eslint-disable-next-line @typescript-eslint/no-var-requires var SlickCarousel = require('react-slick')["default"]; export var CarouselTheme; (function (CarouselTheme) { CarouselTheme["DARK"] = "dark"; CarouselTheme["LIGHT"] = "light"; })(CarouselTheme || (CarouselTheme = {})); var Carousel = /*#__PURE__*/function (_Component) { _inherits(Carousel, _Component); var _super = _createSuper(Carousel); function Carousel() { var _this; _classCallCheck(this, Carousel); _this = _super.apply(this, arguments); _this.onWindowResized = debounce(function () { var autoplay = _this.props.autoplay; if (autoplay && _this.slick && _this.slick.innerSlider && _this.slick.innerSlider.autoPlay) { _this.slick.innerSlider.autoPlay(); } }, 500, { leading: false }); _this.handleArrowMouseChange = function (event) { var pauseOnArrowsHover = _this.props.pauseOnArrowsHover; if (pauseOnArrowsHover) { if (event.type === 'mouseleave') { _this.play(); } else if (event.type === 'mouseenter') { _this.pause(); } } }; _this.saveSlick = function (node) { _this.slick = node; }; return _this; } _createClass(Carousel, [{ key: "componentDidMount", value: function componentDidMount() { var autoplay = this.props.autoplay; if (autoplay) { window.addEventListener('resize', this.onWindowResized); } if (this.wrapper) { this.arrowsRef = this.wrapper.querySelectorAll('.slick-slider .slick-arrow'); this.dotsRef = this.wrapper.querySelectorAll('.slick-slider .slick-dots li'); } this.bindDotsEvent(); this.bindArrowEvent(); this.innerSlider = this.slick && this.slick.innerSlider; } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this2 = this; var autoplay = this.props.autoplay; if (autoplay) { window.removeEventListener('resize', this.onWindowResized); this.onWindowResized.cancel(); } if (this.dotsRef) { this.dotsRef.forEach(function (dot, i) { dot.removeEventListener('mouseenter', function () { return _this2.handleDotsHover(i); }); }); } if (this.arrowsRef) { this.arrowsRef.forEach(function (arrow) { arrow.removeEventListener('mouseenter', _this2.handleArrowMouseChange); arrow.removeEventListener('mouseleave', _this2.handleArrowMouseChange); }); } } }, { key: "bindDotsEvent", value: function bindDotsEvent() { var _this3 = this; if (this.wrapper) { this.dotsRef.forEach(function (dot, i) { dot.addEventListener('mouseenter', function () { return _this3.handleDotsHover(i); }); }); } } }, { key: "bindArrowEvent", value: function bindArrowEvent() { var _this4 = this; var autoplay = this.props.autoplay; if (this.arrowsRef && autoplay) { this.arrowsRef.forEach(function (arrow) { arrow.addEventListener('mouseenter', _this4.handleArrowMouseChange); arrow.addEventListener('mouseleave', _this4.handleArrowMouseChange); }); } } }, { key: "handleDotsHover", value: function handleDotsHover(index) { var dotsActionType = this.props.dotsActionType; if (dotsActionType && dotsActionType.includes('hover')) { this.goTo(index, true); } } }, { key: "play", value: function play() { this.slick.slickPlay(); } }, { key: "pause", value: function pause() { this.slick.slickPause(); } }, { key: "next", value: function next() { this.slick.slickNext(); } }, { key: "prev", value: function prev() { this.slick.slickPrev(); } }, { key: "goTo", value: function goTo(slide, dontAnimate) { this.slick.slickGoTo(slide, dontAnimate); } }, { key: "render", value: function render() { var _classNames, _this5 = this; var getPrefixCls = this.context.getPrefixCls; var dotsClass = this.props.dotsClass; var props = _objectSpread({}, this.props); if (dotsClass) { props.dotsClass = ["slick-dots", dotsClass].join(' '); } if (props.effect === 'fade') { props.fade = true; } var prefixCls = getPrefixCls('carousel', props.prefixCls); var className = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-vertical"), props.vertical), _defineProperty(_classNames, "".concat(prefixCls, "-theme-dark"), props.theme === CarouselTheme.DARK), _classNames)); return /*#__PURE__*/React.createElement("div", { className: className, ref: function ref(wrapper) { _this5.wrapper = wrapper; } }, /*#__PURE__*/React.createElement(SlickCarousel, _extends({ ref: this.saveSlick }, props))); } }], [{ key: "contextType", get: function get() { return ConfigContext; } }]); return Carousel; }(Component); export { Carousel as default }; Carousel.displayName = 'Carousel'; Carousel.defaultProps = { dots: true, arrows: false, draggable: false, theme: CarouselTheme.LIGHT, pauseOnDotsHover: true, pauseOnArrowsHover: true, dotsActionType: ["click"] }; //# sourceMappingURL=index.js.map