Skip to content

Commit dec85fc

Browse files
committed
Fix carousel vModel is allowed toSlide outside of bounds
1 parent 8ba5bf3 commit dec85fc

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/components/Carousel/Carousel.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -484,25 +484,18 @@ export const Carousel = defineComponent({
484484
if (!skipTransition && isSliding.value) {
485485
return
486486
}
487-
488-
let targetIndex = slideIndex
489-
let mappedIndex = slideIndex
490-
491-
prevSlideIndex.value = currentSlideIndex.value
492-
493-
if (!config.wrapAround) {
494-
targetIndex = getNumberInRange({
495-
val: targetIndex,
496-
max: maxSlideIndex.value,
497-
min: minSlideIndex.value,
498-
})
499-
} else {
500-
mappedIndex = mapNumberToRange({
501-
val: targetIndex,
502-
max: maxSlideIndex.value,
503-
min: minSlideIndex.value,
504-
})
487+
488+
const targetIndex = (config.wrapAround ? mapNumberToRange : getNumberInRange)({
489+
val: slideIndex,
490+
max: maxSlideIndex.value,
491+
min: minSlideIndex.value,
492+
})
493+
494+
if (currentSlideIndex.value === targetIndex) {
495+
return;
505496
}
497+
498+
prevSlideIndex.value = currentSlideIndex.value
506499

507500
emit('slide-start', {
508501
slidingToIndex: slideIndex,
@@ -514,17 +507,17 @@ export const Carousel = defineComponent({
514507
stopAutoplay()
515508
isSliding.value = true
516509

517-
currentSlideIndex.value = targetIndex
518-
if (mappedIndex !== targetIndex) {
510+
currentSlideIndex.value = slideIndex
511+
if (targetIndex !== slideIndex) {
519512
modelWatcher.pause()
520513
}
521-
emit('update:modelValue', mappedIndex)
514+
emit('update:modelValue', targetIndex)
522515

523516
const transitionCallback = (): void => {
524-
if (config.wrapAround && mappedIndex !== targetIndex) {
517+
if (config.wrapAround && targetIndex !== slideIndex) {
525518
modelWatcher.resume()
526519

527-
currentSlideIndex.value = mappedIndex
520+
currentSlideIndex.value = targetIndex
528521
emit('loop', {
529522
currentSlideIndex: currentSlideIndex.value,
530523
slidingToIndex: slideIndex,

0 commit comments

Comments
 (0)