Skip to content

Commit

Permalink
chore(core): prevent click on disabled elements
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse committed Dec 4, 2024
1 parent 5dc2205 commit 3d49a41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-fishes-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

prevent click on disabled elements
6 changes: 6 additions & 0 deletions packages/ui/components/core/src/DisabledMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ const DisabledMixinImplementation = superclass =>
}
}
}

click() {
if (this.disabled) return;

super.click();
}
};

export const DisabledMixin = dedupeMixin(DisabledMixinImplementation);
23 changes: 22 additions & 1 deletion packages/ui/components/core/test/DisabledMixin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect, fixture } from '@open-wc/testing';
import { DisabledMixin } from '@lion/ui/core.js';
import { html } from 'lit/static-html.js';
import { LitElement } from 'lit';
import { DisabledMixin } from '@lion/ui/core.js';
import sinon from 'sinon';

describe('DisabledMixin', () => {
class CanBeDisabled extends DisabledMixin(LitElement) {}
Expand Down Expand Up @@ -84,4 +85,24 @@ describe('DisabledMixin', () => {
el.retractRequestToBeDisabled();
expect(el.disabled).to.be.false;
});

it('will not perform click when disabled', async () => {
const el = /** @type {CanBeDisabled} */ (
await fixture(html`<can-be-disabled></can-be-disabled>`)
);
const spy = sinon.spy();
el.addEventListener('click', spy);

el.click();

expect(spy.callCount).to.equal(1);

el.disabled = true;
el.click();
expect(spy.callCount).to.equal(1);

el.disabled = false;
el.click();
expect(spy.callCount).to.equal(2);
});
});

0 comments on commit 3d49a41

Please sign in to comment.