Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add align option to scrollToIndex method #158

Merged
merged 1 commit into from
Aug 13, 2023
Merged
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
240 changes: 171 additions & 69 deletions e2e/VList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,98 +295,200 @@ test.describe("check if scrollToIndex works", () => {
await page.goto(storyUrl("basics-vlist--scroll-to"));
});

test("mid", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);
test.describe("align start", () => {
test("mid", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");
await clearInput(input);
await input.type("700");
await button.click();

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);
await scrollable.waitForElementState("stable");

await clearInput(input);
await input.type("700");
await button.click();
// Check if scrolled precisely
const firstItem = await getFirstItem(scrollable);
await expect(firstItem.text).toEqual("700");
await expect(firstItem.top).toEqual(0);

await scrollable.waitForElementState("stable");
// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("650");
await expect(await scrollable.innerText()).not.toContain("750");
});

// Check if scrolled precisely
const firstItem = await getFirstItem(scrollable);
await expect(firstItem.text).toEqual("700");
await expect(firstItem.top).toEqual(0);
test("start", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("650");
await expect(await scrollable.innerText()).not.toContain("750");
});
// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

test("start", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);
const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");
await clearInput(input);
await input.type("500");
await button.click();

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);
await scrollable.waitForElementState("stable");

await clearInput(input);
await input.type("500");
await button.click();
await expect(await scrollable.innerText()).toContain("500");

await scrollable.waitForElementState("stable");
await clearInput(input);
await input.type("0");
await button.click();

await expect(await scrollable.innerText()).toContain("500");
// Check if scrolled precisely
const firstItem = await getFirstItem(scrollable);
await expect(firstItem.text).toEqual("0");
await expect(firstItem.top).toEqual(0);

await clearInput(input);
await input.type("0");
await button.click();
// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("50\n");
});

test("end", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

await clearInput(input);
await input.type("999");
await button.click();

await scrollable.waitForElementState("stable");

// Check if scrolled precisely
const firstItem = await getFirstItem(scrollable);
await expect(firstItem.text).toEqual("0");
await expect(firstItem.top).toEqual(0);
// Check if scrolled precisely
const lastItem = await getLastItem(scrollable);
await expect(lastItem.text).toEqual("999");
await expect(lastItem.bottom).toEqual(0);

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("50\n");
// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("949");
});
});

test("end", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);
test.describe("align end", () => {
test.beforeEach(async ({ page }) => {
await page.getByRole("radio", { name: "end" }).click();
});

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");
test("mid", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);
// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

await clearInput(input);
await input.type("999");
await button.click();
const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

await scrollable.waitForElementState("stable");
await clearInput(input);
await input.type("700");
await button.click();

await scrollable.waitForElementState("stable");

// Check if scrolled precisely
const lastItem = await getLastItem(scrollable);
await expect(lastItem.text).toEqual("700");
await expect(lastItem.bottom).toBeLessThanOrEqual(1); // FIXME: may not be 0 in Safari

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("650");
await expect(await scrollable.innerText()).not.toContain("750");
});

// Check if scrolled precisely
const lastItem = await getLastItem(scrollable);
await expect(lastItem.text).toEqual("999");
await expect(lastItem.bottom).toEqual(0);
test("start", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("949");
// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

await clearInput(input);
await input.type("500");
await button.click();

await scrollable.waitForElementState("stable");

await expect(await scrollable.innerText()).toContain("500");

await clearInput(input);
await input.type("0");
await button.click();

// Check if scrolled precisely
const firstItem = await getFirstItem(scrollable);
await expect(firstItem.text).toEqual("0");
await expect(firstItem.top).toEqual(0);

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("50\n");
});

test("end", async ({ page }) => {
const scrollable = await page.waitForSelector(scrollableSelector);

// check if start is displayed
await expect((await getFirstItem(scrollable)).text).toEqual("0");

const button = (await page
.getByRole("button", { name: "scroll to index" })
.elementHandle())!;
const input = await page.evaluateHandle(
(el) => el!.previousSibling as HTMLInputElement,
button
);

await clearInput(input);
await input.type("999");
await button.click();

await scrollable.waitForElementState("stable");

// Check if scrolled precisely
const lastItem = await getLastItem(scrollable);
await expect(lastItem.text).toEqual("999");
await expect(lastItem.bottom).toBeLessThanOrEqual(1); // FIXME: may not be 0 in Safari

// Check if unnecessary items are not rendered
await expect(await scrollable.innerText()).not.toContain("949");
});
});
});

Expand Down
35 changes: 19 additions & 16 deletions src/core/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UPDATE_SIZE,
ACTION_MANUAL_SCROLL,
} from "./store";
import { ScrollToIndexAlign } from "./types";
import { debounce, throttle, max, min, timeout } from "./utils";

// Infer scroll state also from wheel events
Expand Down Expand Up @@ -43,7 +44,7 @@ export type Scroller = {
_getActualScrollSize: () => number;
_scrollTo: (offset: number) => void;
_scrollBy: (offset: number) => void;
_scrollToIndex: (index: number) => void;
_scrollToIndex: (index: number, align?: ScrollToIndexAlign) => void;
_fixScrollJump: (jump: ScrollJump) => void;
};

Expand Down Expand Up @@ -73,26 +74,21 @@ export const createScroller = (
return offset;
};

const scrollManually = async (
index: number,
getCurrentOffset: () => number
) => {
const scrollManually = async (getOffset: () => number) => {
if (!rootElement) return;

const getOffset = (): number => {
const getTargetOffset = (): number => {
// Adjust if the offset is over the end, to get correct startIndex.
return min(
getCurrentOffset(),
getActualScrollSize() - store._getViewportSize()
);
return min(getOffset(), getActualScrollSize() - store._getViewportSize());
};

while (true) {
// Sync viewport to scroll destination
// In order to scroll to the correct position, mount the items and measure their sizes before scrolling.
store._update(ACTION_BEFORE_MANUAL_SCROLL, getOffset());
const targetOffset = getTargetOffset();
store._update(ACTION_BEFORE_MANUAL_SCROLL, targetOffset);

if (!store._hasUnmeasuredItemsInRange(index)) {
if (!store._hasUnmeasuredItemsInTargetViewport(targetOffset)) {
break;
}

Expand Down Expand Up @@ -131,14 +127,14 @@ export const createScroller = (
}

// Scroll with the updated value
rootElement[scrollToKey] = normalizeOffset(getOffset());
rootElement[scrollToKey] = normalizeOffset(getTargetOffset());
store._update(ACTION_MANUAL_SCROLL);
};

const scrollTo = (offset: number) => {
offset = max(offset, 0);

scrollManually(store._getItemIndexForScrollTo(offset), () => offset);
scrollManually(() => offset);
};

return {
Expand Down Expand Up @@ -176,10 +172,17 @@ export const createScroller = (
_scrollBy(offset) {
scrollTo(store._getScrollOffset() + offset);
},
_scrollToIndex(index) {
_scrollToIndex(index, align) {
index = min(store._getItemLength() - 1, max(0, index));

scrollManually(index, () => store._getItemOffset(index));
scrollManually(
align === "end"
? () =>
store._getItemOffset(index) +
store._getItemSize(index) -
store._getViewportSize()
: () => store._getItemOffset(index)
);
},
_fixScrollJump: (jump) => {
if (!rootElement) return;
Expand Down
Loading
Loading