fix: Carousel GoTo is ignored if animation is in progress (#41969)

* refactor: rewrite with waitForAnimate

* docs: update Carousel docs

* test: add waitForAnimate test
pull/41983/head
GUAN MING 2 years ago committed by GitHub
parent b31bec59af
commit 9db1ae1a7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -139,4 +139,33 @@ describe('Carousel', () => {
expect(container.querySelector('.slick-dots')).toHaveClass('customDots'); expect(container.querySelector('.slick-dots')).toHaveClass('customDots');
}); });
}); });
it('should not wait for the animation', async () => {
const ref = React.createRef<CarouselRef>();
render(
<Carousel ref={ref}>
<div>1</div>
<div>2</div>
<div>3</div>
</Carousel>,
);
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);
});
}); });

@ -31,6 +31,7 @@ A carousel component. Scales with its container.
| autoplay | Whether to scroll automatically | boolean | false | | | autoplay | Whether to scroll automatically | boolean | false | |
| dotPosition | The position of the dots, which can be one of `top` `bottom` `left` `right` | string | `bottom` | | | 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 | | | 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` | | | easing | Transition interpolation function name | string | `linear` | |
| effect | Transition effect | `scrollx` \| `fade` | `scrollx` | | | effect | Transition effect | `scrollx` \| `fade` | `scrollx` | |
| afterChange | Callback function called after the current index changes | (current: number) => void | - | | | afterChange | Callback function called after the current index changes | (current: number) => void | - | |

@ -18,6 +18,7 @@ export interface CarouselProps extends Omit<Settings, 'dots' | 'dotsClass'> {
dotPosition?: DotPosition; dotPosition?: DotPosition;
children?: React.ReactNode; children?: React.ReactNode;
dots?: boolean | { className?: string }; dots?: boolean | { className?: string };
waitForAnimate?: boolean;
} }
export interface CarouselRef { export interface CarouselRef {
@ -34,6 +35,7 @@ const Carousel = React.forwardRef<CarouselRef, CarouselProps>(
dots = true, dots = true,
arrows = false, arrows = false,
draggable = false, draggable = false,
waitForAnimate = false,
dotPosition = 'bottom', dotPosition = 'bottom',
vertical = dotPosition === 'left' || dotPosition === 'right', vertical = dotPosition === 'left' || dotPosition === 'right',
rootClassName, rootClassName,
@ -109,6 +111,7 @@ const Carousel = React.forwardRef<CarouselRef, CarouselProps>(
dotsClass={dsClass} dotsClass={dsClass}
arrows={arrows} arrows={arrows}
draggable={draggable} draggable={draggable}
waitForAnimate={waitForAnimate}
/> />
</div>, </div>,
); );

@ -32,6 +32,7 @@ demo:
| autoplay | 是否自动切换 | boolean | false | | | autoplay | 是否自动切换 | boolean | false | |
| dotPosition | 面板指示点位置,可选 `top` `bottom` `left` `right` | string | `bottom` | | | dotPosition | 面板指示点位置,可选 `top` `bottom` `left` `right` | string | `bottom` | |
| dots | 是否显示面板指示点,如果为 `object` 则同时可以指定 `dotsClass` 或者 | boolean \| { className?: string } | true | | | dots | 是否显示面板指示点,如果为 `object` 则同时可以指定 `dotsClass` 或者 | boolean \| { className?: string } | true | |
| waitForAnimate | 是否等待切换动画 | boolean | false | |
| easing | 动画效果 | string | `linear` | | | easing | 动画效果 | string | `linear` | |
| effect | 动画效果函数 | `scrollx` \| `fade` | `scrollx` | | | effect | 动画效果函数 | `scrollx` \| `fade` | `scrollx` | |
| afterChange | 切换面板的回调 | (current: number) => void | - | | | afterChange | 切换面板的回调 | (current: number) => void | - | |

Loading…
Cancel
Save