Skip to content

Commit

Permalink
Merge pull request #274 from R-Sourabh/dxp-289
Browse files Browse the repository at this point in the history
Fixed: infinite scroll issue when used with searchbar(dxp-289)
  • Loading branch information
ymaheshwari1 authored Apr 18, 2024
2 parents 81a4589 + 4f964db commit 15b334f
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ion-title>{{ $t("Cycle Count") }}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-content ref="contentRef" :scroll-events="true" @ionScroll="enableScrolling()">
<ion-searchbar @ionFocus="selectSearchBarText($event)" v-model="queryString" :placeholder="$t('Search')" @keyup.enter="queryString = $event.target.value; searchProducts()"/>

<!-- Empty state -->
Expand All @@ -19,8 +19,16 @@
<ion-list-header>{{ $t("Results") }}</ion-list-header>

<product-list-item v-for="product in products" :key="product.productId" :product="product"/>

<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" :disabled="!isScrollable">
<!--
When searching for a keyword, and if the user moves to the last item, then the didFire value inside infinite scroll becomes true and thus the infinite scroll does not trigger again on the same page(https://github.com/hotwax/users/issues/84).
Also if we are at the section that has been loaded by infinite-scroll and then move to the details page then the list infinite scroll does not work after coming back to the page
In ionic v7.6.0, an issue related to infinite scroll has been fixed that when more items can be added to the DOM, but infinite scroll does not fire as the window is not completely filled with the content(https://github.com/ionic-team/ionic-framework/issues/18071).
The above fix in ionic 7.6.0 is resulting in the issue of infinite scroll not being called again.
To fix this we have maintained another variable `isScrollingEnabled` to check whether the scrolling can be performed or not.
If we do not define an extra variable and just use v-show to check for `isScrollable` then when coming back to the page infinite-scroll is called programatically.
We have added an ionScroll event on ionContent to check whether the infiniteScroll can be enabled or not by toggling the value of isScrollingEnabled whenever the height < 0.
-->
<ion-infinite-scroll @ionInfinite="loadMoreProducts($event)" threshold="100px" v-show="isScrollingEnabled && isScrollable" ref="infiniteScrollRef">
<ion-infinite-scroll-content loading-spinner="crescent" :loading-text="$t('Loading')"></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-list>
Expand Down Expand Up @@ -90,7 +98,8 @@ export default defineComponent({
return {
queryString: '',
showErrorMessage: false,
fetchingProducts: false
fetchingProducts: false,
isScrollingEnabled: false
}
},
computed: {
Expand All @@ -100,6 +109,7 @@ export default defineComponent({
})
},
ionViewWillEnter(){
this.isScrollingEnabled = false;
if(this.$route.redirectedFrom) this.queryString = '';
},
methods: {
Expand Down Expand Up @@ -130,13 +140,24 @@ export default defineComponent({
element.select();
})
},
enableScrolling() {
const parentElement = (this as any).$refs.contentRef.$el
const scrollEl = parentElement.shadowRoot.querySelector("main[part='scroll']")
let scrollHeight = scrollEl.scrollHeight, infiniteHeight = (this as any).$refs.infiniteScrollRef.$el.offsetHeight, scrollTop = scrollEl.scrollTop, threshold = 100, height = scrollEl.offsetHeight
const distanceFromInfinite = scrollHeight - infiniteHeight - scrollTop - threshold - height
if(distanceFromInfinite < 0) {
this.isScrollingEnabled = false;
} else {
this.isScrollingEnabled = true;
}
},
async loadMoreProducts (event: any) {
this.searchProducts(
undefined,
Math.ceil(this.products.length / process.env.VUE_APP_VIEW_SIZE).toString()
).then(() => {
event.target.complete();
})
).then(async () => {
await event.target.complete();
});
},
async searchProducts(vSize?: any, vIndex?: any) {
this.queryString ? this.showErrorMessage = true : this.showErrorMessage = false;
Expand Down

0 comments on commit 15b334f

Please sign in to comment.