fix(components): [carousel] avoid stopping autoplay after children change (#23494)

* fix(components): [carousel] Carousel stops autoplay

* test(components): [carousel] add test cases

* Update packages/components/carousel/__tests__/carousel.test.tsx

Co-authored-by: rzzf <cszhjh@gmail.com>

---------

Co-authored-by: keeplearning66 <1256734885@qq.com>
Co-authored-by: rzzf <cszhjh@gmail.com>
This commit is contained in:
E66
2026-02-01 15:11:06 +08:00
committed by GitHub
parent 1bdfcc3b0d
commit 0023b39f01
2 changed files with 38 additions and 1 deletions

View File

@@ -411,4 +411,41 @@ describe('Carousel', () => {
vm.$refs.carousel.prev()
expect(vm.$refs.carousel.activeIndex).toBe(1)
})
it('should continue auto play after adding new items dynamically', async () => {
const data = reactive([1, 2, 3])
wrapper = mount({
setup() {
return () => (
<div>
<Carousel interval={30} ref={'carousel'}>
{data.map((value) => (
<CarouselItem label={value} key={value}>
{value}
</CarouselItem>
))}
</Carousel>
</div>
)
},
})
await nextTick()
const vm = wrapper.vm
expect(vm.$refs.carousel.activeIndex).toBe(0)
await wait(40)
expect(vm.$refs.carousel.activeIndex).toBe(1)
data.push(4)
await nextTick()
expect(wrapper.findAll('.el-carousel__item').length).toBe(4)
expect(vm.$refs.carousel.activeIndex).toBe(0)
await wait(80)
expect(vm.$refs.carousel.activeIndex).toBe(2)
})
})

View File

@@ -214,7 +214,7 @@ export const useCarousel = (
function resetTimer() {
pauseTimer()
if (!props.pauseOnHover) startTimer()
if (!props.pauseOnHover || !hover.value) startTimer()
}
function setContainerHeight(height: number) {