2015-11-20 10:57:08 +08:00
|
|
|
// matchMedia polyfill for
|
|
|
|
// https://github.com/WickyNilliams/enquire.js/issues/82
|
2017-02-21 22:10:32 +08:00
|
|
|
import debounce from 'lodash.debounce';
|
|
|
|
|
2015-11-20 10:57:08 +08:00
|
|
|
if (typeof window !== 'undefined') {
|
2016-08-22 17:26:14 +08:00
|
|
|
const matchMediaPolyfill = function matchMediaPolyfill(mediaQuery: string): MediaQueryList {
|
2015-11-20 10:57:08 +08:00
|
|
|
return {
|
2016-08-22 17:26:14 +08:00
|
|
|
media: mediaQuery,
|
2015-11-20 10:57:08 +08:00
|
|
|
matches: false,
|
2016-01-05 14:42:06 +08:00
|
|
|
addListener() {
|
|
|
|
},
|
|
|
|
removeListener() {
|
2015-11-20 10:57:08 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
window.matchMedia = window.matchMedia || matchMediaPolyfill;
|
|
|
|
}
|
|
|
|
|
2016-03-21 09:22:14 +08:00
|
|
|
import SlickCarousel from 'react-slick';
|
2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2015-08-03 14:52:26 +08:00
|
|
|
|
2017-03-23 21:15:49 +08:00
|
|
|
export type CarouselEffect = 'scrollx' | 'fade';
|
2016-07-01 20:52:17 +08:00
|
|
|
// Carousel
|
2016-07-07 20:25:03 +08:00
|
|
|
export interface CarouselProps {
|
2016-07-13 11:14:24 +08:00
|
|
|
effect?: CarouselEffect;
|
2016-08-22 17:26:14 +08:00
|
|
|
dots?: boolean;
|
2016-07-13 11:14:24 +08:00
|
|
|
vertical?: boolean;
|
|
|
|
autoplay?: boolean;
|
|
|
|
easing?: string;
|
|
|
|
beforeChange?: (from: number, to: number) => void;
|
2016-07-13 17:22:23 +08:00
|
|
|
afterChange?: (current: number) => void;
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2017-03-31 14:35:25 +08:00
|
|
|
accessibility?: boolean;
|
|
|
|
nextArrow?: HTMLElement | any;
|
|
|
|
prevArrow?: HTMLElement | any;
|
|
|
|
pauseOnHover?: boolean;
|
|
|
|
className?: string;
|
|
|
|
adaptiveHeight?: boolean;
|
|
|
|
arrows?: boolean;
|
|
|
|
autoplaySpeed?: number;
|
|
|
|
centerMode?: boolean;
|
|
|
|
centerPadding?: string | any;
|
|
|
|
cssEase?: string | any;
|
|
|
|
dotsClass?: string;
|
|
|
|
draggable?: boolean;
|
|
|
|
fade?: boolean;
|
|
|
|
focusOnSelect?: boolean;
|
|
|
|
infinite?: boolean;
|
|
|
|
initialSlide?: number;
|
|
|
|
lazyLoad?: boolean;
|
|
|
|
rtl?: boolean;
|
|
|
|
slide?: string;
|
|
|
|
slidesToShow?: number;
|
|
|
|
slidesToScroll?: number;
|
|
|
|
speed?: number;
|
|
|
|
swipe?: boolean;
|
|
|
|
swipeToSlide?: boolean;
|
|
|
|
touchMove?: boolean;
|
|
|
|
touchThreshold?: number;
|
|
|
|
variableWidth?: boolean;
|
|
|
|
useCSS?: boolean;
|
|
|
|
slickGoTo?: number;
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Carousel extends React.Component<CarouselProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
dots: true,
|
|
|
|
arrows: false,
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls: 'ant-carousel',
|
2017-01-09 23:21:18 +08:00
|
|
|
draggable: false,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-02-21 22:10:32 +08:00
|
|
|
refs: {
|
|
|
|
slick: any,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onWindowResized = debounce(this.onWindowResized, 500, {
|
|
|
|
leading: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { autoplay } = this.props;
|
|
|
|
if (autoplay) {
|
|
|
|
window.addEventListener('resize', this.onWindowResized);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const { autoplay } = this.props;
|
|
|
|
if (autoplay) {
|
|
|
|
window.removeEventListener('resize', this.onWindowResized);
|
|
|
|
(this.onWindowResized as any).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onWindowResized = () => {
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/2550
|
|
|
|
const { slick } = this.refs;
|
|
|
|
const { autoplay } = this.props;
|
|
|
|
if (autoplay && slick && slick.innerSlider && slick.innerSlider.autoPlay) {
|
|
|
|
slick.innerSlider.autoPlay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 14:52:26 +08:00
|
|
|
render() {
|
2017-07-03 16:57:11 +08:00
|
|
|
let props = {
|
|
|
|
...this.props,
|
|
|
|
};
|
2015-08-03 14:52:26 +08:00
|
|
|
|
|
|
|
if (props.effect === 'fade') {
|
|
|
|
props.fade = true;
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:18:33 +08:00
|
|
|
let className = props.prefixCls;
|
2015-08-03 14:52:26 +08:00
|
|
|
if (props.vertical) {
|
2016-09-14 16:18:33 +08:00
|
|
|
className = `${className} ${className}-vertical`;
|
2015-08-03 14:52:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
2016-09-29 15:50:45 +08:00
|
|
|
<SlickCarousel ref="slick" {...props} />
|
2015-08-03 14:52:26 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|