Skip to content

Commit 136c036

Browse files
Removed click event when dropdown item is disabled (#2023)
* removed click event when disabled is true * updated changelog * updated code
1 parent 4263384 commit 136c036

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

packages/webawesome/docs/docs/resources/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Components with the <wa-badge variant="warning">Experimental</wa-badge> badge sh
1212

1313
<small>TBD</small>
1414

15+
- Fixed a bug in `<wa-dropdown-item>` where a click event would fire on `disabled`. [pr:2023]
1516
- Added `wa-button` class for styling `<a>` elements as buttons [pr:2040]
1617
- Fixed a bug in the custom elements manifest where events may not have a name. [pr:2026]
1718
- Fixed a bug in `<wa-select>` where options with `selected` set via framework property binding (e.g., Vue's `:selected`) were not respected when `with-clear` was present [pr:1985]

packages/webawesome/src/components/dropdown-item/dropdown-item.styles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ export default css`
2828
background-color: var(--wa-color-neutral-fill-normal);
2929
}
3030
31-
:host(:state(disabled)) {
31+
:host(:state(disabled)),
32+
:host([disabled]) {
3233
opacity: 0.5;
3334
cursor: not-allowed;
35+
pointer-events: none;
3436
}
3537
3638
/* Danger variant */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
import { expect, fixture, html } from '@open-wc/testing';
2+
import sinon from 'sinon';
3+
import { clickOnElement } from '../../internal/test/pointer-utilities.js';
4+
import type WaDropdownItem from './dropdown-item.js';
25

36
describe('<wa-dropdown-item>', () => {
47
it('should render a component', async () => {
58
const el = await fixture(html` <wa-dropdown-item></wa-dropdown-item> `);
69

710
expect(el).to.exist;
811
});
12+
13+
it('should not fire click event when disabled', async () => {
14+
const el = await fixture<WaDropdownItem>(html` <wa-dropdown-item disabled>Item</wa-dropdown-item> `);
15+
await el.updateComplete;
16+
17+
const clickHandler = sinon.spy();
18+
el.addEventListener('click', clickHandler);
19+
await clickOnElement(el);
20+
expect(clickHandler).not.to.have.been.called;
21+
});
22+
23+
it('should fire click event when not disabled', async () => {
24+
const el = await fixture<WaDropdownItem>(html` <wa-dropdown-item>Item</wa-dropdown-item> `);
25+
const clickHandler = sinon.spy();
26+
el.addEventListener('click', clickHandler);
27+
await clickOnElement(el);
28+
expect(clickHandler).to.have.been.calledOnce;
29+
});
930
});

packages/webawesome/src/components/dropdown-item/dropdown-item.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ export default class WaDropdownItem extends WebAwesomeElement {
8585
connectedCallback() {
8686
super.connectedCallback();
8787
this.addEventListener('mouseenter', this.handleMouseEnter.bind(this));
88+
this.shadowRoot!.addEventListener('click', this.handleClick, { capture: true });
8889
this.shadowRoot!.addEventListener('slotchange', this.handleSlotChange);
8990
}
9091

9192
disconnectedCallback() {
9293
super.disconnectedCallback();
9394
this.closeSubmenu();
9495
this.removeEventListener('mouseenter', this.handleMouseEnter);
96+
this.shadowRoot!.removeEventListener('click', this.handleClick, { capture: true });
9597
this.shadowRoot!.removeEventListener('slotchange', this.handleSlotChange);
9698
}
9799

@@ -115,6 +117,7 @@ export default class WaDropdownItem extends WebAwesomeElement {
115117
if (changedProperties.has('disabled')) {
116118
this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
117119
this.customStates.set('disabled', this.disabled);
120+
this.style.pointerEvents = this.disabled ? 'none' : '';
118121
}
119122

120123
if (changedProperties.has('type')) {
@@ -236,6 +239,14 @@ export default class WaDropdownItem extends WebAwesomeElement {
236239
) as WaDropdownItem[];
237240
}
238241

242+
/** Prevents click events from firing when the item is disabled. */
243+
private handleClick = (event: MouseEvent) => {
244+
if (this.disabled) {
245+
event.preventDefault();
246+
event.stopImmediatePropagation();
247+
}
248+
};
249+
239250
/** Handles mouse enter to open the submenu */
240251
private handleMouseEnter() {
241252
if (this.hasSubmenu && !this.disabled) {

0 commit comments

Comments
 (0)