Skip to content

Commit

Permalink
[DataGrid] Fix scrollbar position not being updated after `scrollToIn…
Browse files Browse the repository at this point in the history
…dexes` (#14877)
  • Loading branch information
arminmeh authored Oct 8, 2024
1 parent 309199a commit 018dc1d
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const GridVirtualScrollbar = React.forwardRef<HTMLDivElement, GridVirtualScrollb
function GridVirtualScrollbar(props, ref) {
const apiRef = useGridPrivateApiContext();
const rootProps = useGridRootProps();
const isLocked = React.useRef(false);
const lastPosition = React.useRef(0);
const lastPositionScroller = React.useRef(0);
const lastPositionScrollbar = React.useRef(0);
const scrollbarRef = React.useRef<HTMLDivElement>(null);
const contentRef = React.useRef<HTMLDivElement>(null);
const classes = useUtilityClasses(rootProps, props.position);
Expand All @@ -96,34 +96,28 @@ const GridVirtualScrollbar = React.forwardRef<HTMLDivElement, GridVirtualScrollb
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current!;

if (scroller[propertyScroll] === lastPosition.current) {
if (scroller[propertyScroll] === lastPositionScroller.current) {
return;
}

if (isLocked.current) {
isLocked.current = false;
return;
}
isLocked.current = true;

const value = scroller[propertyScroll] / contentSize;
scrollbar[propertyScroll] = value * scrollbarInnerSize;

lastPosition.current = scroller[propertyScroll];
lastPositionScrollbar.current = scrollbar[propertyScroll];
});

const onScrollbarScroll = useEventCallback(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current!;

if (isLocked.current) {
isLocked.current = false;
if (scrollbar[propertyScroll] === lastPositionScrollbar.current) {
return;
}
isLocked.current = true;

const value = scrollbar[propertyScroll] / scrollbarInnerSize;
scroller[propertyScroll] = value * contentSize;

lastPositionScroller.current = scroller[propertyScroll];
});

useOnMount(() => {
Expand Down

0 comments on commit 018dc1d

Please sign in to comment.