Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useVirtualList): List sometimes missing elements #2477

Merged
merged 8 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions packages/core/useVirtualList/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ref } from 'vue-demi'
import { useVirtualList } from '.'

describe('useVirtualList', () => {
it('should be defined', () => {
expect(useVirtualList).toBeDefined()
})
})

describe('useVirtualList, vertical', () => {
it('returns all original items if they fit the container', () => {
const {
list,
containerProps: { ref: containerRef },
scrollTo,
} = useVirtualList(ref(['a', 'b', 'c', 'd', 'e', 'f']), { itemHeight: () => 50, overscan: 1 })
const div = { ...document.createElement('div'), clientHeight: 50 }

containerRef.value = div

containerRef.value = { ...div, clientHeight: 200 }
scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])

containerRef.value = { ...div, clientHeight: 250 }
scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])
})

it('returns the current visible window of items if there are too many for the container', () => {
const {
list,
containerProps: { ref: containerRef },
scrollTo,
} = useVirtualList(ref(['a', 'b', 'c', 'd', 'e', 'f', 'g']), { itemHeight: () => 50, overscan: 1 })
const div = { ...document.createElement('div'), clientHeight: 50 }

containerRef.value = div

scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c'])

scrollTo(1)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c'])

scrollTo(2)
expect(list.value.map(i => i.data)).toEqual(['b', 'c', 'd'])

scrollTo(3)
expect(list.value.map(i => i.data)).toEqual(['c', 'd', 'e'])

scrollTo(4)
expect(list.value.map(i => i.data)).toEqual(['d', 'e', 'f'])

scrollTo(5)
expect(list.value.map(i => i.data)).toEqual(['e', 'f', 'g'])

scrollTo(6)
expect(list.value.map(i => i.data)).toEqual(['f', 'g'])
})
})

describe('useVirtualList, horizontal', () => {
it('returns all original items if they fit the container', () => {
const {
list,
containerProps: { ref: containerRef },
scrollTo,
} = useVirtualList(ref(['a', 'b', 'c', 'd', 'e', 'f']), { itemWidth: () => 50, overscan: 1 })
const div = { ...document.createElement('div'), clientWidth: 50 }

containerRef.value = div

containerRef.value = { ...div, clientWidth: 200 }
scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])

containerRef.value = { ...div, clientWidth: 250 }
scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])
})

it('returns the current visible window of items if there are too many for the container', () => {
const {
list,
containerProps: { ref: containerRef },
scrollTo,
} = useVirtualList(ref(['a', 'b', 'c', 'd', 'e', 'f', 'g']), { itemWidth: () => 50, overscan: 1 })
const div = { ...document.createElement('div'), clientWidth: 50 }

containerRef.value = div

scrollTo(0)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c'])

scrollTo(1)
expect(list.value.map(i => i.data)).toEqual(['a', 'b', 'c'])

scrollTo(2)
expect(list.value.map(i => i.data)).toEqual(['b', 'c', 'd'])

scrollTo(3)
expect(list.value.map(i => i.data)).toEqual(['c', 'd', 'e'])

scrollTo(4)
expect(list.value.map(i => i.data)).toEqual(['d', 'e', 'f'])

scrollTo(5)
expect(list.value.map(i => i.data)).toEqual(['e', 'f', 'g'])

scrollTo(6)
expect(list.value.map(i => i.data)).toEqual(['f', 'g'])
})
})
13 changes: 6 additions & 7 deletions packages/core/useVirtualList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,19 @@ function useVirtualListResources<T>(list: MaybeRef<T[]>): UseVirtualListResource
}

function createGetViewCapacity<T>(state: UseVirtualListResources<T>['state'], source: UseVirtualListResources<T>['source'], itemSize: UseVirtualListItemSize) {
return (containerHeight: number) => {
return (containerSize: number) => {
if (typeof itemSize === 'number')
return Math.ceil(containerHeight / itemSize)
return Math.ceil(containerSize / itemSize)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Height naming was from when list was vertical only. Size is more appropriate, IMO


const { start = 0 } = state.value
let sum = 0
let capacity = 0
for (let i = start; i < source.value.length; i++) {
const height = itemSize(i)
sum += height
if (sum >= containerHeight) {
capacity = i
const size = itemSize(i)
sum += size
capacity = i
if (sum > containerSize)
Comment on lines +133 to +134
Copy link
Contributor Author

@begedin begedin Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving capacity out of the if is the fix. Without this, if the original list size is smaller than what could fit in the view, we end up with a capacity of 1 + overscan

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the same thing be done with 'offset = i' in function 'createGetOffset'?

break
}
}
return capacity - start
}
Expand Down