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

Fire sl-select when clicking an element inside a menu-item #1599

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti

## Next

- Fixed a bug in `<sl-menu>` that caused it not to fire the `sl-select` event if you clicked an element inside of a `<sl-menu-item>` [#1599]
- Fixed a bug [in the localize dependency](https://github.com/shoelace-style/localize/issues/20) that caused underscores in language codes to throw a `RangeError`
- Fixed a bug in `<sl-copy-button>` that prevented exported tooltip parts from being styled [#1586]
- Updated `@shoelace-style/localize` to 3.1.0
Expand Down
13 changes: 8 additions & 5 deletions src/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { html } from 'lit';
import { query } from 'lit/decorators.js';
import ShoelaceElement from '../../internal/shoelace-element.js';
import SlMenuItem from '../menu-item/menu-item.component.js';
import styles from './menu.styles.js';
import type { CSSResultGroup } from 'lit';
import type SlMenuItem from '../menu-item/menu-item.component.js';
export interface MenuSelectEventDetail {
item: SlMenuItem;
}
Expand All @@ -29,11 +29,14 @@ export default class SlMenu extends ShoelaceElement {
}

private handleClick(event: MouseEvent) {
if (!(event.target instanceof SlMenuItem)) {
return;
}
const menuItemTypes = ['menuitem', 'menuitemcheckbox'];

const target = event.composedPath().find((el: Element) => menuItemTypes.includes(el?.getAttribute?.('role') || ''));

if (!target) return;

const item: SlMenuItem = event.target;
// This isn't true. But we use it for TypeScript checks below.
const item = target as SlMenuItem;

if (item.type === 'checkbox') {
item.checked = !item.checked;
Expand Down
22 changes: 22 additions & 0 deletions src/components/menu/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ describe('<sl-menu>', () => {
expect(selectHandler).to.not.have.been.called;
});
});

// @see https://github.com/shoelace-style/shoelace/issues/1596
it('Should fire "sl-select" when clicking an element within a menu-item', async () => {
// eslint-disable-next-line
const selectHandler = sinon.spy(() => {});

const menu: SlMenu = await fixture(html`
<sl-menu>
<sl-menu-label>What Can You Ask Trebellar?</sl-menu-label>
<sl-menu-item value="p1">
<ui-icon slot="prefix" size="1rem" name="chat"></ui-icon>
<span class="q">X</span>
</sl-menu-item>
</sl-menu>
`);
KonnorRogers marked this conversation as resolved.
Show resolved Hide resolved

menu.addEventListener('sl-select', selectHandler);
const span = menu.querySelector('span')!;
await clickOnElement(span);

expect(selectHandler).to.have.been.calledOnce;
});