Skip to content
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
4 changes: 2 additions & 2 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ export namespace Components {
*/
"presentation": DatetimePresentation;
/**
* If `true`, the datetime appears normal but is not interactive.
* If `true`, the datetime appears normal but the selected date cannot be changed.
*/
"readonly": boolean;
/**
Expand Down Expand Up @@ -5595,7 +5595,7 @@ declare namespace LocalJSX {
*/
"presentation"?: DatetimePresentation;
/**
* If `true`, the datetime appears normal but is not interactive.
* If `true`, the datetime appears normal but the selected date cannot be changed.
*/
"readonly"?: boolean;
/**
Expand Down
30 changes: 27 additions & 3 deletions core/src/components/datetime/datetime.scss
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,37 @@ ion-picker-column-internal {
display: none;
}

:host(.datetime-readonly),
:host(.datetime-disabled) {
pointer-events: none;

.calendar-days-of-week,
.datetime-time {
opacity: 0.4;
}
}

:host(.datetime-disabled) {
opacity: 0.4;
:host(.datetime-readonly) {
pointer-events: none;

/**
* Allow user to navigate months
* while in readonly mode
*/
.calendar-action-buttons,
.calendar-body,
.datetime-year {
pointer-events: initial;
}

/**
* Disabled buttons should have full opacity
* in readonly mode
*/

.calendar-day[disabled]:not(.calendar-day-constrained),
.datetime-action-buttons ion-button[disabled] {
opacity: 1;
}
}

/**
Expand Down
105 changes: 80 additions & 25 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Datetime implements ComponentInterface {
@Prop() disabled = false;

/**
* If `true`, the datetime appears normal but is not interactive.
* If `true`, the datetime appears normal but the selected date cannot be changed.
*/
@Prop() readonly = false;

Expand Down Expand Up @@ -599,6 +599,14 @@ export class Datetime implements ComponentInterface {
};

