1
1
/* eslint-disable jsx-a11y/no-static-element-interactions */
2
2
/* eslint-disable jsx-a11y/click-events-have-key-events */
3
- import React , { useEffect , useState } from 'react' ;
3
+ import React , { useState } from 'react' ;
4
4
import PropTypes from 'prop-types' ;
5
- import {
6
- Modal , Button , ModalBody , Loading ,
7
- } from 'carbon-components-react' ;
8
- import classNames from 'classnames' ;
9
- import { CaretRight16 , CaretDown16 , CheckmarkFilled16 } from '@carbon/icons-react' ;
10
- import { http } from '../../http_api' ;
11
- import { findNodeByKey , selectableItem , TREE_CONFIG } from './helper' ;
12
- import './style.scss' ;
5
+ import { Modal , Button , ModalBody } from 'carbon-components-react' ;
6
+ import MiqTree from '../MiqTreeView' ;
13
7
14
8
/** Component to render a tree and to select an embedded method. */
15
9
const AeInlineMethod = ( { type } ) => {
16
- const treeType = TREE_CONFIG [ type ] ;
17
-
18
10
const [ data , setData ] = useState ( {
19
- list : undefined ,
20
11
isModalOpen : false ,
21
- isLoading : true ,
22
12
selectedNode : undefined ,
23
13
} ) ;
24
14
25
- /** When isModal is true, a GET request is made to get the initial data. */
26
- useEffect ( ( ) => {
27
- if ( data . isModalOpen ) {
28
- http . get ( treeType . url )
29
- . then ( ( response ) => {
30
- setData ( {
31
- ...data ,
32
- list : response ,
33
- isLoading : false ,
34
- } ) ;
35
- } ) ;
36
- }
37
- } , [ data . isModalOpen ] ) ;
38
-
39
- console . log ( data ) ;
40
-
41
15
/** Function to show/hide the modal. */
42
16
const showModal = ( status ) => {
43
17
setData ( {
@@ -46,46 +20,6 @@ const AeInlineMethod = ({ type }) => {
46
20
} ) ;
47
21
} ;
48
22
49
- /** Function to handle the click events of tree node. */
50
- const loadSelectedNode = ( node ) => {
51
- const foundItem = findNodeByKey ( data . list [ 0 ] . nodes , node . key ) ;
52
- const newUrl = `${ treeType . url } ?id=${ node . key } ` ;
53
-
54
- if ( selectableItem ( node , treeType . selectKey ) ) {
55
- setData ( {
56
- ...data ,
57
- selectedNode : ( data . selectedNode && data . selectedNode . key === node . key ) ? undefined : node ,
58
- } ) ;
59
- } else if ( ! node . state . expanded ) {
60
- if ( foundItem . nodes && foundItem . nodes . length > 0 ) {
61
- // if nodes are already available, just expand the tree.
62
- foundItem . state . expanded = true ;
63
- setData ( {
64
- ...data ,
65
- list : [ ...data . list ] ,
66
- } ) ;
67
- } else {
68
- // if nodes are not available, request for the data from the url and update the nodes.
69
- http . get ( newUrl )
70
- . then ( ( response ) => {
71
- foundItem . nodes = response ;
72
- foundItem . state . expanded = true ;
73
- setData ( {
74
- ...data ,
75
- list : [ ...data . list ] ,
76
- } ) ;
77
- } ) ;
78
- }
79
- } else {
80
- // Hides the children.
81
- foundItem . state . expanded = false ;
82
- setData ( {
83
- ...data ,
84
- list : [ ...data . list ] ,
85
- } ) ;
86
- }
87
- } ;
88
-
89
23
/** Function to render the Add method button. */
90
24
const renderAddButton = ( ) => (
91
25
< Button
@@ -99,49 +33,6 @@ const AeInlineMethod = ({ type }) => {
99
33
</ Button >
100
34
) ;
101
35
102
- /** Function to render the down and right caret. */
103
- const renderCaret = ( child ) => {
104
- if ( ! child ) {
105
- return undefined ;
106
- }
107
- if ( selectableItem ( child , treeType . selectableItem ) ) {
108
- return undefined ;
109
- }
110
- return child . state . expanded ? < CaretDown16 className = "tree-caret" /> : < CaretRight16 className = "tree-caret" /> ;
111
- } ;
112
-
113
- const renderIcon = ( child ) => ( < div className = "tree-icon" > < i className = { child . icon } /> </ div > ) ;
114
-
115
- const renderText = ( child ) => ( < div className = "tree-text" > { child . text } </ div > ) ;
116
-
117
- const isSelectedNode = ( child ) => ( data . selectedNode && data . selectedNode . key === child . key ) ;
118
-
119
- /** Function to render the Tree Node. */
120
- const renderTreeNode = ( child ) => {
121
- const isSelected = isSelectedNode ( child ) ;
122
- return (
123
- < div key = { child . key } >
124
- < div className = { classNames ( 'tree-row parent-tree' , isSelected && 'selected-node' ) } onClick = { ( ) => loadSelectedNode ( child ) } >
125
- { renderCaret ( child ) }
126
- { renderIcon ( child ) }
127
- { renderText ( child ) }
128
- { isSelected && < CheckmarkFilled16 className = "selected-node-check" /> }
129
- </ div >
130
- { child . state . expanded && child . nodes && child . nodes . length > 0 && (
131
- < div className = "tree-row child-tree intend-right" key = { child . key } >
132
- { child . nodes . map ( ( child ) => renderTreeNode ( child ) ) }
133
- </ div >
134
- ) }
135
- </ div >
136
- ) ;
137
- } ;
138
-
139
- /** Function to render the tree contents. */
140
- const renderTree = ( list ) => ( list && list . map ( ( child ) => renderTreeNode ( child ) ) ) ;
141
-
142
- /** Function to render the modal contents. */
143
- const renderModalContent = ( ) => ( data . list ? data . list [ 0 ] . nodes && renderTree ( data . list [ 0 ] . nodes ) : undefined ) ;
144
-
145
36
return (
146
37
< div >
147
38
{ renderAddButton ( ) }
@@ -164,9 +55,18 @@ const AeInlineMethod = ({ type }) => {
164
55
>
165
56
< ModalBody >
166
57
{
167
- data . isLoading
168
- ? < Loading active small withOverlay = { false } className = "loading" />
169
- : renderModalContent ( )
58
+ data . isModalOpen
59
+ && (
60
+ < MiqTree
61
+ type = { type }
62
+ onNodeSelect = { ( item ) => {
63
+ setData ( {
64
+ ...data ,
65
+ selectedNode : item ,
66
+ } ) ;
67
+ } }
68
+ />
69
+ )
170
70
}
171
71
</ ModalBody >
172
72
</ Modal >
0 commit comments