Skip to content

Commit

Permalink
chore(Badge/MenuToggle): updated implementation of badge toggle (#10172)
Browse files Browse the repository at this point in the history
* chore(Badge/MenuToggle): updated implementation of badge toggle

* Deleted redundant demo that was not being rendered
  • Loading branch information
thatblindgeye authored Mar 27, 2024
1 parent c3f0ac6 commit c12de7a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 317 deletions.
9 changes: 0 additions & 9 deletions packages/react-core/src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Badge/badge';
import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';

export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {
/** Text announced by screen readers to indicate the current content/status of the badge. */
screenReaderText?: string;
/** Adds styling to the badge to indicate it has been read */
isRead?: boolean;
/** Adds styling to the badge to indicate it has a toggle. A badge with a toggle must be passed to a MenuToggle component. */
isToggle?: boolean;
/** content rendered inside the Badge */
children?: React.ReactNode;
/** additional classes added to the Badge */
Expand All @@ -18,7 +15,6 @@ export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {

export const Badge: React.FunctionComponent<BadgeProps> = ({
isRead = false,
isToggle = false,
className = '',
children = '',
screenReaderText,
Expand All @@ -30,11 +26,6 @@ export const Badge: React.FunctionComponent<BadgeProps> = ({
>
{children}
{screenReaderText && <span className="pf-v6-screen-reader">{screenReaderText}</span>}
{isToggle && (
<span className={css(styles.badgeToggleIcon)}>
<CaretDownIcon />
</span>
)}
</span>
);
Badge.displayName = 'Badge';
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ test('Renders with class name pf-m-read when isRead prop is true', () => {
expect(screen.getByText('Test')).toHaveClass('pf-m-read');
});

test('Renders with toggle styles and icon when isToggle prop is true', () => {
render(
<Badge isRead={true} isToggle={true}>
Test
</Badge>
);
expect(screen.getByText('Test').lastChild).toHaveClass(styles.badgeToggleIcon);
});

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: 0 additions & 5 deletions packages/react-core/src/components/Badge/examples/Badge.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ import './Badge.css';

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

### Badge as toggle

```ts file="./BadgeToggle.tsx"
```
13 changes: 0 additions & 13 deletions packages/react-core/src/components/Badge/examples/BadgeToggle.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MenuToggleElement
} from '@patternfly/react-core';
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';

const dropdownItems = [
<DropdownItem
Expand Down Expand Up @@ -66,14 +65,17 @@ export const BreadcrumbDropdown: React.FunctionComponent = () => {
onSelect={onSelect}
onOpenChange={(isOpen: boolean) => setIsOpen(isOpen)}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle ref={toggleRef} onClick={onToggle} isExpanded={isOpen} variant="plain">
<Badge isRead screenReaderText="additional items">
{dropdownItems.length}{' '}
<span>
<CaretDownIcon />
</span>
</Badge>
</MenuToggle>
<MenuToggle
badge={
<Badge isRead screenReaderText="additional items">
{dropdownItems.length}
</Badge>
}
ref={toggleRef}
onClick={onToggle}
isExpanded={isOpen}
variant="plainText"
/>
)}
isOpen={isOpen}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-i
import LayerGroupIcon from '@patternfly/react-icons/dist/esm/icons/layer-group-icon';
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';

export const MenuWithDrilldownBreadcrumbs: React.FunctionComponent = () => {
const [menuDrilledIn, setMenuDrilledIn] = React.useState<string[]>([]);
Expand Down Expand Up @@ -117,14 +116,17 @@ export const MenuWithDrilldownBreadcrumbs: React.FunctionComponent = () => {
isOpen={isOpen}
onOpenChange={(isOpen: boolean) => onToggle(isOpen, 'app')}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle ref={toggleRef} onClick={() => onToggle(true, 'app')} isExpanded={isOpen} variant="plain">
<Badge isRead screenReaderText="additional item">
1{' '}
<span>
<CaretDownIcon />
</span>
</Badge>
</MenuToggle>
<MenuToggle
badge={
<Badge isRead screenReaderText="additional item">
1
</Badge>
}
ref={toggleRef}
onClick={() => onToggle(true, 'app')}
isExpanded={isOpen}
variant="plainText"
/>
)}
>
<DropdownList>
Expand Down Expand Up @@ -161,14 +163,17 @@ export const MenuWithDrilldownBreadcrumbs: React.FunctionComponent = () => {
isOpen={isOpen}
onOpenChange={(isOpen: boolean) => onToggle(isOpen, 'label')}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle ref={toggleRef} onClick={() => onToggle(true, 'label')} isExpanded={isOpen} variant="plain">
<Badge isRead screenReaderText="additional item">
1{' '}
<span>
<CaretDownIcon />
</span>
</Badge>
</MenuToggle>
<MenuToggle
badge={
<Badge isRead screenReaderText="additional item">
1
</Badge>
}
ref={toggleRef}
onClick={() => onToggle(true, 'label')}
isExpanded={isOpen}
variant="plainText"
/>
)}
>
<DropdownList>
Expand Down Expand Up @@ -217,14 +222,17 @@ export const MenuWithDrilldownBreadcrumbs: React.FunctionComponent = () => {
isOpen={isOpen}
onOpenChange={(isOpen: boolean) => onToggle(isOpen, 'pause-app')}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle ref={toggleRef} onClick={() => onToggle(true, 'pause-app')} isExpanded={isOpen} variant="plain">
<Badge isRead screenReaderText="additional item">
1{' '}
<span>
<CaretDownIcon />
</span>
</Badge>
</MenuToggle>
<MenuToggle
badge={
<Badge isRead screenReaderText="additional item">
1
</Badge>
}
ref={toggleRef}
onClick={() => onToggle(true, 'pause-app')}
isExpanded={isOpen}
variant="plainText"
/>
)}
>
<DropdownList>
Expand Down Expand Up @@ -262,18 +270,16 @@ export const MenuWithDrilldownBreadcrumbs: React.FunctionComponent = () => {
onOpenChange={(isOpen: boolean) => onToggle(isOpen, 'pause-label')}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle
badge={
<Badge isRead screenReaderText="additional item">
1
</Badge>
}
ref={toggleRef}
onClick={() => onToggle(true, 'pause-label')}
isExpanded={isOpen}
variant="plain"
>
<Badge isRead screenReaderText="additional item">
1{' '}
<span>
<CaretDownIcon />
</span>
</Badge>
</MenuToggle>
variant="plainText"
/>
)}
>
<DropdownList>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ import { MenuToggle } from '@patternfly/react-core';

### With a badge

To display a count of selected items in a toggle, use the `badge` property.
To display a count of selected items in a toggle, use the `badge` property. You can also pass in `variant="plainText"` for a badge only toggle.

```ts
import React from 'react';
import { MenuToggle, Badge } from '@patternfly/react-core';

<MenuToggle badge={<Badge>4 selected</Badge>}>Count</MenuToggle>

<React.Fragment>
<MenuToggle badge={<Badge>4 selected</Badge>}>Count</MenuToggle>
<MenuToggle variant="plainText" badge={<Badge screenReaderText="additional items">4</Badge>} />
</React.Fragment>
```
### With icons
Expand Down
Loading

0 comments on commit c12de7a

Please sign in to comment.