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

fix(breadcrumbs): trigger change event on breadcrumbs via keyboard #4769

Merged
merged 3 commits into from
Sep 25, 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
7 changes: 7 additions & 0 deletions packages/breadcrumbs/src/BreadcrumbItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export class BreadcrumbItem extends LikeAnchor(Focusable) {
}
}

protected handleKeyDown(event: KeyboardEvent): void {
if (event.key === 'Enter' || event.keyCode === 13) {
this.handleClick(event);
}
}

protected renderLink(): TemplateResult {
return html`
<a
Expand All @@ -91,6 +97,7 @@ export class BreadcrumbItem extends LikeAnchor(Focusable) {
aria-current=${ifDefined(
this.isLastOfType ? 'page' : undefined
)}
@keydown=${this.handleKeyDown}
@click=${this.handleClick}
>
<slot></slot>
Expand Down
28 changes: 28 additions & 0 deletions packages/breadcrumbs/test/breadcrumbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { testForLitDevWarnings } from '../../../test/testing-helpers.js';

import '@spectrum-web-components/breadcrumbs/sp-breadcrumbs.js';
import '@spectrum-web-components/breadcrumbs/sp-breadcrumb-item.js';
import { sendKeys } from '@web/test-runner-commands';

describe('Breadcrumbs', () => {
testForLitDevWarnings(
Expand Down Expand Up @@ -166,4 +167,31 @@ describe('Breadcrumbs', () => {
expect(changeSpy).to.have.been.calledOnce;
expect(changeSpy).to.have.been.calledWith('0');
});

it('should emit a change event on Enter keypress', async () => {
const changeSpy = spy();

const el = await fixture<Breadcrumbs>(html`
<sp-breadcrumbs
@change=${(
event: Event & { detail: BreadcrumbSelectDetail }
) => {
changeSpy(event.detail.value);
}}
>
${getBreadcrumbs(4)}
</sp-breadcrumbs>
`);

await elementUpdated(el);

// Simulate a click from the visible breadcrumb.
const breadcrumbs = el.querySelectorAll('sp-breadcrumb-item');

breadcrumbs[1].focus();
await sendKeys({ press: 'Enter' });

expect(changeSpy).to.have.been.calledOnce;
expect(changeSpy).to.have.been.calledWith('1');
});
});
Loading