Only scroll to the selected node once

This commit is contained in:
Eric Allam
2022-03-15 10:18:14 +00:00
parent 0cd7eacaa8
commit 084a93e03a
2 changed files with 7 additions and 4 deletions
+6 -3
View File
@@ -1,5 +1,5 @@
import { ChevronDownIcon, ChevronRightIcon } from "@heroicons/react/outline";
import { useEffect } from "react";
import { useEffect, useRef } from "react";
import {
useJsonColumnViewAPI,
useJsonColumnViewState,
@@ -15,11 +15,14 @@ export function JsonTreeView() {
const { tree, parentRef } = useJsonTreeViewContext();
const scrolledToNodeRef = useRef(false);
useEffect(() => {
if (selectedNodeId) {
if (!scrolledToNodeRef.current && selectedNodeId) {
tree.scrollToNode(selectedNodeId);
scrolledToNodeRef.current = true;
}
}, [selectedNodeId]);
}, [selectedNodeId, scrolledToNodeRef]);
return (
<div
+1 -1
View File
@@ -160,7 +160,7 @@ export function useVirtualTree<T extends { id: string; children?: T[] }, R>(
const itemIndex = state.items.findIndex((item) => item.id === id);
if (itemIndex !== -1) {
rowVirtualizer.scrollToIndex(itemIndex, { align: "auto" });
rowVirtualizer.scrollToIndex(itemIndex, { align: "center" });
}
},
[state.items, rowVirtualizer]