Skip to content

Commit 8d557a0

Browse files
committed
Fix null reference issues:
- when expandToSelected == true but nothing has been passed to defaultSelectedKeys (it doesn't make much sense tocombine both properties like this BUT defaultSelectedKeys is not required thus this case should be properly handled) - when expandToSelected == true but somewhere in the tree, treeItem.children == null (which is normal when there aren't any children of this specific item)
1 parent 44e1079 commit 8d557a0

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/controls/treeView/TreeView.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
3131
this.handleTreeExpandCollapse = this.handleTreeExpandCollapse.bind(this);
3232
this.handleOnSelect = this.handleOnSelect.bind(this);
3333

34-
if (props.expandToSelected) {
34+
if (props.expandToSelected && props.defaultSelectedKeys) {
3535
props.defaultSelectedKeys.forEach(element => {
3636
this.pathTo(props.items, element);
3737
});
@@ -41,19 +41,21 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
4141

4242
private pathTo = (array: ITreeItem[], target: string): string => {
4343
let result: string;
44-
array.some(({ key, children = [] }) => {
45-
if (key === target) {
46-
this.nodesToExpand.push(key);
47-
result = key;
48-
return true;
49-
}
50-
let temp = this.pathTo(children, target);
51-
if (temp) {
52-
this.nodesToExpand.push(key);
53-
result = key + '.' + temp;
54-
return true;
55-
}
56-
});
44+
if (array) {
45+
array.some(({ key, children = [] }) => {
46+
if (key === target) {
47+
this.nodesToExpand.push(key);
48+
result = key;
49+
return true;
50+
}
51+
let temp = this.pathTo(children, target);
52+
if (temp) {
53+
this.nodesToExpand.push(key);
54+
result = key + '.' + temp;
55+
return true;
56+
}
57+
});
58+
}
5759
return result;
5860
}
5961

0 commit comments

Comments
 (0)