SharePoint Framework - React Based Tree View

Overview

Web parts developed in SharePoint make use of various controls to depict the functionality on the UI. The Office 365 UI Fabric offers various UI controls that can be used with SharePoint Framework client web parts. However, there are certain UI controls which are not available to use. Tree View is one of the examples of such UI controls.
 
In this article, we will explore the Tree View control. We will use ReactJS in this example.

Create SPFx Solution

Step 1
 
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-react-treeview  
Step 2
 
Navigate to the above-created directory.
  1. cd spfx-react-treeview  
Step 3
 
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Step 4
 
Yeoman Generator will present you with the wizard by asking questions about the solution to be created.
 
SharePoint Framework - React Based Tree View
 
Solution Name - Hit enter to have the default name (spfx-react-treeview in this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for component - Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest)
 
Place of files - We may choose to use the same folder or create a subfolder for our solution.
Selected choice - Same folder
 
Deployment option - Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice - N (install on each site explicitly)
 
Type of client-side component to create - We can choose to create client side webpart or an extension. Choose the webpart option.
Selected choice - WebPart
 
Web part name - Hit enter to select the default name or type in any other name.
Selected choice - TreeViewExample
 
Web part description - Hit enter to select the default description or type in any other value.
Selected choice - Tree view control with React
 
Framework to use - Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice - React
 
Step 5
 
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Step 6
 
Once the scaffolding process is completed, lock down the version of project dependencies by running below command
  1. npm shrinkwrap  
Step 7
 
In the command prompt type the below command to open the solution in the code editor of your choice.
  1. code .  

Tree View Control

At the time of writing this article, tree view control is not available in Office 365 UI Fabric controls. We will explore the tree view control called as react-super-treeview (https://www.npmjs.com/package/react-super-treeview)
 
Use the below command to install the tree view control
  1. npm install react-super-treeview --save  
The --save option enables NPM to include the packages to dependencies section of the package.json file.

Code the webpart

Step 1
 
Open TreeViewExample.tsx file under “\src\webparts\treeViewExample\components\” folder.
 
Step 2
 
Import the tree view control.
  1. import SuperTreeview from 'react-super-treeview';  
Step 3
 
Define a tree structure in a state.
  1. this.state = {  
  2.   data: [  
  3.       {  
  4.           id: 1,  
  5.           name: 'Parent A'  
  6.       },  
  7.       {  
  8.           id: 2,  
  9.           name: 'Parent B',  
  10.           isExpanded: true,  
  11.           isChecked: true,  
  12.           children: [  
  13.               {  
  14.                   id: 21,  
  15.                   name: 'Child 1',  
  16.                   isExpanded: true,  
  17.                   children: [  
  18.                       {  
  19.                           id: 5,  
  20.                           name: "Grand Child",  
  21.                           isExpanded: true  
  22.                       }  
  23.                   ]  
  24.               },  
  25.               {  
  26.                   id: 22,  
  27.                   name: 'Child 2'  
  28.               },  
  29.               {  
  30.                   id: 23,  
  31.                   name: 'Child 3'  
  32.               },  
  33.               {  
  34.                   id: 24,  
  35.                   name: 'Child 4'  
  36.               }  
  37.           ]  
  38.       }  
  39.   ]  
  40. };  
Step 4
 
Modify the render method to render the tree view.
  1. public render(): React.ReactElement<ITreeViewExampleProps> {  
  2.   return (  
  3.     // RENDER THE COMPONENT  
  4.     <div className={ styles.treeViewExample }>    
  5.       <div className={ styles.container }>    
  6.         <div className={ styles.row }>    
  7.           <div className={ styles.column }>    
  8.               
  9.             <SuperTreeview  
  10.               data={ this.state.data }  
  11.               noChildrenAvailableMessage= ''  
  12.               isDeletable= {(node, depth) => {  
  13.                 return false;  
  14.               }}  
  15.               onUpdateCb={(updatedData) => {  
  16.                 this.setState({data: updatedData});  
  17.                 let selectedNodeName: string = sessionStorage.getItem(sessionStorageSelectedNodeKey);  
  18.   
  19.                 resetNodes(updatedData);  
  20.           
  21.                 function resetNodes(nodes){  
  22.                     nodes.forEach((node)=>{  
  23.                         if (node.name !== selectedNodeName) {  
  24.                           node.isChecked = false;  
  25.                         }  
  26.                           
  27.                         if(node.children){  
  28.                           resetNodes(node.children);  
  29.                         }  
  30.                     });  
  31.                 }  
  32.   
  33.                 sessionStorage.removeItem(sessionStorageSelectedNodeKey);  
  34.               }}  
  35.               onCheckToggleCb={(nodes, depth)=>{  
  36.                   sessionStorage.setItem(sessionStorageSelectedNodeKey, nodes[0].name);  
  37.             }}  
  38.             />          
  39.           </div>    
  40.         </div>    
  41.       </div>    
  42.     </div>    
  43.   );  
  44. }  

Test the WebPart

  1. On the command prompt, type “gulp serve”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx.
  4. Add the webpart to page.
  5. Verify the tree view.
SharePoint Framework - React Based Tree View

Troubleshooting

In some cases, the SharePoint workbench (https://[tenant].sharepoint.com/_layouts/15/workbench.aspx) shows the below error although “gulp serve” is running.
 
SharePoint Framework - React Based Tree View 
 
Open the following URL in the next tab of the browser. Accept the warning message.
 
https://localhost:4321/temp/manifests.js 

Summary

Office 365 UI Fabric does not provide all the controls, however, we can utilize any third party controls to meet the business needs.