Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(badge): add disabled badge variant #10445

Merged
merged 6 commits into from
Jun 6, 2024
Merged
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
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,
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>
);
4 changes: 3 additions & 1 deletion packages/react-core/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ const ButtonBase: React.FunctionComponent<ButtonProps> = ({
)}
{countOptions && (
<span className={css(styles.buttonCount, countOptions.className)}>
<Badge isRead={countOptions.isRead}>{countOptions.count}</Badge>
<Badge isRead={countOptions.isRead} isDisabled={isDisabled}>
{countOptions.count}
</Badge>
</span>
)}
</Component>
Expand Down
Loading