Skip to content

Commit 62d577d

Browse files
committed
fix: grid SSR works with initial topmost item index
1 parent 78aa3dd commit 62d577d

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react'
2+
import { VirtuosoGrid } from '../src'
3+
4+
export function Example() {
5+
return <VirtuosoGrid id="root" initialTopMostItemIndex={10} totalCount={20000} initialItemCount={30} />
6+
}

examples/grid-initial-item-count.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react'
2-
import { VirtuosoGrid, GridComponents, ListRange } from '../src'
2+
import { VirtuosoGrid, GridComponents, ListRange, GridStateSnapshot } from '../src'
33
import { debounce } from 'lodash'
4-
import { GridStateSnapshot } from '../src/gridSystem'
54
import { createHashRouter, Link, RouterProvider, useParams, useSearchParams } from 'react-router-dom'
65

76
function fetchData(page: number) {

src/gridSystem.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ export const gridSystem = /*#__PURE__*/ u.system(
106106
u.filter(([_, location]) => !!location)
107107
),
108108
() => {
109-
// console.log('block rendering')
110109
u.publish(scrolledToInitialItem, false)
111-
// topmost item index takes precedence over initial item count
112-
u.publish(initialItemCount, 0)
113110
}
114111
)
115112

@@ -235,8 +232,7 @@ export const gridSystem = /*#__PURE__*/ u.system(
235232

236233
if (itemWidth === 0) {
237234
const startIndex = getInitialTopMostItemIndexNumber(initialTopMostItemIndex, totalCount)
238-
// if the initial item count is set, we don't render the items from the initial item count.
239-
const endIndex = startIndex === 0 ? Math.max(initialItemCount - 1, 0) : startIndex
235+
const endIndex = Math.max(startIndex + initialItemCount - 1, 0)
240236
return buildProbeGridState(buildItems(startIndex, endIndex, data))
241237
}
242238

test/ssr.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ describe('SSR List', () => {
2525
})
2626

2727
it('renders 30 grid items with offset', () => {
28+
const html = ReactDOMServer.renderToString(
29+
<VirtuosoGrid id="root" initialTopMostItemIndex={10} totalCount={20000} initialItemCount={30} />
30+
)
31+
const { document } = new JSDOM(html).window
32+
expect(document.querySelector('#root > div > div')!.childElementCount).toEqual(30)
33+
expect(document.querySelector('#root > div > div > div')?.innerHTML).toEqual('Item 10')
34+
})
35+
36+
it('renders 30 items with offset', () => {
2837
const html = ReactDOMServer.renderToString(
2938
<Virtuoso
3039
id="root"

0 commit comments

Comments
 (0)