Skip to content

Commit fbaf149

Browse files
authored
add draggable prop (#433)
Co-authored-by: matthew.gould <matthew.gould@morneaushepell.com> Fixes #364
1 parent ca42b3e commit fbaf149

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

demo/src/App.js

+16
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class App extends Component {
9090
initialDepth: 1,
9191
depthFactor: undefined,
9292
zoomable: true,
93+
draggable: true,
9394
zoom: 1,
9495
scaleExtent: { min: 0.1, max: 1 },
9596
separation: { siblings: 2, nonSiblings: 2 },
@@ -127,6 +128,7 @@ class App extends Component {
127128
this.handleFloatChange = this.handleFloatChange.bind(this);
128129
this.toggleCollapsible = this.toggleCollapsible.bind(this);
129130
this.toggleZoomable = this.toggleZoomable.bind(this);
131+
this.toggleDraggable = this.toggleDraggable.bind(this);
130132
this.toggleCenterNodes = this.toggleCenterNodes.bind(this);
131133
this.setScaleExtent = this.setScaleExtent.bind(this);
132134
this.setSeparation = this.setSeparation.bind(this);
@@ -203,6 +205,10 @@ class App extends Component {
203205
this.setState(prevState => ({ zoomable: !prevState.zoomable }));
204206
}
205207

208+
toggleDraggable() {
209+
this.setState(prevState => ({ draggable: !prevState.draggable }));
210+
}
211+
206212
toggleCenterNodes() {
207213
if (this.state.dimensions !== undefined) {
208214
this.setState({
@@ -414,6 +420,15 @@ class App extends Component {
414420
/>
415421
</div>
416422

423+
<div className="prop-container">
424+
<h4 className="prop">Draggable</h4>
425+
<Switch
426+
name="draggableBtn"
427+
checked={this.state.draggable}
428+
onChange={this.toggleDraggable}
429+
/>
430+
</div>
431+
417432
<div className="prop-container">
418433
<h4 className="prop">
419434
Center Nodes on Click (via <code>dimensions</code> prop)
@@ -662,6 +677,7 @@ class App extends Component {
662677
collapsible={this.state.collapsible}
663678
initialDepth={this.state.initialDepth}
664679
zoomable={this.state.zoomable}
680+
draggable={this.state.draggable}
665681
zoom={this.state.zoom}
666682
scaleExtent={this.state.scaleExtent}
667683
nodeSize={this.state.nodeSize}

src/Tree/index.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
3939
collapsible: true,
4040
initialDepth: undefined,
4141
zoomable: true,
42+
draggable: true,
4243
zoom: 1,
4344
scaleExtent: { min: 0.1, max: 1 },
4445
nodeSize: { x: 140, y: 140 },
@@ -104,6 +105,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
104105
!deepEqual(this.props.translate, prevProps.translate) ||
105106
!deepEqual(this.props.scaleExtent, prevProps.scaleExtent) ||
106107
this.props.zoomable !== prevProps.zoomable ||
108+
this.props.draggable !== prevProps.draggable ||
107109
this.props.zoom !== prevProps.zoom ||
108110
this.props.enableLegacyTransitions !== prevProps.enableLegacyTransitions
109111
) {
@@ -163,6 +165,10 @@ class Tree extends React.Component<TreeProps, TreeState> {
163165
return true;
164166
})
165167
.on('zoom', (event: any) => {
168+
if (!this.props.draggable && (event.sourceEvent.type === 'mousemove')) {
169+
return
170+
}
171+
166172
g.attr('transform', event.transform);
167173
if (typeof onUpdate === 'function') {
168174
// This callback is magically called not only on "zoom", but on "drag", as well,

src/Tree/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ export interface TreeProps {
200200
*/
201201
zoomable?: boolean;
202202

203+
/**
204+
* Toggles ability to drag the Tree.
205+
*
206+
* {@link Tree.defaultProps.draggable | Default value}
207+
*/
208+
draggable?: boolean;
209+
203210
/**
204211
* A floating point number to set the initial zoom level. It is constrained by `scaleExtent`.
205212
*

0 commit comments

Comments
 (0)