Skip to content

Commit 9262f7d

Browse files
fix(datetime): do not animate to new value when multiple values in different months are set (#28847)
Issue number: resolves #28602 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> We animate to the new date when the value is changed, but we do not account for multiple selection. This behavior is valuable for when the value is set asynchronously on a different month than what it shown. However, this is confusing when there are multiple dates selected in different months, because we do not reasonably know which date to animate to. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> Datetime no longer animates to the new value if more than one date is selected, and the selected dates aren't all in the same month. An alternative strategy would be to always animate unless one of the selected dates is in the month currently being viewed. However, this would still mean guessing at which value the user wants to see, which we are trying to avoid. Based on this PR: #28605 ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Co-authored-by: Liam DeBeasi <[email protected]> --------- Co-authored-by: Liam DeBeasi <[email protected]>
1 parent c47a16d commit 9262f7d

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

core/src/components/datetime/datetime.tsx

+36-15
Original file line numberDiff line numberDiff line change
@@ -1284,21 +1284,42 @@ export class Datetime implements ComponentInterface {
12841284
(month !== undefined && month !== workingParts.month) || (year !== undefined && year !== workingParts.year);
12851285
const bodyIsVisible = el.classList.contains('datetime-ready');
12861286
const { isGridStyle, showMonthAndYear } = this;
1287-
if (isGridStyle && didChangeMonth && bodyIsVisible && !showMonthAndYear) {
1288-
this.animateToDate(targetValue);
1289-
} else {
1290-
/**
1291-
* We only need to do this if we didn't just animate to a new month,
1292-
* since that calls prevMonth/nextMonth which calls setWorkingParts for us.
1293-
*/
1294-
this.setWorkingParts({
1295-
month,
1296-
day,
1297-
year,
1298-
hour,
1299-
minute,
1300-
ampm,
1301-
});
1287+
1288+
let areAllSelectedDatesInSameMonth = true;
1289+
if (Array.isArray(valueToProcess)) {
1290+
const firstMonth = valueToProcess[0].month;
1291+
for (const date of valueToProcess) {
1292+
if (date.month !== firstMonth) {
1293+
areAllSelectedDatesInSameMonth = false;
1294+
break;
1295+
}
1296+
}
1297+
}
1298+
1299+
/**
1300+
* If there is more than one date selected
1301+
* and the dates aren't all in the same month,
1302+
* then we should neither animate to the date
1303+
* nor update the working parts because we do
1304+
* not know which date the user wants to view.
1305+
*/
1306+
if (areAllSelectedDatesInSameMonth) {
1307+
if (isGridStyle && didChangeMonth && bodyIsVisible && !showMonthAndYear) {
1308+
this.animateToDate(targetValue);
1309+
} else {
1310+
/**
1311+
* We only need to do this if we didn't just animate to a new month,
1312+
* since that calls prevMonth/nextMonth which calls setWorkingParts for us.
1313+
*/
1314+
this.setWorkingParts({
1315+
month,
1316+
day,
1317+
year,
1318+
hour,
1319+
minute,
1320+
ampm,
1321+
});
1322+
}
13021323
}
13031324
};
13041325

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

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { configs, test } from '@utils/test/playwright';
55

66
const SINGLE_DATE = '2022-06-01';
77
const MULTIPLE_DATES = ['2022-06-01', '2022-06-02', '2022-06-03'];
8-
const MULTIPLE_DATES_SEPARATE_MONTHS = ['2022-04-01', '2022-05-01', '2022-06-01'];
8+
const MULTIPLE_DATES_SEPARATE_MONTHS = ['2022-03-01', '2022-04-01', '2022-05-01'];
99

1010
interface DatetimeMultipleConfig {
1111
multiple?: boolean;
@@ -158,10 +158,32 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
158158
});
159159
});
160160

161-
test('multiple default values across months should display at least one value', async () => {
161+
test('should scroll to new month when value is updated with multiple dates in the same month', async ({ page }) => {
162+
test.info().annotations.push({
163+
type: 'issue',
164+
description: 'https://github.com/ionic-team/ionic-framework/issues/28602',
165+
});
162166
const datetime = await datetimeFixture.goto(config, MULTIPLE_DATES_SEPARATE_MONTHS);
167+
await datetime.evaluate((el: HTMLIonDatetimeElement, dates: string[]) => {
168+
el.value = dates;
169+
}, MULTIPLE_DATES);
170+
171+
await page.waitForChanges();
172+
173+
const monthYear = datetime.locator('.calendar-month-year');
174+
await expect(monthYear).toHaveText(/June 2022/);
175+
});
176+
177+
test('should not scroll to new month when value is updated with dates in different months', async ({ page }) => {
178+
const datetime = await datetimeFixture.goto(config, MULTIPLE_DATES);
179+
await datetime.evaluate((el: HTMLIonDatetimeElement, dates: string[]) => {
180+
el.value = dates;
181+
}, MULTIPLE_DATES_SEPARATE_MONTHS);
182+
183+
await page.waitForChanges();
184+
163185
const monthYear = datetime.locator('.calendar-month-year');
164-
await expect(monthYear).toHaveText(/April 2022/);
186+
await expect(monthYear).toHaveText(/June 2022/);
165187
});
166188

167189
test('with buttons, should only update value when confirm is called', async ({ page }) => {

0 commit comments

Comments
 (0)