Skip to content

Commit fca20fa

Browse files
committed
fix: handle undefined initial topmost index
Closes #1451
1 parent 9c8fcd0 commit fca20fa

10 files changed

Lines changed: 88 additions & 11 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-virtuoso": patch
3+
---
4+
5+
Fix explicit `undefined` initial top-most item index values crashing when empty data updates to an object-form initial index.

‎packages/react-virtuoso/src/component-interfaces/TableVirtuoso.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export interface TableVirtuosoProps<Data, Context> extends Omit<VirtuosoProps<Da
232232
* Set to a value between 0 and totalCount - 1 to make the list start scrolled to that item.
233233
* Pass in an object to achieve additional effects similar to `scrollToIndex`.
234234
*/
235-
initialTopMostItemIndex?: IndexLocationWithAlign | number
235+
initialTopMostItemIndex?: IndexLocationWithAlign | number | undefined
236236

237237
/**
238238
* Called when the list starts/stops scrolling.

‎packages/react-virtuoso/src/component-interfaces/Virtuoso.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export interface VirtuosoProps<Data, Context> extends ListRootProps {
345345
* Set to a value between 0 and totalCount - 1 to make the list start scrolled to that item.
346346
* Pass in an object to achieve additional effects similar to `scrollToIndex`.
347347
*/
348-
initialTopMostItemIndex?: IndexLocationWithAlign | number
348+
initialTopMostItemIndex?: IndexLocationWithAlign | number | undefined
349349

350350
/**
351351
* Called when the list starts/stops scrolling.

‎packages/react-virtuoso/src/component-interfaces/VirtuosoGrid.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export interface VirtuosoGridProps<Data, Context = unknown> extends GridRootProp
152152
* Set to a value between 0 and totalCount - 1 to make the grid start scrolled to that item.
153153
* Pass in an object to achieve additional effects similar to `scrollToIndex`.
154154
*/
155-
initialTopMostItemIndex?: GridIndexLocation
155+
initialTopMostItemIndex?: GridIndexLocation | undefined
156156

