-
Notifications
You must be signed in to change notification settings - Fork 4
✨(tree-view) load children on keyboard navigation and simplify toggle #115
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; | ||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better pattern to do like that.
Suggested change
|
||||||||||||||||||
}, [isOver, node.isOpen, node.isDragging, node]); | ||||||||||||||||||
|
||||||||||||||||||
if (node.data.value.nodeType === TreeViewNodeTypeEnum.SEPARATOR) { | ||||||||||||||||||
return <div className="c__tree-view--node__separator" />; | ||||||||||||||||||
|
@@ -131,7 +125,7 @@ export const TreeViewItem = <T,>({ | |||||||||||||||||
<span | ||||||||||||||||||
onClick={(e) => { | ||||||||||||||||||
e.stopPropagation(); | ||||||||||||||||||
void handleClick(); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove totally the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you’re right. In this case, useCallback isn’t needed. 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" | ||||||||||||||||||
> | ||||||||||||||||||
|
There was a problem hiding this comment.
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
.