Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]

## 0.16.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be under unreleased like for Docs.

Suggested change
## 0.16.2


### Patch Changes

- Enable child node loading and fold/unfold via keyboard without requiring an initial mouse click.

## 0.16.1

### Patch Changes
Expand Down
64 changes: 29 additions & 35 deletions src/components/tree-view/TreeViewItem.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { NodeRendererProps } from "react-arborist";
import { TreeDataItem, TreeViewNodeTypeEnum } from "./types";
import {
PropsWithChildren,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { PropsWithChildren, useEffect, useRef, useState } from "react";
import clsx from "clsx";
import { Spinner } from "../loader/Spinner";
import { Droppable } from "../dnd/Droppable";
Expand Down Expand Up @@ -39,40 +33,40 @@ export const TreeViewItem = <T,>({
const hasLoadedChildren = node.children?.length ?? 0 > 0;

const isLeaf = node.isLeaf || !hasChildren;
const handleClick = useCallback(async () => {
if (isLeaf) {
return;
}

if (hasLoadedChildren || node.data.value.hasLoadedChildren) {
node.toggle();
useEffect(() => {
if (isLeaf || hasLoadedChildren || node.data.value.hasLoadedChildren) {
return;
}

setIsLoading(true);
await context?.treeData.handleLoadChildren(node.data.value.id);
setIsLoading(false);
node.open();
}, [isLeaf, hasLoadedChildren, node, context?.treeData]);
if (node.isOpen) {
setIsLoading(true);
context?.treeData
.handleLoadChildren(node.data.value.id)
.then(() => setIsLoading(false));
}
}, [
node.isOpen,
isLeaf,
hasLoadedChildren,
node.data.value.hasLoadedChildren,
node.data.value.id,
context?.treeData,
]);

const handleOver = useCallback(
(isOver: boolean) => {
if (isOver && !node.isOpen && !node.isDragging) {
timeoutRef.current = setTimeout(() => {
void handleClick();
}, 500);
}
useEffect(() => {
const shouldOpenNode = isOver && !node.isOpen && !node.isDragging;

if (timeoutRef.current && !isOver) {
clearTimeout(timeoutRef.current);
}
},
[handleClick, node.isOpen, node.isDragging]
);
if (shouldOpenNode) {
timeoutRef.current = setTimeout(() => {
node.open();
}, 500);
}

useEffect(() => {
handleOver(isOver);
}, [isOver, handleOver]);
if (timeoutRef.current && !isOver) {
clearTimeout(timeoutRef.current);
}
Comment on lines +66 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better pattern to do like that.

Suggested change
if (timeoutRef.current && !isOver) {
clearTimeout(timeoutRef.current);
}
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};

}, [isOver, node.isOpen, node.isDragging, node]);

if (node.data.value.nodeType === TreeViewNodeTypeEnum.SEPARATOR) {
return <div className="c__tree-view--node__separator" />;
Expand Down Expand Up @@ -131,7 +125,7 @@ export const TreeViewItem = <T,>({
<span
onClick={(e) => {
e.stopPropagation();
void handleClick();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove totally the function handleClick, inside handleOver you can replace void handleClick(); by node.open();. We don't need to memoized handleOver neither, it can directly go inside the useEffect under I think.

Copy link
Collaborator Author

@Ovgodd Ovgodd Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you’re right.

In this case, useCallback isn’t needed.
We can put the logic directly in the useEffect.

Another thing : React 19’s compiler will help with memoization in many cases, so we don't need memoize function for many cases,

good catch

node.toggle();
}}
className="c__tree-view--node__arrow material-icons"
>
Expand Down
Loading