Skip to content

Commit 719f078

Browse files
feat(NotificationBadge): refactor to use status buttons for penta (#10020)
* feat(NotificationBadge): refactor to use status buttons for penta * chore(NotificationBadge): update integration test * chore(NotificationBadge): fix bug causing icon to not be centered
1 parent d00a526 commit 719f078

File tree

8 files changed

+42
-55
lines changed

8 files changed

+42
-55
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as React from 'react';
22
import { Button, ButtonVariant, ButtonProps } from '../Button';
3-
import { css } from '@patternfly/react-styles';
4-
import styles from '@patternfly/react-styles/css/components/NotificationBadge/notification-badge';
53
import AttentionBellIcon from '@patternfly/react-icons/dist/esm/icons/attention-bell-icon';
64
import BellIcon from '@patternfly/react-icons/dist/esm/icons/bell-icon';
75

@@ -42,20 +40,26 @@ export const NotificationBadge: React.FunctionComponent<NotificationBadgeProps>
4240
isExpanded = false,
4341
...props
4442
}: NotificationBadgeProps) => {
45-
let notificationChild = icon;
46-
if (children !== undefined) {
47-
notificationChild = children;
48-
} else if (variant === NotificationBadgeVariant.attention) {
49-
notificationChild = attentionIcon;
50-
}
43+
const hasCount = count > 0;
44+
const hasChildren = children !== undefined;
45+
const isAttention = variant === NotificationBadgeVariant.attention;
46+
47+
const notificationIcon = isAttention ? attentionIcon : icon;
48+
const notificationContent = hasChildren ? children : notificationIcon;
49+
50+
const [iconProp, notificationChild] = hasCount ? [notificationContent, count] : [undefined, notificationContent];
51+
5152
return (
52-
<Button variant={ButtonVariant.plain} className={className} aria-expanded={isExpanded} {...props}>
53-
<span
54-
className={css(styles.notificationBadge, styles.modifiers[variant], isExpanded && styles.modifiers.expanded)}
55-
>
56-
{notificationChild}
57-
{count > 0 && <span className={css(`${styles.notificationBadge}__count`)}>{count}</span>}
58-
</span>
53+
<Button
54+
variant={ButtonVariant.stateful}
55+
className={className}
56+
aria-expanded={isExpanded}
57+
state={variant}
58+
isClicked={isExpanded}
59+
icon={iconProp}
60+
{...props}
61+
>
62+
{notificationChild}
5963
</Button>
6064
);
6165
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ test('Renders with aria-expanded="true" when isExpanded is passed in.', () => {
3030
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'true');
3131
});
3232

