ant-design/components/carousel/index.tsx
lijianan 38474628fd
feat: prepend use-client directive for with Next.js App Router (#43573)
* fix: prepend use-client directive for with Next.js App Router

* Update components/affix/index.tsx

Co-authored-by: MadCcc <1075746765@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

* Update components/badge/index.tsx

Co-authored-by: MadCcc <1075746765@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

* Update components/divider/index.tsx

Co-authored-by: MadCcc <1075746765@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

* Update components/cascader/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/list/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/mentions/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/mentions/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/mentions/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/mentions/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/qrcode/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/spin/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/select/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/spin/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/spin/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/steps/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/time-picker/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/transfer/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update components/tree-select/index.tsx

Signed-off-by: lijianan <574980606@qq.com>

---------

Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: MadCcc <1075746765@qq.com>
2023-07-15 12:57:03 +08:00

128 lines
3.2 KiB
TypeScript

'use client';
import type { Settings } from '@ant-design/react-slick';
import SlickCarousel from '@ant-design/react-slick';
import classNames from 'classnames';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import useStyle from './style';
export type CarouselEffect = 'scrollx' | 'fade';
export type DotPosition = 'top' | 'bottom' | 'left' | 'right';
// Carousel
export interface CarouselProps extends Omit<Settings, 'dots' | 'dotsClass'> {
effect?: CarouselEffect;
style?: React.CSSProperties;
prefixCls?: string;
rootClassName?: string;
slickGoTo?: number;
dotPosition?: DotPosition;
children?: React.ReactNode;
dots?: boolean | { className?: string };
waitForAnimate?: boolean;
}
export interface CarouselRef {
goTo: (slide: number, dontAnimate?: boolean) => void;
next: () => void;
prev: () => void;
autoPlay: (palyType?: 'update' | 'leave' | 'blur') => void;
innerSlider: any;
}
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,
...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(
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(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, carousel?.className),
style: { ...carousel?.style, ...style },
...otherProps,
};
if (newProps.effect === 'fade') {
newProps.fade = true;
}
const prefixCls = getPrefixCls('carousel', newProps.prefixCls);
const dotsClass = 'slick-dots';
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(
<div className={className}>
<SlickCarousel
ref={slickRef}
{...newProps}
dots={enableDots}
dotsClass={dsClass}
arrows={arrows}
draggable={draggable}
waitForAnimate={waitForAnimate}
/>
</div>,
);
});
if (process.env.NODE_ENV !== 'production') {
Carousel.displayName = 'Carousel';
}
export default Carousel;