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
49 changes: 49 additions & 0 deletions src/__tests__/initialScrollIndex2307.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import { Text } from "react-native";
import "@quilted/react-testing/matchers";
import { render } from "@quilted/react-testing";

import { FlashList } from "..";

// Items are 300px tall, which is above the 200px default the layout manager
// seeds unmeasured items with. That gap is what issue #2307 depends on.
jest.mock("../recyclerview/utils/measureLayout", () => {
const originalModule = jest.requireActual(
"../recyclerview/utils/measureLayout"
);
return {
...originalModule,
measureParentSize: jest
.fn()
.mockImplementation(() => ({ width: 400, height: 900 })),
measureFirstChildLayout: jest
.fn()
.mockImplementation(() => ({ x: 0, y: 0, width: 400, height: 900 })),
measureItemLayout: jest
.fn()
.mockImplementation(() => ({ x: 0, y: 0, width: 400, height: 300 })),
};
});

describe("initialScrollIndex with items taller than the default estimate", () => {
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
});

it("opens on the requested item rather than one far past it", () => {
const result = render(
<FlashList
data={Array.from({ length: 600 }, (_, index) => index)}
keyExtractor={(item) => String(item)}
initialScrollIndex={250}
renderItem={({ item }) => <Text>{item}</Text>}
overrideProps={{ initialDrawBatchSize: 1 }}
drawDistance={0}
/>
);

expect(result).toContainReactComponent(Text, { children: 250 });
expect(result).not.toContainReactComponent(Text, { children: 333 });
});
});
10 changes: 9 additions & 1 deletion src/recyclerview/RecyclerViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,15 @@ export class RecyclerViewManager<T> {
// re-estimate unmeasured items with an updated average height, changing
// the target item's position. Reading before recompute would capture a
// stale offset, causing the wrong items to be rendered.
this.layoutManager.recomputeLayouts(0, initialScrollIndex);
//
// This has to run to the end of the list rather than stopping at
// initialScrollIndex. Items are positioned from their predecessor, so
// recomputing only the prefix moves the target item without moving the
// items after it, and the layout array is left out of order at that
// boundary. getVisibleLayouts binary searches that array and documents
// that it assumes it is sorted, so once it is not, the search can return
// an item nowhere near the one requested.
this.layoutManager.recomputeLayouts(0, this.getDataLength() - 1);
const initialItemLayout =
this.layoutManager.getLayout(initialScrollIndex);
const initialItemOffset = this.propsRef.horizontal
Expand Down
Loading