11import React , { useEffect , useState } from 'react' ;
2- import { ChevronRight16 , ChevronDown16 } from '@carbon/icons-react' ;
2+ import {
3+ Camera16 , ChevronRight16 , ChevronDown16 , VirtualMachine16 ,
4+ } from '@carbon/icons-react' ;
35import TreeView , { flattenTree } from 'react-accessible-treeview' ;
46import './styles.css' ;
5-
6- const atree = [
7- {
8- key : 'a' ,
9- text : 'ag-cp4ai-ia-4-6-0' ,
10- nodes : [
11- {
12- key : 'sn-1553' ,
13- text : 'snapshot1' ,
14- nodes : [
15- {
16- key : 'sn-1553_sn-1554' ,
17- text : 'snapshot2 (Active)' ,
18- nodes : [
19- {
20- key : 'sn-1553_sn-1554_sn-1555' ,
21- text : 'snapshot3' ,
22- } ,
23- {
24- key : 'sn-1553_sn-1554_sn-1550' ,
25- text : 'snapshot0' ,
26- } ,
27- ] ,
28- } ,
29- ] ,
30- } ,
31- {
32- key : 'sn-1556' ,
33- text : 'snapshot4' ,
34- nodes : [
35- {
36- key : 'sn-1556_sn-1557' ,
37- text : 'snapshot5' ,
38- nodes : [
39- {
40- key : 'sn-1556_sn-1557_sn-1558' ,
41- text : 'snapshot6' ,
42- } ,
43- ] ,
44- } ,
45- ] ,
46- } ,
47- ] ,
48- } ,
49- ] ;
50-
51- const folder = {
52- name : '' ,
53- id : 'root' ,
54- children : [
55- {
56- name : 'Fruits' ,
57- children : [
58- { name : 'Avocados' , id : 'a' } ,
59- { name : 'Bananas' , id : 'b' } ,
60- { name : 'Berries' , id : 'c' } ,
61- { name : 'Oranges' , id : 'd' } ,
62- { name : 'Pears' , id : 'e' } ,
63- ] ,
64- } ,
65- {
66- name : 'Drinks' ,
67- id : 'f' ,
68- children : [
69- {
70- name : 'Hot drinks' ,
71- id : 7 ,
72- children : [
73- {
74- name : 'Non-alcohol' ,
75- id : 8 ,
76- children : [
77- {
78- name : 'Tea' ,
79- id : 9 ,
80- children : [
81- {
82- name : 'Black Tea' ,
83- id : 10 ,
84- } ,
85- ] ,
86- } ,
87- ] ,
88- } ,
89- ] ,
90- } ,
91- ] ,
92- } ,
93- {
94- name : 'Vegetables' ,
95- id : 11 ,
96- children : [ { name : 'Beets' , id : 12 } ] ,
97- } ,
98- ] ,
99- } ;
7+ import PropTypes from 'prop-types' ;
8+
9+ // const atree = [
10+ // {
11+ // key: 'a',
12+ // text: 'ag-cp4ai-ia-4-6-0',
13+ // nodes: [
14+ // {
15+ // key: 'sn-1553',
16+ // text: 'snapshot1',
17+ // nodes: [
18+ // {
19+ // key: 'sn-1553_sn-1554',
20+ // text: 'snapshot2 (Active)',
21+ // nodes: [
22+ // {
23+ // key: 'sn-1553_sn-1554_sn-1555',
24+ // text: 'snapshot3',
25+ // },
26+ // {
27+ // key: 'sn-1553_sn-1554_sn-1550',
28+ // text: 'snapshot0',
29+ // },
30+ // ],
31+ // },
32+ // ],
33+ // },
34+ // {
35+ // key: 'sn-1556',
36+ // text: 'snapshot4',
37+ // nodes: [
38+ // {
39+ // key: 'sn-1556_sn-1557',
40+ // text: 'snapshot5',
41+ // nodes: [
42+ // {
43+ // key: 'sn-1556_sn-1557_sn-1558',
44+ // text: 'snapshot6',
45+ // },
46+ // ],
47+ // },
48+ // ],
49+ // },
50+ // ],
51+ // },
52+ // ];
10053
10154const allNodeData = [ ] ;
10255
@@ -132,18 +85,18 @@ const SnapshotTree = ({ nodes }) => {
13285
13386 allNodeData . forEach ( ( nodeData ) => {
13487 if ( nodeData . id === node . id ) {
135- const metaData = {
88+ const metadata = {
13689 selectable : nodeData . selectable || false ,
13790 tooltip : nodeData . tooltip || nodeData . name ,
13891 icon : nodeData . icon || 'fa fa-camera' ,
13992 } ;
140- node . metadata = metaData ;
93+ node . metadata = metadata ;
14194 }
14295 } ) ;
14396 } ) ;
14497
145- const testClick = ( e ) => {
146- if ( e . target . id === selectedNode || e . target . id === 'root' ) {
98+ const nodeClick = ( e , node ) => {
99+ if ( node . metadata . selectable === false || e . target . id === selectedNode ) {
147100 // If the clicked node is already selected or root is selected, do nothing
148101 if ( e . target . id === selectedNode ) {
149102 e . stopPropagation ( ) ;
@@ -168,7 +121,6 @@ const SnapshotTree = ({ nodes }) => {
168121 } ;
169122
170123 useEffect ( ( ) => {
171- console . log ( selectedNode ) ;
172124 addSelectedClassName ( ) ;
173125 } , [ selectedNode ] ) ;
174126
@@ -187,6 +139,13 @@ const SnapshotTree = ({ nodes }) => {
187139 return < ChevronRight16 /> ;
188140 } ;
189141
142+ const NodeIcon = ( icon ) => {
143+ if ( icon === 'pficon pficon-virtual-machine' ) {
144+ return < VirtualMachine16 /> ;
145+ }
146+ return < Camera16 /> ;
147+ } ;
148+
190149 // First pull in node data and go through flattened tree to add metadata like icons and selectable
191150 // Then add icons, tooltip and special handling
192151
@@ -215,11 +174,16 @@ const SnapshotTree = ({ nodes }) => {
215174 style = { { paddingLeft : 40 * ( level - 1 ) } }
216175 >
217176 { isBranch && < ArrowIcon isOpen = { isExpanded } /> }
177+ { element . metadata && element . metadata . icon && (
178+ < div className = "node-icon-div" >
179+ < NodeIcon icon = { element . metadata . icon } />
180+ </ div >
181+ ) }
218182 < span
219183 key = { element . id }
220184 id = { element . id }
221- onClick = { testClick }
222- onKeyDown = { ( e ) => e . key === 'Enter' && testClick ( e ) }
185+ onClick = { ( e ) => nodeClick ( e , element ) }
186+ onKeyDown = { ( e ) => e . key === 'Enter' && nodeClick ( e ) }
223187 role = "button"
224188 tabIndex = { 0 }
225189 className = "name"
@@ -234,4 +198,11 @@ const SnapshotTree = ({ nodes }) => {
234198 ) ;
235199} ;
236200
201+ SnapshotTree . propTypes = {
202+ nodes : PropTypes . arrayOf ( PropTypes . any ) . isRequired ,
203+ } ;
204+
205+ SnapshotTree . defaultProps = {
206+ } ;
207+
237208export default SnapshotTree ;
0 commit comments