ant-design-vue/components/carousel/index.jsx

174 lines
4.5 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import debounce from 'lodash/debounce';
import { initDefaultProps, getComponentFromProp, filterEmpty } from '../_util/props-util';
2019-03-14 21:21:58 +08:00
import { ConfigConsumerProps } from '../config-provider';
import Base from '../base';
2018-07-20 16:13:21 +08:00
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
if (typeof window !== 'undefined') {
2019-01-12 11:33:27 +08:00
const matchMediaPolyfill = mediaQuery => {
2018-07-20 16:13:21 +08:00
return {
media: mediaQuery,
matches: false,
2019-01-12 11:33:27 +08:00
addListener() {},
removeListener() {},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
2018-07-20 16:13:21 +08:00
}
// Use require over import (will be lifted up)
// make sure matchMedia polyfill run before require('vc-slick')
// Fix https://github.com/ant-design/ant-design/issues/6560
// Fix https://github.com/ant-design/ant-design/issues/3308
2019-01-12 11:33:27 +08:00
const SlickCarousel = require('../vc-slick/src').default;
2018-07-20 16:13:21 +08:00
2019-01-12 11:33:27 +08:00
export const CarouselEffect = PropTypes.oneOf(['scrollx', 'fade']);
2018-07-20 16:13:21 +08:00
// Carousel
export const CarouselProps = {
effect: CarouselEffect,
dots: PropTypes.bool,
vertical: PropTypes.bool,
autoplay: PropTypes.bool,
easing: PropTypes.string,
beforeChange: PropTypes.func,
afterChange: PropTypes.func,
// style: PropTypes.React.CSSProperties,
prefixCls: PropTypes.string,
accessibility: PropTypes.bool,
nextArrow: PropTypes.any,
prevArrow: PropTypes.any,
pauseOnHover: PropTypes.bool,
// className: PropTypes.string,
adaptiveHeight: PropTypes.bool,
arrows: PropTypes.bool,
autoplaySpeed: PropTypes.number,
centerMode: PropTypes.bool,
centerPadding: PropTypes.string,
cssEase: PropTypes.string,
dotsClass: PropTypes.string,
draggable: PropTypes.bool,
fade: PropTypes.bool,
focusOnSelect: PropTypes.bool,
infinite: PropTypes.bool,
initialSlide: PropTypes.number,
lazyLoad: PropTypes.bool,
rtl: PropTypes.bool,
slide: PropTypes.string,
slidesToShow: PropTypes.number,
slidesToScroll: PropTypes.number,
speed: PropTypes.number,
swipe: PropTypes.bool,
swipeToSlide: PropTypes.bool,
touchMove: PropTypes.bool,
touchThreshold: PropTypes.number,
variableWidth: PropTypes.bool,
useCSS: PropTypes.bool,
slickGoTo: PropTypes.number,
2019-10-09 18:39:09 +08:00
responsive: PropTypes.array,
2019-01-12 11:33:27 +08:00
};
2018-07-20 16:13:21 +08:00
const Carousel = {
2018-07-20 16:13:21 +08:00
name: 'ACarousel',
props: initDefaultProps(CarouselProps, {
dots: true,
arrows: false,
draggable: false,
}),
2019-03-14 21:21:58 +08:00
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2019-03-14 21:21:58 +08:00
},
2018-07-20 16:13:21 +08:00
2019-01-12 11:33:27 +08:00
beforeMount() {
2018-07-20 16:13:21 +08:00
this.onWindowResized = debounce(this.onWindowResized, 500, {
leading: false,
2019-01-12 11:33:27 +08:00
});
2018-07-20 16:13:21 +08:00
},
2019-01-12 11:33:27 +08:00
mounted() {
const { autoplay } = this;
2018-07-20 16:13:21 +08:00
if (autoplay) {
2019-01-12 11:33:27 +08:00
window.addEventListener('resize', this.onWindowResized);
2018-07-20 16:13:21 +08:00
}
// https://github.com/ant-design/ant-design/issues/7191
2019-01-12 11:33:27 +08:00
this.innerSlider = this.$refs.slick && this.$refs.slick.innerSlider;
2018-07-20 16:13:21 +08:00
},
2019-01-12 11:33:27 +08:00
beforeDestroy() {
const { autoplay } = this;
2018-07-20 16:13:21 +08:00
if (autoplay) {
2019-01-12 11:33:27 +08:00
window.removeEventListener('resize', this.onWindowResized);
this.onWindowResized.cancel();
2018-07-20 16:13:21 +08:00
}
},
methods: {
2019-01-12 11:33:27 +08:00
onWindowResized() {
2018-07-20 16:13:21 +08:00
// Fix https://github.com/ant-design/ant-design/issues/2550
2019-01-12 11:33:27 +08:00
const { autoplay } = this;
if (
autoplay &&
this.$refs.slick &&
this.$refs.slick.innerSlider &&
this.$refs.slick.innerSlider.autoPlay
) {
this.$refs.slick.innerSlider.autoPlay();
2018-07-20 16:13:21 +08:00
}
},
2019-01-12 11:33:27 +08:00
next() {
this.$refs.slick.slickNext();
2018-07-20 16:13:21 +08:00
},
2019-01-12 11:33:27 +08:00
prev() {
this.$refs.slick.slickPrev();
2018-07-20 16:13:21 +08:00
},
2019-01-12 11:33:27 +08:00
goTo(slide, dontAnimate = false) {
this.$refs.slick.slickGoTo(slide, dontAnimate);
2018-07-20 16:13:21 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
2018-07-20 16:13:21 +08:00
const props = {
...this.$props,
2019-01-12 11:33:27 +08:00
};
const { $slots, $listeners } = this;
2018-07-20 16:13:21 +08:00
if (props.effect === 'fade') {
2019-01-12 11:33:27 +08:00
props.fade = true;
2018-07-20 16:13:21 +08:00
}
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-03-14 21:21:58 +08:00
let className = getPrefixCls('carousel', props.prefixCls);
2018-07-20 16:13:21 +08:00
if (props.vertical) {
2019-01-12 11:33:27 +08:00
className = `${className} ${className}-vertical`;
2018-07-20 16:13:21 +08:00
}
const SlickCarouselProps = {
props: {
...props,
nextArrow: getComponentFromProp(this, 'nextArrow'),
prevArrow: getComponentFromProp(this, 'prevArrow'),
},
on: $listeners,
scopedSlots: this.$scopedSlots,
2019-01-12 11:33:27 +08:00
};
2018-07-20 16:13:21 +08:00
return (
<div class={className}>
2019-01-12 11:33:27 +08:00
<SlickCarousel ref="slick" {...SlickCarouselProps}>
2018-07-20 16:13:21 +08:00
{filterEmpty($slots.default)}
</SlickCarousel>
</div>
2019-01-12 11:33:27 +08:00
);
2018-07-20 16:13:21 +08:00
},
2019-01-12 11:33:27 +08:00
};
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
Carousel.install = function(Vue) {
Vue.use(Base);
2019-01-12 11:33:27 +08:00
Vue.component(Carousel.name, Carousel);
};
2019-01-12 11:33:27 +08:00
export default Carousel;