2023-08-28 14:58:42 +08:00
|
|
|
import * as React from 'react';
|
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-08-28 14:58:42 +08:00
|
|
|
|
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;
|
2023-08-07 19:57:58 +08:00
|
|
|
id?: 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;
|
|
|
|
}
|
|
|
|
|
2023-07-11 23:01:17 +08:00
|
|
|
const Carousel = React.forwardRef<CarouselRef, CarouselProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
dots = true,
|
|
|
|
arrows = false,
|
|
|
|
draggable = false,
|
|
|
|
waitForAnimate = false,
|
|
|
|
dotPosition = 'bottom',
|
|
|
|
vertical = dotPosition === 'left' || dotPosition === 'right',
|
|
|
|
rootClassName,
|
|
|
|
className: customClassName,
|
|
|
|
style,
|
2023-08-07 19:57:58 +08:00
|
|
|
id,
|
2023-07-11 23:01:17 +08:00
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls, direction, carousel } = React.useContext(ConfigContext);
|
|
|
|
const slickRef = React.useRef<any>();
|
|
|
|
|
|
|
|
const goTo = (slide: number, dontAnimate = false) => {
|
|
|
|
slickRef.current.slickGoTo(slide, dontAnimate);
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useImperativeHandle(
|
2022-05-05 10:52:41 +08:00
|
|
|
ref,
|
2023-07-11 23:01:17 +08:00
|
|
|
() => ({
|
|
|
|
goTo,
|
|
|
|
autoPlay: slickRef.current.innerSlider.autoPlay,
|
|
|
|
innerSlider: slickRef.current.innerSlider,
|
|
|
|
prev: slickRef.current.slickPrev,
|
|
|
|
next: slickRef.current.slickNext,
|
|
|
|
}),
|
|
|
|
[slickRef.current],
|
|
|
|
);
|
|
|
|
|
|
|
|
const prevCount = React.useRef(React.Children.count(props.children));
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (prevCount.current !== React.Children.count(props.children)) {
|
|
|
|
goTo(props.initialSlide || 0, false);
|
|
|
|
prevCount.current = React.Children.count(props.children);
|
2015-08-03 14:52:26 +08:00
|
|
|
}
|
2023-07-11 23:01:17 +08:00
|
|
|
}, [props.children]);
|
|
|
|
|
|
|
|
const newProps = {
|
|
|
|
vertical,
|
|
|
|
className: classNames(customClassName, carousel?.className),
|
|
|
|
style: { ...carousel?.style, ...style },
|
|
|
|
...otherProps,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (newProps.effect === 'fade') {
|
|
|
|
newProps.fade = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('carousel', newProps.prefixCls);
|
|
|
|
const dotsClass = 'slick-dots';
|
2015-08-03 14:52:26 +08:00
|
|
|
|
2023-07-11 23:01:17 +08:00
|
|
|
const enableDots = !!dots;
|
|
|
|
const dsClass = classNames(
|
|
|
|
dotsClass,
|
|
|
|
`${dotsClass}-${dotPosition}`,
|
|
|
|
typeof dots === 'boolean' ? false : dots?.className,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
|
|
const className = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-vertical`]: newProps.vertical,
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
rootClassName,
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapSSR(
|
2023-08-07 19:57:58 +08:00
|
|
|
<div className={className} id={id}>
|
2023-07-11 23:01:17 +08:00
|
|
|
<SlickCarousel
|
|
|
|
ref={slickRef}
|
|
|
|
{...newProps}
|
|
|
|
dots={enableDots}
|
|
|
|
dotsClass={dsClass}
|
|
|
|
arrows={arrows}
|
|
|
|
draggable={draggable}
|
2023-08-28 14:58:42 +08:00
|
|
|
verticalSwiping={vertical}
|
2023-07-11 23:01:17 +08:00
|
|
|
waitForAnimate={waitForAnimate}
|
|
|
|
/>
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
});
|
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;
|