|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en" dir="ltr"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Infinite Scroll - Item Replacement</title> |
| 6 | + <meta |
| 7 | + name="viewport" |
| 8 | + content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" |
| 9 | + /> |
| 10 | + <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" /> |
| 11 | + <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" /> |
| 12 | + <script src="../../../../../scripts/testing/scripts.js"></script> |
| 13 | + <script nomodule src="../../../../../dist/ionic/ionic.js"></script> |
| 14 | + <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> |
| 15 | + </head> |
| 16 | + |
| 17 | + <body> |
| 18 | + <ion-app> |
| 19 | + <ion-header> |
| 20 | + <ion-toolbar> |
| 21 | + <ion-title>Infinite Scroll - Item Replacement</ion-title> |
| 22 | + </ion-toolbar> |
| 23 | + </ion-header> |
| 24 | + |
| 25 | + <ion-content class="ion-padding" id="content"> |
| 26 | + <IonHeader collapse="condense"> |
| 27 | + <IonToolbar> |
| 28 | + <IonTitle size="large">Title</IonTitle> |
| 29 | + </IonToolbar> |
| 30 | + </IonHeader> |
| 31 | + |
| 32 | + <div className="ion-padding">Scroll the list to see the title collapse.</div> |
| 33 | + |
| 34 | + <ion-list id="list"></ion-list> |
| 35 | + |
| 36 | + <ion-infinite-scroll threshold="100px" id="infinite-scroll" preserve-rerender-scroll-position> |
| 37 | + <ion-infinite-scroll-content loading-spinner="crescent" loading-text="Loading more data..."> |
| 38 | + </ion-infinite-scroll-content> |
| 39 | + </ion-infinite-scroll> |
| 40 | + </ion-content> |
| 41 | + </ion-app> |
| 42 | + |
| 43 | + <script> |
| 44 | + const list = document.getElementById('list'); |
| 45 | + const infiniteScroll = document.getElementById('infinite-scroll'); |
| 46 | + const content = document.getElementById('content'); |
| 47 | + const scrollPositionDiv = document.getElementById('scroll-position'); |
| 48 | + const modeDiv = document.getElementById('mode'); |
| 49 | + let loading = false; |
| 50 | + let itemCount = 0; |
| 51 | + let generationCount = 0; |
| 52 | + |
| 53 | + // Track scroll position for debugging |
| 54 | + content.addEventListener('ionScroll', () => { |
| 55 | + const scrollTop = content.scrollTop; |
| 56 | + scrollPositionDiv.textContent = `Scroll Position: ${scrollTop}`; |
| 57 | + }); |
| 58 | + |
| 59 | + infiniteScroll.addEventListener('ionInfinite', async function () { |
| 60 | + // Save current scroll position before replacement |
| 61 | + const currentScrollTop = content.scrollTop; |
| 62 | + window.currentScrollBeforeReplace = currentScrollTop; |
| 63 | + console.log('loading', loading); |
| 64 | + if (loading) { |
| 65 | + infiniteScroll.complete(); |
| 66 | + return; |
| 67 | + } |
| 68 | + loading = true; |
| 69 | + |
| 70 | + await wait(300); |
| 71 | + replaceAllItems(); |
| 72 | + infiniteScroll.complete(); |
| 73 | + |
| 74 | + window.dispatchEvent( |
| 75 | + new CustomEvent('ionInfiniteComplete', { |
| 76 | + detail: { |
| 77 | + scrollTopBefore: currentScrollTop, |
| 78 | + scrollTopAfter: content.scrollTop, |
| 79 | + generation: generationCount, |
| 80 | + mode: 'normal', |
| 81 | + }, |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + setTimeout(() => { |
| 86 | + console.log('setting loading to false'); |
| 87 | + loading = false; |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + function replaceAllItems() { |
| 92 | + console.log('replaceAllItems'); |
| 93 | + // This simulates what happens in React when all items get new keys |
| 94 | + // Clear all existing items |
| 95 | + list.innerHTML = ''; |
| 96 | + |
| 97 | + generationCount++; |
| 98 | + const generation = generationCount; |
| 99 | + |
| 100 | + // Add new items with new "keys" (different content/identifiers) |
| 101 | + // Start with more items to ensure scrollable content |
| 102 | + const totalItems = generation === 1 ? 30 : 30 + generation * 20; |
| 103 | + itemCount = 0; |
| 104 | + |
| 105 | + for (let i = 0; i < totalItems; i++) { |
| 106 | + const el = document.createElement('ion-item'); |
| 107 | + el.setAttribute('data-key', `gen-${generation}-item-${i}`); |
| 108 | + el.setAttribute('data-generation', generation); |
| 109 | + el.textContent = `Gen ${generation} - Item ${ |
| 110 | + i + 1 |
| 111 | + } - Additional content to make this item taller and ensure scrolling`; |
| 112 | + el.id = `item-gen-${generation}-${i}`; |
| 113 | + list.appendChild(el); |
| 114 | + itemCount++; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + function wait(time) { |
| 119 | + return new Promise((resolve) => { |
| 120 | + setTimeout(() => { |
| 121 | + resolve(); |
| 122 | + }, time); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + // Initial load |
| 127 | + replaceAllItems(); |
| 128 | + </script> |
| 129 | + </body> |
| 130 | +</html> |
0 commit comments