From 4bd9a891c78c8f3199f0d1253019b94ae32ea328 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 7 Nov 2026 12:04:08 +0530 Subject: [PATCH] fix(datetime): keep month when calendar is hidden Skip month detection in the calendar scroll listener when the calendar body has no layout, which is the case while the month/year picker is open or the host is hidden. Every box measures 0 then, which the listener read as having scrolled to the previous month, so opening the picker right after a scroll could move the month back. Force-rendered months still go through, as animateToDate waits on the listener to finish. Skipping them would leave forceRenderDate set and make every later scroll commit that stale date. closes #31416 --- core/src/components/datetime/datetime.tsx | 13 ++++ .../test/month-year-picker/datetime.e2e.ts | 74 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/core/src/components/datetime/datetime.tsx b/core/src/components/datetime/datetime.tsx index ff04d36f55a..b18e287eec3 100644 --- a/core/src/components/datetime/datetime.tsx +++ b/core/src/components/datetime/datetime.tsx @@ -951,6 +951,19 @@ export class Datetime implements ComponentInterface { calendarBodyRef.scrollLeft = startMonth.clientWidth * (isRTL(this.el) ? -1 : 1); const getChangedMonth = (parts: DatetimeParts): DatetimeParts | undefined => { + /** + * The calendar body has no layout while the month/year picker + * is open or the host is hidden. Every box measures 0 in that + * case, which would read below as having scrolled to the + * previous month, so there is no month change to detect. + * + * A force-rendered month must still go through so that + * updateActiveMonth can resolve the promise animateToDate is + * waiting on. Otherwise, forceRenderDate would never be cleared + * and every later scroll would commit that stale date. + */ + if (calendarBodyRef.clientWidth === 0 && this.forceRenderDate === undefined) return; + const box = calendarBodyRef.getBoundingClientRect(); /** diff --git a/core/src/components/datetime/test/month-year-picker/datetime.e2e.ts b/core/src/components/datetime/test/month-year-picker/datetime.e2e.ts index 4d2dc8e7dd5..26ebc3ef6d1 100644 --- a/core/src/components/datetime/test/month-year-picker/datetime.e2e.ts +++ b/core/src/components/datetime/test/month-year-picker/datetime.e2e.ts @@ -1,4 +1,5 @@ import { expect } from '@playwright/test'; +import type { E2EPage } from '@utils/test/playwright'; import { configs, test } from '@utils/test/playwright'; /** * This is testing element rendering which @@ -26,4 +27,77 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(monthyearFooter).toBeVisible(); }); }); + + test.describe(title('datetime: month-year picker with a hidden calendar body'), () => { + test.beforeEach(async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31416', + }); + + await page.setContent( + ` + + `, + config + ); + await page.locator('.datetime-ready').waitFor(); + }); + + /** + * Opening the picker hides the calendar body, so a real scroll would + * have to be pending at that exact moment for the scroll listener to + * run. Dispatching the event, then waiting out the listener's debounce, + * makes that timing deterministic. + */ + const flushCalendarScroll = async (page: E2EPage) => { + await page.locator('ion-datetime .calendar-body').dispatchEvent('scroll'); + await page.waitForTimeout(100); + await page.waitForChanges(); + }; + + test('should not change the month when the scroll listener runs', async ({ page }) => { + const pickerButton = page.locator('ion-datetime .calendar-month-year-toggle'); + + await expect(pickerButton).toContainText('April 2026'); + + await pickerButton.click(); + await page.waitForChanges(); + + await flushCalendarScroll(page); + + await expect(pickerButton).toContainText('April 2026'); + + // Showing the calendar body again should not change the month either. + await pickerButton.click(); + await page.waitForChanges(); + + await flushCalendarScroll(page); + + await expect(pickerButton).toContainText('April 2026'); + }); + + test('should finish scrolling to a new value when the picker opens during the animation', async ({ page }) => { + const pickerButton = page.locator('ion-datetime .calendar-month-year-toggle'); + const nextMonthButton = page.locator('ion-datetime .calendar-next-prev ion-button + ion-button'); + + await page.locator('ion-datetime').evaluate((el: HTMLIonDatetimeElement) => { + el.value = '2026-06-15T10:00:00'; + el.shadowRoot!.querySelector('.calendar-month-year-toggle')!.dispatchEvent(new MouseEvent('click')); + }); + await page.waitForChanges(); + + await flushCalendarScroll(page); + + await expect(pickerButton).toContainText('June 2026'); + + // The month arrows only keep working if the new value is no longer being forced. + await pickerButton.click(); + await page.waitForChanges(); + + await nextMonthButton.click(); + + await expect(pickerButton).toContainText('July 2026'); + }); + }); });