Skip to content

Commit

Permalink
feat(Breadcrunbs): allow items to be disabled independently (#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Nov 27, 2024
1 parent 2be5eb8 commit 301e4ab
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
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` | |
16 changes: 16 additions & 0 deletions src/components/lab/Breadcrumbs/__stories__/Breadcrumbs.stories.tsx
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

0 comments on commit 301e4ab

Please sign in to comment.