jakezatecky / react-checkbox-tree

A simple and elegant checkbox tree for React.
https://jakezatecky.github.io/react-checkbox-tree/
MIT License
709 stars 211 forks source link

Extend Node object #321

Open diegocamy opened 2 years ago

diegocamy commented 2 years ago

Hi Jake, first of all, thank you for creating this library.

Is there a way to extend the Node object? I'm asking because I would like to have a path property.

What I did, that didn't work, was the following.

Created a file named react-checkbox-tree.d.ts in my types folder, and wrote the following

import { Node } from "react-checkbox-tree"

declare module "react-checkbox-tree" {
  interface Node {
    path: string
  }
}

I'm able to create a node using this interface but when I use the onClick function on my CheckboxTree component to console.log() the path, it shows undefined

<CheckboxTree
   nodes={nodes}
   noCascade
   expanded={expandedNodes}
   checked={checkedNodes}
   onExpand={(expanded) => setExpandedNodes(expanded)}
   onCheck={(checked) => setCheckedNodes(checked)}
   onClick={(node) => console.log(node.path)}
   showNodeIcon={false}
    />

Thank you!

jakezatecky commented 2 years ago

The library as written does not apply any additional extensions to the node objects. However, this is a straight-forward and reasonable enough use case that it can be a future enhancement.