Skip to content

Commit

Permalink
feat: 馃幐 added TreeState support to Tree component
Browse files Browse the repository at this point in the history
  • Loading branch information
diogofcunha committed Jan 27, 2019
1 parent 5a16d13 commit 60aa935
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@ import PropTypes from 'prop-types';
import {AutoSizer, List, CellMeasurerCache, CellMeasurer} from 'react-virtualized';

import {FlattenedNode} from './shapes/nodeShapes';
import TreeState, {State} from './state/TreeState';

export default class Tree extends React.Component {
_cache = new CellMeasurerCache({
fixedWidth: true,
minHeight: 20,
});

rowRenderer = ({node, key, measure, style, NodeRenderer}) => {
getRowCount = () => {
const {nodes} = this.props;

return nodes instanceof State ? nodes.flattenedTree.length : nodes.length;
};

getNodeDeepness = (node, index) => {
const {nodes} = this.props;

if (nodes instanceof State) {
TreeState.getNodeDeepness(nodes, index);
}

return nodes instanceof State ? TreeState.getNodeDeepness(nodes, index) : node.deepness;
};

getNode = index => {
const {nodes} = this.props;

return nodes instanceof State
? {...TreeState.getNodeAt(nodes, index), deepness: this.getNodeDeepness({}, index)}
: nodes[index];
};

rowRenderer = ({node, key, measure, style, NodeRenderer, index}) => {
const {nodeMarginLeft} = this.props;

return (
Expand All @@ -25,17 +50,18 @@ export default class Tree extends React.Component {
node={node}
onChange={this.props.onChange}
measure={measure}
index={index}
/>
);
};

measureRowRenderer = nodes => ({key, index, style, parent}) => {
const {NodeRenderer} = this.props;
const node = nodes[index];
const node = this.getNode(index);

return (
<CellMeasurer cache={this._cache} columnIndex={0} key={key} rowIndex={index} parent={parent}>
{m => this.rowRenderer({...m, node, key, style, NodeRenderer})}
{m => this.rowRenderer({...m, index, node, key, style, NodeRenderer})}
</CellMeasurer>
);
};
Expand All @@ -50,7 +76,7 @@ export default class Tree extends React.Component {
deferredMeasurementCache={this._cache}
ref={r => (this._list = r)}
height={height}
rowCount={nodes.length}
rowCount={this.getRowCount()}
rowHeight={this._cache.rowHeight}
rowRenderer={this.measureRowRenderer(nodes)}
width={width || autoWidth}
Expand Down

0 comments on commit 60aa935

Please sign in to comment.