ant-design/components/carousel/index.tsx
偏右 8501b708ea refactor: 📦 smaller bundlesize limit (#20356)
* 📦 samller bundlesize limit

* 🗑️ remove React static PropTypes

* 🗑️ remove react-lifecycles-compat

* 🗑️ remove matchMedia polyfill

* 🗑️ remove Transfer buggy lazy prop

* 🗑️ remove enquire.js dep

* 🗑️ remove Transfer lazy related code and fix ci

* 🗑️ remove used dom-closest

*  replace dom-scroll-into-view to scroll-into-view for bundle size

*  fix eslint

* 🆙 upgrade browserslist

*  fix test cases

* 🗑️ remove @ant-design/create-react-context

* 🆙 upgrade @ant-design/bisheng-plugin

* 🆙 upgrade rc-slider

*  fix ci

* 🆙 upgrade rc-tabs and rc-mentions

* 📦 scroll-into-view -> scroll-into-view-if-needed

* remove unused devDep

* docs: 📝 update instruction about IE9/10

* 📦 reduce css bundle size by drop IE9/10 support

* 🆙 upgrade rc-upload

* 🗑️ drop unused swing motion css

*  update upload snapshots

* 📦 lift css bundlesize limit to 55kb
2019-12-23 18:33:08 +08:00

138 lines
3.6 KiB
TypeScript

import * as React from 'react';
import debounce from 'lodash/debounce';
import { Settings } from 'react-slick';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import warning from '../_util/warning';
// 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;
export type CarouselEffect = 'scrollx' | 'fade';
export type DotPosition = 'top' | 'bottom' | 'left' | 'right';
// Carousel
export interface CarouselProps extends Settings {
effect?: CarouselEffect;
style?: React.CSSProperties;
prefixCls?: string;
slickGoTo?: number;
dotPosition?: DotPosition;
children?: React.ReactNode;
}
export default class Carousel extends React.Component<CarouselProps, {}> {
static defaultProps = {
dots: true,
arrows: false,
draggable: false,
};
innerSlider: any;
private slick: any;
constructor(props: CarouselProps) {
super(props);
this.onWindowResized = debounce(this.onWindowResized, 500, {
leading: false,
});
if ('vertical' in this.props) {
warning(
!this.props.vertical,
'Carousel',
'`vertical` is deprecated, please use `dotPosition` instead.',
);
}
}
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;
}
componentDidUpdate(prevProps: CarouselProps) {
if (React.Children.count(this.props.children) !== React.Children.count(prevProps.children)) {
this.goTo(this.props.initialSlide || 0, false);
}
}
componentWillUnmount() {
const { autoplay } = this.props;
if (autoplay) {
window.removeEventListener('resize', this.onWindowResized);
(this.onWindowResized as any).cancel();
}
}
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;
};
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();
}
};
next() {
this.slick.slickNext();
}
prev() {
this.slick.slickPrev();
}
goTo(slide: number, dontAnimate = false) {
this.slick.slickGoTo(slide, dontAnimate);
}
renderCarousel = ({ getPrefixCls }: ConfigConsumerProps) => {
const props = {
...this.props,
};
if (props.effect === 'fade') {
props.fade = true;
}
let className = getPrefixCls('carousel', props.prefixCls);
const dotsClass = 'slick-dots';
const dotPosition = this.getDotPosition();
props.vertical = dotPosition === 'left' || dotPosition === 'right';
props.dotsClass = `${dotsClass} ${dotsClass}-${dotPosition || 'bottom'}`;
if (props.vertical) {
className = `${className} ${className}-vertical`;
}
return (
<div className={className}>
<SlickCarousel ref={this.saveSlick} {...props} />
</div>
);
};
render() {
return <ConfigConsumer>{this.renderCarousel}</ConfigConsumer>;
}
}