Skip to content

Commit

Permalink
fix(ui5-popup): initialFocus won't work if 'autofocus' is set (#8956)
Browse files Browse the repository at this point in the history
* fix(ui5-popup): initialFocus won't work if 'autofocus' is set
  • Loading branch information
TeodorTaushanov committed Jun 5, 2024
1 parent aedcdb9 commit 2c9f775
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/main/src/Popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ type PopupBeforeCloseEventDetail = {
abstract class Popup extends UI5Element {
/**
* Defines the ID of the HTML Element, which will get the initial focus.
*
* **Note:** If an element with `autofocus` attribute is added inside the component,
* `initialFocus` won't take effect.
* @default ""
* @public
*/
Expand Down Expand Up @@ -453,6 +456,11 @@ abstract class Popup extends UI5Element {
* @returns Promise that resolves when the focus is applied
*/
async applyFocus(): Promise<void> {
// do nothing if the standard HTML autofocus is used
if (this.querySelector("[autofocus]")) {
return;
}

await this._waitForDomRef();

if (this.getRootNode() === this) {
Expand Down
16 changes: 16 additions & 0 deletions packages/main/test/pages/Dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@
</ui5-dialog>
<br>
<br>
<ui5-button id="btnDialogAutofocus">Dialog with autofocus</ui5-button>
<ui5-dialog header-text="Dialog" id="dialogAutofocus">
Some Dialog
<input />
<ui5-button autofocus id="btnDialogAutofocusClose">Close</ui5-button>
</ui5-dialog>
<br>
<br>
<div>
<ui5-title level="H3">Open Dialog from list</ui5-title>
<ui5-list id="listContainerId" selection-mode="Single">
Expand Down Expand Up @@ -839,6 +847,14 @@
window["dialogFocus1"].open = false;
});

window["btnDialogAutofocus"].addEventListener("click", function () {
window["dialogAutofocus"].open = true;
});

window["btnDialogAutofocusClose"].addEventListener("click", function () {
window["dialogAutofocus"].open = false;
});

</script>
</body>

Expand Down
13 changes: 13 additions & 0 deletions packages/main/test/specs/Dialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ describe("Dialog general interaction", () => {
assert.strictEqual(await firstActiveBtn.isFocused(), true, "Correct element is focused");
await browser.keys(["Shift", "Tab"]);
assert.strictEqual(await secondActiveBtn.isFocused(), true, "Correct element is focused");

await browser.keys("Escape");
});

it("initial focus with autofocus", async () => {
const openDialogAutofocus = await browser.$("#btnDialogAutofocus");
await openDialogAutofocus.scrollIntoView();
await openDialogAutofocus.click();

const closeButton = await browser.$("#btnDialogAutofocusClose");

assert.ok(closeButton.isFocused(), "initial focus is correct");
await closeButton.click();
});
});

Expand Down

0 comments on commit 2c9f775

Please sign in to comment.