Skip to content

Commit 7d9e409

Browse files
committed
fix(ui5-icon): fix click event fired twice (#2858)
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
1 parent 8ad9979 commit 7d9e409

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

packages/main/src/Icon.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,17 @@ class Icon extends UI5Element {
194194
}
195195

196196
_onkeydown(event) {
197-
if (this.interactive && isEnter(event)) {
197+
if (!this.interactive) {
198+
return;
199+
}
200+
201+
if (isEnter(event)) {
198202
this.fireEvent("click");
199203
}
204+
205+
if (isSpace(event)) {
206+
event.preventDefault(); // prevent scrolling
207+
}
200208
}
201209

202210
_onkeyup(event) {
@@ -207,8 +215,8 @@ class Icon extends UI5Element {
207215

208216
_onclick(event) {
209217
if (this.interactive) {
210-
event.preventDefault();
211-
// Prevent the native event and fire custom event because otherwise the noConfict event won't be thrown
218+
// prevent the native event and fire custom event to ensure the noConfict "ui5-click" is fired
219+
event.stopPropagation();
212220
this.fireEvent("click");
213221
}
214222
}

packages/main/test/pages/Icon.html

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
<ui5-icon name="add-employee" class="icon-red icon-small"></ui5-icon>
3838
<ui5-icon show-tooltip name="message-error"></ui5-icon>
3939

40+
<h3>Interactive Icon</h3>
41+
<ui5-icon
42+
id="myInteractiveIcon"
43+
interactive
44+
name="add-equipment"
45+
style="width: 50px; height: 50px">
46+
</ui5-icon>
47+
<br>
48+
<ui5-input id="click-event-2"></ui5-input>
49+
4050
<br>
4151
<ui5-icon name="add-employee"></ui5-icon>
4252
<br>
@@ -103,7 +113,9 @@
103113
var icon = document.getElementById("interactive-icon"),
104114
nonInteractiveIcon = document.getElementById("non-interactive-icon"),
105115
input = document.getElementById("click-event"),
116+
input2 = document.getElementById("click-event-2"),
106117
inputValue = 0;
118+
inputValue2 = 0;
107119

108120
icon.addEventListener("ui5-click", function() {
109121
input.value = ++inputValue;
@@ -112,6 +124,10 @@
112124
nonInteractiveIcon.addEventListener("ui5-click", function() {
113125
input.value = ++inputValue;
114126
});
127+
128+
myInteractiveIcon.addEventListener("click", function() {
129+
input2.value = ++inputValue2;
130+
});
115131
</script>
116132

117133
<script type="module">

packages/main/test/specs/Icon.spec.js

+35-22
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,48 @@ describe("Icon general interaction", () => {
55

66
it("Tests icon rendering", () => {
77
const iconRoot = browser.$("#interactive-icon").shadow$("ui5-icon-root");
8+
const iconWithTooltip = browser.$("#iconWithTooltip");
9+
const iconTooltip = iconWithTooltip.shadow$(`#${iconWithTooltip.getProperty("_id")}-tooltip`);
10+
const ICON_TOOLTIP_TEXT = "Save";
811

912
assert.ok(iconRoot, "Icon is rendered");
10-
});
13+
assert.strictEqual(iconTooltip.getHTML(false).includes(ICON_TOOLTIP_TEXT), true,
14+
"Built-in tooltip is correct");
15+
});
16+
17+
it("Tests noConflict 'ui5-click' event is thrown for interactive icons", () => {
18+
const iconRoot = browser.$("#interactive-icon").shadow$(".ui5-icon-root");
19+
const input = browser.$("#click-event");
20+
21+
iconRoot.click();
22+
assert.strictEqual(input.getAttribute("value"), "1", "Mouse click throws event");
1123

12-
it("Tests if clicked event is thrown for interactive icons", () => {
13-
const iconRoot = browser.$("#interactive-icon").shadow$(".ui5-icon-root");
14-
const input = browser.$("#click-event");
24+
iconRoot.keys("Enter");
25+
assert.strictEqual(input.getAttribute("value"), "2", "Enter throws event");
1526

16-
iconRoot.click();
17-
assert.strictEqual(input.getAttribute("value"), "1", "Mouse click throws event");
27+
iconRoot.keys("Space");
28+
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
29+
});
30+
31+
it("Tests noConflict 'ui5-click' event is not thrown for non interactive icons", () => {
32+
const iconRoot = browser.$("#non-interactive-icon");
33+
const input = browser.$("#click-event");
1834

19-
iconRoot.keys("Enter");
20-
assert.strictEqual(input.getAttribute("value"), "2", "Enter throws event");
35+
iconRoot.click();
36+
assert.strictEqual(input.getAttribute("value"), "3", "Mouse click throws event");
2137

22-
iconRoot.keys("Space");
23-
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
24-
});
25-
26-
it("Tests if clicked event is not thrown for non interactive icons", () => {
27-
const iconRoot = browser.$("#non-interactive-icon");
28-
const input = browser.$("#click-event");
38+
iconRoot.keys("Enter");
39+
assert.strictEqual(input.getAttribute("value"), "3", "Enter throws event");
2940

30-
iconRoot.click();
31-
assert.strictEqual(input.getAttribute("value"), "3", "Mouse click throws event");
41+
iconRoot.keys("Space");
42+
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
43+
});
3244

33-
iconRoot.keys("Enter");
34-
assert.strictEqual(input.getAttribute("value"), "3", "Enter throws event");
45+
it("Tests native 'click' event thrown", () => {
46+
const icon = browser.$("#myInteractiveIcon");
47+
const input = browser.$("#click-event-2");
3548

36-
iconRoot.keys("Space");
37-
assert.strictEqual(input.getAttribute("value"), "3", "Space throws event");
49+
icon.click();
50+
assert.strictEqual(input.getAttribute("value"), "1", "Mouse click throws event");
3851
});
39-
});
52+
});

0 commit comments

Comments
 (0)