Skip to content
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
1 change: 1 addition & 0 deletions packages/webawesome/docs/docs/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Components with the <wa-badge variant="warning">Experimental</wa-badge> badge sh

<small>TBD</small>

- Fixed a bug in `<wa-dropdown-item>` where a click event would fire on `disabled`. [pr:2023]
- Added `wa-button` class for styling `<a>` elements as buttons [pr:2040]
- Fixed a bug `<wa-color-picker>` that prevented it from flipping horizontally when position to the right of the viewport. [pr:2024]
- Fixed a bug by adding `color: inherit` to the `<wa-dialog>` and `<wa-drawer>` styles so they inherit the text color from the document context rather than the browser default. [pr:2064]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ export default css`
background-color: var(--wa-color-neutral-fill-normal);
}

:host(:state(disabled)) {
:host(:state(disabled)),
:host([disabled]) {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
}

/* Danger variant */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { expect, fixture, html } from '@open-wc/testing';
import sinon from 'sinon';
import { clickOnElement } from '../../internal/test/pointer-utilities.js';
import type WaDropdownItem from './dropdown-item.js';

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

expect(el).to.exist;
});

it('should not fire click event when disabled', async () => {
const el = await fixture<WaDropdownItem>(html` <wa-dropdown-item disabled>Item</wa-dropdown-item> `);
await el.updateComplete;

const clickHandler = sinon.spy();
el.addEventListener('click', clickHandler);
await clickOnElement(el);
expect(clickHandler).not.to.have.been.called;
});

it('should fire click event when not disabled', async () => {
const el = await fixture<WaDropdownItem>(html` <wa-dropdown-item>Item</wa-dropdown-item> `);
const clickHandler = sinon.spy();
el.addEventListener('click', clickHandler);
await clickOnElement(el);
expect(clickHandler).to.have.been.calledOnce;
});
});
11 changes: 11 additions & 0 deletions packages/webawesome/src/components/dropdown-item/dropdown-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ export default class WaDropdownItem extends WebAwesomeElement {
connectedCallback() {
super.connectedCallback();
this.addEventListener('mouseenter', this.handleMouseEnter.bind(this));
this.shadowRoot!.addEventListener('click', this.handleClick, { capture: true });
this.shadowRoot!.addEventListener('slotchange', this.handleSlotChange);
}

disconnectedCallback() {
super.disconnectedCallback();
this.closeSubmenu();
this.removeEventListener('mouseenter', this.handleMouseEnter);
this.shadowRoot!.removeEventListener('click', this.handleClick, { capture: true });
this.shadowRoot!.removeEventListener('slotchange', this.handleSlotChange);
}

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

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

/** Prevents click events from firing when the item is disabled. */
private handleClick = (event: MouseEvent) => {
if (this.disabled) {
event.preventDefault();
event.stopImmediatePropagation();
}
};

/** Handles mouse enter to open the submenu */
private handleMouseEnter() {
if (this.hasSubmenu && !this.disabled) {
Expand Down