Skip to content

Commit

Permalink
fix: initialItemCount no longer blocks data change
Browse files Browse the repository at this point in the history
Fixes #801
  • Loading branch information
petyosi committed May 1, 2023
1 parent 315be28 commit ec97e6f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
31 changes: 31 additions & 0 deletions examples/grid-initial-item-count.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react'
import { VirtuosoGrid } from '../src'

function generateItems(length: number) {
return Array.from({ length }, (_, index) => `My Item ${index}`)
}

const itemContent = (_: number, data: string) => {
return <div style={{ height: 30 }}>{data}</div>
}

export function Example() {
const [data, setData] = React.useState(() => generateItems(100))

const onEndReached = () => {
setData((prevData) => {
return generateItems(prevData.length + 100)
})
}

return (
<VirtuosoGrid
useWindowScroll
initialItemCount={100}
endReached={onEndReached}
data={data}
itemContent={itemContent}
style={{ height: 300 }}
/>
)
}
22 changes: 18 additions & 4 deletions src/gridSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ export const gridSystem = /*#__PURE__*/ u.system(

u.connect(
u.pipe(
u.combineLatest(didMount, initialItemCount, data),
u.filter(([, count]) => count !== 0),
didMount,
u.withLatestFrom(initialItemCount, data),
u.filter(([didMount, count]) => didMount && count !== 0),
u.map(([, count, data]) => {
return {
items: buildItems(0, count - 1, data),
Expand Down Expand Up @@ -188,12 +189,25 @@ export const gridSystem = /*#__PURE__*/ u.system(
listBoundary
)

const hasScrolled = u.statefulStream(false)

u.connect(
u.pipe(
scrollTop,
u.withLatestFrom(hasScrolled),
u.map(([scrollTop, hasScrolled]) => {
return hasScrolled || scrollTop !== 0
})
),
hasScrolled
)

const endReached = u.streamFromEmitter(
u.pipe(
u.duc(gridState),
u.filter(({ items }) => items.length > 0),
u.withLatestFrom(totalCount),
u.filter(([{ items }, totalCount]) => items[items.length - 1].index === totalCount - 1),
u.withLatestFrom(totalCount, hasScrolled),
u.filter(([{ items }, totalCount, hasScrolled]) => hasScrolled && items[items.length - 1].index === totalCount - 1),
u.map(([, totalCount]) => totalCount - 1),
u.distinctUntilChanged()
)
Expand Down

0 comments on commit ec97e6f

Please sign in to comment.