|
2 | 2 | import { ALIGNMENT, SCROLL_BEHAVIOR, type VLSlotSignature } from '$lib'; |
3 | 3 | import VirtualList from 'svelte-virtuallists/new/VirtualListNew.svelte'; |
4 | 4 |
|
5 | | - const myModel = new Array(10000).fill(1).map((v, i) => { |
6 | | - return { text: 'Item ' + i + ' item ' + i, lineHeight: 20 + (i % 30) + 'px' }; |
7 | | - }); |
8 | | -
|
9 | | - let virtualList; |
| 5 | + const myModel = $state(new Array(10000)); |
10 | 6 |
|
11 | 7 | // on the UI |
12 | 8 | let theScrollToIndex: number | undefined = $state(); |
|
46 | 42 |
|
47 | 43 | let szCalculator: ((index: number, item: unknown) => number) | undefined = $state(); |
48 | 44 |
|
49 | | - function randomize() { |
50 | | - szCalculator = (_index: number, _item: any) => Math.round(Math.random() * (155 - 30) + 30); |
| 45 | + let randSizes: Array<number>; |
| 46 | + function randomizeSize() { |
| 47 | + randSizes = new Array(myModel.length); |
| 48 | + for (let i = 0; i < randSizes.length; i++) { |
| 49 | + randSizes[i] = Math.round(Math.random() * 65 + 30); |
| 50 | + } |
| 51 | +
|
| 52 | + szCalculator = (_index: number, _item: any) => randSizes[_index]; |
51 | 53 | } |
52 | 54 |
|
53 | 55 | function sameSize() { |
54 | | - szCalculator = (_index: number, _item: any) => 25; |
| 56 | + szCalculator = () => 25; |
| 57 | + } |
| 58 | +
|
| 59 | + function randomizeContent() { |
| 60 | + for (let i = 0; i < myModel.length; i++) { |
| 61 | + myModel[i] = { text: Math.floor(Math.random() * myModel.length) }; // Random number between 0 and 9999 |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + function stripItemsBy10() { |
| 66 | + for (let i = 0; i < 10; i++) myModel.pop(); |
55 | 67 | } |
56 | 68 |
|
57 | | - randomize(); |
| 69 | + randomizeContent(); |
| 70 | + randomizeSize(); |
58 | 71 | </script> |
59 | 72 |
|
60 | 73 | <div class="actions"> |
|
120 | 133 | {scrollToBehaviour} |
121 | 134 | sizingCalculator={szCalculator} |
122 | 135 | onVisibleRangeUpdate={handleMessage}> |
123 | | - {#snippet vl_slot({ item, index, size })} |
| 136 | + {#snippet vl_slot({ index, item, size }: VLSlotSignature)} |
124 | 137 | <div |
125 | 138 | style="border: 1px solid rgb(204, 204, 204); line-height: {size}px;" |
126 | 139 | class:highlighted={index === scrollToIndex}> |
| 140 | + #{index} |
127 | 141 | {item.text} |
128 | 142 | </div> |
129 | 143 | {/snippet} |
130 | 144 | </VirtualList> |
131 | 145 |
|
132 | 146 | <div class="actions"> |
133 | | - <button onclick={randomize} class="button">Randomize row heights</button> |
| 147 | + <button onclick={randomizeSize} class="button">Randomize row heights</button> |
134 | 148 | <button onclick={sameSize} class="button">Same row heights</button> |
| 149 | + <button onclick={randomizeContent} class="button">Randomize content</button> |
| 150 | + <button onclick={stripItemsBy10} class="button">array size -10</button> |
135 | 151 | </div> |
136 | 152 |
|
137 | 153 | <style> |
|
0 commit comments