1
1
import React , { useState } from 'react' ;
2
- import { Label , LabelGroup , Alert , AlertVariant } from '@patternfly/react-core' ;
2
+ import { Label , LabelGroup , Alert , AlertVariant , LabelProps } from '@patternfly/react-core' ;
3
3
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing' ;
4
4
import DashboardDescriptionListGroup from '~/components/DashboardDescriptionListGroup' ;
5
5
@@ -10,6 +10,9 @@ interface EditableLabelsProps {
10
10
title ?: string ;
11
11
contentWhenEmpty ?: string ;
12
12
allExistingKeys : string [ ] ;
13
+ labelProps ?: LabelProps ;
14
+ overflowCount ?: number ; // if isCollapsible is true, this is the number of labels to show before collapsing
15
+ isCollapsible ?: boolean ;
13
16
}
14
17
15
18
export const EditableLabelsDescriptionListGroup : React . FC < EditableLabelsProps > = ( {
@@ -19,6 +22,9 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
19
22
onLabelsChange,
20
23
isArchive,
21
24
allExistingKeys,
25
+ labelProps = { } ,
26
+ isCollapsible = true ,
27
+ overflowCount,
22
28
} ) => {
23
29
const [ isEditing , setIsEditing ] = useState ( false ) ;
24
30
const [ isSavingEdits , setIsSavingEdits ] = useState ( false ) ;
@@ -90,11 +96,11 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
90
96
} ) ;
91
97
} ;
92
98
93
- const removeUnsavedLabel = ( text : string ) => {
99
+ const removeUnsavedLabel = ( index : number ) => {
94
100
if ( isSavingEdits ) {
95
101
return ;
96
102
}
97
- setUnsavedLabels ( unsavedLabels . filter ( ( label ) => label !== text ) ) ;
103
+ setUnsavedLabels ( unsavedLabels . filter ( ( _ , i ) => i !== index ) ) ;
98
104
} ;
99
105
100
106
const addNewLabel = ( ) => {
@@ -117,7 +123,7 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
117
123
} ;
118
124
119
125
const labelErrors = validateLabels ( ) ;
120
- const shouldBeRed = ( label : string , index : number ) : boolean => {
126
+ const hasDuplicate = ( label : string , index : number ) : boolean => {
121
127
const firstIndex = unsavedLabels . findIndex ( ( l ) => l === label ) ;
122
128
123
129
if ( firstIndex !== index ) {
@@ -167,9 +173,8 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
167
173
< Label
168
174
data-testid = { `editable-label-${ label } ` }
169
175
key = { label + index }
170
- color = { shouldBeRed ( label , index ) ? 'red' : 'blue' }
171
176
isEditable = { ! isSavingEdits }
172
- onClose = { ( ) => removeUnsavedLabel ( label ) }
177
+ onClose = { ( ) => removeUnsavedLabel ( index ) }
173
178
closeBtnProps = { {
174
179
isDisabled : isSavingEdits ,
175
180
'data-testid' : `remove-label-${ label } ` ,
@@ -180,6 +185,9 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
180
185
'aria-label' : 'Edit label' ,
181
186
'data-testid' : `edit-label-input-${ label } ` ,
182
187
} }
188
+ { ...labelProps }
189
+ color = { hasDuplicate ( label , index ) ? 'red' : ( labelProps . color ?? 'blue' ) }
190
+ variant = { hasDuplicate ( label , index ) ? 'filled' : labelProps . variant }
183
191
>
184
192
{ label }
185
193
</ Label >
@@ -236,9 +244,10 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
236
244
data-testid = "display-label-group"
237
245
defaultIsOpen = { hasSavedEdits }
238
246
key = { String ( hasSavedEdits ) } // Force this to fully remount when we change defaultIsOpen
247
+ numLabels = { isCollapsible ? overflowCount : labels . length }
239
248
>
240
249
{ labels . map ( ( label ) => (
241
- < Label key = { label } color = "blue" data-testid = "label" >
250
+ < Label key = { label } color = "blue" data-testid = "label" { ... labelProps } >
242
251
{ label }
243
252
</ Label >
244
253
) ) }
0 commit comments