157157
/**
158158
* Called when the list starts/stops scrolling.

‎packages/react-virtuoso/src/gridSystem.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { rangeComparator, tupleComparator } from './comparators'
22
import { domIOSystem } from './domIOSystem'
3-
import { getInitialTopMostItemIndexNumber } from './initialTopMostItemIndexSystem'
3+
import { getInitialTopMostItemIndexNumber, initialTopMostItemIndexIsZero } from './initialTopMostItemIndexSystem'
44
import { loggerSystem } from './loggerSystem'
55
import { propsReadySystem } from './propsReadySystem'
66
import { scrollSeekSystem } from './scrollSeekSystem'
@@ -95,7 +95,7 @@ export const gridSystem = /*#__PURE__*/ u.system(
9595
const stateChanged = u.stream<GridStateSnapshot>()
9696
const restoreStateFrom = u.stream<GridStateSnapshot | null | undefined>()
9797
const stateRestoreInProgress = u.statefulStream(false)
98-
const initialTopMostItemIndex = u.statefulStream<GridIndexLocation>(0)
98+
const initialTopMostItemIndex = u.statefulStream<GridIndexLocation | undefined>(0)
9999
const scrolledToInitialItem = u.statefulStream(true)
100100
const scrollScheduled = u.statefulStream(false)
101101
const horizontalDirection = u.statefulStream(false)
@@ -104,7 +104,7 @@ export const gridSystem = /*#__PURE__*/ u.system(
104104
u.pipe(
105105
didMount,
106106
u.withLatestFrom(initialTopMostItemIndex),
107-
u.filter(([_, location]) => location !== 0)
107+
u.filter(([_, location]) => !initialTopMostItemIndexIsZero(location))
108108
),
109109
() => {
110110
u.publish(scrolledToInitialItem, false)
@@ -119,6 +119,11 @@ export const gridSystem = /*#__PURE__*/ u.system(
119119
})
120120
),
121121
([, , , , initialTopMostItemIndex]) => {
122+
if (initialTopMostItemIndex === undefined) {
123+
u.publish(scrolledToInitialItem, true)
124+
return
125+
}
126+
122127
u.publish(scrollScheduled, true)
123128

124129
skipFrames(1, () => {

‎packages/react-virtuoso/src/initialTopMostItemIndexSystem.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@ import { skipFrames } from './utils/skipFrames'
88

99
import type { FlatIndexLocationWithAlign } from './interfaces'
1010

11-
export function getInitialTopMostItemIndexNumber(location: FlatIndexLocationWithAlign | number, totalCount: number): number {
11+
export type InitialTopMostItemIndexLocation = FlatIndexLocationWithAlign | number | undefined
12+
13+
export function getInitialTopMostItemIndexNumber(location: InitialTopMostItemIndexLocation, totalCount: number): number {
14+
if (location === undefined) {
15+
return 0
16+
}
17+
1218
const lastIndex = totalCount - 1
1319
const index = typeof location === 'number' ? location : location.index === 'LAST' ? lastIndex : location.index
1420
return Math.max(0, Math.min(index, lastIndex))
1521
}
1622

17-
export function initialTopMostItemIndexIsZero(location: FlatIndexLocationWithAlign | number): boolean {
23+
export function initialTopMostItemIndexIsZero(location: InitialTopMostItemIndexLocation): boolean {
24+
if (location === undefined) {
25+
return true
26+
}
27+
1828
if (typeof location === 'number') {
1929
return location === 0
2030
}
@@ -29,7 +39,7 @@ export function initialTopMostItemIndexIsZero(location: FlatIndexLocationWithAli
2939
export const initialTopMostItemIndexSystem = u.system(
3040
([{ defaultItemSize, listRefresh, sizes }, { scrollTop }, { scrollTargetReached, scrollToIndex }, { didMount }]) => {
3141
const scrolledToInitialItem = u.statefulStream(true)
32-
const initialTopMostItemIndex = u.statefulStream<FlatIndexLocationWithAlign | number>(0)
42+
const initialTopMostItemIndex = u.statefulStream<InitialTopMostItemIndexLocation>(0)
3343
const initialItemFinalLocationReached = u.statefulStream(true)
3444

3545
u.connect(
@@ -61,6 +71,12 @@ export const initialTopMostItemIndexSystem = u.system(
6171
u.withLatestFrom(initialTopMostItemIndex)
6272
),
6373
([, initialTopMostItemIndex]) => {
74+
if (initialTopMostItemIndex === undefined) {
75+
u.publish(scrolledToInitialItem, true)
76+
u.publish(initialItemFinalLocationReached, true)
77+
return
78+
}
79+
6480
u.handleNext(scrollTargetReached, () => {
6581
u.publish(initialItemFinalLocationReached, true)
6682
})

‎packages/react-virtuoso/src/listStateSystem.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { stateFlagsSystem } from './stateFlagsSystem'
1111
import * as u from './urx'
1212

1313
import type { Range } from './AATree'
14-
import type { FlatIndexLocationWithAlign, Item, ListItem, ListRange } from './interfaces'
14+
import type { InitialTopMostItemIndexLocation } from './initialTopMostItemIndexSystem'
15+
import type { Item, ListItem, ListRange } from './interfaces'
1516
import type { Data, SizeState } from './sizeSystem'
1617

1718
export type ListItems = ListItem<unknown>[]
@@ -92,7 +93,7 @@ function buildListState(
9293

9394
export function buildListStateFromItemCount(
9495
itemCount: number,
95-
initialTopMostItemIndex: FlatIndexLocationWithAlign | number,
96+
initialTopMostItemIndex: InitialTopMostItemIndexLocation,
9697
sizes: SizeState,
9798
firstItemIndex: number,
9899
gap: number,

‎packages/react-virtuoso/test/Virtuoso.test.tsx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,48 @@ describe('Virtuoso', () => {
146146
expect(listParent.firstElementChild.textContent).toBe('Item 0')
147147
})
148148

149+
it('updates when data and an object-form initial index are propagated after an undefined initial index', () => {
150+
const Case = () => {
151+
const [data, setData] = React.useState([] as { name: string }[])
152+
const [initialTopMostItemIndex, setInitialTopMostItemIndex] = React.useState<undefined | { align: 'start'; index: number }>(undefined)
153+
154+
return (
155+
<>
156+
<Virtuoso
157+
data={data}
158+
initialTopMostItemIndex={initialTopMostItemIndex}
159+
itemContent={(_: number, item: { name: string }) => item.name}
160+
/>
161+
<button
162+
onClick={() => {
163+
setData([{ name: 'Item 0' }])
164+
setInitialTopMostItemIndex({ align: 'start', index: 0 })
165+
}}
166+
>
167+
Set Data
168+
</button>
169+
</>
170+
)
171+
}
172+
173+
act(() => {
174+
ReactDOM.createRoot(container).render(<Case />)
175+
})
176+
177+
const scroller = container.firstElementChild as any
178+
const viewport = scroller.firstElementChild
179+
const listParent = viewport.firstElementChild
180+
181+
act(() => {
182+
scroller.triggerScroll({ scrollHeight: 700, scrollTop: 0, viewportHeight: 200 })
183+
viewport.triggerResize({ getBoundingClientRect: () => ({ height: 100 }) })
184+
listParent.triggerChangedChildSizes([{ endIndex: 0, size: 10, startIndex: 0 }])
185+
container.querySelector('button')!.dispatchEvent(new MouseEvent('click', { bubbles: true }))
186+
})
187+
188+
expect(listParent.firstElementChild.textContent).toBe('Item 0')
189+
})
190+
149191
it('updates when data is propagated (same length)', () => {
150192
const Case = () => {
151193
const [data, setData] = React.useState([{ name: 'Item 0' }] as any[])

‎packages/react-virtuoso/test/initialTopMostItemIndexIsZero.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { describe, expect, it } from 'vitest'
33
import { initialTopMostItemIndexIsZero } from '../src/initialTopMostItemIndexSystem'
44

55
describe('initialTopMostItemIndexIsZero', () => {
6+
it('treats undefined as zero', () => {
7+
expect(initialTopMostItemIndexIsZero(undefined)).toBe(true)
8+
})
9+
610
it('treats the numeric 0 as zero', () => {
711
expect(initialTopMostItemIndexIsZero(0)).toBe(true)
812
})

‎packages/react-virtuoso/test/initialTopMostItemIndexSystem.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('getInitialTopMostItemIndexNumber', () => {
66
const totalCount = 50
77
const lastIndex = totalCount - 1
88

9+
it('returns zero for an undefined location', () => {
10+
expect(getInitialTopMostItemIndexNumber(undefined, totalCount)).toBe(0)
11+
})
12+
913
it('clamps an out-of-range numeric index down to the last index', () => {
1014
expect(getInitialTopMostItemIndexNumber(200, totalCount)).toBe(lastIndex)
1115
})

0 commit comments

Comments
 (0)