ant-design/components/carousel/index.tsx
Ryan 16bedba7ad
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: Carousel support show dot duration (#52672)
* feat: carousel support show dot duration

* docs: add carousel dot duration demo

* test: add carousel dot duration test

* chore: add jest snap

* docs: fix typos in test file

* feat(api): redesign API for dot duration

* test: update dot duration test items

* docs: update md file for dot duration

* chore: set default false to autoplay

* Update components/carousel/index.zh-CN.md

Signed-off-by: afc163 <afc163@gmail.com>

* Update components/carousel/index.en-US.md

Signed-off-by: afc163 <afc163@gmail.com>

---------

Signed-off-by: afc163 <afc163@gmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
2025-02-10 11:17:56 +08:00

160 lines
4.2 KiB
TypeScript

import * as React from 'react';
import type { Settings } from '@ant-design/react-slick';
import SlickCarousel from '@ant-design/react-slick';
import classNames from 'classnames';
import { useComponentConfig } from '../config-provider/context';
import useStyle, { DotDuration } from './style';
export type CarouselEffect = 'scrollx' | 'fade';
export type DotPosition = 'top' | 'bottom' | 'left' | 'right';
// Carousel
export interface CarouselProps extends Omit<Settings, 'dots' | 'dotsClass' | 'autoplay'> {
effect?: CarouselEffect;
style?: React.CSSProperties;
prefixCls?: string;
rootClassName?: string;
id?: string;
slickGoTo?: number;
dotPosition?: DotPosition;
children?: React.ReactNode;
dots?: boolean | { className?: string };
waitForAnimate?: boolean;
autoplay?: boolean | { dotDuration?: boolean };
}
export interface CarouselRef {
goTo: (slide: number, dontAnimate?: boolean) => void;
next: () => void;
prev: () => void;
autoPlay: (palyType?: 'update' | 'leave' | 'blur') => void;
innerSlider: any;
}
const dotsClass = 'slick-dots';
interface ArrowType extends React.ButtonHTMLAttributes<HTMLButtonElement> {
currentSlide?: number;
slideCount?: number;
}
const ArrowButton: React.FC<ArrowType> = ({ currentSlide, slideCount, ...rest }) => (
<button type="button" {...rest} />
);
const Carousel = React.forwardRef<CarouselRef, CarouselProps>((props, ref) => {
const {
dots = true,
arrows = false,
prevArrow = <ArrowButton aria-label="prev" />,
nextArrow = <ArrowButton aria-label="next" />,
draggable = false,
waitForAnimate = false,
dotPosition = 'bottom',
vertical = dotPosition === 'left' || dotPosition === 'right',
rootClassName,
className: customClassName,
style,
id,
autoplay = false,
...otherProps
} = props;
const {
getPrefixCls,
direction,
className: contextClassName,
style: contextStyle,
} = useComponentConfig('carousel');
const slickRef = React.useRef<any>(null);
const goTo = (slide: number, dontAnimate = false) => {
slickRef.current.slickGoTo(slide, dontAnimate);
};
React.useImperativeHandle(
ref,
() => ({
goTo,
autoPlay: slickRef.current.innerSlider.autoPlay,
innerSlider: slickRef.current.innerSlider,
prev: slickRef.current.slickPrev,
next: slickRef.current.slickNext,
}),
[slickRef.current],
);
const prevCount = React.useRef<number>(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);
}
}, [props.children]);
const newProps = {
vertical,
className: classNames(customClassName, contextClassName),
style: { ...contextStyle, ...style },
autoplay: !!autoplay,
...otherProps,
};
if (newProps.effect === 'fade') {
newProps.fade = true;
}
const prefixCls = getPrefixCls('carousel', newProps.prefixCls);
const enableDots = !!dots;
const dsClass = classNames(
dotsClass,
`${dotsClass}-${dotPosition}`,
typeof dots === 'boolean' ? false : dots?.className,
);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const className = classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-vertical`]: newProps.vertical,
},
hashId,
cssVarCls,
rootClassName,
);
const { autoplaySpeed = 3000 } = props;
const mergedShowDuration =
autoplay && (typeof autoplay === 'object' ? autoplay.dotDuration : false);
const dotDurationStyle = mergedShowDuration
? ({ [DotDuration]: `${autoplaySpeed / 1000}s` } as React.CSSProperties)
: {};
return wrapCSSVar(
<div className={className} id={id} style={dotDurationStyle}>
<SlickCarousel
ref={slickRef}
{...newProps}
dots={enableDots}
dotsClass={dsClass}
arrows={arrows}
prevArrow={prevArrow}
nextArrow={nextArrow}
draggable={draggable}
verticalSwiping={vertical}
waitForAnimate={waitForAnimate}
/>
</div>,
);
});
if (process.env.NODE_ENV !== 'production') {
Carousel.displayName = 'Carousel';
}
export default Carousel;