Skip to content

Commit c8eee52

Browse files
committed
Merge branch 'Abderahman88-TreeViewFix' into dev
2 parents 008161a + 01f0a64 commit c8eee52

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

docs/documentation/docs/controls/TreeView.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { TreeView, ITreeItem } from "@pnp/spfx-controls-react/lib/TreeView";
3939
treeItemActionsDisplayMode={TreeItemActionsDisplayMode.ContextualMenu}
4040
defaultSelectedKeys={['key1', 'key2']},
4141
expandToSelected={true}
42+
defaultExpandedChildren={true}
4243
onSelect={this.onTreeItemSelect}
4344
onExpandCollapse={this.onTreeItemExpandCollapse}
4445
onRenderItem={this.renderCustomTreeItem} />
@@ -88,7 +89,7 @@ The `TreeView` control can be configured with the following properties:
8889
| Property | Type | Required | Description |
8990
|--------------------------------|----------------------------|----------|------------------------------------------------------------------------------------------------------------------------------------|
9091
| items | ITreeItem[] | yes | An array of tree items to display. refer [example](#example-of-array-of-tree-items-used-to-render-control-as-in-first-screenshot). |
91-
| defaultExpanded | boolean | no | Specify if the tree items are displayed as expanded by default (defaults to false). |
92+
| defaultExpanded | boolean | no | Specify if the tree items are displayed as expanded by default (defaults to false. |
9293
| selectionMode | enum | no | Specifies the selection mode of tree view (defaults to Single selection). |
9394
| selectChildrenIfParentSelected | boolean | no | Specifies if the childrens should be selected when parent item is selected (defaults to false). |
9495
| showCheckboxes | boolean | yes | Specify if the checkboxes should be displayed for selection. |
@@ -98,6 +99,7 @@ The `TreeView` control can be configured with the following properties:
9899
| onExpandCollapse | function | no | Defines a onExpandCollapse function to raise when the tree item has expanded or collapsed. |
99100
| onSelect | function | no | Captures the event of when the tree item selection has changed. |
100101
| onRenderItem | function | no | Optional callback to provide custom rendering of the item (default is simple text of item label and a checkbox for selection). |
102+
| defaultExpandedChildren | boolean | no | Default expand / collapse behavior for the child nodes. By default this is set to true. |
101103
102104
Enum `TreeViewSelectionMode`
103105

src/controls/treeView/ITreeViewProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,9 @@ export interface ITreeViewProps {
6767
* @argument item The tree item.
6868
*/
6969
onRenderItem?: (item: ITreeItem) => JSX.Element;
70+
/**
71+
* Default expand / collapse behavior for the child nodes.
72+
* By default this is set to true.
73+
*/
74+
defaultExpandedChildren?: boolean;
7075
}

src/controls/treeView/TreeItem.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export interface ITreeItemProps {
6161
onRenderItem?: (item: ITreeItem) => JSX.Element;
6262

6363
nodesToExpand: any[];
64+
/**
65+
* Specifies whether current tree item's children should be rendered as expanded.
66+
*/
67+
defaultExpandedChildren?: boolean;
6468
}
6569

6670

@@ -217,14 +221,18 @@ export default class TreeItem extends React.Component<ITreeItemProps, ITreeItemS
217221
parentCallbackOnSelect,
218222
onRenderItem,
219223
showCheckboxes,
220-
treeItemActionsDisplayMode
224+
treeItemActionsDisplayMode,
225+
defaultExpandedChildren
221226
} = this.props;
222227

228+
const { expanded } = this.state;
229+
223230
let childrenWithHandlers = list.map((item, index) => {
224231
return (
225232
<TreeItem
226233
treeItem={item}
227-
defaultExpanded={this.state.expanded}
234+
defaultExpanded={defaultExpandedChildren ? expanded : expanded && !item.hasOwnProperty('children')}
235+
defaultExpandedChildren={defaultExpandedChildren}
228236
leftOffset={paddingLeft}
229237
selectionMode={selectionMode}
230238
activeItems={activeItems}

src/controls/treeView/TreeView.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
203203
onRenderItem,
204204
showCheckboxes,
205205
treeItemActionsDisplayMode,
206-
defaultExpanded
206+
defaultExpanded,
207+
defaultExpandedChildren
207208
} = this.props;
208209

209210
return (
@@ -215,6 +216,7 @@ export class TreeView extends React.Component<ITreeViewProps, ITreeViewState> {
215216
leftOffset={20}
216217
isFirstRender={true}
217218
defaultExpanded={defaultExpanded}
219+
defaultExpandedChildren={defaultExpandedChildren !== undefined ? defaultExpandedChildren : true}
218220
selectionMode={selectionMode}
219221
activeItems={this.state.activeItems}
220222
parentCallbackExpandCollapse={this.handleTreeExpandCollapse}

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,13 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
271271
children: [
272272
{
273273
key: "gc3",
274-
label: "Child of Parent 10"
274+
label: "Child of Parent 10",
275+
children: [
276+
{
277+
key: "ggc1",
278+
label: "Grandchild of Parent 10"
279+
}
280+
]
275281
},
276282
]
277283
},
@@ -1317,7 +1323,8 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
13171323
defaultSelectedKeys={['gc1', 'gc3']}
13181324
onExpandCollapse={this.onExpandCollapseTree}
13191325
onSelect={this.onItemSelected}
1320-
//expandToSelected={true}
1326+
defaultExpandedChildren={true}
1327+
//expandToSelected={true}
13211328
// onRenderItem={this.renderCustomTreeItem}
13221329
/>
13231330

0 commit comments

Comments
 (0)