Skip to content

Commit

Permalink
fix(ui5-icon): fix click event fired twice (#2858)
Browse files Browse the repository at this point in the history
We used to fire a custom event, but did not stop the native one and end up with firing two "click" events.
Now, the native one is stopped properly (we need to fire the custom one, so the noConflict ui5-click is also fired). Also, one additional fix has been performed - the content no longer scrolls when the SPACE key is pressed over "interactive" icon.

FIXES: #2857
  • Loading branch information
ilhan007 committed Feb 23, 2021
1 parent deb173a commit 6fd6a5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
14 changes: 11 additions & 3 deletions packages/main/src/Icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,17 @@ class Icon extends UI5Element {
}

_onkeydown(event) {
if (this.interactive && isEnter(event)) {
if (!this.interactive) {
return;
}

if (isEnter(event)) {
this.fireEvent("click");
}

if (isSpace(event)) {
event.preventDefault(); // prevent scrolling
}
}

_onkeyup(event) {
Expand All @@ -212,8 +220,8 @@ class Icon extends UI5Element {

_onclick(event) {
if (this.interactive) {
event.preventDefault();
// Prevent the native event and fire custom event because otherwise the noConfict event won't be thrown
// prevent the native event and fire custom event to ensure the noConfict "ui5-click" is fired
event.stopPropagation();
this.fireEvent("click");
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/main/test/pages/Icon.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
<ui5-icon name="add-employee" class="icon-red icon-small"></ui5-icon>
<ui5-icon show-tooltip name="message-error"></ui5-icon>

<h3>Interactive Icon</h3>
<ui5-icon
id="myInteractiveIcon"
interactive
name="add-equipment"
style="width: 50px; height: 50px">
</ui5-icon>
<br>
<ui5-input id="click-event-2"></ui5-input>

<br>
<ui5-icon name="add-employee"></ui5-icon>
<br>
Expand Down Expand Up @@ -89,7 +99,9 @@
var icon = document.getElementById("interactive-icon"),
nonInteractiveIcon = document.getElementById("non-interactive-icon"),
input = document.getElementById("click-event"),
input2 = document.getElementById("click-event-2"),
inputValue = 0;
inputValue2 = 0;

icon.addEventListener("ui5-click", function() {
input.value = ++inputValue;
Expand All @@ -98,6 +110,10 @@
nonInteractiveIcon.addEventListener("ui5-click", function() {
input.value = ++inputValue;
});

myInteractiveIcon.addEventListener("click", function() {
input2.value = ++inputValue2;
});
</script>

<script type="module">
Expand Down
12 changes: 10 additions & 2 deletions packages/main/test/specs/Icon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Icon general interaction", () => {
"Built-in tooltip is correct");
});

it("Tests if clicked event is thrown for interactive icons", () => {
it("Tests noConflict 'ui5-click' event is thrown for interactive icons", () => {
const iconRoot = browser.$("#interactive-icon").shadow$(".ui5-icon-root");
const input = browser.$("#click-event");

Expand All @@ -28,7 +28,7 @@ describe("Icon general interaction", () => {
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
});

it("Tests if clicked event is not thrown for non interactive icons", () => {
it("Tests noConflict 'ui5-click' event is not thrown for non interactive icons", () => {
const iconRoot = browser.$("#non-interactive-icon");
const input = browser.$("#click-event");

Expand All @@ -41,4 +41,12 @@ describe("Icon general interaction", () => {
iconRoot.keys("Space");
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
});

it("Tests native 'click' event thrown", () => {
const icon = browser.$("#myInteractiveIcon");
const input = browser.$("#click-event-2");

icon.click();
assert.strictEqual(input.getAttribute("value"), "1", "Mouse click throws event");
});
});

0 comments on commit 6fd6a5e

Please sign in to comment.