-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathTree.tsx
140 lines (116 loc) · 3.4 KB
/
Tree.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {boolean, number, withKnobs} from '@storybook/addon-knobs';
import {storiesOf} from '@storybook/react';
import * as React from 'react';
import {AutoSizer} from 'react-virtualized';
import Tree from '../src';
import {Node} from '../src/types';
import {Update} from '../src/utils';
interface DataNode {
children: DataNode[];
id: number;
name: string;
}
interface TreePresenterProps {
enableDynamicHeight: boolean;
rootChildrenHeight: number;
rowHeight: number;
}
interface TreePresenterState {
update: Update;
}
class TreePresenter extends React.PureComponent<TreePresenterProps, TreePresenterState> {
public state: TreePresenterState = {
update: Update.None,
};
private id: number = 0;
private root: DataNode = this.createNode();
constructor(props: TreePresenterProps, context: any) {
super(props, context);
this.nodeGetter = this.nodeGetter.bind(this);
}
public componentWillReceiveProps({
enableDynamicHeight: nextEnableDynamicHeight,
rootChildrenHeight: nextRootChildrenHeight,
}: TreePresenterProps): void {
const {enableDynamicHeight, rootChildrenHeight} = this.props;
this.setState({
update: enableDynamicHeight !== nextEnableDynamicHeight
|| rootChildrenHeight !== nextRootChildrenHeight ? Update.Nodes : Update.None,
});
}
public render(): JSX.Element {
const {rowHeight} = this.props;
const {update} = this.state;
return (
<AutoSizer disableHeight>
{({width}) => (
<Tree
height={500}
nodeGetter={this.nodeGetter}
rowHeight={rowHeight}
update={update}
width={width}
/>
)}
</AutoSizer>
);
}
private createNode(depth: number = 0): DataNode {
const node: DataNode = {
children: [],
id: this.id,
name: `test-${this.id}`,
};
this.id += 1;
if (depth === 5) {
return node;
}
// tslint:disable-next-line:no-increment-decrement
for (let i = 0; i < 5; i++) {
node.children.push(this.createNode(depth + 1));
}
return node;
}
private * nodeGetter(refresh: boolean): IterableIterator<Node | string> {
interface StackElement {
nestingLevel: number;
node: DataNode;
}
const {enableDynamicHeight, rootChildrenHeight} = this.props;
const stack = [];
stack.push({
nestingLevel: 0,
node: this.root,
});
while (stack.length !== 0) {
const {node, nestingLevel}: StackElement = stack.pop()!;
const id = node.id.toString();
const isOpened = yield refresh ? {
childrenCount: node.children.length,
height: enableDynamicHeight && nestingLevel === 1 ? rootChildrenHeight : undefined,
id,
isOpenedByDefault: true,
nestingLevel,
nodeData: node.name,
} : id;
if (node.children.length !== 0 && isOpened) {
// tslint:disable-next-line:no-increment-decrement
for (let i = node.children.length - 1; i >= 0; i--) {
stack.push({
nestingLevel: nestingLevel + 1,
node: node.children[i],
});
}
}
}
}
}
storiesOf('Tree', module)
.addDecorator(withKnobs)
.add('Default', () => (
<TreePresenter
enableDynamicHeight={boolean('Enable dynamic height', false)}
rootChildrenHeight={number('Root direct children height', 30)}
rowHeight={number('Row height', 30)}
/>
));