Skip to content

Commit

Permalink
fix(): make defaultItemHeight work with initialTopMostItemIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Aug 10, 2021
1 parent eb64bed commit 125aec4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
22 changes: 22 additions & 0 deletions e2e/prepend-last-tall-item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react'
import { Virtuoso } from '../src/'

function itemContent(index: number) {
const height = index === 1099 ? 120 : 30
const backgroundColor = index === 1099 ? 'red' : 'transparent'
return <div style={{ height, backgroundColor }}>Item {index}</div>
}

export default function App() {
const [fii] = React.useState(1000)
return (
<Virtuoso
totalCount={100}
defaultItemHeight={30}
firstItemIndex={fii}
initialTopMostItemIndex={99}
itemContent={itemContent}
style={{ height: 800 }}
/>
)
}
4 changes: 3 additions & 1 deletion src/initialTopMostItemIndexSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const initialTopMostItemIndexSystem = u.system(
u.publish(scrolledToInitialItem, true)
})

u.publish(scrollToIndex, initialTopMostItemIndex)
setTimeout(() => {
u.publish(scrollToIndex, initialTopMostItemIndex)
})
}
)

Expand Down
55 changes: 30 additions & 25 deletions test/listSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('list engine', () => {
})

describe('initial index', () => {
it('starts from a specified location', () => {
it('starts from a specified location', (done) => {
const INITIAL_INDEX = 300
const SIZE = 30
const { propsReady, initialTopMostItemIndex, listState, scrollTop, scrollTo, viewportHeight, totalCount, sizeRanges } = init(
Expand All @@ -106,14 +106,17 @@ describe('list engine', () => {

expect(getValue(listState).items).toHaveLength(0)

expect(sub).toHaveBeenCalledWith({
top: INITIAL_INDEX * SIZE,
behavior: 'auto',
})
setTimeout(() => {
expect(sub).toHaveBeenCalledWith({
top: INITIAL_INDEX * SIZE,
behavior: 'auto',
})

// the UI responds by publishing back through the scrollTop stream
publish(scrollTop, INITIAL_INDEX * SIZE)
expect(getValue(listState).items).toHaveLength(7)
// the UI responds by publishing back through the scrollTop stream
publish(scrollTop, INITIAL_INDEX * SIZE)
expect(getValue(listState).items).toHaveLength(7)
done()
})
})
})

Expand Down Expand Up @@ -230,27 +233,29 @@ describe('list engine', () => {

expect(getValue(listState).items).toHaveLength(0)

expect(sub).toHaveBeenCalledWith({
top: INITIAL_INDEX * SIZE,
behavior: 'auto',
})

setTimeout(() => {
publish(scrollTop, INITIAL_INDEX * SIZE)
expect(sub).toHaveBeenCalledWith({
top: INITIAL_INDEX * SIZE,
behavior: 'auto',
})

publish(scrollTop, INITIAL_INDEX * SIZE - 2)
setTimeout(() => {
publish(scrollTop, INITIAL_INDEX * SIZE)

publish(sizeRanges, [
{
startIndex: INITIAL_INDEX - 1,
endIndex: INITIAL_INDEX - 1,
size: SIZE + 40,
},
])
publish(scrollTop, INITIAL_INDEX * SIZE - 2)

expect(scrollBySub).toHaveBeenCalledWith(-40)
done()
}, 2500)
publish(sizeRanges, [
{
startIndex: INITIAL_INDEX - 1,
endIndex: INITIAL_INDEX - 1,
size: SIZE + 40,
},
])

expect(scrollBySub).toHaveBeenCalledWith(-40)
done()
}, 2500)
})
})
})

Expand Down

0 comments on commit 125aec4

Please sign in to comment.