2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-03-08 13:36:12 +08:00
|
|
|
import debounce from 'lodash/debounce';
|
2019-02-27 23:28:48 +08:00
|
|
|
import { Settings } from 'react-slick';
|
2019-08-05 18:38:10 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-05-06 12:04:39 +08:00
|
|
|
import warning from '../_util/warning';
|
2017-02-21 22:10:32 +08:00
|
|
|
|
2017-08-27 16:53:36 +08:00
|
|
|
// matchMedia polyfill for
|
|
|
|
// https://github.com/WickyNilliams/enquire.js/issues/82
|
2019-09-16 18:19:18 +08:00
|
|
|
// TODO: Will be removed in antd 4.0 because we will no longer support ie9
|
2015-11-20 10:57:08 +08:00
|
|
|
if (typeof window !== 'undefined') {
|
2018-09-30 16:07:01 +08:00
|
|
|
const matchMediaPolyfill = (mediaQuery: string) => {
|
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,
|
2018-12-07 20:02:01 +08:00
|
|
|
addListener() {},
|
|
|
|
removeListener() {},
|
2015-11-20 10:57:08 +08:00
|
|
|
};
|
|
|
|
};
|
2019-09-16 18:19:18 +08:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/18774
|
|
|
|
if (!window.matchMedia) window.matchMedia = matchMediaPolyfill as any;
|
2015-11-20 10:57:08 +08:00
|
|
|
}
|
2017-08-27 16:53:36 +08:00
|
|
|
// Use require over import (will be lifted up)
|
|
|
|
// make sure matchMedia polyfill run before require('react-slick')
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/6560
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/3308
|
|
|
|
const SlickCarousel = require('react-slick').default;
|
2015-08-03 14:52:26 +08:00
|
|
|
|
2017-03-23 21:15:49 +08:00
|
|
|
export type CarouselEffect = 'scrollx' | 'fade';
|
2019-05-06 12:04:39 +08:00
|
|
|
export type DotPosition = 'top' | 'bottom' | 'left' | 'right';
|
|
|
|
|
2016-07-01 20:52:17 +08:00
|
|
|
// Carousel
|
2019-02-27 23:28:48 +08:00
|
|
|
export interface CarouselProps extends Settings {
|
2016-07-13 11:14:24 +08:00
|
|
|
effect?: CarouselEffect;
|
|
|
|
style?: React.CSSProperties;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2017-03-31 14:35:25 +08:00
|
|
|
slickGoTo?: number;
|
2019-05-06 12:04:39 +08:00
|
|
|
dotPosition?: DotPosition;
|
2019-05-15 00:39:09 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 10:54:27 +08:00
|
|
|
export default class Carousel extends React.Component<CarouselProps, {}> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
dots: true,
|
|
|
|
arrows: false,
|
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-08-13 14:44:13 +08:00
|
|
|
innerSlider: any;
|
|
|
|
|
2017-09-17 15:48:44 +08:00
|
|
|
private slick: any;
|
|
|
|
|
2017-11-22 10:54:27 +08:00
|
|
|
constructor(props: CarouselProps) {
|
2017-11-09 19:45:55 +08:00
|
|
|
super(props);
|
2017-02-21 22:10:32 +08:00
|
|
|
this.onWindowResized = debounce(this.onWindowResized, 500, {
|
|
|
|
leading: false,
|
|
|
|
});
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
if ('vertical' in this.props) {
|
|
|
|
warning(
|
|
|
|
!this.props.vertical,
|
|
|
|
'Carousel',
|
|
|
|
'`vertical` is deprecated, please use `dotPosition` instead.',
|
|
|
|
);
|
|
|
|
}
|
2017-02-21 22:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { autoplay } = this.props;
|
|
|
|
if (autoplay) {
|
|
|
|
window.addEventListener('resize', this.onWindowResized);
|
|
|
|
}
|
2017-08-13 14:44:13 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/7191
|
2017-09-08 09:41:14 +08:00
|
|
|
this.innerSlider = this.slick && this.slick.innerSlider;
|
2017-02-21 22:10:32 +08:00
|
|
|
}
|
|
|
|
|
2019-05-15 00:39:09 +08:00
|
|
|
componentDidUpdate(prevProps: CarouselProps) {
|
|
|
|
if (React.Children.count(this.props.children) !== React.Children.count(prevProps.children)) {
|
2019-05-23 23:08:03 +08:00
|
|
|
this.goTo(this.props.initialSlide || 0, false);
|
2019-05-15 00:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 22:10:32 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
const { autoplay } = this.props;
|
|
|
|
if (autoplay) {
|
|
|
|
window.removeEventListener('resize', this.onWindowResized);
|
|
|
|
(this.onWindowResized as any).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
getDotPosition(): DotPosition {
|
|
|
|
if (this.props.dotPosition) {
|
|
|
|
return this.props.dotPosition;
|
|
|
|
}
|
|
|
|
if ('vertical' in this.props) {
|
|
|
|
return this.props.vertical ? 'right' : 'bottom';
|
|
|
|
}
|
|
|
|
return 'bottom';
|
|
|
|
}
|
|
|
|
|
|
|
|
saveSlick = (node: any) => {
|
|
|
|
this.slick = node;
|
|
|
|
};
|
|
|
|
|
2017-02-21 22:10:32 +08:00
|
|
|
onWindowResized = () => {
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/2550
|
|
|
|
const { autoplay } = this.props;
|
2017-09-08 09:41:14 +08:00
|
|
|
if (autoplay && this.slick && this.slick.innerSlider && this.slick.innerSlider.autoPlay) {
|
|
|
|
this.slick.innerSlider.autoPlay();
|
2017-02-21 22:10:32 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-02-21 22:10:32 +08:00
|
|
|
|
2017-10-10 13:41:04 +08:00
|
|
|
next() {
|
|
|
|
this.slick.slickNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
prev() {
|
|
|
|
this.slick.slickPrev();
|
|
|
|
}
|
|
|
|
|
2018-09-12 14:21:59 +08:00
|
|
|
goTo(slide: number, dontAnimate = false) {
|
|
|
|
this.slick.slickGoTo(slide, dontAnimate);
|
2017-10-10 13:41:04 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderCarousel = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2018-11-09 20:31:43 +08:00
|
|
|
const props = {
|
2017-07-03 16:57:11 +08:00
|
|
|
...this.props,
|
|
|
|
};
|
2015-08-03 14:52:26 +08:00
|
|
|
|
|
|
|
if (props.effect === 'fade') {
|
|
|
|
props.fade = true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
let className = getPrefixCls('carousel', props.prefixCls);
|
2019-05-06 12:04:39 +08:00
|
|
|
const dotsClass = 'slick-dots';
|
|
|
|
const dotPosition = this.getDotPosition();
|
|
|
|
props.vertical = dotPosition === 'left' || dotPosition === 'right';
|
|
|
|
props.dotsClass = `${dotsClass} ${dotsClass}-${dotPosition || 'bottom'}`;
|
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}>
|
2017-09-08 09:41:14 +08:00
|
|
|
<SlickCarousel ref={this.saveSlick} {...props} />
|
2015-08-03 14:52:26 +08:00
|
|
|
</div>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderCarousel}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|