Skip to content

Commit fc5115c

Browse files
committed
fix: set active month view to the latest value
1 parent 0a89e66 commit fc5115c

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,21 +1262,20 @@ export class Datetime implements ComponentInterface {
12621262
}
12631263

12641264
/**
1265-
* If there are multiple values, pick an arbitrary one to clamp to. This way,
1266-
* if the values are across months, we always show at least one of them. Note
1267-
* that the values don't necessarily have to be in order.
1265+
* If there are multiple values, clamp to the last one.
1266+
* This is because the last value is the one that the user
1267+
* has most recently interacted with.
12681268
*/
1269-
const singleValue = Array.isArray(valueToProcess) ? valueToProcess[0] : valueToProcess;
1269+
const singleValue = Array.isArray(valueToProcess) ? valueToProcess[valueToProcess.length - 1] : valueToProcess;
12701270
const targetValue = clampDate(singleValue, minParts, maxParts);
12711271

12721272
const { month, day, year, hour, minute } = targetValue;
12731273
const ampm = parseAmPm(hour!);
12741274

12751275
/**
1276-
* Since `activeParts` indicates a value that
1277-
* been explicitly selected either by the
1278-
* user or the app, only update `activeParts`
1279-
* if the `value` property is set.
1276+
* Since `activeParts` indicates a value that been explicitly selected
1277+
* either by the user or the app, only update `activeParts` if the
1278+
* `value` property is set.
12801279
*/
12811280
if (hasValue) {
12821281
if (Array.isArray(valueToProcess)) {
@@ -1300,28 +1299,21 @@ export class Datetime implements ComponentInterface {
13001299
this.activeParts = [];
13011300
}
13021301

1303-
/**
1304-
* Only animate if:
1305-
* 1. We're using grid style (wheel style pickers should just jump to new value)
1306-
* 2. The month and/or year actually changed, and both are defined (otherwise there's nothing to animate to)
1307-
* 3. The calendar body is visible (prevents animation when in collapsed datetime-button, for example)
1308-
* 4. The month/year picker is not open (since you wouldn't see the animation anyway)
1309-
*/
13101302
const didChangeMonth =
13111303
(month !== undefined && month !== workingParts.month) || (year !== undefined && year !== workingParts.year);
13121304
const bodyIsVisible = el.classList.contains('datetime-ready');
13131305
const { isGridStyle, showMonthAndYear } = this;
13141306

13151307
if (isGridStyle && didChangeMonth && bodyIsVisible && !showMonthAndYear) {
13161308
/**
1317-
* Animate to the target date, either the first value in the array or the single value.
1309+
* Only animate if:
1310+
* 1. We're using grid style (wheel style pickers should just jump to new value)
1311+
* 2. The month and/or year actually changed, and both are defined (otherwise there's nothing to animate to)
1312+
* 3. The calendar body is visible (prevents animation when in collapsed datetime-button, for example)
1313+
* 4. The month/year picker is not open (since you wouldn't see the animation anyway)
13181314
*/
13191315
this.animateToDate(targetValue);
13201316
} else {
1321-
/**
1322-
* We only need to do this if we didn't just animate to a new month,
1323-
* since that calls prevMonth/nextMonth which calls setWorkingParts for us.
1324-
*/
13251317
this.setWorkingParts({
13261318
month,
13271319
day,

core/src/components/datetime/test/multiple/datetime.e2e.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,27 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
313313
});
314314

315315
test.describe('with selected days in different months', () => {
316-
test('set the working parts to the first selected value', async ({ page }) => {
316+
test(`set the active month view to the latest value's month`, async ({ page }) => {
317317
const datetime = await new DatetimeMultipleFixture(page).goto(config, MULTIPLE_DATES_SEPARATE_MONTHS);
318-
const monthYear = datetime.locator('.calendar-month-year');
318+
const calendarMonthYear = datetime.locator('.calendar-month-year');
319+
320+
await expect(calendarMonthYear).toHaveText(/May 2022/);
321+
});
322+
323+
test('does not change the active month view when selecting a day in a different month', async ({ page }) => {
324+
const datetime = await new DatetimeMultipleFixture(page).goto(config, MULTIPLE_DATES_SEPARATE_MONTHS);
325+
const nextButton = page.locator('.calendar-next-prev ion-button:nth-child(2)');
326+
const calendarMonthYear = datetime.locator('.calendar-month-year');
327+
328+
await nextButton.click();
329+
330+
await expect(calendarMonthYear).toHaveText(/June 2022/);
331+
332+
const june8Button = datetime.locator('[data-month="6"][data-day="8"]');
333+
334+
await june8Button.click();
319335

320-
await expect(monthYear).toHaveText(/March 2022/);
336+
await expect(calendarMonthYear).toHaveText(/June 2022/);
321337
});
322338
});
323339
});

0 commit comments

Comments
 (0)