2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-03-23 11:38:16 +08:00
|
|
|
import SlickCarousel, { Settings } from '@ant-design/react-slick';
|
2020-01-02 19:10:16 +08:00
|
|
|
import classNames from 'classnames';
|
2020-11-11 17:23:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2017-02-21 22:10:32 +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
|
2020-03-04 14:13:52 +08:00
|
|
|
export interface CarouselProps extends Omit<Settings, 'dots' | 'dotsClass'> {
|
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;
|
2020-03-04 14:13:52 +08:00
|
|
|
dots?:
|
|
|
|
| boolean
|
|
|
|
| {
|
|
|
|
className?: string;
|
|
|
|
};
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
2020-11-23 10:08:24 +08:00
|
|
|
export interface CarouselRef {
|
2020-12-01 17:30:38 +08:00
|
|
|
goTo: (slide: number, dontAnimate?: boolean) => void;
|
2020-11-23 10:08:24 +08:00
|
|
|
next: () => void;
|
|
|
|
prev: () => void;
|
2021-12-01 16:52:22 +08:00
|
|
|
autoPlay: (palyType?: 'update' | 'leave' | 'blur') => void;
|
2020-11-23 10:08:24 +08:00
|
|
|
innerSlider: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Carousel = React.forwardRef<CarouselRef, CarouselProps>(
|
2020-11-11 17:23:05 +08:00
|
|
|
({ dots = true, arrows = false, draggable = false, dotPosition = 'bottom', ...props }, ref) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const slickRef = React.useRef<any>();
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
const goTo = (slide: number, dontAnimate = false) => {
|
|
|
|
slickRef.current.slickGoTo(slide, dontAnimate);
|
|
|
|
};
|
2017-02-21 22:10:32 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
React.useImperativeHandle(
|
|
|
|
ref,
|
|
|
|
() => ({
|
|
|
|
goTo,
|
|
|
|
autoPlay: slickRef.current.innerSlider.autoPlay,
|
|
|
|
innerSlider: slickRef.current.innerSlider,
|
|
|
|
prev: slickRef.current.slickPrev,
|
|
|
|
next: slickRef.current.slickNext,
|
|
|
|
}),
|
|
|
|
[slickRef.current],
|
|
|
|
);
|
2019-05-15 00:39:09 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
const prevCount = React.useRef(React.Children.count(props.children));
|
2017-10-10 13:41:04 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (prevCount.current !== React.Children.count(props.children)) {
|
|
|
|
goTo(props.initialSlide || 0, false);
|
|
|
|
prevCount.current = React.Children.count(props.children);
|
|
|
|
}
|
|
|
|
}, [props.children]);
|
2017-10-10 13:41:04 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
const newProps = {
|
|
|
|
...props,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2015-08-03 14:52:26 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
if (newProps.effect === 'fade') {
|
|
|
|
newProps.fade = true;
|
2015-08-03 14:52:26 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
const prefixCls = getPrefixCls('carousel', newProps.prefixCls);
|
2019-05-06 12:04:39 +08:00
|
|
|
const dotsClass = 'slick-dots';
|
2020-11-11 17:23:05 +08:00
|
|
|
newProps.vertical = dotPosition === 'left' || dotPosition === 'right';
|
2020-03-04 14:13:52 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
const enableDots = !!dots;
|
2020-03-04 14:13:52 +08:00
|
|
|
const dsClass = classNames(
|
|
|
|
dotsClass,
|
2020-11-11 17:23:05 +08:00
|
|
|
`${dotsClass}-${dotPosition}`,
|
|
|
|
typeof dots === 'boolean' ? false : dots?.className,
|
2020-03-04 14:13:52 +08:00
|
|
|
);
|
2015-08-03 14:52:26 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
const className = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-11-11 17:23:05 +08:00
|
|
|
[`${prefixCls}-vertical`]: newProps.vertical,
|
2020-01-02 19:10:16 +08:00
|
|
|
});
|
|
|
|
|
2015-08-03 14:52:26 +08:00
|
|
|
return (
|
|
|
|
<div className={className}>
|
2020-11-11 17:23:05 +08:00
|
|
|
<SlickCarousel
|
|
|
|
ref={slickRef}
|
|
|
|
{...newProps}
|
|
|
|
dots={enableDots}
|
|
|
|
dotsClass={dsClass}
|
|
|
|
arrows={arrows}
|
|
|
|
draggable={draggable}
|
|
|
|
/>
|
2015-08-03 14:52:26 +08:00
|
|
|
</div>
|
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
},
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
export default Carousel;
|