33-
test('Does not render with .pf-m-expanded when isExpanded is not passed in.', () => {
33+
test('Does not render with .pf-m-clicked when isExpanded is not passed in.', () => {
3434
render(<NotificationBadge />);
3535

36-
expect(screen.getByRole('button').firstChild).not.toHaveClass('pf-m-expanded');
36+
expect(screen.getByRole('button')).not.toHaveClass('pf-m-clicked');
3737
});
3838

39-
test('Renders with .pf-m-expanded when isExpanded is passed in.', () => {
39+
test('Renders with .pf-m-clicked when isExpanded is passed in.', () => {
4040
render(<NotificationBadge isExpanded />);
4141

42-
expect(screen.getByRole('button').firstChild).toHaveClass('pf-m-expanded');
42+
expect(screen.getByRole('button')).toHaveClass('pf-m-clicked');
4343
});

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

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ exports[`NotificationBadge count 1`] = `
55
<button
66
aria-disabled="false"
77
aria-expanded="false"
8-
class="pf-v5-c-button pf-m-plain"
9-
data-ouia-component-id="OUIA-Generated-Button-plain-3"
8+
class="pf-v5-c-button pf-m-stateful pf-m-read"
9+
data-ouia-component-id="OUIA-Generated-Button-stateful-3"
1010
data-ouia-component-type="PF5/Button"
1111
data-ouia-safe="true"
1212
type="button"
1313
>
1414
<span
15-
class="pf-v5-c-notification-badge pf-m-read"
15+
class="pf-v5-c-button__icon pf-m-start"
1616
>
1717
<svg
1818
aria-hidden="true"
@@ -27,12 +27,8 @@ exports[`NotificationBadge count 1`] = `
2727
d="M448,0 C465.333333,0 480.333333,6.33333333 493,19 C505.666667,31.6666667 512,46.6666667 512,64 L512,106 L514.23,106.45 C587.89,121.39 648.48,157.24 696,214 C744,271.333333 768,338.666667 768,416 C768,500 780,568.666667 804,622 C818.666667,652.666667 841.333333,684 872,716 C873.773676,718.829136 875.780658,721.505113 878,724 C890,737.333333 896,752.333333 896,769 C896,785.666667 890,800.333333 878,813 C866,825.666667 850.666667,832 832,832 L63.3,832 C44.9533333,831.84 29.8533333,825.506667 18,813 C6,800.333333 0,785.666667 0,769 C0,752.333333 6,737.333333 18,724 L24,716 L25.06,714.9 C55.1933333,683.28 77.5066667,652.313333 92,622 C116,568.666667 128,500 128,416 C128,338.666667 152,271.333333 200,214 C248,156.666667 309.333333,120.666667 384,106 L384,63.31 C384.166667,46.27 390.5,31.5 403,19 C415.666667,6.33333333 430.666667,0 448,0 Z M576,896 L576,897.08 C575.74,932.6 563.073333,962.573333 538,987 C512.666667,1011.66667 482.666667,1024 448,1024 C413.333333,1024 383.333333,1011.66667 358,987 C332.666667,962.333333 320,932 320,896 L576,896 Z"
2828
/>
2929
</svg>
30-
<span
31-
class="pf-v5-c-notification-badge__count"
32-
>
33-
3
34-
</span>
3530
</span>
31+
3
3632
</button>
3733
</DocumentFragment>
3834
`;
@@ -42,17 +38,13 @@ exports[`false NotificationBadge needs attention 1`] = `
4238
<button
4339
aria-disabled="false"
4440
aria-expanded="false"
45-
class="pf-v5-c-button pf-m-plain"
46-
data-ouia-component-id="OUIA-Generated-Button-plain-2"
41+
class="pf-v5-c-button pf-m-stateful pf-m-attention"
42+
data-ouia-component-id="OUIA-Generated-Button-stateful-2"
4743
data-ouia-component-type="PF5/Button"
4844
data-ouia-safe="true"
4945
type="button"
5046
>
51-
<span
52-
class="pf-v5-c-notification-badge pf-m-attention"
53-
>
54-
does not need attention Badge
55-
</span>
47+
does not need attention Badge
5648
</button>
5749
</DocumentFragment>
5850
`;
@@ -62,17 +54,13 @@ exports[`true NotificationBadge needs attention 1`] = `
6254
<button
6355
aria-disabled="false"
6456
aria-expanded="false"
65-
class="pf-v5-c-button pf-m-plain"
66-
data-ouia-component-id="OUIA-Generated-Button-plain-1"
57+
class="pf-v5-c-button pf-m-stateful pf-m-attention"
58+
data-ouia-component-id="OUIA-Generated-Button-stateful-1"
6759
data-ouia-component-type="PF5/Button"
6860
data-ouia-safe="true"
6961
type="button"
7062
>
71-
<span
72-
class="pf-v5-c-notification-badge pf-m-attention"
73-
>
74-
needs attention Badge
75-
</span>
63+
needs attention Badge
7664
</button>
7765
</DocumentFragment>
7866
`;

packages/react-core/src/components/NotificationBadge/examples/NotificationBadge.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ propComponents: ['NotificationBadge']
66
---
77

88
import BellIcon from '@patternfly/react-icons/dist/esm/icons/bell-icon';
9-
import './notificationBadge.css';
109

1110
## Examples
1211

packages/react-core/src/components/NotificationBadge/examples/NotificationBadgeBasic.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const NotificationBadgeBasic: React.FunctionComponent = () => {
1919
};
2020

2121
return (
22-
<div className="pf-t-dark">
22+
<>
2323
<NotificationBadge
2424
variant={NotificationBadgeVariant.read}
2525
onClick={onReadClick}
@@ -38,6 +38,6 @@ export const NotificationBadgeBasic: React.FunctionComponent = () => {
3838
aria-label="Basic notification badge with attention variant"
3939
isExpanded={attentionExpanded}
4040
/>
41-
</div>
41+
</>
4242
);
4343
};

packages/react-core/src/components/NotificationBadge/examples/NotificationBadgeWithCount.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const NotificationBadgeWithCount: React.FunctionComponent = () => {
1919
};
2020

2121
return (
22-
<div className="pf-t-dark">
22+
<>
2323
<NotificationBadge
2424
variant={NotificationBadgeVariant.read}
2525
onClick={onReadClick}
@@ -41,6 +41,6 @@ export const NotificationBadgeWithCount: React.FunctionComponent = () => {
4141
isExpanded={attentionExpanded}
4242
count={10}
4343
/>
44-
</div>
44+
</>
4545
);
4646
};

packages/react-core/src/components/NotificationBadge/examples/notificationBadge.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/react-integration/cypress/integration/notificationbadge.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ describe('Notification Badge Demo Test', () => {
44
});
55

66
it('Verify notification badge is unread', () => {
7-
cy.get('.pf-v5-c-notification-badge').first().should('have.class', 'pf-m-unread');
8-
cy.get('.pf-v5-c-notification-badge').last().should('have.class', 'pf-m-attention');
9-
cy.get('.pf-v5-c-notification-badge__count').contains('30');
7+
cy.get('#notification-demo-badge1').should('have.class', 'pf-m-unread');
8+
cy.get('#notification-demo-badge2').should('have.class', 'pf-m-attention');
9+
cy.get('#notification-demo-badge2').contains('30');
1010
});
1111

1212
it('Verify notification badge is read after click', () => {
1313
cy.get('#notification-demo-badge1').click();
14-
cy.get('.pf-v5-c-notification-badge').first().should('have.class', 'pf-m-read');
14+
cy.get('#notification-demo-badge1').should('have.class', 'pf-m-read');
1515
cy.get('#notification-demo-badge2').click();
16-
cy.get('.pf-v5-c-notification-badge').last().should('have.class', 'pf-m-read');
16+
cy.get('#notification-demo-badge2').should('have.class', 'pf-m-read');
1717
});
1818
});

0 commit comments

Comments
 (0)