Skip to content

Commit ff26974

Browse files
committed
Implements nodeSize prop (#17)
1 parent 6c3419c commit ff26974

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/Tree/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,29 @@ export default class Tree extends React.Component {
197197
/**
198198
* generateTree - Generates tree elements (`nodes` and `links`) by
199199
* grabbing the rootNode from `this.state.data[0]`.
200-
* Restricts tree depth to `props.initial` if defined and this is
200+
* Restricts tree depth to `props.initialDepth` if defined and if this is
201201
* the initial render of the tree.
202202
*
203-
* @return {object} Object containing `nodes` and `links` fields.
203+
* @return {object} Object containing `nodes` and `links`.
204204
*/
205205
generateTree() {
206-
const { initialDepth, depthFactor, separation } = this.props;
206+
const {
207+
initialDepth,
208+
depthFactor,
209+
separation,
210+
nodeSize,
211+
orientation,
212+
} = this.props;
213+
207214
const tree = layout.tree()
208-
.nodeSize([140, 140])
209-
.separation((d) => d._children ? separation.node : separation.leafNode)
215+
.nodeSize(orientation === 'horizontal' ?
216+
[nodeSize.y, nodeSize.x] :
217+
[nodeSize.x, nodeSize.y]
218+
)
219+
.separation((a, b) => deepEqual(a.parent, b.parent) ?
220+
separation.node :
221+
separation.leafNode
222+
)
210223
.children((d) => d._collapsed ? null : d._children);
211224

212225
const rootNode = this.state.data[0];
@@ -285,6 +298,7 @@ Tree.defaultProps = {
285298
initialDepth: undefined,
286299
zoomable: true,
287300
scaleExtent: { min: 0.1, max: 1 },
301+
nodeSize: { x: 140, y: 140 },
288302
separation: { node: 1.2, leafNode: 0.9 },
289303
styles: {
290304
nodes: {
@@ -326,6 +340,10 @@ Tree.propTypes = {
326340
min: PropTypes.number,
327341
max: PropTypes.number,
328342
}),
343+
nodeSize: PropTypes.shape({
344+
x: PropTypes.number,
345+
y: PropTypes.number,
346+
}),
329347
separation: PropTypes.shape({
330348
node: PropTypes.number,
331349
leafNode: PropTypes.number,

0 commit comments

Comments
 (0)