Fix cancel event from within drawer or dialog triggering them to close (#1636)

This commit is contained in:
Jeremy Walton
2025-10-22 11:26:10 -04:00
committed by GitHub
parent 6dd1d6263c
commit 122ce96d26
4 changed files with 27 additions and 2 deletions

View File

@@ -119,6 +119,19 @@ describe('<wa-dialog>', () => {
expect(el.open).to.be.true;
});
it('should not close when bubbled cancel event originates from within the drawer', async () => {
const el = await fixture<WaDialog>(html` <wa-dialog open><input type="file" /></wa-dialog> `);
const input = el.querySelector('input')!;
await clickOnElement(el); // Chromium wants the page to have been clicked prior to closing the dialog.
const cancelEvent = new Event('cancel', { bubbles: true });
input.dispatchEvent(cancelEvent);
await aTimeout(250);
expect(el.open).to.be.true;
});
it('should allow initial focus to be set', async () => {
const el = await fixture<WaDialog>(html` <wa-dialog><wa-input autofocus></wa-input></wa-dialog> `);
const input = el.querySelector('wa-input')!;

View File

@@ -129,7 +129,7 @@ export default class WaDialog extends WebAwesomeElement {
private handleDialogCancel(event: Event) {
event.preventDefault();
if (!this.dialog.classList.contains('hide')) {
if (!this.dialog.classList.contains('hide') && event.target === this.dialog) {
this.requestClose(this.dialog);
}
}

View File

@@ -115,6 +115,18 @@ describe('<wa-drawer>', () => {
expect(el.open).to.be.true;
});
it('should not close when bubbled cancel event originates from within the drawer', async () => {
const el = await fixture<WaDrawer>(html`<wa-drawer open><input type="file" /></wa-drawer>`);
const input = el.querySelector('input')!;
const cancelEvent = new Event('cancel', { bubbles: true });
input.dispatchEvent(cancelEvent);
await aTimeout(250);
expect(el.open).to.be.true;
});
it('should allow initial focus to be set', async () => {
const el = await fixture<WaDrawer>(html` <wa-drawer><wa-input autofocus></wa-input></wa-drawer> `);
const input = el.querySelector('wa-input')!;

View File

@@ -141,7 +141,7 @@ export default class WaDrawer extends WebAwesomeElement {
private handleDialogCancel(event: Event) {
event.preventDefault();
if (!this.drawer.classList.contains('hide')) {
if (!this.drawer.classList.contains('hide') && event.target === this.drawer) {
this.requestClose(this.drawer);
}
}