Skip to content

Commit 78bfab6

Browse files
authored
fix: fire single event when changing accordion opened index (#9124)
1 parent 89a5241 commit 78bfab6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/accordion/src/vaadin-accordion-mixin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ export const AccordionMixin = (superClass) =>
114114
/** @private */
115115
_updateItems(items, opened) {
116116
if (items) {
117+
this.__itemsSync = true;
117118
const itemToOpen = items[opened];
118119
items.forEach((item) => {
119120
item.opened = item === itemToOpen;
120121
});
122+
this.__itemsSync = false;
121123
}
122124
}
123125

@@ -140,6 +142,11 @@ export const AccordionMixin = (superClass) =>
140142

141143
/** @private */
142144
_updateOpened(e) {
145+
// Item sync applies the current opened index to each item, in which
146+
// case we don't need to update the opened index again.
147+
if (this.__itemsSync) {
148+
return;
149+
}
143150
const target = this._filterItems(e.composedPath())[0];
144151
const idx = this.items.indexOf(target);
145152
if (e.detail.value) {

packages/accordion/test/accordion.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,22 @@ describe('vaadin-accordion', () => {
139139
expect(accordion.opened).to.equal(0);
140140
});
141141

142-
it('should dispatch opened-changed event when opened changes', async () => {
142+
it('should dispatch single opened-changed event when opened changes', async () => {
143143
const spy = sinon.spy();
144144
accordion.addEventListener('opened-changed', spy);
145145
getHeading(1).click();
146146
await nextUpdate(accordion);
147147
expect(spy.calledOnce).to.be.true;
148148
});
149149

150+
it('should dispatch single opened-changed event when changing opened index property', async () => {
151+
const spy = sinon.spy();
152+
accordion.addEventListener('opened-changed', spy);
153+
accordion.opened = 1;
154+
await nextUpdate(accordion);
155+
expect(spy).to.be.calledOnce;
156+
});
157+
150158
it('should open panel when component in summary is clicked', async () => {
151159
getHeading(2).firstChild.click();
152160
await nextUpdate(accordion);

0 commit comments

Comments
 (0)