Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/shaky-chicken-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/virtual": patch
---

expose `firstIndex` and `lastIndex` in virtual lists, to support counting
31 changes: 18 additions & 13 deletions packages/virtual/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type VirtualListReturn<T extends readonly any[]> = [
containerHeight: number;
viewerTop: number;
visibleItems: T;
firstIndex: number;
lastIndex: number | undefined;
}>,
onScroll: (e: Event) => void,
];
Expand All @@ -41,20 +43,23 @@ export function createVirtualList<T extends readonly any[]>({

const [offset, setOffset] = createSignal(0);

const getFirstIdx = () => Math.max(0, Math.floor(offset() / rowHeight) - overscanCount);

const getLastIdx = () =>
Math.min(
items.length,
Math.floor(offset() / rowHeight) + Math.ceil(rootHeight / rowHeight) + overscanCount,
);

return [
() => ({
containerHeight: items.length * rowHeight,
viewerTop: getFirstIdx() * rowHeight,
visibleItems: items.slice(getFirstIdx(), getLastIdx()) as unknown as T,
}),
() => {
const firstIndex = Math.max(0, Math.floor(offset() / rowHeight) - overscanCount);
const lastIndex = Math.min(
items.length,
Math.floor(offset() / rowHeight) + Math.ceil(rootHeight / rowHeight) + overscanCount,
);

return {
containerHeight: items.length * rowHeight,
viewerTop: firstIndex * rowHeight,
visibleItems: items.slice(firstIndex, lastIndex) as unknown as T,
firstIndex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're at it, you can also expose lastIndex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can. I did originally, but removed it because its a little weird semantically: when a list is empty, should it return -1, or undefined?

firstIndex being zero makes sense, kinda, but I didn't know what to do with last's typing, so I skipped it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, it's more like nextIndex than last because it's an exclusive range.

This comment was marked as duplicate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, too.

lastIndex: lastIndex > 0 ? lastIndex - 1 : undefined,
// -1 because slice is an exclusive range
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be items.length if it is -1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'm not sure what you're asking.

The -1 is an explanation of the reason we're not just passing it in straight, and it's already based on items.length, hence the whole thing ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I read the whole thing in a different order. My bad, I'm currently down with a flu.

};
},
e => {
// @ts-expect-error
if (e.target?.scrollTop !== undefined) setOffset(e.target.scrollTop);
Expand Down
79 changes: 79 additions & 0 deletions packages/virtual/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ describe("createVirtualList", () => {
expect(virtual().visibleItems).toEqual([0, 1, 2]);
});

test("returns firstIndex representing the first index of the visibleList", () => {
const [virtual] = createVirtualList({
items: TEST_LIST,
rootHeight: 20,
rowHeight: 10,
});

expect(virtual().firstIndex).toEqual(0);
});

test("returns lastIndex representing the last item in the visibleList's index", () => {
const [virtual] = createVirtualList({
items: TEST_LIST,
rootHeight: 20,
rowHeight: 10,
});

expect(virtual().lastIndex).toEqual(2);
});

test("returns onScroll which sets viewerTop and visibleItems based on rootElement's scrolltop", () => {
const el = document.createElement("div");

Expand All @@ -62,35 +82,55 @@ describe("createVirtualList", () => {

expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(2);

el.scrollTop += 10;

// no change until onScroll is called
expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(2);

onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2, 3]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(3);

el.scrollTop += 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([1, 2, 3, 4]);
expect(virtual().viewerTop).toEqual(10);
expect(virtual().firstIndex).toEqual(1);
expect(virtual().lastIndex).toEqual(4);

el.scrollTop -= 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2, 3]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(3);

el.scrollTop -= 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(2);

el.scrollTop += 7_000;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([699, 700, 701, 702]);
expect(virtual().viewerTop).toEqual(6990);
expect(virtual().firstIndex).toEqual(699);
expect(virtual().lastIndex).toEqual(702);
});

test("onScroll handles reaching the bottom of the list", () => {
Expand Down Expand Up @@ -126,6 +166,8 @@ describe("createVirtualList", () => {
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([8, 9, 10, 11, 12, 13]);
expect(virtual().firstIndex).toEqual(8);
expect(virtual().lastIndex).toEqual(13);
});

test("overscanCount defaults to 1 if undefined or zero", () => {
Expand All @@ -147,6 +189,41 @@ describe("createVirtualList", () => {
expect(virtualZero().visibleItems).toEqual([0, 1, 2]);
});

test("lastIndex is undefined in an empty list", () => {
const [virtual] = createVirtualList({
items: [],
rootHeight: 20,
rowHeight: 10,
});

expect(virtual().lastIndex).toEqual(undefined);
});

test("lastIndex is 0 in a singleton list", () => {
const [virtual] = createVirtualList({
items: [10],
rootHeight: 20,
rowHeight: 10,
});

expect(virtual().lastIndex).toEqual(0);
});

test("handles singleton list", () => {
const [virtual] = createVirtualList({
items: [10],
rootHeight: 20,
rowHeight: 10,
overscanCount: 0,
});

expect(virtual().containerHeight).toEqual(10);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().visibleItems).toEqual([10]);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(0);
});

test("handles empty list", () => {
const [virtual] = createVirtualList({
items: [],
Expand All @@ -158,6 +235,8 @@ describe("createVirtualList", () => {
expect(virtual().containerHeight).toEqual(0);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().visibleItems).toEqual([]);
expect(virtual().firstIndex).toEqual(0);
expect(virtual().lastIndex).toEqual(undefined);
});
});

Expand Down
Loading