diff --git a/core/src/components/datetime/datetime.tsx b/core/src/components/datetime/datetime.tsx index a09eb0fdad..8afefa45ac 100644 --- a/core/src/components/datetime/datetime.tsx +++ b/core/src/components/datetime/datetime.tsx @@ -883,21 +883,18 @@ export class Datetime implements ComponentInterface { const getChangedMonth = (parts: DatetimeParts): DatetimeParts | undefined => { const box = calendarBodyRef.getBoundingClientRect(); - const root = this.el!.shadowRoot!; /** - * Get the element that is in the center of the calendar body. - * This will be an element inside of the active month. + * If the current scroll position is all the way to the left + * then we have scrolled to the previous month. + * Otherwise, assume that we have scrolled to the next + * month. We have a tolerance of 2px to account for + * sub pixel rendering. + * + * Check below the next line ensures that we did not + * swipe and abort (i.e. we swiped but we are still on the current month). */ - const elementAtCenter = root.elementFromPoint(box.x + box.width / 2, box.y + box.height / 2); - /** - * If there is no element then the - * component may be re-rendering on a slow device. - */ - if (!elementAtCenter) return; - - const month = elementAtCenter.closest('.calendar-month'); - if (!month) return; + const month = calendarBodyRef.scrollLeft <= 2 ? startMonth : endMonth; /** * The edge of the month must be lined up with diff --git a/core/src/components/datetime/test/minmax/datetime.e2e.ts b/core/src/components/datetime/test/minmax/datetime.e2e.ts index 2c44afa258..ae25f71f49 100644 --- a/core/src/components/datetime/test/minmax/datetime.e2e.ts +++ b/core/src/components/datetime/test/minmax/datetime.e2e.ts @@ -301,21 +301,20 @@ configs({ directions: ['ltr'], modes: ['ios'] }).forEach(({ title, config }) => `, config ); const datetime = page.locator('ion-datetime'); - const monthYearToggle = page.locator('ion-datetime .calendar-month-year'); const monthColumnItems = page.locator('ion-datetime .month-column .picker-item:not(.picker-item-empty)'); + const ionChange = await page.spyOnEvent('ionChange'); - await monthYearToggle.click(); - await page.waitForChanges(); + await page.waitForSelector('.datetime-ready'); await monthColumnItems.nth(0).click(); // switch to January - await page.waitForChanges(); + await ionChange.next(); await expect(datetime).toHaveJSProperty('value', '2022-01-15T00:00:00'); }); diff --git a/core/src/components/datetime/test/set-value/datetime.e2e.ts b/core/src/components/datetime/test/set-value/datetime.e2e.ts index 821affaa63..4012605fa5 100644 --- a/core/src/components/datetime/test/set-value/datetime.e2e.ts +++ b/core/src/components/datetime/test/set-value/datetime.e2e.ts @@ -34,13 +34,14 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => `, config ); + await page.waitForSelector('.datetime-ready'); const datetime = page.locator('ion-datetime'); const activeDayButton = page.locator('.calendar-day-active'); const monthYearButton = page.locator('.calendar-month-year'); const monthColumn = page.locator('.month-column'); - const yearColumn = page.locator('.year-column'); + const ionChange = await page.spyOnEvent('ionChange'); await datetime.evaluate((el: HTMLIonDatetimeElement) => (el.value = '2021-10-05')); @@ -49,11 +50,9 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await page.waitForChanges(); // Select October 2021 + // The year will automatically switch to 2021 when selecting 10 await monthColumn.locator('.picker-item[data-value="10"]').click(); - await page.waitForChanges(); - - await yearColumn.locator('.picker-item[data-value="2021"]').click(); - await page.waitForChanges(); + await ionChange.next(); // Close month/year picker await monthYearButton.click();