Skip to content

Commit

Permalink
fix(breadcrumbs): trigger change event on breadcrumbs via keyboard (#…
Browse files Browse the repository at this point in the history
…4769)

* fix(breadcrumbs): trigger change event on breadcrumbs via keyboard

---------

Co-authored-by: rmanole <[email protected]>
Co-authored-by: Rajdeep Chandra <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent a2b6bea commit e14d082
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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');
});
});

0 comments on commit e14d082

Please sign in to comment.