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-menu): fix runtime js error on getElementById call #8021

Merged
merged 3 commits into from
Dec 19, 2023
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
10 changes: 7 additions & 3 deletions packages/main/src/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,9 @@ class Menu extends UI5Element {
if (!this.opener) {
return;
}
if (this.open) {
const rootNode = this.getRootNode() as Document;
const opener = this.opener instanceof HTMLElement ? this.opener : rootNode && rootNode.getElementById(this.opener);

if (this.open) {
const opener = this.getOpener();
if (opener) {
this.showAt(opener);
}
Expand Down Expand Up @@ -444,6 +443,11 @@ class Menu extends UI5Element {
return this._popover;
}

getOpener() {
const rootNode = this.getRootNode() as Document;
return this.opener instanceof HTMLElement ? this.opener : rootNode?.getElementById?.(this.opener);
}

_navigateBack() {
const parentMenuItem = this._parentItemsStack.pop();

Expand Down
67 changes: 21 additions & 46 deletions packages/main/test/pages/Menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" type="text/css" href="./styles/Menu.css">
</head>

<body class="responsivepopover2auto">
<body class="bg">
<ui5-button id="btnOpen">Open Menu</ui5-button> <br/>
<ui5-menu id="menu" header-text="My ui5-menu">

Expand All @@ -39,51 +39,26 @@
<ui5-menu-item text="Exit" icon="journey-arrive"></ui5-menu-item>
</ui5-menu>

<header class="header">
<h3 class="header-title">Clicked menu item text</h3>
</header>

<div class="samples-margin">
<ui5-input id="selectionInput"></ui5-input>
</div>

<header class="header">
<h3 class="header-title">Text Direction</h3>
</header>
<div class="samples-margin">
<ui5-switch id="direction" text-on="RTL" text-off="LTR"></ui5-switch>
</div>

<header class="header">
<h3 class="header-title">Prevent "before-open" event</h3>
</header>
<div class="samples-margin">
<ui5-switch id="preventBeforeOpen" text-on="Yes" text-off="No"></ui5-switch>
</div>

<header class="header">
<h3 class="header-title">Prevent "before-close" event</h3>
</header>
<div class="samples-margin">
<ui5-switch id="preventBeforeClose" text-on="Yes" text-off="No"></ui5-switch>
</div>

<header class="header">
<h3 class="header-title">open/opener</h3>
</header>
<div class="samples-margin">
<ui5-button id="btnAddOpener">Add opener</ui5-button>
<ui5-button id="btnRemoveOpener">Remove opener</ui5-button>
<ui5-toggle-button id="btnToggleOpen">Toggle open</ui5-toggle-button> <br/>
<ui5-input id="openerInput"></ui5-input>
</div>

<header class="header">
<h3 class="header-title">Event logger</h3>
</header>
<div class="samples-margin">
<ui5-input id="eventLogger" style="width: 100%;"></ui5-input>
</div>
<ui5-title level="H5" class="header-title">Clicked menu item text</ui5-title>
<ui5-input id="selectionInput"></ui5-input>

<ui5-title level="H5" class="header-title">Text Direction</ui5-title>
<ui5-switch id="direction" text-on="RTL" text-off="LTR"></ui5-switch>

<ui5-title level="H5" class="header-title">Prevent "before-open" event</ui5-title>
<ui5-switch id="preventBeforeOpen" text-on="Yes" text-off="No"></ui5-switch>

<ui5-title level="H5" class="header-title">Prevent "before-close" event</ui5-title>
<ui5-switch id="preventBeforeClose" text-on="Yes" text-off="No"></ui5-switch>

<ui5-title level="H5" class="header-title">open/opener</ui5-title>
<ui5-button id="btnAddOpener">Add opener</ui5-button>
<ui5-button id="btnRemoveOpener">Remove opener</ui5-button>
<ui5-toggle-button id="btnToggleOpen">Toggle open</ui5-toggle-button> <br/>
<ui5-input id="openerInput"></ui5-input>

<ui5-title level="H5" class="header-title">Event logger</ui5-title>
<ui5-input id="eventLogger" style="width: 100%;"></ui5-input>

<script>
btnOpen.accessibilityAttributes = {
Expand Down
8 changes: 8 additions & 0 deletions packages/main/test/pages/styles/Menu.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.height100percent {
height: 100%
}

.bg {
background-color: var(--sapBackgroundColor);
}

.header-title {
margin-block-start: 0.5rem;
}