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

feat: Add posibilty to reset component after content change #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<VEndlessScroll
:state="state"
:identifier="identifier"
class="example"
@feed-me="feedIt"
>
Expand All @@ -10,6 +11,7 @@
{{line}}<br>
</span>
</VEndlessScroll>
<button @click="reset">Reset</button>
</template>

<script lang="ts">
Expand All @@ -29,7 +31,8 @@ export default defineComponent({
'pierwsza linia',
'druga linia',
'trzecia linia'
]
],
identifier: 1
}
},
methods: {
Expand All @@ -43,6 +46,21 @@ export default defineComponent({
} else {
this.state = InifiniteScrollState.COMPLETE
}
},
reset (): void {
this.lines = [
'pierwsza linia',
'druga linia',
'trzecia linia',
'pierwsza linia',
'druga linia',
'trzecia linia',
'pierwsza linia',
'druga linia',
'trzecia linia'
]
this.state = InifiniteScrollState.LOADED
this.identifier++
}
}
})
Expand Down
24 changes: 10 additions & 14 deletions src/components/Component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</template>

<script lang="ts">
import { defineComponent, PropType, onMounted, onBeforeUnmount, ref, toRefs } from 'vue'
import { defineComponent, PropType, onMounted, onBeforeUnmount, ref, toRefs, watch } from 'vue'
import { InifiniteScrollState } from './models'

export default defineComponent({
Expand Down Expand Up @@ -70,6 +70,8 @@ export default defineComponent({
let observer = null as any
const container = ref(null)
const end = ref(null)
const { state, identifier } = toRefs(props)

const setObserver = (): void => {
const options = {
root: container.value,
Expand All @@ -86,8 +88,6 @@ export default defineComponent({
observer.observe(end.value!)
}

const { state } = toRefs(props)

const handleScroll = (): void => {
if (state.value === InifiniteScrollState.LOADED) {
emit('feed-me')
Expand All @@ -97,6 +97,13 @@ export default defineComponent({
}
}

const reset = (): void => {
(container.value! as HTMLElement).scrollTop = 0
setObserver()
}

watch(identifier, reset)

onMounted(() => {
setObserver()
})
Expand All @@ -121,17 +128,6 @@ export default defineComponent({
isError (): boolean {
return this.state === InifiniteScrollState.ERROR
}
},
methods: {

reset (): void {
(this.$refs.container as HTMLElement).scrollTop = 0
},
setIdentifierReset (): void {
if (this.identifier !== null) {
this.$watch('identifier', this.reset)
}
}
}
})
</script>
Expand Down