Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
`
<ion-datetime presentation="date-time" value="2026-04-28T10:00:00"></ion-datetime>
`,
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');
});
});
});
Loading