Skip to content

Commit

Permalink
feat(badge): add disabled badge variant
Browse files Browse the repository at this point in the history
  • Loading branch information
evwilkin committed May 23, 2024
1 parent a104a4b commit e88f691
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/react-core/src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {
screenReaderText?: string;
/** Adds styling to the badge to indicate it has been read */
isRead?: boolean;
/** Adds styling to the badge to indicate it is disabled */
isDisabled?: boolean;
/** content rendered inside the Badge */
children?: React.ReactNode;
/** additional classes added to the Badge */
Expand All @@ -15,14 +17,20 @@ export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {

export const Badge: React.FunctionComponent<BadgeProps> = ({
isRead = false,
isDisabled = false,
className = '',
children = '',
screenReaderText,
...props
}: BadgeProps) => (
<span
{...props}
className={css(styles.badge, (isRead ? styles.modifiers.read : styles.modifiers.unread) as any, className)}
className={css(
styles.badge,
(isRead ? styles.modifiers.read : styles.modifiers.unread) as any,
(isDisabled ? styles.modifiers.disabled : '') as any,
className
)}
>
{children}
{screenReaderText && <span className="pf-v6-screen-reader">{screenReaderText}</span>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ test('Renders with class name pf-m-read when isRead prop is true', () => {
expect(screen.getByText('Test')).toHaveClass('pf-m-read');
});

test(`Renders with class name ${styles.modifiers.disabled} when isDisabled prop is true`, () => {
render(<Badge isDisabled>Test</Badge>);
expect(screen.getByText('Test')).toHaveClass(styles.modifiers.disabled);
});

test('Does not render pf-v6-screen-reader class by default', () => {
render(<Badge>Test</Badge>);
expect(screen.getByText('Test')).not.toContainHTML('<span class="pf-v6-screen-reader"></span>');
Expand Down
5 changes: 5 additions & 0 deletions packages/react-core/src/components/Badge/examples/Badge.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ import './Badge.css';

```ts file="./BadgeUnread.tsx"
```

## Disabled

```ts file="./BadgeDisabled.tsx"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { Badge } from '@patternfly/react-core';

export const BadgeRead: React.FunctionComponent = () => (
<React.Fragment>
<Badge key={1} isDisabled isRead>
7
</Badge>
<Badge key={2} isDisabled isRead>
24
</Badge>
<Badge key={3} isDisabled>
240
</Badge>
<Badge key={4} isDisabled>
999+
</Badge>
</React.Fragment>
);

0 comments on commit e88f691

Please sign in to comment.