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;
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:53:26 +08:00
|
|
|
const dotsClass = 'slick-dots';
|
|
|
|
|
2024-06-19 11:11:08 +08:00
|
|
|
interface ArrowType extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
2024-05-23 10:24:14 +08:00
|
|
|
currentSlide?: number;
|
|
|
|
slideCount?: number;
|
|
|
|
}
|
|
|
|
|
2024-06-19 11:11:08 +08:00
|
|
|
const ArrowButton: React.FC<ArrowType> = ({ currentSlide, slideCount, ...rest }) => (
|
|
|
|
<button type="button" {...rest} />
|
|
|
|
);
|
|
|
|
|
2023-07-11 23:01:17 +08:00
|
|
|
const Carousel = React.forwardRef<CarouselRef, CarouselProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
dots = true,
|
|
|
|
arrows = false,
|
2024-05-23 10:24:14 +08:00
|
|
|
prevArrow = <ArrowButton aria-label="prev" />,
|
|
|
|
nextArrow = <ArrowButton aria-label="next" />,
|
2023-07-11 23:01:17 +08:00
|
|
|
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],
|
|
|
|
);
|
|
|
|
|
2024-06-23 14:57:41 +08:00
|
|
|
const prevCount = React.useRef<number>(React.Children.count(props.children));
|
2023-07-11 23:01:17 +08:00
|
|
|
|
|
|
|
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);
|
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,
|
|
|
|
);
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2023-07-11 23:01:17 +08:00
|
|
|
|
|
|
|
const className = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-vertical`]: newProps.vertical,
|
|
|
|
},
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-07-11 23:01:17 +08:00
|
|
|
rootClassName,
|
|
|
|
);
|
|
|
|
|
2023-11-13 20:14:13 +08:00
|
|
|
return wrapCSSVar(
|
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}
|
2024-04-19 17:17:56 +08:00
|
|
|
prevArrow={prevArrow}
|
|
|
|
nextArrow={nextArrow}
|
2023-07-11 23:01:17 +08:00
|
|
|
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;
|