Skip to content

Commit 8f715a4

Browse files
authored
chore(Label): Added enums for LabelColor and LabelStatus (#12338)
* chore(Label): Added enums for LabelColor and LabelStatus * fix type * update more uses of ststus color * update snapshots * update imports
1 parent ea6c8d2 commit 8f715a4

22 files changed

Lines changed: 475 additions & 327 deletions

packages/react-core/src/components/Label/Label.tsx

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,35 @@ import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-erro
1313
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon';
1414
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';
1515

16+
/** Semantic status for labels (default icon and status styling). */
17+
export enum LabelStatus {
18+
success = 'success',
19+
warning = 'warning',
20+
danger = 'danger',
21+
info = 'info',
22+
custom = 'custom'
23+
}
24+
25+
/** Label palette color when not using the `status` prop. */
26+
export enum LabelColor {
27+
blue = 'blue',
28+
teal = 'teal',
29+
green = 'green',
30+
orange = 'orange',
31+
purple = 'purple',
32+
red = 'red',
33+
orangered = 'orangered',
34+
grey = 'grey',
35+
yellow = 'yellow'
36+
}
37+
1638
export interface LabelProps extends React.HTMLProps<HTMLSpanElement> {
1739
/** Content rendered inside the label. */
1840
children?: React.ReactNode;
1941
/** Additional classes added to the label. */
2042
className?: string;
2143
/** Color of the label. */
22-
color?: 'blue' | 'teal' | 'green' | 'orange' | 'purple' | 'red' | 'orangered' | 'grey' | 'yellow';
44+
color?: 'blue' | 'teal' | 'green' | 'orange' | 'purple' | 'red' | 'orangered' | 'grey' | 'yellow' | LabelColor;
2345
/** Variant of the label. */
2446
variant?: 'outline' | 'filled' | 'overflow' | 'add';
2547
/** Status of the label with a respective icon and color. Overrides the color set by the color property. */
@@ -82,30 +104,30 @@ export interface LabelProps extends React.HTMLProps<HTMLSpanElement> {
82104
}) => React.ReactNode;
83105
}
84106

