Skip to content

Commit 2095f2f

Browse files
committed
fix(accordion): animating open if the first opening is done by the user
1 parent 1a1dcda commit 2095f2f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

core/src/components/accordion-group/accordion-group.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ export class AccordionGroup implements ComponentInterface {
7474
@Event() ionValueChange!: EventEmitter<AccordionGroupChangeEventDetail>;
7575

7676
private hasEmittedInitialValue = false;
77+
private isUserInitiatedChange = false;
7778

7879
@Watch('value')
7980
valueChanged() {
80-
this.emitValueChange(false);
81+
this.emitValueChange(false, this.isUserInitiatedChange);
82+
this.isUserInitiatedChange = false;
8183
}
8284

8385
@Watch('disabled')
@@ -179,6 +181,7 @@ export class AccordionGroup implements ComponentInterface {
179181
* accordion group to an array.
180182
*/
181183
private setValue(accordionValue: string | string[] | null | undefined) {
184+
this.isUserInitiatedChange = true;
182185
const value = (this.value = accordionValue);
183186
this.ionChange.emit({ value });
184187
}
@@ -255,7 +258,7 @@ export class AccordionGroup implements ComponentInterface {
255258
return Array.from(this.el.querySelectorAll(':scope > ion-accordion')) as HTMLIonAccordionElement[];
256259
}
257260

258-
private emitValueChange(initial: boolean) {
261+
private emitValueChange(initial: boolean, isUserInitiated = false) {
259262
const { value, multiple } = this;
260263

261264
if (!multiple && Array.isArray(value)) {
@@ -280,7 +283,7 @@ export class AccordionGroup implements ComponentInterface {
280283
* Track if this is the initial value update so accordions
281284
* can skip transition animations when they first render.
282285
*/
283-
const shouldMarkInitial = initial || (!this.hasEmittedInitialValue && value !== undefined);
286+
const shouldMarkInitial = initial || (!this.hasEmittedInitialValue && value !== undefined && !isUserInitiated);
284287

285288
this.ionValueChange.emit({ value, initial: shouldMarkInitial });
286289

core/src/components/accordion/test/accordion.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,33 @@ it('should not animate when initial value is set after load', async () => {
273273
expect(firstAccordion.classList.contains('accordion-expanding')).toEqual(false);
274274
});
275275

276+
it('should animate when accordion is first opened by user', async () => {
277+
const page = await newSpecPage({
278+
components: [Item, Accordion, AccordionGroup],
279+
html: `
280+
<ion-accordion-group>
281+
<ion-accordion value="first">
282+
<ion-item slot="header">Label</ion-item>
283+
<div slot="content">Content</div>
284+
</ion-accordion>
285+
</ion-accordion-group>
286+
`,
287+
});
288+
289+
const accordionGroup = page.body.querySelector('ion-accordion-group')!;
290+
291+
const details: AccordionGroupChangeEventDetail[] = [];
292+
accordionGroup.addEventListener('ionValueChange', (event: CustomEvent<AccordionGroupChangeEventDetail>) => {
293+
details.push(event.detail);
294+
});
295+
296+
await accordionGroup.requestAccordionToggle('first', true);
297+
await page.waitForChanges();
298+
299+
const lastDetail = details[details.length - 1];
300+
expect(lastDetail?.initial).toBe(false);
301+
});
302+
276303
// Verifies fix for https://github.com/ionic-team/ionic-framework/issues/27047
277304
it('should not have animated class when animated="false"', async () => {
278305
const page = await newSpecPage({

0 commit comments

Comments
 (0)