Skip to content

Commit

Permalink
feat(useInfiniteScroll): add a reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
schelmo committed Mar 26, 2024
1 parent 1558cd2 commit 13e9147
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/core/useInfiniteScroll/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { ref } from 'vue'
import { useInfiniteScroll } from '@vueuse/core'
const el = ref<HTMLElement | null>(null)
const data = ref([1])
const data = ref<number[]>([])
useInfiniteScroll(
const { reset } = useInfiniteScroll(
el,
() => {
const length = data.value.length + 1
data.value.push(...Array.from({ length: 5 }, (_, i) => length + i))
},
{ distance: 10 },
)
function resetList() {
data.value = []
reset()
}
</script>

<template>
Expand All @@ -21,4 +26,7 @@ useInfiniteScroll(
{{ item }}
</div>
</div>
<button @click="resetList()">
Reset
</button>
</template>
10 changes: 9 additions & 1 deletion packages/core/useInfiniteScroll/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ import { useInfiniteScroll } from '@vueuse/core'
const el = ref<HTMLElement | null>(null)
const data = ref([1, 2, 3, 4, 5, 6])
useInfiniteScroll(
const { reset } = useInfiniteScroll(
el,
() => {
// load more
data.value.push(...moreData)
},
{ distance: 10 }
)
function resetList() {
data.value = []
reset()
}
</script>
<template>
Expand All @@ -32,6 +37,9 @@ useInfiniteScroll(
{{ item }}
</div>
</div>
<button @click="resetList()">
Reset
</button>
</template>
```

Expand Down
3 changes: 3 additions & 0 deletions packages/core/useInfiniteScroll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,8 @@ export function useInfiniteScroll<T extends InfiniteScrollElement>(

return {
isLoading,
reset() {
nextTick(() => checkAndLoad())
},
}
}

0 comments on commit 13e9147

Please sign in to comment.