ant-design/components/carousel/index.tsx

118 lines
2.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-03-08 13:36:12 +08:00
import debounce from 'lodash/debounce';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { Settings } from 'react-slick';
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
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
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
}
// 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
export type CarouselEffect = 'scrollx' | 'fade';
// Carousel
export interface CarouselProps extends Settings {
2016-07-13 11:14:24 +08:00
effect?: CarouselEffect;
style?: React.CSSProperties;
prefixCls?: string;
slickGoTo?: number;
}
2017-11-22 10:54:27 +08:00
export default class Carousel extends React.Component<CarouselProps, {}> {
static defaultProps = {
dots: true,
arrows: false,
draggable: false,
2016-07-13 11:14:24 +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);
this.onWindowResized = debounce(this.onWindowResized, 500, {
leading: false,
});
}
componentDidMount() {
const { autoplay } = this.props;
if (autoplay) {
window.addEventListener('resize', this.onWindowResized);
}
// https://github.com/ant-design/ant-design/issues/7191
this.innerSlider = this.slick && this.slick.innerSlider;
}
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 { autoplay } = this.props;
if (autoplay && this.slick && this.slick.innerSlider && this.slick.innerSlider.autoPlay) {
this.slick.innerSlider.autoPlay();
}
2018-12-07 20:02:01 +08:00
};
2017-11-22 10:54:27 +08:00
saveSlick = (node: any) => {
this.slick = node;
2018-12-07 20:02:01 +08:00
};
next() {
this.slick.slickNext();
}
prev() {
this.slick.slickPrev();
}
goTo(slide: number, dontAnimate = false) {
this.slick.slickGoTo(slide, dontAnimate);
}
renderCarousel = ({ getPrefixCls }: ConfigConsumerProps) => {
2018-11-09 20:31:43 +08:00
const props = {
...this.props,
};
2015-08-03 14:52:26 +08:00
if (props.effect === 'fade') {
props.fade = true;
}
let className = getPrefixCls('carousel', props.prefixCls);
2015-08-03 14:52:26 +08:00
if (props.vertical) {
className = `${className} ${className}-vertical`;
2015-08-03 14:52:26 +08:00
}
return (
<div className={className}>
<SlickCarousel ref={this.saveSlick} {...props} />
2015-08-03 14:52:26 +08:00
</div>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderCarousel}</ConfigConsumer>;
}
}