2022-05-07 14:31:54 +08:00
|
|
|
import type { Settings } from '@ant-design/react-slick';
|
|
|
|
import SlickCarousel from '@ant-design/react-slick';
|
2020-01-02 19:10:16 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-05 20:52:44 +08:00
|
|
|
import React from 'react';
|
2020-11-11 17:23:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-26 17:32:36 +08:00
|
|
|
import useStyle from './style';
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: 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;
|
2022-12-29 18:33:13 +08:00
|
|
|
dots?: boolean | { className?: string };
|
2023-04-25 10:08:35 +08:00
|
|
|
waitForAnimate?: boolean;
|
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>(
|
2022-05-05 10:52:41 +08:00
|
|
|
(
|
|
|
|
{
|
|
|
|
dots = true,
|
|
|
|
arrows = false,
|
|
|
|
draggable = false,
|
2023-04-25 10:08:35 +08:00
|
|
|
waitForAnimate = false,
|
2022-05-05 10:52:41 +08:00
|
|
|
dotPosition = 'bottom',
|
|
|
|
vertical = dotPosition === 'left' || dotPosition === 'right',
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-05-05 10:52:41 +08:00
|
|
|
...props
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
2020-11-11 17:23:05 +08:00
|
|
|
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 = {
|
2022-05-05 10:52:41 +08:00
|
|
|
vertical,
|
2020-11-11 17:23:05 +08:00
|
|
|
...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-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
|
|
|
|
2022-04-07 17:02:54 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2022-03-26 17:32:36 +08:00
|
|
|
const className = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-vertical`]: newProps.vertical,
|
|
|
|
},
|
|
|
|
hashId,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-26 17:32:36 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return wrapSSR(
|
2015-08-03 14:52:26 +08:00
|
|
|
<div className={className}>
|
2020-11-11 17:23:05 +08:00
|
|
|
<SlickCarousel
|
|
|
|
ref={slickRef}
|
|
|
|
{...newProps}
|
|
|
|
dots={enableDots}
|
|
|
|
dotsClass={dsClass}
|
|
|
|
arrows={arrows}
|
|
|
|
draggable={draggable}
|
2023-04-25 10:08:35 +08:00
|
|
|
waitForAnimate={waitForAnimate}
|
2020-11-11 17:23:05 +08:00
|
|
|
/>
|
2022-03-26 17:32:36 +08:00
|
|
|
</div>,
|
2015-08-03 14:52:26 +08:00
|
|
|
);
|
2020-11-11 17:23:05 +08:00
|
|
|
},
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Carousel.displayName = 'Carousel';
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:23:05 +08:00
|
|
|
export default Carousel;
|