Skip to content

Commit 492503e

Browse files
committed
docs(readme): Customizing the Tree, wip
1 parent b3657cd commit 492503e

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ React D3 Tree is a [React](http://facebook.github.io/react/) component that lets
3232
- [Usage](#usage)
3333
- [Props](#props)
3434
- [Up & Running](#up--running)
35-
- [Styling nodes](#styling-nodes)
36-
- [Styling links](#styling-links)
37-
- [Rendering custom nodes](#rendering-custom-nodes)
35+
- [Styling Nodes](#styling-nodes)
36+
- [Styling Links](#styling-links)
37+
- [Event Handlers](#event-handlers)
38+
- [Customizing the Tree](#customizing-the-tree)
39+
- [`renderCustomNodeElement`](#rendercustomnodeelement)
40+
- [`pathFunc`](#pathfunc)
3841
- [Development](#development)
3942
- [Setup](#setup)
4043
- [Hot reloading](#hot-reloading)
@@ -54,7 +57,7 @@ import Tree from 'react-d3-tree';
5457

5558
// This is a simplified example of an org chart with a depth of 2.
5659
// Note how deeper levels are defined recursively via the `children` property.
57-
const orgChartJson = {
60+
const orgChart = {
5861
name: 'CEO',
5962
children: [
6063
{
@@ -94,7 +97,7 @@ export default function OrgChartTree() {
9497
return (
9598
// `<Tree />` will fill width/height of its container; in this case `#treeWrapper`.
9699
<div id="treeWrapper" style={{ width: '50em', height: '20em' }}>
97-
<Tree data={orgChartJson} />
100+
<Tree data={orgChart} />
98101
</div>
99102
);
100103
}
@@ -103,9 +106,11 @@ export default function OrgChartTree() {
103106
## Props
104107
For details on the props accepted by `Tree`, check out the [TreeProps reference docs](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html).
105108

109+
The only required prop is [data](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#data), all other props on `Tree` are optional/pre-defined (see "Default value" on each prop).
110+
106111
<!--(TODO: REVISE TITLE) -->
107112
## Up & Running
108-
### Styling nodes
113+
### Styling Nodes
109114
`Tree` provides the following props to style different types of nodes, all of which use an SVG `circle` by default:
110115

111116
- `rootNodeClassName` - will be applied to the root node.
@@ -117,14 +122,17 @@ To visually distinguish these three types of nodes from each other by color, we
117122
```css
118123
/* custom-tree.css */
119124

120-
.node__root {
125+
.node__root > circle {
121126
fill: red;
122127
}
123-
.node__branch {
124-
fill: yellow
128+
129+
.node__branch > circle {
130+
fill: yellow;
125131
}
126-
.node__leaf {
127-
fill: green;
132+
133+
.node__leaf > circle {
134+
/* Let's make the radius of leaf nodes larger */
135+
r: 40;
128136
}
129137
```
130138

@@ -153,7 +161,7 @@ export default function StyledNodesTree() {
153161
154162
<!-- For a full list of options of CSS properties that can be used for the default nodes, check the SVG circle [specification](TODO:) -->
155163

156-
### Styling links
164+
### Styling Links
157165
`Tree` provides the `pathClassFunc` property to pass additional classNames to every link to be rendered.
158166

159167
Each link calls `pathClassFunc` with its own `TreeLinkDatum` and the tree's current `orientation`. `Tree` expects `pathClassFunc` to return a `className` string.
@@ -186,8 +194,35 @@ function StyledLinksTree() {
186194

187195
> For more details, see the `PathClassFunction` [reference docs](https://bkrem.github.io/react-d3-tree/docs/modules/_types_common_.html#pathclassfunction).
188196
189-
## Rendering custom nodes
190-
TODO:
197+
### Event Handlers
198+
`Tree` exposes the following event handler callbacks by default:
199+
200+
- [onLinkClick](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onlinkclick)
201+
- [onLinkMouseOut](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onlinkmouseout)
202+
- [onLinkMouseOver](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onlinkmouseover)
203+
- [onNodeClick](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onnodeclick)
204+
- [onNodeMouseOut](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onnodemouseout)
205+
- [onNodeMouseOver](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#onnodemouseover)
206+
207+
> **Note:** Nodes are expanded/collapsed whenever `onNodeClick` fires. To prevent this, set the [`collapsible` prop](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#collapsible) to `false`.
208+
> `onNodeClick` will still fire, but it will not change the target node's expanded/collapsed state.
209+
210+
## Customizing the Tree
211+
<!-- Using the `<nodeType>NodeClassName` and `pathClassFunc` approaches above should give -->
212+
213+
### `renderCustomNodeElement`
214+
The [`renderCustomNodeElement` prop](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html#rendercustomnodeelement) accepts a **custom render function that will be used for every node in the tree.**
215+
216+
Cases where you may find rendering your own `Node` element useful include:
217+
218+
<!-- TODO: attach examples for each use case -->
219+
220+
- Using a **different SVG tag for your nodes** (instead of the default `<circle>`).
221+
- Gaining **fine-grained control over event handling** (e.g. to implement events not covered by the default API).
222+
- Building **richer & more complex nodes/labels** by leveraging the `foreignObject` tag to render HTML inside the SVG namespace.
223+
224+
### `pathFunc`
225+
TODO
191226

192227
## Development
193228
### Setup

0 commit comments

Comments
 (0)