85-
const colorStyles = {
86-
blue: styles.modifiers.blue,
87-
teal: styles.modifiers.teal,
88-
green: styles.modifiers.green,
89-
orange: styles.modifiers.orange,
90-
purple: styles.modifiers.purple,
91-
red: styles.modifiers.red,
92-
orangered: styles.modifiers.orangered,
93-
yellow: styles.modifiers.yellow,
94-
grey: ''
107+
const colorStyles: Record<LabelColor, string> = {
108+
[LabelColor.blue]: styles.modifiers.blue,
109+
[LabelColor.teal]: styles.modifiers.teal,
110+
[LabelColor.green]: styles.modifiers.green,
111+
[LabelColor.orange]: styles.modifiers.orange,
112+
[LabelColor.purple]: styles.modifiers.purple,
113+
[LabelColor.red]: styles.modifiers.red,
114+
[LabelColor.orangered]: styles.modifiers.orangered,
115+
[LabelColor.yellow]: styles.modifiers.yellow,
116+
[LabelColor.grey]: ''
95117
};
96118

97-
const statusIcons = {
98-
success: <RhUiCheckCircleFillIcon />,
99-
warning: <RhUiWarningFillIcon />,
100-
danger: <RhUiErrorFillIcon />,
101-
info: <RhUiInformationFillIcon />,
102-
custom: <RhUiNotificationFillIcon />
119+
const statusIcons: Record<LabelStatus, React.ReactNode> = {
120+
[LabelStatus.success]: <RhUiCheckCircleFillIcon />,
121+
[LabelStatus.warning]: <RhUiWarningFillIcon />,
122+
[LabelStatus.danger]: <RhUiErrorFillIcon />,
123+
[LabelStatus.info]: <RhUiInformationFillIcon />,
124+
[LabelStatus.custom]: <RhUiNotificationFillIcon />
103125
};
104126

105127
export const Label: React.FunctionComponent<LabelProps> = ({
106128
children,
107129
className = '',
108-
color = 'grey',
130+
color = LabelColor.grey,
109131
variant = 'filled',
110132
status,
111133
isCompact = false,

packages/react-core/src/components/Label/__tests__/Label.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import '@testing-library/jest-dom';
44

5-
import { Label } from '../Label';
5+
import { Label, LabelColor } from '../Label';
66

7-
const labelColors = ['blue', 'teal', 'green', 'orange', 'purple', 'red', 'grey', 'yellow'];
7+
const labelColors = Object.values(LabelColor);
88

99
describe('Label', () => {
1010
test('renders', () => {
@@ -51,17 +51,17 @@ describe('Label', () => {
5151
expect(asFragment()).toMatchSnapshot();
5252
});
5353

54-
labelColors.forEach((color: string) =>
54+
labelColors.forEach((color) =>
5555
test(`label with ${color} color`, () => {
56-
const { asFragment } = render(<Label color={color as any}>Something</Label>);
56+
const { asFragment } = render(<Label color={color}>Something</Label>);
5757
expect(asFragment()).toMatchSnapshot();
5858
})
5959
);
6060

61-
labelColors.forEach((color: string) =>
61+
labelColors.forEach((color) =>
6262
test(`label with ${color} color with outline variant`, () => {
6363
const { asFragment } = render(
64-
<Label color={color as any} variant="outline">
64+
<Label color={color} variant="outline">
6565
Something
6666
</Label>
6767
);

packages/react-core/src/components/Label/__tests__/__snapshots__/Label.test.tsx.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,42 @@ exports[`Label label with red color with outline variant 1`] = `
452452
</DocumentFragment>
453453
`;
454454

455+
exports[`Label label with orangered color 1`] = `
456+
<DocumentFragment>
457+
<span
458+
class="pf-v6-c-label pf-m-orangered pf-m-filled"
459+
>
460+
<span
461+
class="pf-v6-c-label__content"
462+
>
463+
<span
464+
class="pf-v6-c-label__text"
465+
>
466+
Something
467+
</span>
468+
</span>
469+
</span>
470+
</DocumentFragment>
471+
`;
472+
473+
exports[`Label label with orangered color with outline variant 1`] = `
474+
<DocumentFragment>
475+
<span
476+
class="pf-v6-c-label pf-m-orangered pf-m-outline"
477+
>
478+
<span
479+
class="pf-v6-c-label__content"
480+
>
481+
<span
482+
class="pf-v6-c-label__text"
483+
>
484+
Something
485+
</span>
486+
</span>
487+
</span>
488+
</DocumentFragment>
489+
`;
490+
455491
exports[`Label label with teal color 1`] = `
456492
<DocumentFragment>
457493
<span

packages/react-core/src/components/Label/examples/LabelCompact.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fragment } from 'react';
2-
import { Label } from '@patternfly/react-core';
2+
import { Label, LabelColor } from '@patternfly/react-core';
33
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
44
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';
55

@@ -42,24 +42,24 @@ export const LabelCompact: React.FunctionComponent = () => (
4242
>
4343
Compact clickable removable (disabled)
4444
</Label>
45-
<Label variant="outline" color="blue" isCompact icon={<CubeIcon />}>
45+
<Label variant="outline" color={LabelColor.blue} isCompact icon={<CubeIcon />}>
4646
Compact icon
4747
</Label>
48-
<Label variant="outline" color="blue" isCompact onClose={() => Function.prototype}>
48+
<Label variant="outline" color={LabelColor.blue} isCompact onClose={() => Function.prototype}>
4949
Compact removable
5050
</Label>
51-
<Label variant="outline" color="blue" isCompact icon={<CubeIcon />} onClose={() => Function.prototype}>
51+
<Label variant="outline" color={LabelColor.blue} isCompact icon={<CubeIcon />} onClose={() => Function.prototype}>
5252
Compact icon removable
5353
</Label>
54-
<Label variant="outline" color="blue" isCompact href="#compact">
54+
<Label variant="outline" color={LabelColor.blue} isCompact href="#compact">
5555
Compact link
5656
</Label>
57-
<Label variant="outline" color="blue" isCompact href="#compact" onClose={() => Function.prototype}>
57+
<Label variant="outline" color={LabelColor.blue} isCompact href="#compact" onClose={() => Function.prototype}>
5858
Compact link removable
5959
</Label>
6060
<Label
6161
variant="outline"
62-
color="blue"
62+
color={LabelColor.blue}
6363
isCompact
6464
icon={<CubeIcon />}
6565
onClose={() => Function.prototype}

packages/react-core/src/components/Label/examples/LabelCustomRender.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Label } from '@patternfly/react-core';
1+
import { Label, LabelColor } from '@patternfly/react-core';
22
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';
33

44
export const LabelCustomRender: React.FunctionComponent = () => (
55
<Label
6-
color="blue"
6+
color={LabelColor.blue}
77
icon={<RhUiInformationFillIcon />}
88
onClose={() => Function.prototype}
99
render={({ className, content, componentRef }) => (

packages/react-core/src/components/Label/examples/LabelEditable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fragment, useState } from 'react';
2-
import { Label } from '@patternfly/react-core';
2+
import { Label, LabelColor } from '@patternfly/react-core';
33

44
export const LabelEditable: React.FunctionComponent = () => {
55
const [labelText, setLabelText] = useState('Editable label');
@@ -24,7 +24,7 @@ export const LabelEditable: React.FunctionComponent = () => {
2424
return (
2525
<Fragment>
2626
<Label
27-
color="blue"
27+
color={LabelColor.blue}
2828
onClose={() => {}}
2929
closeBtnAriaLabel="Custom close button for editable label"
3030
onEditCancel={onEditCancel}
@@ -38,7 +38,7 @@ export const LabelEditable: React.FunctionComponent = () => {
3838
{labelText}
3939
</Label>
4040
<Label
41-
color="grey"
41+
color={LabelColor.grey}
4242
isCompact
4343
onClose={() => {}}
4444
closeBtnAriaLabel="Custom close button for compact editable label"

0 commit comments

Comments
 (0)