Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
fix(virtual-scroll): scrollIntoItem failed at initial setting, close #49
Browse files Browse the repository at this point in the history
  • Loading branch information
b2nil committed Oct 24, 2020
1 parent c873efb commit e3082f8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/components/virtual-scroll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,26 @@ const AtVirtualScroll = defineComponent({

watch(() => props.height, updateFirstAndLast)
watch(() => props.itemHeight, updateFirstAndLast)
watch(() => props.scrollIntoItem, (index, prevIndex) => {
const parsedIndex = parseInt(`${index}`, 10)
if (parsedIndex >= 0 && parsedIndex < props.items.length) {
watch(() => props.scrollIntoItem, (itemIndex, prevItemIndex) => {
let parsedIndex = parseInt(`${itemIndex || 0}`, 10)

// make sure index is within length of items
parsedIndex = Math.min(props.items.length - 1, Math.max(0, parsedIndex))

scrollTop.value = parsedIndex * __itemHeight.value
updateFirstAndLast()
})

onMounted(() => {
if (Boolean(props.scrollIntoItem)) {
let parsedIndex = parseInt(`${props.scrollIntoItem || 0}`, 10)
scrollTop.value = parsedIndex * __itemHeight.value
updateFirstAndLast()
} else {
warn(`index should not exceed the length of items: ${index}`)
last.value = getLast(0)
}
})

onMounted(() => {
last.value = getLast(0)
})

function getChildren() {
return props.items.slice(
firstToRender.value,
Expand Down

0 comments on commit e3082f8

Please sign in to comment.