Skip to content

Commit 8506be9

Browse files
author
Guillaume Chau
committed
feat: scrollToBottom
1 parent c642fa2 commit 8506be9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/components/DynamicScroller.vue

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,18 @@ export default {
8282
for (let i = 0; i < items.length; i++) {
8383
const item = items[i]
8484
const id = item[keyField]
85+
let height = heights[id]
86+
if (typeof height === 'undefined' && !this.$_undefinedMap[id]) {
87+
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
88+
this.$_undefinedHeights++
89+
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
90+
this.$_undefinedMap[id] = true
91+
height = 0
92+
}
8593
result.push({
8694
item,
8795
id,
88-
height: heights[id] || 0,
96+
height,
8997
})
9098
}
9199
return result
@@ -110,6 +118,8 @@ export default {
110118
111119
created () {
112120
this.$_updates = []
121+
this.$_undefinedHeights = 0
122+
this.$_undefinedMap = {}
113123
},
114124
115125
mounted () {
@@ -153,6 +163,30 @@ export default {
153163
const scroller = this.$refs.scroller
154164
if (scroller) scroller.scrollToItem(index)
155165
},
166+
167+
getItemSize (item) {
168+
const id = item[this.keyField]
169+
return this.vscrollData.heights[id] || 0
170+
},
171+
172+
scrollToBottom () {
173+
if (this.$_scrollingToBottom) return
174+
this.$_scrollingToBottom = true
175+
const el = this.$el
176+
// Item is inserted to the DOM
177+
this.$nextTick(() => {
178+
// Item sizes are computed
179+
const cb = () => {
180+
el.scrollTop = el.scrollHeight
181+
if (this.$_undefinedHeights === 0) {
182+
this.$_scrollingToBottom = false
183+
} else {
184+
requestAnimationFrame(cb)
185+
}
186+
}
187+
requestAnimationFrame(cb)
188+
})
189+
},
156190
},
157191
}
158192
</script>

src/components/DynamicScrollerItem.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ export default {
141141
if (this.id === id) {
142142
const size = this.getSize()
143143
if (size.height && this.height !== size.height) {
144+
if (this.vscrollBus.$_undefinedMap[id]) {
145+
this.vscrollBus.$_undefinedHeights--
146+
this.vscrollBus.$_undefinedMap[id] = undefined
147+
}
144148
this.$set(this.vscrollData.heights, this.id, size.height)
145149
if (this.emitResize) this.$emit('resize', this.id)
146150
}

0 commit comments

Comments
 (0)