private setActiveParts = (parts: DatetimeParts, removeDate = false) => {
/** if the datetime component is in readonly mode,
* allow browsing of the calendar without changing
* the set value
*/
if (this.readonly) {
return;
}

const { multiple, minParts, maxParts, activeParts } = this;

/**
Expand Down Expand Up @@ -1414,7 +1422,13 @@ export class Datetime implements ComponentInterface {
*/

private renderFooter() {
const { showDefaultButtons, showClearButton } = this;
const { disabled, readonly, showDefaultButtons, showClearButton } = this;
/**
* The cancel, clear, and confirm buttons
* should not be interactive if the datetime
* is disabled or readonly.
*/
const isButtonDisabled = disabled || readonly;
const hasSlottedButtons = this.el.querySelector('[slot="buttons"]') !== null;
if (!hasSlottedButtons && !showDefaultButtons && !showClearButton) {
return;
Expand Down Expand Up @@ -1444,18 +1458,33 @@ export class Datetime implements ComponentInterface {
<slot name="buttons">
<ion-buttons>
{showDefaultButtons && (
<ion-button id="cancel-button" color={this.color} onClick={() => this.cancel(true)}>
<ion-button
id="cancel-button"
color={this.color}
onClick={() => this.cancel(true)}
disabled={isButtonDisabled}
>
{this.cancelText}
</ion-button>
)}
<div class="datetime-action-buttons-container">
{showClearButton && (
<ion-button id="clear-button" color={this.color} onClick={() => clearButtonClick()}>
<ion-button
id="clear-button"
color={this.color}
onClick={() => clearButtonClick()}
disabled={isButtonDisabled}
>
{this.clearText}
</ion-button>
)}
{showDefaultButtons && (
<ion-button id="confirm-button" color={this.color} onClick={() => this.confirm(true)}>
<ion-button
id="confirm-button"
color={this.color}
onClick={() => this.confirm(true)}
disabled={isButtonDisabled}
>
{this.doneText}
</ion-button>
)}
Expand Down Expand Up @@ -1957,11 +1986,12 @@ export class Datetime implements ComponentInterface {
*/

private renderCalendarHeader(mode: Mode) {
const { disabled } = this;
const expandedIcon = mode === 'ios' ? chevronDown : caretUpSharp;
const collapsedIcon = mode === 'ios' ? chevronForward : caretDownSharp;

const prevMonthDisabled = isPrevMonthDisabled(this.workingParts, this.minParts, this.maxParts);
const nextMonthDisabled = isNextMonthDisabled(this.workingParts, this.maxParts);
const prevMonthDisabled = disabled || isPrevMonthDisabled(this.workingParts, this.minParts, this.maxParts);
const nextMonthDisabled = disabled || isNextMonthDisabled(this.workingParts, this.maxParts);

// don't use the inheritAttributes util because it removes dir from the host, and we still need that
const hostDir = this.el.getAttribute('dir') || undefined;
Expand All @@ -1977,6 +2007,7 @@ export class Datetime implements ComponentInterface {
aria-label="Show year picker"
detail={false}
lines="none"
disabled={disabled}
onClick={() => {
this.toggleMonthAndYearView();
/**
Expand Down Expand Up @@ -2043,23 +2074,28 @@ export class Datetime implements ComponentInterface {
);
}
private renderMonth(month: number, year: number) {
const { disabled, readonly } = this;

const yearAllowed = this.parsedYearValues === undefined || this.parsedYearValues.includes(year);
const monthAllowed = this.parsedMonthValues === undefined || this.parsedMonthValues.includes(month);
const isCalMonthDisabled = !yearAllowed || !monthAllowed;
const swipeDisabled = isMonthDisabled(
{
month,
year,
day: null,
},
{
// The day is not used when checking if a month is disabled.
// Users should be able to access the min or max month, even if the
// min/max date is out of bounds (e.g. min is set to Feb 15, Feb should not be disabled).
minParts: { ...this.minParts, day: null },
maxParts: { ...this.maxParts, day: null },
}
);
const isDatetimeDisabled = disabled || readonly;
const swipeDisabled =
disabled ||
isMonthDisabled(
{
month,
year,
day: null,
},
{
// The day is not used when checking if a month is disabled.
// Users should be able to access the min or max month, even if the
// min/max date is out of bounds (e.g. min is set to Feb 15, Feb should not be disabled).
minParts: { ...this.minParts, day: null },
maxParts: { ...this.maxParts, day: null },
}
);
// The working month should never have swipe disabled.
// Otherwise the CSS scroll snap will not work and the user
// can free-scroll the calendar.
Expand All @@ -2083,7 +2119,14 @@ export class Datetime implements ComponentInterface {
const { el, highlightedDates, isDateEnabled, multiple } = this;
const referenceParts = { month, day, year };
const isCalendarPadding = day === null;
const { isActive, isToday, ariaLabel, ariaSelected, disabled, text } = getCalendarDayState(
const {
isActive,
isToday,
ariaLabel,
ariaSelected,
disabled: isDayDisabled,
text,
} = getCalendarDayState(
this.locale,
referenceParts,
this.activeParts,
Expand All @@ -2094,7 +2137,8 @@ export class Datetime implements ComponentInterface {
);

const dateIsoString = convertDataToISO(referenceParts);
let isCalDayDisabled = isCalMonthDisabled || disabled;

let isCalDayDisabled = isCalMonthDisabled || isDayDisabled;

if (!isCalDayDisabled && isDateEnabled !== undefined) {
try {
Expand All @@ -2113,6 +2157,15 @@ export class Datetime implements ComponentInterface {
}
}

/**
* Some days are constrained through max & min or allowed dates
* and also disabled because the component is readonly or disabled.
* These need to be displayed differently.
*/
const isCalDayConstrained = isCalDayDisabled && isDatetimeDisabled;

const isButtonDisabled = isCalDayDisabled || isDatetimeDisabled;

let dateStyle: DatetimeHighlightStyle | undefined = undefined;

/**
Expand Down Expand Up @@ -2158,11 +2211,12 @@ export class Datetime implements ComponentInterface {
data-year={year}
data-index={index}
data-day-of-week={dayOfWeek}
disabled={isCalDayDisabled}
disabled={isButtonDisabled}
class={{
'calendar-day-padding': isCalendarPadding,
'calendar-day': true,
'calendar-day-active': isActive,
'calendar-day-constrained': isCalDayConstrained,
'calendar-day-today': isToday,
}}
part={dateParts}
Expand Down Expand Up @@ -2237,7 +2291,7 @@ export class Datetime implements ComponentInterface {
}

private renderTimeOverlay() {
const { hourCycle, isTimePopoverOpen, locale } = this;
const { disabled, hourCycle, isTimePopoverOpen, locale } = this;
const computedHourCycle = getHourCycle(locale, hourCycle);
const activePart = this.getActivePartsWithFallback();

Expand All @@ -2251,6 +2305,7 @@ export class Datetime implements ComponentInterface {
part={`time-button${isTimePopoverOpen ? ' active' : ''}`}
aria-expanded="false"
aria-haspopup="true"
disabled={disabled}
onClick={async (ev) => {
const { popoverRef } = this;

Expand Down
99 changes: 99 additions & 0 deletions core/src/components/datetime/test/a11y/datetime.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,102 @@ configs({ directions: ['ltr'] }).forEach(({ title, screenshot, config }) => {
});
});
});

/**
* This behavior does not differ across
* modes/directions.
*/
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
test.describe(title('datetime: a11y'), () => {
test('datetime should be keyboard navigable', async ({ page, browserName }) => {
await page.setContent(
`
<ion-datetime value="2022-02-22T16:30:00"></ion-datetime>
`,
config
);
const tabKey = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';

const datetime = page.locator('ion-datetime');
const monthYearButton = page.locator('.calendar-month-year ion-item');
const prevButton = page.locator('.calendar-next-prev ion-button:nth-child(1)');
const nextButton = page.locator('.calendar-next-prev ion-button:nth-child(2)');

await page.keyboard.press(tabKey);
await expect(monthYearButton).toBeFocused();

await page.keyboard.press(tabKey);
await expect(prevButton).toBeFocused();

await page.keyboard.press(tabKey);
await expect(nextButton).toBeFocused();

// check value before & after selecting via keyboard
const initialValue = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(initialValue).toBe('2022-02-22T16:30:00');

await page.keyboard.press(tabKey);
await page.waitForChanges();

await page.keyboard.press('ArrowLeft');
await page.waitForChanges();

await page.keyboard.press('Enter');

await page.waitForChanges();

const newValue = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(newValue).not.toBe('2022-02-22T16:30:00');
});

test('buttons should be keyboard navigable', async ({ page }) => {
await page.setContent(
`

<ion-datetime value="2022-02-22T16:30:00" show-default-buttons="true" show-clear-button="true"></ion-datetime>
`,
config
);

await page.waitForSelector('.datetime-ready');

const clearButton = page.locator('#clear-button button');
const selectedDay = page.locator('.calendar-day-active');

await expect(selectedDay).toHaveText('22');

await clearButton.focus();
await page.waitForChanges();

await expect(clearButton).toBeFocused();
await page.keyboard.press('Enter');

await page.waitForChanges();

await expect(selectedDay).toHaveCount(0);
});

test('should navigate through months via right arrow key', async ({ page }) => {
await page.setContent(
`

<ion-datetime value="2022-02-28"></ion-datetime>
`,
config
);

await page.waitForSelector('.datetime-ready');
const calendarMonthYear = page.locator('ion-datetime .calendar-month-year');
const calendarBody = page.locator('.calendar-body');
await expect(calendarMonthYear).toHaveText('February 2022');

await calendarBody.focus();
await page.waitForChanges();

await page.keyboard.press('ArrowRight');
await page.waitForChanges();

await expect(calendarMonthYear).toHaveText('March 2022');
});
});
});
Loading