Skip to content

Commit

Permalink
refactor: make small changes in TreeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
orlinmalkja committed Feb 7, 2025
1 parent 6fd7716 commit 4ccf7f6
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/components/tree/TreeNode.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useRef, MouseEvent, useState } from 'react'
import { FC, MouseEvent, useState } from 'react'

import { useTree } from '@/contexts/TreeContext'

Expand All @@ -13,21 +13,19 @@ const TreeNode: FC<TreeNodeProps> = ({ node }) => {
const [isExpanded, setIsExpanded] = useState(false)
const { onSelect, getChildren } = useTree()

const itemRef = useRef(null)

async function handleNodeClick(e: MouseEvent<HTMLElement>) {
e.preventDefault()

if (!hasChildren) {
node.children = [...await getChildren(node)]
setHasChildren(true)
if (node.children.length > 0) setHasChildren(true)
}

if (node.children.length > 0) {
toggleExpand()
return
}

onSelect(node, e.target)
}

Expand All @@ -47,7 +45,7 @@ const TreeNode: FC<TreeNodeProps> = ({ node }) => {
))}
</div>

return <div ref={itemRef}
return <div
className="t-mb-1 t-py-[2px] t-px-2 hover:t-bg-gray-100 hover:t-cursor-pointer hover:t-rounded-md"
onClick={(e) => handleNodeClick(e)}>{node.label}</div>
}
Expand Down

0 comments on commit 4ccf7f6

Please sign in to comment.