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(Breadcrunbs): allow items to be disabled independently #1962

Merged
merged 1 commit into from
Nov 27, 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
6 changes: 3 additions & 3 deletions src/components/lab/Breadcrumbs/BreadcrumbItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function BreadcrumbItem(props: BreadcrumbProps) {
}

const handleAction = (event: React.MouseEvent | React.KeyboardEvent) => {
if (props.disabled || props.current) {
if (props.disabled) {
event.preventDefault();
return;
}
Expand All @@ -43,7 +43,7 @@ export function BreadcrumbItem(props: BreadcrumbProps) {
}
};

const isDisabled = props.disabled || props.current;
const isDisabled = props.disabled;
let linkProps: React.AnchorHTMLAttributes<HTMLElement> = {
title,
onClick: handleAction,
Expand All @@ -68,7 +68,7 @@ export function BreadcrumbItem(props: BreadcrumbProps) {
}

if (props.current) {
linkProps['aria-current'] = 'page';
linkProps['aria-current'] = props['aria-current'] ?? 'page';
}

if (props.itemType === 'menu') {
Expand Down
4 changes: 3 additions & 1 deletion src/components/lab/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export interface BreadcrumbsItemProps {
ping?: string;
referrerPolicy?: React.HTMLAttributeReferrerPolicy;
'aria-label'?: string;
'aria-current'?: React.AriaAttributes['aria-current'];
routerOptions?: RouterOptions;
disabled?: boolean;
}

function Item(_props: BreadcrumbsItemProps): React.ReactElement | null {
Expand Down Expand Up @@ -237,7 +239,7 @@ export const Breadcrumbs = React.forwardRef(function Breadcrumbs(
{...child.props}
key={key}
current={isCurrent}
disabled={props.disabled}
disabled={props.disabled || child.props.disabled}
onAction={handleAction}
navigate={navigate}
>
Expand Down
57 changes: 57 additions & 0 deletions src/components/lab/Breadcrumbs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,62 @@ LANDING_BLOCK-->

<!--/GITHUB_BLOCK-->

### Disabled items

<!--LANDING_BLOCK

<ExampleBlock
code={`
<Breadcrumbs>
<Breadcrumbs.Item href="#Region">Region</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Country" disabled>
Country
</Breadcrumbs.Item>
<Breadcrumbs.Item href="#City">City</Breadcrumbs.Item>
<Breadcrumbs.Item href="#District">District</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Street" disabled>
Street
</Breadcrumbs.Item>
</Breadcrumbs>
`}
>
<UIKit.Breadcrumbs>
<UIKit.Breadcrumbs.Item href="#Region">Region</UIKit.Breadcrumbs.Item>
<UIKit.Breadcrumbs.Item href="#Country" disabled>
Country
</UIKit.Breadcrumbs.Item>
<UIKit.Breadcrumbs.Item href="#City">City</UIKit.Breadcrumbs.Item>
<UIKit.Breadcrumbs.Item href="#District">District</UIKit.Breadcrumbs.Item>
<UIKit.Breadcrumbs.Item href="#Street" disabled>
Street
</UIKit.Breadcrumbs.Item>
</UIKit.Breadcrumbs>
</ExampleBlock>

LANDING_BLOCK-->

<!--GITHUB_BLOCK-->

```jsx
<Breadcrumbs>
<Breadcrumbs.Item href="#Region">Region</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Country" disabled>
Country
</Breadcrumbs.Item>
<Breadcrumbs.Item href="#City">City</Breadcrumbs.Item>
<Breadcrumbs.Item href="#District">District</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Street" disabled>
Street
</Breadcrumbs.Item>
</Breadcrumbs>
```

<!-- Storybook example -->

<BreadcrumbsDisabledItems />

<!--/GITHUB_BLOCK-->

## Properties

| Name | Description | Type | Default |
Expand Down Expand Up @@ -499,3 +555,4 @@ LANDING_BLOCK-->
| href | A URL to link to. | `string` | |
| target | The target window for the link. | `React.HTMLAttributeAnchorTarget` | |
| rel | The relationship between the linked resource and the current page. | `string` | |
| disabled | Whether the BreadcrumbsItem is disabled. | `boolean` | |
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,19 @@ export const WithIcons = {
export const Landmarks = {
render: (args) => <nav aria-label="Breadcrumb">{Links.render(args)}</nav>,
} satisfies Story;

export const DisabledItems = {
render: (args) => (
<Breadcrumbs {...args}>
<Breadcrumbs.Item href="#Region">Region</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Country" disabled>
Country
</Breadcrumbs.Item>
<Breadcrumbs.Item href="#City">City</Breadcrumbs.Item>
<Breadcrumbs.Item href="#District">District</Breadcrumbs.Item>
<Breadcrumbs.Item href="#Street" disabled>
Street
</Breadcrumbs.Item>
</Breadcrumbs>
),
} satisfies Story;
2 changes: 2 additions & 0 deletions src/components/lab/Breadcrumbs/__stories__/Docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const BreadcrumbsRootContext = () => <Canvas of={Stories.RootContext} sou
export const BreadcrumbsSeparator = () => <Canvas of={Stories.Separator} sourceState="none" />;
export const BreadcrumbsWithIcons = () => <Canvas of={Stories.WithIcons} sourceState="none" />;
export const BreadcrumbsLandmarks = () => <Canvas of={Stories.Landmarks} sourceState="none" />;
export const BreadcrumbsDisabledItems = () => <Canvas of={Stories.DisabledItems} sourceState="none" />;

<Meta of={Stories} />

Expand All @@ -32,6 +33,7 @@ export const BreadcrumbsLandmarks = () => <Canvas of={Stories.Landmarks} sourceS
BreadcrumbsSeparator,
BreadcrumbsWithIcons,
BreadcrumbsLandmarks,
BreadcrumbsDisabledItems,
},
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ it('handles multiple items', () => {
<Breadcrumbs>
<Breadcrumbs.Item>Folder 1</Breadcrumbs.Item>
<Breadcrumbs.Item>Folder 2</Breadcrumbs.Item>
<Breadcrumbs.Item>Folder 3</Breadcrumbs.Item>
<Breadcrumbs.Item disabled>Folder 3</Breadcrumbs.Item>
</Breadcrumbs>,
);
const item1 = screen.getByText('Folder 1');
Expand All @@ -32,6 +32,7 @@ it('handles multiple items', () => {
expect(item2).not.toHaveAttribute('aria-current');
const item3 = screen.getByText('Folder 3');
expect(item3.tabIndex).toBe(-1);
expect(item3).toHaveAttribute('aria-disabled', 'true');
expect(item3).toHaveAttribute('aria-current', 'page');
});

Expand Down
Loading