Skip to content

Commit

Permalink
feat(useVirtual): add resetScroll option for filtering/sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jun 18, 2021
1 parent acb47e6 commit 05ea7ff
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-dryers-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": minor
---

feat(useVirtual): add `resetScroll` option for filtering/sorting
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ The size of an item (default = 50). When working with **dynamic size**, it will
The layout/orientation of the list (default = false). When `true` means left/right scrolling, so the hook will use `width` as the [item size](#itemsize) and use the `left` as the [start](#items) position.
#### resetScroll
`boolean`
It's used to tell the hook to reset the scroll position when the [itemCount](#itemcount-required) is changed (default = false). It's useful for filtering/sorting.
#### overscanCount
`number`
Expand Down
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"files": [
{
"path": "dist/index.umd.production.min.js",
"maxSize": "3 kB"
"maxSize": "3.5 kB"
}
]
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface Options {
ssrItemCount?: SsrItemCount;
itemSize?: ItemSize;
horizontal?: boolean;
resetScroll?: boolean;
overscanCount?: number;
useIsScrolling?: UseIsScrolling;
stickyIndices?: number[];
Expand Down
1 change: 1 addition & 0 deletions src/types/react-cool-virtual.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ declare module "react-cool-virtual" {
ssrItemCount?: number | [number, number];
itemSize?: ItemSize;
horizontal?: boolean;
resetScroll?: boolean;
overscanCount?: number;
useIsScrolling?: UseIsScrolling;
stickyIndices?: number[];
Expand Down
9 changes: 6 additions & 3 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default <
ssrItemCount,
itemSize = 50,
horizontal,
resetScroll,
overscanCount = 1,
useIsScrolling,
stickyIndices,
Expand Down Expand Up @@ -472,13 +473,15 @@ export default <
const { width, height } = outerRectRef.current;
const isSameWidth = width === rect.width;
const isSameSize = isSameWidth && height === rect.height;
const prevTotalSize =
msDataRef.current[msDataRef.current.length - 1]?.end;
const prevItemCount = msDataRef.current.length;
const prevTotalSize = msDataRef.current[prevItemCount - 1]?.end;

outerRectRef.current = rect;
measureItems(hasDynamicSizeRef.current);
handleScroll(scrollOffsetRef.current);

if (resetScroll && itemCount !== prevItemCount) scrollTo(0);

if (!hasDynamicSizeRef.current && !isSameWidth) {
const totalSize = msDataRef.current[msDataRef.current.length - 1]?.end;
const ratio = totalSize / prevTotalSize || 1;
Expand All @@ -491,7 +494,7 @@ export default <

isMountedRef.current = true;
},
[itemCount, handleScroll, measureItems, onResizeRef, scrollTo]
[itemCount, resetScroll, handleScroll, measureItems, onResizeRef, scrollTo]
);

useIsoLayoutEffect(() => {
Expand Down

0 comments on commit 05ea7ff

Please sign in to comment.