Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datetime): emit ionChange for non-calendar picker presentation #25380

Merged
merged 6 commits into from
Jun 1, 2022
Merged
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
16 changes: 11 additions & 5 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,13 @@ export class Datetime implements ComponentInterface {
@Method()
async confirm(closeOverlay = false) {
/**
* If highlightActiveParts is false, this means the datetime was inited
* without a value, and the user hasn't selected one yet. We shouldn't
* update the value in this case, since otherwise it would be mysteriously
* set to today.
* We only update the value if the presentation is not a calendar picker,
* or if `highlightActiveParts` is true; indicating that the user
* has selected a date from the calendar picker.
*
* Otherwise "today" would accidentally be set as the value.
*/
if (this.highlightActiveParts) {
if (this.highlightActiveParts || !this.isCalendarPicker) {
/**
* Prevent convertDataToISO from doing any
* kind of transformation based on timezone
Expand Down Expand Up @@ -522,6 +523,11 @@ export class Datetime implements ComponentInterface {
this.confirm();
};

private get isCalendarPicker() {
const { presentation } = this;
return presentation === 'date' || presentation === 'date-time' || presentation === 'time-date';
}

/**
* Stencil sometimes sets calendarBodyRef to null on rerender, even though
* the element is present. Query for it manually as a fallback.
Expand Down
56 changes: 56 additions & 0 deletions core/src/components/datetime/test/presentation/datetime.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,62 @@ test.describe('datetime: presentation', () => {
);
}
});

test('presentation: time: should emit ionChange', async ({ page }) => {
await page.goto(`/src/components/datetime/test/presentation`);

const ionChangeSpy = await page.spyOnEvent('ionChange');
await page.locator('select').selectOption('time');
await page.waitForChanges();

await page.locator('text=AM').click();

await ionChangeSpy.next();

expect(ionChangeSpy.length).toBe(1);
});

test('presentation: month-year: should emit ionChange', async ({ page }) => {
await page.goto(`/src/components/datetime/test/presentation`);

const ionChangeSpy = await page.spyOnEvent('ionChange');
await page.locator('select').selectOption('month-year');
await page.waitForChanges();

await page.locator('text=April').click();

await ionChangeSpy.next();

expect(ionChangeSpy.length).toBe(1);
});

test('presentation: month: should emit ionChange', async ({ page }) => {
await page.goto(`/src/components/datetime/test/presentation`);

const ionChangeSpy = await page.spyOnEvent('ionChange');
await page.locator('select').selectOption('month');
await page.waitForChanges();

await page.locator('text=April').click();

await ionChangeSpy.next();

expect(ionChangeSpy.length).toBe(1);
});

test('presentation: year: should emit ionChange', async ({ page }) => {
await page.goto(`/src/components/datetime/test/presentation`);

const ionChangeSpy = await page.spyOnEvent('ionChange');
await page.locator('select').selectOption('year');
await page.waitForChanges();

await page.locator('text=2021').click();

await ionChangeSpy.next();

expect(ionChangeSpy.length).toBe(1);
});
});

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