Skip to content

Commit 4e6a60b

Browse files
authored
fix(datetime): emit ionChange for non-calendar picker presentation (#25380)
Resolves #25375
1 parent 3e31025 commit 4e6a60b

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,13 @@ export class Datetime implements ComponentInterface {
442442
@Method()
443443
async confirm(closeOverlay = false) {
444444
/**
445-
* If highlightActiveParts is false, this means the datetime was inited
446-
* without a value, and the user hasn't selected one yet. We shouldn't
447-
* update the value in this case, since otherwise it would be mysteriously
448-
* set to today.
445+
* We only update the value if the presentation is not a calendar picker,
446+
* or if `highlightActiveParts` is true; indicating that the user
447+
* has selected a date from the calendar picker.
448+
*
449+
* Otherwise "today" would accidentally be set as the value.
449450
*/
450-
if (this.highlightActiveParts) {
451+
if (this.highlightActiveParts || !this.isCalendarPicker) {
451452
/**
452453
* Prevent convertDataToISO from doing any
453454
* kind of transformation based on timezone
@@ -522,6 +523,11 @@ export class Datetime implements ComponentInterface {
522523
this.confirm();
523524
};
524525

526+
private get isCalendarPicker() {
527+
const { presentation } = this;
528+
return presentation === 'date' || presentation === 'date-time' || presentation === 'time-date';
529+
}
530+
525531
/**
526532
* Stencil sometimes sets calendarBodyRef to null on rerender, even though
527533
* the element is present. Query for it manually as a fallback.

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,62 @@ test.describe('datetime: presentation', () => {
2727
);
2828
}
2929
});
30+
31+
test('presentation: time: should emit ionChange', async ({ page }) => {
32+
await page.goto(`/src/components/datetime/test/presentation`);
33+
34+
const ionChangeSpy = await page.spyOnEvent('ionChange');
35+
await page.locator('select').selectOption('time');
36+
await page.waitForChanges();
37+
38+
await page.locator('text=AM').click();
39+
40+
await ionChangeSpy.next();
41+
42+
expect(ionChangeSpy.length).toBe(1);
43+
});
44+
45+
test('presentation: month-year: should emit ionChange', async ({ page }) => {
46+
await page.goto(`/src/components/datetime/test/presentation`);
47+
48+
const ionChangeSpy = await page.spyOnEvent('ionChange');
49+
await page.locator('select').selectOption('month-year');
50+
await page.waitForChanges();
51+
52+
await page.locator('text=April').click();
53+
54+
await ionChangeSpy.next();
55+
56+
expect(ionChangeSpy.length).toBe(1);
57+
});
58+
59+
test('presentation: month: should emit ionChange', async ({ page }) => {
60+
await page.goto(`/src/components/datetime/test/presentation`);
61+
62+
const ionChangeSpy = await page.spyOnEvent('ionChange');
63+
await page.locator('select').selectOption('month');
64+
await page.waitForChanges();
65+
66+
await page.locator('text=April').click();
67+
68+
await ionChangeSpy.next();
69+
70+
expect(ionChangeSpy.length).toBe(1);
71+
});
72+
73+
test('presentation: year: should emit ionChange', async ({ page }) => {
74+
await page.goto(`/src/components/datetime/test/presentation`);
75+
76+
const ionChangeSpy = await page.spyOnEvent('ionChange');
77+
await page.locator('select').selectOption('year');
78+
await page.waitForChanges();
79+
80+
await page.locator('text=2021').click();
81+
82+
await ionChangeSpy.next();
83+
84+
expect(ionChangeSpy.length).toBe(1);
85+
});
3086
});
3187

3288
test.describe('datetime: presentation: time', () => {

0 commit comments

Comments
 (0)