Skip to content

Commit a4d2c3a

Browse files
committed
Use better size for adjacent area
1 parent 94c7285 commit a4d2c3a

File tree

3 files changed

+346
-44
lines changed

3 files changed

+346
-44
lines changed

Libraries/Lists/VirtualizedList.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,11 @@ class VirtualizedList extends React.PureComponent<Props, State> {
771771
// Keep the items around the last focused rendered, to allow for keyboard
772772
// navigation
773773
if (lastFocusedItem) {
774-
const first = Math.max(0, lastFocusedItem - 1);
775-
const last = Math.min(itemCount - 1, lastFocusedItem + 1);
774+
const bufferAroundItem = Math.round(
775+
initialNumToRenderOrDefault(props.initialNumToRender) / 2,
776+
);
777+
const first = Math.max(0, lastFocusedItem - bufferAroundItem);
778+
const last = Math.min(itemCount - 1, lastFocusedItem + bufferAroundItem);
776779
renderMask.addCells({first, last});
777780
}
778781

Libraries/Lists/__tests__/VirtualizedList-test.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,14 +1446,14 @@ it('renders windowSize derived region at bottom', () => {
14461446
});
14471447

14481448
it('keeps last focused item rendered', () => {
1449-
const items = generateItems(20);
1449+
const items = generateItems(40);
14501450
const ITEM_HEIGHT = 10;
14511451

14521452
let component;
14531453
ReactTestRenderer.act(() => {
14541454
component = ReactTestRenderer.create(
14551455
<VirtualizedList
1456-
initialNumToRender={1}
1456+
initialNumToRender={5}
14571457
windowSize={1}
14581458
{...baseItemProps(items)}
14591459
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
@@ -1464,39 +1464,45 @@ it('keeps last focused item rendered', () => {
14641464
ReactTestRenderer.act(() => {
14651465
simulateLayout(component, {
14661466
viewport: {width: 10, height: 50},
1467-
content: {width: 10, height: 200},
1467+
content: {width: 10, height: 400},
14681468
});
14691469

14701470
performAllBatches();
14711471
});
14721472

14731473
ReactTestRenderer.act(() => {
1474-
component.getInstance()._onCellFocusCapture(3);
1474+
simulateScroll(component, {x: 0, y: 100});
1475+
performAllBatches();
14751476
});
14761477

14771478
ReactTestRenderer.act(() => {
1478-
simulateScroll(component, {x: 0, y: 150});
1479+
component.getInstance()._onCellFocusCapture(10);
1480+
});
1481+
1482+
ReactTestRenderer.act(() => {
1483+
simulateScroll(component, {x: 0, y: 350});
14791484
performAllBatches();
14801485
});
14811486

1482-
// Cells 1-4 should remain rendered after scrolling to the bottom of the list
1487+
// Cell 10, and `initialNumToRender` / 2 cells on each side, should remain
1488+
// rendered
14831489
expect(component).toMatchSnapshot();
14841490

14851491
ReactTestRenderer.act(() => {
1486-
component.getInstance()._onCellFocusCapture(17);
1492+
component.getInstance()._onCellFocusCapture(37);
14871493
});
14881494

1489-
// Cells 2-4 should no longer be rendered after focus is moved to the end of
1490-
// the list
1495+
// Cells around 10 should no longer be rendered after focus is moved to the
1496+
// end of the list
14911497
expect(component).toMatchSnapshot();
14921498

14931499
ReactTestRenderer.act(() => {
14941500
simulateScroll(component, {x: 0, y: 0});
14951501
performAllBatches();
14961502
});
14971503

1498-
// Cells 16-18 should remain rendered after scrolling back to the top of the
1499-
// list
1504+
// Cell 37, and `initialNumToRender` / 2 cells on each side, should remain
1505+
// rendered
15001506
expect(component).toMatchSnapshot();
15011507
});
15021508

0 commit comments

Comments
 (0)