Skip to content

Commit

Permalink
fix(comp:carousel): after click the same dot repeatedly, will not work (
Browse files Browse the repository at this point in the history
  • Loading branch information
danranVm committed Jun 28, 2022
1 parent a263497 commit 33e136e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
15 changes: 9 additions & 6 deletions packages/components/carousel/src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default defineComponent({
}
}

onBeforeUpdate(() => (sliderRefs.value = []))
onBeforeUpdate(() => {
sliderRefs.value = []
})

const { activeIndex, runningIndex, unitHeight, unitWidth, goTo, next, prev } = useStrategy(
props,
Expand Down Expand Up @@ -119,7 +121,8 @@ export default defineComponent({
}

const onTransitionend = () => {
if (runningIndex.value === -1) {
const currRunningIndex = runningIndex.value
if (currRunningIndex === -1) {
return
}
const sliderTrackElement = sliderTrackRef.value!
Expand All @@ -131,13 +134,13 @@ export default defineComponent({
element.style.left = ''
}
})
const currIndex = runningIndex.value

if (mergedVertical.value) {
sliderTrackElement.style.transform = `translate3d(0, ${-currIndex * unitHeight.value}px, 0)`
sliderTrackElement.style.transform = `translate3d(0, ${-currRunningIndex * unitHeight.value}px, 0)`
} else {
sliderTrackElement.style.transform = `translate3d(${-currIndex * unitWidth.value}px,0 , 0)`
sliderTrackElement.style.transform = `translate3d(${-currRunningIndex * unitWidth.value}px,0 , 0)`
}
activeIndex.value = currIndex
activeIndex.value = currRunningIndex
runningIndex.value = -1
startAutoplay()
}
Expand Down
76 changes: 43 additions & 33 deletions packages/components/carousel/src/composables/useStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,54 +64,64 @@ export function useStrategy(
// this will not track dependencies used in the slot.
// Invoke the slot function inside the render function instead.
onMounted(() => {
watch(mergedLength, () => {
goTo(0)
layout()
})
watch(
mergedLength,
() => {
layout()
runningIndex.value = -1
goTo(0)
},
{ flush: 'post' },
)
})

const goTo = (nextIndex: number) => {
const length = mergedLength.value
const sliderTrackElement = sliderTrackRef.value
const firstSliderElement = convertElement(sliderRefs.value[0])
const lastSliderElement = convertElement(sliderRefs.value[length - 1])
if (!sliderTrackElement || !firstSliderElement || !lastSliderElement) {

if (
length <= 1 ||
runningIndex.value !== -1 ||
nextIndex === activeIndex.value ||
!sliderTrackElement ||
!firstSliderElement ||
!lastSliderElement
) {
return
}

if (length > 1 && runningIndex.value === -1) {
const currIndex = activeIndex.value
const { from, to } = getBoundary(currIndex, nextIndex, length)
runningIndex.value = to
sliderTrackElement.style.transition = `transform 0.5s ease`

const needToAdjust = length > 2 && nextIndex !== to
if (mergedVertical.value) {
if (needToAdjust) {
if (to < from) {
firstSliderElement.style.top = `${length * unitHeight.value}px`
lastSliderElement.style.top = ''
} else {
firstSliderElement.style.top = ''
lastSliderElement.style.top = `${-length * unitHeight.value}px`
}
sliderTrackElement.style.transform = `translate3d(0, ${-nextIndex * unitHeight.value}px, 0)`
const { from, to } = getBoundary(activeIndex.value, nextIndex, length)
runningIndex.value = to
sliderTrackElement.style.transition = `transform 0.5s ease`

const needToAdjust = length > 2 && nextIndex !== to
if (mergedVertical.value) {
if (needToAdjust) {
if (to < from) {
firstSliderElement.style.top = `${length * unitHeight.value}px`
lastSliderElement.style.top = ''
} else {
sliderTrackElement.style.transform = `translate3d(0, ${-to * unitHeight.value}px, 0)`
firstSliderElement.style.top = ''
lastSliderElement.style.top = `${-length * unitHeight.value}px`
}
sliderTrackElement.style.transform = `translate3d(0, ${-nextIndex * unitHeight.value}px, 0)`
} else {
if (needToAdjust) {
if (to < from) {
firstSliderElement.style.left = `${length * unitWidth.value}px`
lastSliderElement.style.left = ''
} else {
firstSliderElement.style.left = ''
lastSliderElement.style.left = `${-length * unitWidth.value}px`
}
sliderTrackElement.style.transform = `translate3d(${-nextIndex * unitWidth.value}px, 0, 0)`
sliderTrackElement.style.transform = `translate3d(0, ${-to * unitHeight.value}px, 0)`
}
} else {
if (needToAdjust) {
if (to < from) {
firstSliderElement.style.left = `${length * unitWidth.value}px`
lastSliderElement.style.left = ''
} else {
sliderTrackElement.style.transform = `translate3d(${-to * unitWidth.value}px, 0, 0)`
firstSliderElement.style.left = ''
lastSliderElement.style.left = `${-length * unitWidth.value}px`
}
sliderTrackElement.style.transform = `translate3d(${-nextIndex * unitWidth.value}px, 0, 0)`
} else {
sliderTrackElement.style.transform = `translate3d(${-to * unitWidth.value}px, 0, 0)`
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/components/pagination/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

&-sizes {
display: inline-flex;
min-width: 104px;


&-select-wrapper {
flex: auto;
margin: 0 4px;
min-width: 64px;
}
}

Expand Down

0 comments on commit 33e136e

Please sign in to comment.