Skip to content

Commit 1884137

Browse files
authored
fix(ui5-date-time-picker): correct initial focus on small screens (#12752)
Previously on initial open of the ui5-date-time-picker web component on smaller screen, the focus was being set on the ui5-segmented-button component. The reason behind this was that, the button was first in the DOM tree. So now, instead of relying on automatic detection, we explicitly tell the ui5-responsive-popover which element should receive focus, by utilizing the initialFocus property (in phone mode only).
1 parent 08f8e35 commit 1884137

5 files changed

Lines changed: 40 additions & 2 deletions

File tree

packages/main/cypress/specs/DateTimePicker.cy.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,26 @@ describe("DateTimePicker general interaction", () => {
550550
});
551551

552552
describe("Accessibility", () => {
553+
it("initial focus goes to calendar's current date in phone mode", () => {
554+
// Using viewport instead of device simulation (focus is not available on actual phone devices), to trigger SegmentedButton view.
555+
cy.viewport(500, 800);
556+
557+
cy.mount(<DateTimePickerTemplate value="13/04/2020, 03:16:16 PM" formatPattern="dd/MM/yyyy, hh:mm:ss a" />);
558+
559+
cy.get<DateTimePicker>("[ui5-datetime-picker]").as("dtp");
560+
561+
cy.get<DateTimePicker>("@dtp").ui5DateTimePickerOpen();
562+
563+
cy.get<DateTimePicker>("@dtp")
564+
.ui5DateTimePickerGetPopover()
565+
.find("[ui5-calendar]")
566+
.shadow()
567+
.find("[ui5-daypicker]")
568+
.shadow()
569+
.find(".ui5-dp-item--selected")
570+
.should("be.focused");
571+
});
572+
553573
it("picker popover accessible name", () => {
554574
const LABEL = "Deadline";
555575
cy.mount(<DateTimePicker accessible-name={LABEL} />);

packages/main/src/Calendar.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ class Calendar extends CalendarPart {
612612
return this.shadowRoot!.querySelector(`[ui5-${this._currentPicker}picker]`)! as unknown as ICalendarPicker;
613613
}
614614

615+
/**
616+
* Returns the focusable element inside the Calendar (the current picker)
617+
* @override
618+
*/
619+
getFocusDomRef() {
620+
return this._currentPickerDOM as HTMLElement | undefined;
621+
}
622+
615623
/**
616624
* The year clicked the "Previous" button in the header
617625
*/

packages/main/src/DatePickerPopoverTemplate.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import information from "@ui5/webcomponents-icons/dist/information.js";
1414

1515
type TemplateHook = () => void;
1616

17-
export default function DatePickerPopoverTemplate(this: DatePicker, hooks?: { header?: TemplateHook, content?: TemplateHook, footer?: TemplateHook }) {
17+
export default function DatePickerPopoverTemplate(this: DatePicker, hooks?: { header?: TemplateHook, content?: TemplateHook, footer?: TemplateHook, initialFocus?: string }) {
1818
const header = hooks?.header || defaultHeader;
1919
const content = hooks?.content || defaultContent;
2020
const footer = hooks?.footer || defaultFooter;
21+
const initialFocus = hooks?.initialFocus;
2122

2223
return (
2324
<ResponsivePopover
@@ -31,6 +32,7 @@ export default function DatePickerPopoverTemplate(this: DatePicker, hooks?: { he
3132
accessibleName={this.pickerAccessibleName}
3233
hideArrow={true}
3334
_hideHeader={this._shouldHideHeader}
35+
initialFocus={initialFocus}
3436
onKeyDown={this._onkeydown}
3537
onClose={this.onResponsivePopoverAfterClose}
3638
onOpen={this.onResponsivePopoverAfterOpen}

packages/main/src/DateTimePicker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ class DateTimePicker extends DatePicker implements IFormInputElement {
322322
return false;
323323
}
324324

325+
/**
326+
* Returns the ID of the element to focus initially when the picker opens in phone mode
327+
* @private
328+
*/
329+
get _initialFocusId() {
330+
return this._phoneMode ? `${this._id}-calendar` : undefined;
331+
}
332+
325333
/**
326334
* EVENT HANDLERS
327335
*/

packages/main/src/DateTimePickerTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import TimeSelectionClocks from "./TimeSelectionClocks.js";
1212
export default function DateTimePickerTemplate(this: DateTimePicker) {
1313
return [
1414
DatePickerInputTemplate.call(this),
15-
DatePickerPopoverTemplate.call(this, { content, footer }),
15+
DatePickerPopoverTemplate.call(this, { content, footer, initialFocus: this._initialFocusId }),
1616
];
1717
}
1818

0 commit comments

Comments
 (0)