Skip to content

Commit 451eb53

Browse files
committed
fix(zoomable): allows for dragging without scaling if props.zoomable is false
1 parent 531f825 commit 451eb53

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed

src/Tree/index.tsx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
100100
if (
101101
!deepEqual(this.props.translate, prevProps.translate) ||
102102
!deepEqual(this.props.scaleExtent, prevProps.scaleExtent) ||
103+
this.props.zoomable !== prevProps.zoomable ||
103104
this.props.zoom !== prevProps.zoom ||
104105
this.props.enableLegacyTransitions !== prevProps.enableLegacyTransitions
105106
) {
@@ -140,35 +141,34 @@ class Tree extends React.Component<TreeProps, TreeState> {
140141
const { zoomable, scaleExtent, translate, zoom, onUpdate } = props;
141142
const svg = select(`.${this.svgInstanceRef}`);
142143
const g = select(`.${this.gInstanceRef}`);
143-
if (zoomable) {
144-
// Sets initial offset, so that first pan and zoom does not jump back to default [0,0] coords.
145-
// @ts-ignore
146-
svg.call(d3zoom().transform, zoomIdentity.translate(translate.x, translate.y).scale(zoom));
147-
svg.call(
148-
d3zoom()
149-
.scaleExtent([scaleExtent.min, scaleExtent.max])
150-
// TODO: break this out into a separate zoom handler fn, rather than inlining it.
151-
.on('zoom', () => {
152-
g.attr('transform', event.transform);
153-
if (typeof onUpdate === 'function') {
154-
// This callback is magically called not only on "zoom", but on "drag", as well,
155-
// even though event.type == "zoom".
156-
// Taking advantage of this and not writing a "drag" handler.
157-
onUpdate({
158-
node: null,
159-
zoom: event.transform.k,
160-
translate: { x: event.transform.x, y: event.transform.y },
161-
});
162-
// TODO: remove this? Shouldn't be mutating state keys directly.
163-
this.state.d3.scale = event.transform.k;
164-
this.state.d3.translate = {
165-
x: event.transform.x,
166-
y: event.transform.y,
167-
};
168-
}
169-
})
170-
);
171-
}
144+
145+
// Sets initial offset, so that first pan and zoom does not jump back to default [0,0] coords.
146+
// @ts-ignore
147+
svg.call(d3zoom().transform, zoomIdentity.translate(translate.x, translate.y).scale(zoom));
148+
svg.call(
149+
d3zoom()
150+
.scaleExtent(zoomable ? [scaleExtent.min, scaleExtent.max] : [zoom, zoom])
151+
// TODO: break this out into a separate zoom handler fn, rather than inlining it.
152+
.on('zoom', () => {
153+
g.attr('transform', event.transform);
154+
if (typeof onUpdate === 'function') {
155+
// This callback is magically called not only on "zoom", but on "drag", as well,
156+
// even though event.type == "zoom".
157+
// Taking advantage of this and not writing a "drag" handler.
158+
onUpdate({
159+
node: null,
160+
zoom: event.transform.k,
161+
translate: { x: event.transform.x, y: event.transform.y },
162+
});
163+
// TODO: remove this? Shouldn't be mutating state keys directly.
164+
this.state.d3.scale = event.transform.k;
165+
this.state.d3.translate = {
166+
x: event.transform.x,
167+
y: event.transform.y,
168+
};
169+
}
170+
})
171+
);
172172
}
173173

174174
/**
@@ -470,7 +470,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
470470
};
471471

472472
return (
473-
<div className={`rd3t-tree-container ${zoomable ? 'rd3t-grabbable' : undefined}`}>
473+
<div className="rd3t-tree-container rd3t-grabbable">
474474
<style>{globalCss}</style>
475475
<svg
476476
className={`rd3t-svg ${this.svgInstanceRef} ${svgClassName}`}

src/Tree/tests/index.test.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,6 @@ describe('<Tree />', () => {
270270
});
271271
});
272272

273-
describe('zoomable', () => {
274-
it('adds the `.rd3t-grabbable` class if `props.zoomable`', () => {
275-
const zoomableComponent = shallow(<Tree data={mockData} />);
276-
const nonZoomableComponent = shallow(<Tree data={mockData} zoomable={false} />);
277-
278-
expect(zoomableComponent.find('.rd3t-tree-container').hasClass('rd3t-grabbable')).toBe(true);
279-
expect(nonZoomableComponent.find('.rd3t-tree-container').hasClass('rd3t-grabbable')).toBe(
280-
false
281-
);
282-
});
283-
});
284-
285273
describe('zoom', () => {
286274
it('applies the `zoom` prop when specified', () => {
287275
const zoomLevel = 0.3;

0 commit comments

Comments
 (0)