Skip to content

onRefAssignment PoC #373

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-d3-tree",
"version": "3.2.0",
"name": "react-d3-tree_callback-ref-prop",
"version": "3.3.0",
"description": "React component to create interactive D3 tree hierarchies",
"author": "Ben Kremer",
"license": "MIT",
6 changes: 5 additions & 1 deletion src/Node/index.tsx
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ type NodeProps = {
onNodeClick: NodeEventHandler;
onNodeMouseOver: NodeEventHandler;
onNodeMouseOut: NodeEventHandler;
callbackRefForNode: (context: TreeNodeDatum, element: SVGGElement) => void;
subscriptions: object;
};

@@ -158,11 +159,14 @@ export default class Node extends React.Component<NodeProps, NodeState> {
}

render() {
const { data, nodeClassName } = this.props;
const { data, nodeClassName, callbackRefForNode } = this.props;
return (
<g
id={data.__rd3t.id}
ref={n => {
if (callbackRefForNode) {
callbackRefForNode(data, n);
}
this.nodeRef = n;
}}
style={this.state.initialStyle}
9 changes: 8 additions & 1 deletion src/Tree/index.tsx
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
onLinkClick: undefined,
onLinkMouseOver: undefined,
onLinkMouseOut: undefined,
callbackRefForNode: undefined,
onUpdate: undefined,
orientation: 'horizontal',
translate: { x: 0, y: 0 },
@@ -151,7 +152,12 @@ class Tree extends React.Component<TreeProps, TreeState> {
.scaleExtent(zoomable ? [scaleExtent.min, scaleExtent.max] : [zoom, zoom])
// TODO: break this out into a separate zoom handler fn, rather than inlining it.
.filter(() => {
if (hasInteractiveNodes) return event.target.classList.contains(this.svgInstanceRef) || event.target.classList.contains(this.gInstanceRef) || event.shiftKey;
if (hasInteractiveNodes)
return (
event.target.classList.contains(this.svgInstanceRef) ||
event.target.classList.contains(this.gInstanceRef) ||
event.shiftKey
);
return true;
})
.on('zoom', () => {
@@ -519,6 +525,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
nodeSize={nodeSize}
orientation={orientation}
enableLegacyTransitions={enableLegacyTransitions}
callbackRefForNode={this.props.callbackRefForNode}
transitionDuration={transitionDuration}
onNodeToggle={this.handleNodeToggle}
onNodeClick={this.handleOnNodeClickCb}
5 changes: 5 additions & 0 deletions src/Tree/types.ts
Original file line number Diff line number Diff line change
@@ -285,4 +285,9 @@ export interface TreeProps {
* {@link Tree.defaultProps.hasInteractiveNodes | Default value}
*/
hasInteractiveNodes?: boolean;

/**
* A function that is called when the ref gets assigned for each node
*/
callbackRefForNode?: (context: TreeNodeDatum, element: SVGGElement) => void;
}