Skip to content

Commit 60aa935

Browse files
committed
feat: 🎸 added TreeState support to Tree component
1 parent 5a16d13 commit 60aa935

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/Tree.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@ import PropTypes from 'prop-types';
33
import {AutoSizer, List, CellMeasurerCache, CellMeasurer} from 'react-virtualized';
44

55
import {FlattenedNode} from './shapes/nodeShapes';
6+
import TreeState, {State} from './state/TreeState';
67

78
export default class Tree extends React.Component {
89
_cache = new CellMeasurerCache({
910
fixedWidth: true,
1011
minHeight: 20,
1112
});
1213

13-
rowRenderer = ({node, key, measure, style, NodeRenderer}) => {
14+
getRowCount = () => {
15+
const {nodes} = this.props;
16+
17+
return nodes instanceof State ? nodes.flattenedTree.length : nodes.length;
18+
};
19+
20+
getNodeDeepness = (node, index) => {
21+
const {nodes} = this.props;
22+
23+
if (nodes instanceof State) {
24+
TreeState.getNodeDeepness(nodes, index);
25+
}
26+
27+
return nodes instanceof State ? TreeState.getNodeDeepness(nodes, index) : node.deepness;
28+
};
29+
30+
getNode = index => {
31+
const {nodes} = this.props;
32+
33+
return nodes instanceof State
34+
? {...TreeState.getNodeAt(nodes, index), deepness: this.getNodeDeepness({}, index)}
35+
: nodes[index];
36+
};
37+
38+
rowRenderer = ({node, key, measure, style, NodeRenderer, index}) => {
1439
const {nodeMarginLeft} = this.props;
1540

1641
return (
@@ -25,17 +50,18 @@ export default class Tree extends React.Component {
2550
node={node}
2651
onChange={this.props.onChange}
2752
measure={measure}
53+
index={index}
2854
/>
2955
);
3056
};
3157

3258
measureRowRenderer = nodes => ({key, index, style, parent}) => {
3359
const {NodeRenderer} = this.props;
34-
const node = nodes[index];
60+
const node = this.getNode(index);
3561

3662
return (
3763
<CellMeasurer cache={this._cache} columnIndex={0} key={key} rowIndex={index} parent={parent}>
38-
{m => this.rowRenderer({...m, node, key, style, NodeRenderer})}
64+
{m => this.rowRenderer({...m, index, node, key, style, NodeRenderer})}
3965
</CellMeasurer>
4066
);
4167
};
@@ -50,7 +76,7 @@ export default class Tree extends React.Component {
5076
deferredMeasurementCache={this._cache}
5177
ref={r => (this._list = r)}
5278
height={height}
53-
rowCount={nodes.length}
79+
rowCount={this.getRowCount()}
5480
rowHeight={this._cache.rowHeight}
5581
rowRenderer={this.measureRowRenderer(nodes)}
5682
width={width || autoWidth}

0 commit comments

Comments
 (0)