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): don't update value on confirm call if no date was selected #25338

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 18 additions & 10 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,26 @@ export class Datetime implements ComponentInterface {
@Method()
async confirm(closeOverlay = false) {
/**
* Prevent convertDataToISO from doing any
* kind of transformation based on timezone
* This cancels out any change it attempts to make
*
* Important: Take the timezone offset based on
* the date that is currently selected, otherwise
* there can be 1 hr difference when dealing w/ DST
* 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.
*/
const date = new Date(convertDataToISO(this.activeParts));
this.activeParts.tzOffset = date.getTimezoneOffset() * -1;
if (this.highlightActiveParts) {
/**
* Prevent convertDataToISO from doing any
* kind of transformation based on timezone
* This cancels out any change it attempts to make
*
* Important: Take the timezone offset based on
* the date that is currently selected, otherwise
* there can be 1 hr difference when dealing w/ DST
*/
const date = new Date(convertDataToISO(this.activeParts));
this.activeParts.tzOffset = date.getTimezoneOffset() * -1;

this.value = convertDataToISO(this.activeParts);
this.value = convertDataToISO(this.activeParts);
}

if (closeOverlay) {
this.closeParentOverlay();
Expand Down
17 changes: 17 additions & 0 deletions core/src/components/datetime/test/basic/datetime.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ test.describe('datetime: selecting a day', () => {
await testHighlight(page, 'custom-datetime');
});
});

test.describe('datetime: confirm date', () => {
test('should not update value if Done was clicked without selecting a day first', async ({ page }) => {
await page.goto('/src/components/datetime/test/basic');

const datetime = page.locator('#custom-datetime');

const value = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(value).toBeUndefined();

await datetime.evaluate(async (el: HTMLIonDatetimeElement) => {
await el.confirm();
});
const valueAgain = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(valueAgain).toBeUndefined();
});
});