mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 04:09:12 +00:00
Fix: Allow wa-hide to be cancelled in WaDropdown (#1774)
* Fix: Allow wa-hide to be cancelled in WaDropdown * add tests around open attribute / property * update changelog
This commit is contained in:
@@ -16,6 +16,7 @@ Components with the <wa-badge variant="warning">Experimental</wa-badge> badge sh
|
||||
- 🚨 BREAKING: Changed `appearance="filled outlined"` to `appearance="filled-outlined"` in `<wa-card>` [issue:1671]
|
||||
- Fixed a bug in `<wa-slider>` that caused some touch devices to end up with the incorrect value [issue:1703]
|
||||
- Fixed a bug in `<wa-card>` that prevented some slots from being detected correctly [discuss:1450]
|
||||
- Fixed a bug in `<wa-dropdown>` that caused the browser to hang when cancelling the `wa-hide` event [issue:1483]
|
||||
- Improved performance of `<wa-icon>` so initial rendering occurs faster, especially with multiple icons on the page [issue:1729]
|
||||
|
||||
## 3.0.0
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { expect, fixture, html } from '@open-wc/testing';
|
||||
import { aTimeout, expect, fixture, html, waitUntil } from '@open-wc/testing';
|
||||
import sinon from 'sinon';
|
||||
import type WaDropdown from './dropdown.js';
|
||||
|
||||
describe('<wa-dropdown>', () => {
|
||||
it('should render a component', async () => {
|
||||
@@ -6,4 +8,112 @@ describe('<wa-dropdown>', () => {
|
||||
|
||||
expect(el).to.exist;
|
||||
});
|
||||
|
||||
it('should respect the open attribute when included', async () => {
|
||||
const el = await fixture<WaDropdown>(html`
|
||||
<wa-dropdown open>
|
||||
<wa-button slot="trigger">Dropdown</wa-button>
|
||||
<wa-dropdown-item>One</wa-dropdown-item>
|
||||
</wa-dropdown>
|
||||
`);
|
||||
|
||||
await el.updateComplete;
|
||||
await aTimeout(200);
|
||||
|
||||
expect(el.open).to.be.true;
|
||||
});
|
||||
|
||||
it('should fire a single show/after-show and hide/after-hide in normal open/close flow', async () => {
|
||||
const el = await fixture<WaDropdown>(html`
|
||||
<wa-dropdown>
|
||||
<wa-button slot="trigger">Dropdown</wa-button>
|
||||
<wa-dropdown-item>One</wa-dropdown-item>
|
||||
<wa-dropdown-item>Two</wa-dropdown-item>
|
||||
</wa-dropdown>
|
||||
`);
|
||||
|
||||
// setup spies to track how often we see different show/hide events
|
||||
const showSpy = sinon.spy();
|
||||
const afterShowSpy = sinon.spy();
|
||||
const hideSpy = sinon.spy();
|
||||
const afterHideSpy = sinon.spy();
|
||||
|
||||
el.addEventListener('wa-show', showSpy);
|
||||
el.addEventListener('wa-after-show', afterShowSpy);
|
||||
el.addEventListener('wa-hide', hideSpy);
|
||||
el.addEventListener('wa-after-hide', afterHideSpy);
|
||||
|
||||
// open the dropdown by triggering a click on the trigger
|
||||
const trigger = el.querySelector<HTMLElement>('[slot="trigger"]')!;
|
||||
trigger.click();
|
||||
|
||||
await waitUntil(() => showSpy.calledOnce);
|
||||
await waitUntil(() => afterShowSpy.calledOnce);
|
||||
|
||||
expect(showSpy.callCount).to.equal(1);
|
||||
expect(afterShowSpy.callCount).to.equal(1);
|
||||
|
||||
expect(el.open).to.be.true;
|
||||
|
||||
// close the dropdown by clicking the trigger again
|
||||
trigger.click();
|
||||
|
||||
await waitUntil(() => hideSpy.calledOnce);
|
||||
await waitUntil(() => afterHideSpy.calledOnce);
|
||||
|
||||
expect(hideSpy.callCount).to.equal(1);
|
||||
expect(afterHideSpy.callCount).to.equal(1);
|
||||
|
||||
expect(el.open).to.be.false;
|
||||
});
|
||||
|
||||
it('should fire a single show/after-show and hide/after-hide when wa-hide event is cancelled', async () => {
|
||||
const el = await fixture<WaDropdown>(html`
|
||||
<wa-dropdown>
|
||||
<wa-button slot="trigger">Dropdown</wa-button>
|
||||
<wa-dropdown-item>One</wa-dropdown-item>
|
||||
<wa-dropdown-item>Two</wa-dropdown-item>
|
||||
</wa-dropdown>
|
||||
`);
|
||||
|
||||
// setup spies to track how often we see different show/hide events
|
||||
const showSpy = sinon.spy();
|
||||
const afterShowSpy = sinon.spy();
|
||||
const hideSpy = sinon.spy();
|
||||
const afterHideSpy = sinon.spy();
|
||||
|
||||
el.addEventListener('wa-show', showSpy);
|
||||
el.addEventListener('wa-after-show', afterShowSpy);
|
||||
|
||||
// Intercept wa-hide and prevent it
|
||||
el.addEventListener('wa-hide', event => {
|
||||
event.preventDefault();
|
||||
hideSpy(event);
|
||||
});
|
||||
|
||||
el.addEventListener('wa-after-hide', afterHideSpy);
|
||||
|
||||
// open the dropdown by triggering a click on the trigger
|
||||
const trigger = el.querySelector<HTMLElement>('[slot="trigger"]')!;
|
||||
trigger.click();
|
||||
|
||||
await waitUntil(() => showSpy.calledOnce);
|
||||
await waitUntil(() => afterShowSpy.calledOnce);
|
||||
|
||||
expect(showSpy.callCount).to.equal(1);
|
||||
expect(afterShowSpy.callCount).to.equal(1);
|
||||
|
||||
expect(el.open).to.be.true;
|
||||
|
||||
// click on the trigger (which should do nothing to the open state)
|
||||
trigger.click();
|
||||
|
||||
await waitUntil(() => hideSpy.calledOnce);
|
||||
|
||||
expect(hideSpy.callCount).to.equal(1);
|
||||
// after-hide should not have been called if hide is cancelled
|
||||
expect(afterHideSpy.callCount).to.equal(0);
|
||||
|
||||
expect(el.open).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -109,6 +109,18 @@ export default class WaDropdown extends WebAwesomeElement {
|
||||
|
||||
async updated(changedProperties: PropertyValues) {
|
||||
if (changedProperties.has('open')) {
|
||||
const previousOpen = changedProperties.get('open');
|
||||
// check if the previous value is the same
|
||||
// (if they are, do not trigger menu showing / hiding)
|
||||
if (previousOpen === this.open) {
|
||||
return;
|
||||
}
|
||||
// check if we are changing from undefined to false
|
||||
// (if we are, we can skip menu hiding)
|
||||
if (previousOpen === undefined && this.open === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.customStates.set('open', this.open);
|
||||
|
||||
if (this.open) {
|
||||
@@ -227,6 +239,12 @@ export default class WaDropdown extends WebAwesomeElement {
|
||||
return;
|
||||
}
|
||||
|
||||
// if this dropdown is already open, do nothing
|
||||
// (this can happen when wa-hide was cancelled)
|
||||
if (this.popup.active) {
|
||||
return;
|
||||
}
|
||||
|
||||
openDropdowns.forEach(dropdown => (dropdown.open = false));
|
||||
|
||||
this.popup.active = true; // Use wa-popup's active property instead of showPopover
|
||||
|
||||
Reference in New Issue
Block a user