Skip to content

Commit 1f4f40b

Browse files
authored
Allow to change style and collapsible in EditableLabelsDescriptionListGroup and allow to set truncateMaxLines in EditableTextDescriptionListGroup (#23)
1 parent b1d97f8 commit 1f4f40b

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

components/EditableLabelsDescriptionListGroup.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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';
33
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing';
44
import DashboardDescriptionListGroup from '~/components/DashboardDescriptionListGroup';
55

@@ -10,6 +10,9 @@ interface EditableLabelsProps {
1010
title?: string;
1111
contentWhenEmpty?: string;
1212
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;
1316
}
1417

1518
export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> = ({
@@ -19,6 +22,9 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
1922
onLabelsChange,
2023
isArchive,
2124
allExistingKeys,
25+
labelProps = {},
26+
isCollapsible = true,
27+
overflowCount,
2228
}) => {
2329
const [isEditing, setIsEditing] = useState(false);
2430
const [isSavingEdits, setIsSavingEdits] = useState(false);
@@ -90,11 +96,11 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
9096
});
9197
};
9298

93-
const removeUnsavedLabel = (text: string) => {
99+
const removeUnsavedLabel = (index: number) => {
94100
if (isSavingEdits) {
95101
return;
96102
}
97-
setUnsavedLabels(unsavedLabels.filter((label) => label !== text));
103+
setUnsavedLabels(unsavedLabels.filter((_, i) => i !== index));
98104
};
99105

100106
const addNewLabel = () => {
@@ -117,7 +123,7 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
117123
};
118124

119125
const labelErrors = validateLabels();
120-
const shouldBeRed = (label: string, index: number): boolean => {
126+
const hasDuplicate = (label: string, index: number): boolean => {
121127
const firstIndex = unsavedLabels.findIndex((l) => l === label);
122128

123129
if (firstIndex !== index) {
@@ -167,9 +173,8 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
167173
<Label
168174
data-testid={`editable-label-${label}`}
169175
key={label + index}
170-
color={shouldBeRed(label, index) ? 'red' : 'blue'}
171176
isEditable={!isSavingEdits}
172-
onClose={() => removeUnsavedLabel(label)}
177+
onClose={() => removeUnsavedLabel(index)}
173178
closeBtnProps={{
174179
isDisabled: isSavingEdits,
175180
'data-testid': `remove-label-${label}`,
@@ -180,6 +185,9 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
180185
'aria-label': 'Edit label',
181186
'data-testid': `edit-label-input-${label}`,
182187
}}
188+
{...labelProps}
189+
color={hasDuplicate(label, index) ? 'red' : (labelProps.color ?? 'blue')}
190+
variant={hasDuplicate(label, index) ? 'filled' : labelProps.variant}
183191
>
184192
{label}
185193
</Label>
@@ -236,9 +244,10 @@ export const EditableLabelsDescriptionListGroup: React.FC<EditableLabelsProps> =
236244
data-testid="display-label-group"
237245
defaultIsOpen={hasSavedEdits}
238246
key={String(hasSavedEdits)} // Force this to fully remount when we change defaultIsOpen
247+
numLabels={isCollapsible ? overflowCount : labels.length}
239248
>
240249
{labels.map((label) => (
241-
<Label key={label} color="blue" data-testid="label">
250+
<Label key={label} color="blue" data-testid="label" {...labelProps}>
242251
{label}
243252
</Label>
244253
))}

components/EditableTextDescriptionListGroup.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type EditableTextDescriptionListGroupProps = Pick<
1414
baseTestId?: string;
1515
isArchive?: boolean;
1616
editableVariant: 'TextInput' | 'TextArea';
17+
truncateMaxLines?: number;
1718
};
1819

1920
const EditableTextDescriptionListGroup: React.FC<EditableTextDescriptionListGroupProps> = ({
@@ -24,6 +25,7 @@ const EditableTextDescriptionListGroup: React.FC<EditableTextDescriptionListGrou
2425
saveEditedValue,
2526
baseTestId,
2627
editableVariant,
28+
truncateMaxLines = 12,
2729
}) => {
2830
const [isEditing, setIsEditing] = React.useState(false);
2931
const [unsavedValue, setUnsavedValue] = React.useState(value);
@@ -86,7 +88,7 @@ const EditableTextDescriptionListGroup: React.FC<EditableTextDescriptionListGrou
8688
<ExpandableSection
8789
data-testid={baseTestId}
8890
variant="truncate"
89-
truncateMaxLines={12}
91+
truncateMaxLines={truncateMaxLines}
9092
toggleText={isTextExpanded ? 'Show less' : 'Show more'}
9193
onToggle={(_event, isExpanded) => setIsTextExpanded(isExpanded)}
9294
isExpanded={isTextExpanded}

0 commit comments

Comments
 (0)