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(ui5-popup): initialFocus won't work if 'autofocus' is set #8956

Merged
merged 2 commits into from
Jun 5, 2024
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
8 changes: 8 additions & 0 deletions packages/main/src/Popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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 @@ -438,6 +441,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