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

Commit e3082f8

Browse files
committed
fix(virtual-scroll): scrollIntoItem failed at initial setting, close #49
1 parent c873efb commit e3082f8

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/components/virtual-scroll/index.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,26 @@ const AtVirtualScroll = defineComponent({
106106

107107
watch(() => props.height, updateFirstAndLast)
108108
watch(() => props.itemHeight, updateFirstAndLast)
109-
watch(() => props.scrollIntoItem, (index, prevIndex) => {
110-
const parsedIndex = parseInt(`${index}`, 10)
111-
if (parsedIndex >= 0 && parsedIndex < props.items.length) {
109+
watch(() => props.scrollIntoItem, (itemIndex, prevItemIndex) => {
110+
let parsedIndex = parseInt(`${itemIndex || 0}`, 10)
111+
112+
// make sure index is within length of items
113+
parsedIndex = Math.min(props.items.length - 1, Math.max(0, parsedIndex))
114+
115+
scrollTop.value = parsedIndex * __itemHeight.value
116+
updateFirstAndLast()
117+
})
118+
119+
onMounted(() => {
120+
if (Boolean(props.scrollIntoItem)) {
121+
let parsedIndex = parseInt(`${props.scrollIntoItem || 0}`, 10)
112122
scrollTop.value = parsedIndex * __itemHeight.value
113123
updateFirstAndLast()
114124
} else {
115-
warn(`index should not exceed the length of items: ${index}`)
125+
last.value = getLast(0)
116126
}
117127
})
118128

119-
onMounted(() => {
120-
last.value = getLast(0)
121-
})
122-
123129
function getChildren() {
124130
return props.items.slice(
125131
firstToRender.value,

0 commit comments

Comments
 (0)