5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import { Chip , FormControl , Grid , IconButton , Theme , Tooltip } from '@mui/material' ;
8
+ import { Box , Chip , FormControl , FormHelperText , Grid , IconButton , Theme , Tooltip } from '@mui/material' ;
9
9
import { Folder as FolderIcon } from '@mui/icons-material' ;
10
10
import { useCallback , useMemo , useState } from 'react' ;
11
11
import { FieldValues , useController , useFieldArray } from 'react-hook-form' ;
12
- import { useIntl } from 'react-intl' ;
12
+ import { FormattedMessage , useIntl } from 'react-intl' ;
13
13
import { UUID } from 'crypto' ;
14
14
import { RawReadOnlyInput } from './RawReadOnlyInput' ;
15
- import { FieldLabel } from './utils/FieldLabel' ;
16
- import { useCustomFormContext } from './provider/useCustomFormContext' ;
17
- import { isFieldRequired } from './utils/functions' ;
18
- import { ErrorInput } from './errorManagement/ErrorInput' ;
19
- import { useSnackMessage } from '../../../hooks/useSnackMessage' ;
15
+ import { FieldLabel , isFieldRequired } from './utils' ;
16
+ import { useCustomFormContext } from './provider' ;
17
+ import { ErrorInput , MidFormError } from './errorManagement' ;
18
+ import { useSnackMessage } from '../../../hooks' ;
20
19
import { TreeViewFinderNodeProps } from '../../treeViewFinder' ;
21
- import { mergeSx } from '../../../utils/styles' ;
22
20
import { OverflowableText } from '../../overflowableText' ;
23
- import { MidFormError } from './errorManagement/MidFormError' ;
24
- import { DirectoryItemSelector } from '../../directoryItemSelector/DirectoryItemSelector' ;
21
+ import { DirectoryItemSelector } from '../../directoryItemSelector' ;
25
22
import { fetchDirectoryElementPath } from '../../../services' ;
26
- import { ElementAttributes } from '../../../utils' ;
23
+ import { BASE_EQUIPMENTS , ElementAttributes , EquipmentType , mergeSx } from '../../../utils' ;
27
24
import { NAME } from './constants' ;
28
25
29
26
const styles = {
@@ -67,6 +64,12 @@ export interface DirectoryItemsInputProps {
67
64
disable ?: boolean ;
68
65
allowMultiSelect ?: boolean ;
69
66
labelRequiredFromContext ?: boolean ;
67
+ equipmentColorsMap ?: Map < string , string > ;
68
+ }
69
+
70
+ interface EquipmentMetaData {
71
+ color : string ;
72
+ translateLabel : string | undefined ;
70
73
}
71
74
72
75
export function DirectoryItemsInput ( {
@@ -82,6 +85,7 @@ export function DirectoryItemsInput({
82
85
disable = false ,
83
86
allowMultiSelect = true ,
84
87
labelRequiredFromContext = true ,
88
+ equipmentColorsMap,
85
89
} : Readonly < DirectoryItemsInputProps > ) {
86
90
const { snackError } = useSnackMessage ( ) ;
87
91
const intl = useIntl ( ) ;
@@ -90,6 +94,7 @@ export function DirectoryItemsInput({
90
94
const [ multiSelect , setMultiSelect ] = useState ( allowMultiSelect ) ;
91
95
const types = useMemo ( ( ) => [ elementType ] , [ elementType ] ) ;
92
96
const [ directoryItemSelectorOpen , setDirectoryItemSelectorOpen ] = useState ( false ) ;
97
+ const [ elementsMetadata , setElementsMetadata ] = useState < EquipmentMetaData [ ] > ( [ ] ) ;
93
98
const {
94
99
fields : elements ,
95
100
append,
@@ -118,6 +123,7 @@ export function DirectoryItemsInput({
118
123
remove ( getValues ( name ) . findIndex ( ( item : FieldValues ) => item . id === chip ) ) ;
119
124
} ) ;
120
125
}
126
+ const currentColors = [ ...elementsMetadata ] ;
121
127
values . forEach ( ( value ) => {
122
128
const { icon, children, ...otherElementAttributes } = value ;
123
129
@@ -129,23 +135,49 @@ export function DirectoryItemsInput({
129
135
} ) ;
130
136
} else {
131
137
append ( otherElementAttributes ) ;
138
+ if ( equipmentColorsMap && value ?. specificMetadata ?. equipmentType ) {
139
+ const type : EquipmentType = value ?. specificMetadata ?. equipmentType as EquipmentType ;
140
+ currentColors . push ( {
141
+ color : equipmentColorsMap . get ( value . specificMetadata . equipmentType ) ?? '' ,
142
+ translateLabel :
143
+ type !== EquipmentType . HVDC_LINE ? BASE_EQUIPMENTS [ type ] ?. label : 'HvdcLines' ,
144
+ } ) ;
145
+ }
132
146
onRowChanged ?.( true ) ;
133
147
onChange ?.( getValues ( name ) ) ;
134
148
}
135
149
} ) ;
136
150
setDirectoryItemSelectorOpen ( false ) ;
137
151
setSelected ( [ ] ) ;
152
+ if ( equipmentColorsMap ) {
153
+ setElementsMetadata ( currentColors ) ;
154
+ }
138
155
} ,
139
- [ append , getValues , snackError , name , onRowChanged , onChange , selected , remove ]
156
+ [
157
+ selected ,
158
+ elementsMetadata ,
159
+ equipmentColorsMap ,
160
+ remove ,
161
+ getValues ,
162
+ name ,
163
+ snackError ,
164
+ append ,
165
+ onRowChanged ,
166
+ onChange ,
167
+ ]
140
168
) ;
141
169
142
170
const removeElements = useCallback (
143
171
( index : number ) => {
144
172
remove ( index ) ;
145
173
onRowChanged ?.( true ) ;
146
174
onChange ?.( getValues ( name ) ) ;
175
+ if ( elementsMetadata . length > 0 ) {
176
+ const currentColors = [ ...elementsMetadata . slice ( 0 , index ) , ...elementsMetadata . slice ( index + 1 ) ] ;
177
+ setElementsMetadata ( currentColors ) ;
178
+ }
147
179
} ,
148
- [ onRowChanged , remove , getValues , name , onChange ]
180
+ [ remove , onRowChanged , onChange , getValues , name , elementsMetadata ]
149
181
) ;
150
182
151
183
const handleChipClick = useCallback (
@@ -189,18 +221,28 @@ export function DirectoryItemsInput({
189
221
{ elements ?. length > 0 && (
190
222
< FormControl sx = { styles . formDirectoryElements2 } >
191
223
{ elements . map ( ( item , index ) => (
192
- < Chip
193
- key = { item . id }
194
- size = "small"
195
- onDelete = { ( ) => removeElements ( index ) }
196
- onClick = { ( ) => handleChipClick ( index ) }
197
- label = {
198
- < OverflowableText
199
- text = { < RawReadOnlyInput name = { `${ name } .${ index } .${ NAME } ` } /> }
200
- sx = { { width : '100%' } }
201
- />
202
- }
203
- />
224
+ < Box key = { `Box${ item . id } ` } sx = { { display : 'flex' , flexDirection : 'column' , gap : 1 } } >
225
+ < Chip
226
+ key = { item . id }
227
+ size = "small"
228
+ sx = { { backgroundColor : elementsMetadata ?. [ index ] . color } }
229
+ onDelete = { ( ) => removeElements ( index ) }
230
+ onClick = { ( ) => handleChipClick ( index ) }
231
+ label = {
232
+ < OverflowableText
233
+ text = { < RawReadOnlyInput name = { `${ name } .${ index } .${ NAME } ` } /> }
234
+ sx = { { width : '100%' } }
235
+ />
236
+ }
237
+ />
238
+ < FormHelperText >
239
+ { elementsMetadata ?. [ index ] ?. translateLabel ? (
240
+ < FormattedMessage id = { elementsMetadata ?. [ index ] ?. translateLabel } />
241
+ ) : (
242
+ ''
243
+ ) }
244
+ </ FormHelperText >
245
+ </ Box >
204
246
) ) }
205
247
</ FormControl >
206
248
) }
@@ -241,6 +283,7 @@ export function DirectoryItemsInput({
241
283
selected = { selected }
242
284
expanded = { expanded }
243
285
multiSelect = { multiSelect }
286
+ fetchMetaData = { ! ! equipmentColorsMap }
244
287
/>
245
288
</ >
246
289
) ;
0 commit comments