Skip to content

Commit 451a2b9

Browse files
committed
#default expanded treeItem
1 parent 973799c commit 451a2b9

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/controls/treeView/ITreeViewProps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface ITreeViewProps {
5656
* Display mode of the tree item actions.
5757
*/
5858
treeItemActionsDisplayMode?: TreeItemActionsDisplayMode;
59+
/**
60+
* Keys of items expanded by default
61+
*/
62+
defaultExpandedKeys?: string[];
5963
/**
6064
* Keys of items selected by default
6165
*/

src/controls/treeView/TreeItem.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class TreeItem extends React.Component<ITreeItemProps, ITreeItemS
106106
let active = props.activeItems.filter(item => item.key === props.treeItem.key);
107107

108108
let expanded = props.defaultExpanded;
109-
if (props.nodesToExpand.indexOf(props.treeItem.key) != -1) {
109+
if (!expanded && props.nodesToExpand.indexOf(props.treeItem.key) != -1) {
110110
expanded = true;
111111
}
112112

@@ -150,12 +150,20 @@ export default class TreeItem extends React.Component<ITreeItemProps, ITreeItemS
150150
*/
151151
public componentWillReceiveProps(nextProps: ITreeItemProps): void {
152152
// If selection is turned on, set the item as selected
153-
if (this.props.selectionMode != TreeViewSelectionMode.None) {
153+
if (this.props.selectionMode != TreeViewSelectionMode.None) {
154154
let active = nextProps.activeItems.filter(item => item.key === this.props.treeItem.key);
155155

156+
let _isExpanded:boolean=this.state.expanded;
157+
if(!_isExpanded && this.props.nodesToExpand.indexOf(this.props.treeItem.key) != -1) {
158+
_isExpanded = true;
159+
}
160+
if(_isExpanded && this.props.nodesToExpand.indexOf(this.props.treeItem.key) == -1){
161+
_isExpanded=false;
162+
}
163+
156164
this.setState({
157165
selected: active.length > 0,
158-
expanded: this.state.expanded
166+
expanded: _isExpanded
159167
});
160168
}
161169
}

src/controls/treeView/TreeView.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
240240
showCheckboxes,
241241
treeItemActionsDisplayMode,
242242
defaultExpanded,
243-
defaultExpandedChildren
243+
defaultExpandedChildren,
244+
defaultExpandedKeys
244245
} = this.props;
245246

246247
return (
@@ -260,7 +261,7 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
260261
onRenderItem={onRenderItem}
261262
showCheckboxes={showCheckboxes}
262263
treeItemActionsDisplayMode={treeItemActionsDisplayMode}
263-
nodesToExpand={this.nodesToExpand}
264+
nodesToExpand={defaultExpandedKeys}
264265
/>
265266
))
266267
}

0 commit comments

Comments
 (0)