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): max and min works with showDefaultButtons #26257

Merged
merged 10 commits into from
Nov 14, 2022
25 changes: 19 additions & 6 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,13 @@ export class Datetime implements ComponentInterface {
* defaultParts if no active date is selected.
*/
private getActivePartsWithFallback = () => {
const { activePartsClone, defaultParts } = this;
const { defaultParts } = this;
return this.getActivePart() ?? defaultParts;
};

const firstPart = Array.isArray(activePartsClone) ? activePartsClone[0] : activePartsClone;
return firstPart ?? defaultParts;
private getActivePart = () => {
const { activePartsClone } = this;
return Array.isArray(activePartsClone) ? activePartsClone[0] : activePartsClone;
};

private closeParentOverlay = () => {
Expand Down Expand Up @@ -1729,14 +1732,24 @@ export class Datetime implements ComponentInterface {
return [];
}

const valueIsDefined = this.value !== null && this.value !== undefined;
/**
* If a user has not selected a date,
* then we should show all times. If the
* user has selected a date (even if it has
* not been confirmed yet), we should apply
* the max and min restrictions so that the
* time picker shows values that are
* appropriate for the selected date.
*/
const activePart = this.getActivePart();
const userHasSelectedDate = activePart !== undefined;

const { hoursData, minutesData, dayPeriodData } = getTimeColumnsData(
this.locale,
this.workingParts,
this.hourCycle,
valueIsDefined ? this.minParts : undefined,
valueIsDefined ? this.maxParts : undefined,
userHasSelectedDate ? this.minParts : undefined,
userHasSelectedDate ? this.maxParts : undefined,
this.parsedHourValues,
this.parsedMinuteValues
);
Expand Down
48 changes: 48 additions & 0 deletions core/src/components/datetime/test/minmax/datetime.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,52 @@ test.describe('datetime: minmax', () => {
await expect(value!.includes('2022-10-10T08:00')).toBe(true);
});
});

test.describe('datetime: confirm button', () => {
test.beforeEach(({ skip }) => {
skip.rtl();
});
test('should apply max and min constraints even when user confirmation is required', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/25073',
});

await page.setContent(`
<ion-datetime max="2022-01-10T15:30" show-default-buttons="true"></ion-datetime>

<script>
const mockToday = '2022-01-10T12:22';
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(mockToday)
} else {
super(...args);
}
}
}
</script>
`);
await page.waitForSelector('.datetime-ready');

// Select Jan 10, 2022
const maxDate = page.locator('ion-datetime .calendar-day[data-day="10"][data-month="1"][data-year="2022"]');
await maxDate.click();
await page.waitForChanges();

// Check to see that the hours have been filtered.
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const timeButton = page.locator('ion-datetime .time-body');
await timeButton.click();

await ionPopoverDidPresent.next();

const hours = page.locator(
'ion-popover ion-picker-column-internal:nth-child(1) .picker-item:not(.picker-item-empty)'
);

await expect(await hours.count()).toBe(4);
});
});
});