Skip to content

Commit 7d4b254

Browse files
authored
Merge pull request #15 from EdwardLemayDev/master
fix: make component generic + fix isDisabled
2 parents d9dd76f + 1b84122 commit 7d4b254

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/lib/VirtualListNew.svelte

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
})();
3131
</script>
3232

33-
<script lang="ts">
34-
import { onMount, onDestroy, type Snippet } from 'svelte';
33+
<script lang="ts" generics="ItemType">
34+
import { onDestroy, onMount, type Snippet } from 'svelte';
3535
import {
3636
ALIGNMENT,
3737
SCROLL_BEHAVIOR,
38-
type VLScrollEvent,
3938
type SizingCalculatorFn,
40-
type VLSlotSignature,
41-
type VLRangeEvent
39+
type VLRangeEvent,
40+
type VLScrollEvent,
41+
type VLSlotSignature
4242
} from '.';
4343
4444
import clsx from 'clsx';
@@ -103,7 +103,7 @@
103103
// calculates the size of a given index
104104
sizingCalculator
105105
}: {
106-
items: any[];
106+
items: ItemType[];
107107
108108
isDisabled?: boolean;
109109
isHorizontal?: boolean;
@@ -122,7 +122,7 @@
122122
// snippets
123123
header?: Snippet;
124124
// size is passed when a sizingCalculator is defined
125-
vl_slot: Snippet<[VLSlotSignature]>;
125+
vl_slot: Snippet<[VLSlotSignature<ItemType>]>;
126126
footer?: Snippet;
127127
128128
// events
@@ -201,11 +201,11 @@
201201
return p;
202202
});
203203
204-
const visibleItemsInfo: VLSlotSignature[] = $derived.by(() => {
204+
const visibleItemsInfo: VLSlotSignature<ItemType>[] = $derived.by(() => {
205205
if (!items || isDisabled) {
206206
return [];
207207
}
208-
const r: VLSlotSignature[] = [];
208+
const r: VLSlotSignature<ItemType>[] = [];
209209
for (let index = startIdx; index <= end2; index++) {
210210
const item = items[index];
211211
if (item) {
@@ -342,12 +342,7 @@
342342
function onscroll(event: Event): void {
343343
const offset = getScroll(listContainer);
344344
345-
if (
346-
event.target !== listContainer ||
347-
offset < 0 ||
348-
curState.offset === offset
349-
)
350-
return;
345+
if (event.target !== listContainer || offset < 0 || curState.offset === offset) return;
351346
352347
curState = {
353348
offset,
@@ -656,8 +651,8 @@
656651
{/if}
657652
<tbody>
658653
{#if isDisabled}
659-
{#each items as item}
660-
{@render vl_slot(item)}
654+
{#each items as item, index}
655+
{@render vl_slot({ index, item })}
661656
{/each}
662657
{:else}
663658
{#each visibleItemsInfo as item}
@@ -675,8 +670,8 @@
675670
{@render header()}
676671
{/if}
677672
{#if isDisabled}
678-
{#each items as item}
679-
{@render vl_slot(item)}
673+
{#each items as item, index}
674+
{@render vl_slot({ index, item })}
680675
{/each}
681676
{:else}
682677
{#each visibleItemsInfo as item}

src/lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ export { default as VirtualList } from './VirtualListNew.svelte';
44
export type SizingCalculatorFn = (index: number, item: unknown) => number;
55

66
// used by the vl_slot() snippet
7-
export interface VLSlotSignature {
7+
export interface VLSlotSignature<ItemType> {
88
// The row's index being rendered, from the original dataset
99
// The index is a string if the IDs are processed via the getKey() function
1010
index: number | string;
1111

1212
// the item being rendered
13-
item: any;
13+
item: ItemType;
1414

1515
// only present if there a custom sizing calculator configured, holds calculated size in pixels
1616
size?: number;

0 commit comments

Comments
 (0)