From 9db1ae1a7eb90a03cf851812760e77245f1d6250 Mon Sep 17 00:00:00 2001 From: GUAN MING <105915352+guan404ming@users.noreply.github.com> Date: Tue, 25 Apr 2023 10:08:35 +0800 Subject: [PATCH] fix: Carousel GoTo is ignored if animation is in progress (#41969) * refactor: rewrite with waitForAnimate * docs: update Carousel docs * test: add waitForAnimate test --- components/carousel/__tests__/index.test.tsx | 29 ++++++++++++++++++++ components/carousel/index.en-US.md | 1 + components/carousel/index.tsx | 3 ++ components/carousel/index.zh-CN.md | 1 + 4 files changed, 34 insertions(+) diff --git a/components/carousel/__tests__/index.test.tsx b/components/carousel/__tests__/index.test.tsx index 11bfff6b83..46b01865a5 100644 --- a/components/carousel/__tests__/index.test.tsx +++ b/components/carousel/__tests__/index.test.tsx @@ -139,4 +139,33 @@ describe('Carousel', () => { expect(container.querySelector('.slick-dots')).toHaveClass('customDots'); }); }); + + it('should not wait for the animation', async () => { + const ref = React.createRef(); + render( + +
1
+
2
+
3
+
, + ); + const { prev, next, goTo } = ref.current || {}; + expect(typeof prev).toBe('function'); + expect(typeof next).toBe('function'); + expect(typeof goTo).toBe('function'); + expect(ref.current?.innerSlider.state.currentSlide).toBe(0); + ref.current?.goTo(1); + ref.current?.goTo(2); + ref.current?.goTo(1); + await waitFakeTimer(); + expect(ref.current?.innerSlider.state.currentSlide).toBe(1); + ref.current?.prev(); + ref.current?.next(); + ref.current?.next(); + await waitFakeTimer(); + expect(ref.current?.innerSlider.state.currentSlide).toBe(2); + ref.current?.prev(); + await waitFakeTimer(); + expect(ref.current?.innerSlider.state.currentSlide).toBe(1); + }); }); diff --git a/components/carousel/index.en-US.md b/components/carousel/index.en-US.md index 658961e14f..d8cf484682 100644 --- a/components/carousel/index.en-US.md +++ b/components/carousel/index.en-US.md @@ -31,6 +31,7 @@ A carousel component. Scales with its container. | autoplay | Whether to scroll automatically | boolean | false | | | dotPosition | The position of the dots, which can be one of `top` `bottom` `left` `right` | string | `bottom` | | | dots | Whether to show the dots at the bottom of the gallery, `object` for `dotsClass` and any others | boolean \| { className?: string } | true | | +| waitForAnimate | Whether to wait for the animation when switching | boolean | false | | | easing | Transition interpolation function name | string | `linear` | | | effect | Transition effect | `scrollx` \| `fade` | `scrollx` | | | afterChange | Callback function called after the current index changes | (current: number) => void | - | | diff --git a/components/carousel/index.tsx b/components/carousel/index.tsx index 0ffd892e8d..dd7360d702 100644 --- a/components/carousel/index.tsx +++ b/components/carousel/index.tsx @@ -18,6 +18,7 @@ export interface CarouselProps extends Omit { dotPosition?: DotPosition; children?: React.ReactNode; dots?: boolean | { className?: string }; + waitForAnimate?: boolean; } export interface CarouselRef { @@ -34,6 +35,7 @@ const Carousel = React.forwardRef( dots = true, arrows = false, draggable = false, + waitForAnimate = false, dotPosition = 'bottom', vertical = dotPosition === 'left' || dotPosition === 'right', rootClassName, @@ -109,6 +111,7 @@ const Carousel = React.forwardRef( dotsClass={dsClass} arrows={arrows} draggable={draggable} + waitForAnimate={waitForAnimate} /> , ); diff --git a/components/carousel/index.zh-CN.md b/components/carousel/index.zh-CN.md index 20fec2cda5..41fbd4a32d 100644 --- a/components/carousel/index.zh-CN.md +++ b/components/carousel/index.zh-CN.md @@ -32,6 +32,7 @@ demo: | autoplay | 是否自动切换 | boolean | false | | | dotPosition | 面板指示点位置,可选 `top` `bottom` `left` `right` | string | `bottom` | | | dots | 是否显示面板指示点,如果为 `object` 则同时可以指定 `dotsClass` 或者 | boolean \| { className?: string } | true | | +| waitForAnimate | 是否等待切换动画 | boolean | false | | | easing | 动画效果 | string | `linear` | | | effect | 动画效果函数 | `scrollx` \| `fade` | `scrollx` | | | afterChange | 切换面板的回调 | (current: number) => void | - | |