Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: call calculateRange when options change #403

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-virtual/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function List({
observeElementRect: (_, cb) => {
cb({ height, width })
},
measureElement: () => itemSize ?? 0,
measureElement: React.useCallback(() => itemSize ?? 0, [itemSize]),
rangeExtractor,
})

Expand Down
65 changes: 50 additions & 15 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const observeElementRect = (
const observer = new ResizeObserver((entries) => {
cb({
width: entries[0]?.contentRect.width as number,
height: entries[0]?.contentRect.height as number
height: entries[0]?.contentRect.height as number,
})
})

Expand Down Expand Up @@ -270,7 +270,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
number,
(measurableItem: TItemElement | null) => void
> = {}
private range: { startIndex: number; endIndex: number } = {
range: { startIndex: number; endIndex: number } = {
startIndex: 0,
endIndex: 0,
}
Expand All @@ -288,6 +288,17 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
if (typeof value === 'undefined') delete (opts as any)[key]
})

const cloneOptions = (
opt: VirtualizerOptions<TScrollElement, TItemElement>,
) => ({
...opt,
initialRect: {
...opt.initialRect,
},
})

const prevOptions = this.options ? cloneOptions(this.options) : undefined

this.options = {
debug: false,
initialOffset: 0,
Expand All @@ -305,6 +316,27 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
initialRect: { width: 0, height: 0 },
...opts,
}

if (!prevOptions) {
return
} else if (
prevOptions.count !== this.options.count ||
prevOptions.horizontal !== this.options.horizontal ||
prevOptions.initialOffset !== this.options.initialOffset ||
prevOptions.initialRect.height !== this.options.initialRect.height ||
prevOptions.initialRect.width !== this.options.initialRect.width ||
prevOptions.measureElement !== this.options.measureElement ||
prevOptions.overscan !== this.options.overscan ||
prevOptions.paddingEnd !== this.options.paddingEnd ||
prevOptions.paddingEnd !== this.options.paddingEnd ||
prevOptions.paddingStart !== this.options.paddingStart ||
prevOptions.paddingStart !== this.options.paddingStart ||
prevOptions.rangeExtractor !== this.options.rangeExtractor ||
prevOptions.scrollPaddingEnd !== this.options.scrollPaddingEnd ||
prevOptions.scrollPaddingStart !== this.options.scrollPaddingStart
) {
this.calculateRange()
}
}

private notify = () => {
Expand Down Expand Up @@ -391,7 +423,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
},
)

private calculateRange = memo(
calculateRange = memo(
() => [this.getMeasurements(), this.getSize(), this.scrollOffset],
(measurements, outerSize, scrollOffset) => {
const range = calculateRange({
Expand Down Expand Up @@ -503,7 +535,10 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {

scrollToOffset = (
toOffset: number,
{ align = 'start', smoothScroll = this.options.enableSmoothScroll }: ScrollToOffsetOptions = {},
{
align = 'start',
smoothScroll = this.options.enableSmoothScroll,
}: ScrollToOffsetOptions = {},
) => {
const offset = this.scrollOffset
const size = this.getSize()
Expand All @@ -519,17 +554,21 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
}

if (align === 'start') {
this._scrollToOffset(toOffset, smoothScroll)
} else if (align === 'end') {
this._scrollToOffset(toOffset - size, smoothScroll)
} else if (align === 'center') {
this._scrollToOffset(toOffset - size / 2, smoothScroll)
this._scrollToOffset(toOffset, smoothScroll)
} else if (align === 'end') {
this._scrollToOffset(toOffset - size, smoothScroll)
} else if (align === 'center') {
this._scrollToOffset(toOffset - size / 2, smoothScroll)
}
}

scrollToIndex = (
index: number,
{ align = 'auto', smoothScroll = this.options.enableSmoothScroll, ...rest }: ScrollToIndexOptions = {},
{
align = 'auto',
smoothScroll = this.options.enableSmoothScroll,
...rest
}: ScrollToIndexOptions = {},
) => {
const measurements = this.getMeasurements()
const offset = this.scrollOffset
Expand Down Expand Up @@ -571,11 +610,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
clearTimeout(this.scrollCheckFrame)

this.destinationOffset = offset
this.options.scrollToFn(
offset,
canSmooth,
this,
)
this.options.scrollToFn(offset, canSmooth, this)

let scrollCheckFrame: ReturnType<typeof setTimeout>

Expand Down