Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions packages/react-core/src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-erro
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon';
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';

/** Semantic status for labels (default icon and status styling). */
export enum LabelStatus {
success = 'success',
warning = 'warning',
danger = 'danger',
info = 'info',
custom = 'custom'
}

/** Label palette color when not using the `status` prop. */
export enum LabelColor {
blue = 'blue',
teal = 'teal',
green = 'green',
orange = 'orange',
purple = 'purple',
red = 'red',
orangered = 'orangered',
grey = 'grey',
yellow = 'yellow'
}
Comment on lines +25 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Keep Label color tests in sync with the exported enum.

Now that LabelColor is a public source of truth, tests should derive cases from it (or include all members) so new values like orangered are always covered. The current hardcoded array in packages/react-core/src/components/Label/__tests__/Label.test.tsx can drift.

Suggested test alignment
-const labelColors = ['blue', 'teal', 'green', 'orange', 'purple', 'red', 'grey', 'yellow'];
+import { LabelColor } from '../Label';
+const labelColors = Object.values(LabelColor);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-core/src/components/Label/Label.tsx` around lines 25 - 36,
Tests for Label currently use a hardcoded color array that can drift from the
exported LabelColor enum; update the tests in Label.test.tsx to derive test
cases from the LabelColor enum by importing LabelColor and using
Object.values(LabelColor) (or Object.keys/values as appropriate) to drive the
color matrix/snapshots so any new member (e.g., orangered) is automatically
covered, and replace the existing hardcoded color list with that derived list in
the relevant test loops/assertions.


export interface LabelProps extends React.HTMLProps<HTMLSpanElement> {
/** Content rendered inside the label. */
children?: React.ReactNode;
Expand Down Expand Up @@ -82,30 +104,30 @@ export interface LabelProps extends React.HTMLProps<HTMLSpanElement> {
}) => React.ReactNode;
}

const colorStyles = {
blue: styles.modifiers.blue,
teal: styles.modifiers.teal,
green: styles.modifiers.green,
orange: styles.modifiers.orange,
purple: styles.modifiers.purple,
red: styles.modifiers.red,
orangered: styles.modifiers.orangered,
yellow: styles.modifiers.yellow,
grey: ''
const colorStyles: Record<LabelColor, string> = {
[LabelColor.blue]: styles.modifiers.blue,
[LabelColor.teal]: styles.modifiers.teal,
[LabelColor.green]: styles.modifiers.green,
[LabelColor.orange]: styles.modifiers.orange,
[LabelColor.purple]: styles.modifiers.purple,
[LabelColor.red]: styles.modifiers.red,
[LabelColor.orangered]: styles.modifiers.orangered,
[LabelColor.yellow]: styles.modifiers.yellow,
[LabelColor.grey]: ''
};

const statusIcons = {
success: <RhUiCheckCircleFillIcon />,
warning: <RhUiWarningFillIcon />,
danger: <RhUiErrorFillIcon />,
info: <RhUiInformationFillIcon />,
custom: <RhUiNotificationFillIcon />
const statusIcons: Record<LabelStatus, React.ReactNode> = {
[LabelStatus.success]: <RhUiCheckCircleFillIcon />,
[LabelStatus.warning]: <RhUiWarningFillIcon />,
[LabelStatus.danger]: <RhUiErrorFillIcon />,
[LabelStatus.info]: <RhUiInformationFillIcon />,
[LabelStatus.custom]: <RhUiNotificationFillIcon />
};

export const Label: React.FunctionComponent<LabelProps> = ({
children,
className = '',
color = 'grey',
color = LabelColor.grey,
variant = 'filled',
status,
isCompact = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fragment } from 'react';
import { Label } from '@patternfly/react-core';
import { Label, LabelColor } from '@patternfly/react-core';
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';

Expand Down Expand Up @@ -42,24 +42,24 @@ export const LabelCompact: React.FunctionComponent = () => (
>
Compact clickable removable (disabled)
</Label>
<Label variant="outline" color="blue" isCompact icon={<CubeIcon />}>
<Label variant="outline" color={LabelColor.blue} isCompact icon={<CubeIcon />}>
Compact icon
</Label>
<Label variant="outline" color="blue" isCompact onClose={() => Function.prototype}>
<Label variant="outline" color={LabelColor.blue} isCompact onClose={() => Function.prototype}>
Compact removable
</Label>
<Label variant="outline" color="blue" isCompact icon={<CubeIcon />} onClose={() => Function.prototype}>
<Label variant="outline" color={LabelColor.blue} isCompact icon={<CubeIcon />} onClose={() => Function.prototype}>
Compact icon removable
</Label>
<Label variant="outline" color="blue" isCompact href="#compact">
<Label variant="outline" color={LabelColor.blue} isCompact href="#compact">
Compact link
</Label>
<Label variant="outline" color="blue" isCompact href="#compact" onClose={() => Function.prototype}>
<Label variant="outline" color={LabelColor.blue} isCompact href="#compact" onClose={() => Function.prototype}>
Compact link removable
</Label>
<Label
variant="outline"
color="blue"
color={LabelColor.blue}
isCompact
icon={<CubeIcon />}
onClose={() => Function.prototype}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Label } from '@patternfly/react-core';
import { Label, LabelColor } from '@patternfly/react-core';
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';

export const LabelCustomRender: React.FunctionComponent = () => (
<Label
color="blue"
color={LabelColor.blue}
icon={<RhUiInformationFillIcon />}
onClose={() => Function.prototype}
render={({ className, content, componentRef }) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fragment, useState } from 'react';
import { Label } from '@patternfly/react-core';
import { Label, LabelColor } from '@patternfly/react-core';

export const LabelEditable: React.FunctionComponent = () => {
const [labelText, setLabelText] = useState('Editable label');
Expand All @@ -24,7 +24,7 @@ export const LabelEditable: React.FunctionComponent = () => {
return (
<Fragment>
<Label
color="blue"
color={LabelColor.blue}
onClose={() => {}}
closeBtnAriaLabel="Custom close button for editable label"
onEditCancel={onEditCancel}
Expand All @@ -38,7 +38,7 @@ export const LabelEditable: React.FunctionComponent = () => {
{labelText}
</Label>
<Label
color="grey"
color={LabelColor.grey}
isCompact
onClose={() => {}}
closeBtnAriaLabel="Custom close button for compact editable label"
Expand Down
Loading
Loading