Skip to content

Commit c55bcdc

Browse files
authored
feat(ui5-carousel): add navigate event (#1454)
The event is fired whenever the selectedIndex changes due to user interaction - when the user clicks on the navigation arrows or while resizing, based on the "items-per-page-s/m/l" properties. Fixes: #1449
1 parent c6868af commit c55bcdc

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

packages/main/src/Carousel.hbs

+4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
{{#if arrows.content}}
2525
<div class="ui5-carousel-navigation-arrows">
2626
<ui5-button
27+
arrow-back
2728
class="ui5-carousel-navigation-button {{classes.navPrevButton}}"
2829
icon="slim-arrow-left"
2930
tabindex="-1"
3031
@click={{navigateLeft}}
3132
@keydown={{_onbuttonkeydown}}
3233
></ui5-button>
3334
<ui5-button
35+
arrow-forward
3436
class="ui5-carousel-navigation-button {{classes.navNextButton}}"
3537
icon="slim-arrow-right"
3638
tabindex="-1"
@@ -44,6 +46,7 @@
4446
<div class="{{classes.navigation}}">
4547
{{#if arrows.navigation}}
4648
<ui5-button
49+
arrow-back
4750
class="ui5-carousel-navigation-button {{classes.navPrevButton}}"
4851
icon="slim-arrow-left"
4952
tabindex="-1"
@@ -69,6 +72,7 @@
6972

7073
{{#if arrows.navigation}}
7174
<ui5-button
75+
arrow-forward
7276
class="ui5-carousel-navigation-button {{classes.navNextButton}}"
7377
icon="slim-arrow-right"
7478
tabindex="-1"

packages/main/src/Carousel.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const metadata = {
9090
},
9191

9292
/**
93-
* Defines the index of the initially selected page.
93+
* Defines the index of the initially selected item.
9494
* @type {Integer}
9595
* @defaultvalue 0
9696
* @public
@@ -152,7 +152,22 @@ const metadata = {
152152
},
153153
},
154154
events: /** @lends sap.ui.webcomponents.main.Carousel.prototype */ {
155-
//
155+
156+
/**
157+
* Fired whenever the <code>selectedIndex</code> changes due to user interaction,
158+
* when the user clicks on the navigation arrows or while resizing,
159+
* based on the <code>items-per-page-l</code>, <code>items-per-page-m</code> and <code>items-per-page-s</code> properties.
160+
*
161+
* @event
162+
* @param {Integer} selectedIndex the current <code>selectedIndex</code>.
163+
* @public
164+
* @since 1.0.0-rc.7
165+
*/
166+
navigate: {
167+
detail: {
168+
selectedIndex: { type: Integer },
169+
},
170+
},
156171
},
157172
};
158173

@@ -263,6 +278,7 @@ class Carousel extends UI5Element {
263278

264279
if (this.selectedIndex > this.pagesCount - 1) {
265280
this.selectedIndex = this.pagesCount - 1;
281+
this.fireEvent("navigate", { selectedIndex: this.selectedIndex });
266282
}
267283
}
268284

@@ -291,23 +307,39 @@ class Carousel extends UI5Element {
291307
}
292308

293309
navigateLeft() {
310+
this._resizing = false;
311+
312+
const peviousSelectedIndex = this.selectedIndex;
313+
294314
if (this.selectedIndex - 1 < 0) {
295315
if (this.cyclic) {
296316
this.selectedIndex = this.pagesCount - 1;
297317
}
298318
} else {
299319
--this.selectedIndex;
300320
}
321+
322+
if (peviousSelectedIndex !== this.selectedIndex) {
323+
this.fireEvent("navigate", { selectedIndex: this.selectedIndex });
324+
}
301325
}
302326

303327
navigateRight() {
328+
this._resizing = false;
329+
330+
const peviousSelectedIndex = this.selectedIndex;
331+
304332
if (this.selectedIndex + 1 > this.pagesCount - 1) {
305333
if (this.cyclic) {
306334
this.selectedIndex = 0;
307335
}
308336
} else {
309337
++this.selectedIndex;
310338
}
339+
340+
if (peviousSelectedIndex !== this.selectedIndex) {
341+
this.fireEvent("navigate", { selectedIndex: this.selectedIndex });
342+
}
311343
}
312344

313345
/**

packages/main/test/pages/Carousel.html

+21
Original file line numberDiff line numberDiff line change
@@ -484,5 +484,26 @@
484484
<ui5-button>Button 9</ui5-button>
485485
</ui5-carousel>
486486

487+
<ui5-carousel id="carousel8" items-per-page-m="4" items-per-page-l="4">
488+
<ui5-button>Button 1</ui5-button>
489+
<ui5-button>Button 2</ui5-button>
490+
<ui5-button>Button 3</ui5-button>
491+
<ui5-button>Button 4</ui5-button>
492+
<ui5-button>Button 5</ui5-button>
493+
<ui5-button>Button 6</ui5-button>
494+
<ui5-button>Button 7</ui5-button>
495+
<ui5-button>Button 8</ui5-button>
496+
</ui5-carousel>
497+
<ui5-input id="result"></ui5-input>
498+
<ui5-input id="resultCounter"></ui5-input>
499+
487500
</body>
501+
502+
<script>
503+
var counter = 0;
504+
carousel8.addEventListener("ui5-navigate", function(event){
505+
result.value = event.detail.selectedIndex;
506+
resultCounter.value = ++counter;
507+
});
508+
</script>
488509
</html>

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

+39
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,43 @@ describe("Carousel general interaction", () => {
110110
assert.strictEqual(selectedIndex, NORMALIZED_INDEX,
111111
"Although '15' is set, the actual selectedIndex is changed to 0.");
112112
});
113+
114+
it("Event navigate fired when pressing navigation arrows", () => {
115+
const carousel = browser.$("#carousel8");
116+
const selectedIndex = browser.$("#result");
117+
const eventCounter = browser.$("#resultCounter");
118+
const navigationArrowForward = carousel.shadow$("ui5-button[arrow-forward]");
119+
const navigationArrowsBack = carousel.shadow$("ui5-button[arrow-back]");
120+
121+
// using the navigtion arrows
122+
navigationArrowForward.click(); // forward
123+
assert.strictEqual(selectedIndex.getProperty("value"), "1", "The selectedIndex is correct.");
124+
assert.strictEqual(eventCounter.getProperty("value"), "1", "The navigate event is fired.");
125+
126+
navigationArrowForward.click(); // forward
127+
assert.strictEqual(selectedIndex.getProperty("value"), "2", "The selectedIndex is correct.");
128+
assert.strictEqual(eventCounter.getProperty("value"), "2", "The navigate event is fired.");
129+
130+
navigationArrowsBack.click(); // back
131+
assert.strictEqual(selectedIndex.getProperty("value"), "1", "The selectedIndex is correct");
132+
assert.strictEqual(eventCounter.getProperty("value"), "3", "The navigate event is fired.");
133+
134+
navigationArrowsBack.click(); // back
135+
assert.strictEqual(selectedIndex.getProperty("value"), "0", "The selectedIndex is correct.");
136+
assert.strictEqual(eventCounter.getProperty("value"), "4", "The navigate event is fired.");
137+
138+
// using the keyboard navigation
139+
carousel.click();
140+
carousel.keys("ArrowRight");
141+
assert.strictEqual(selectedIndex.getProperty("value"), "1", "The selectedIndex is correct.");
142+
assert.strictEqual(eventCounter.getProperty("value"), "5", "The navigate event is fired.");
143+
144+
carousel.keys("ArrowLeft");
145+
assert.strictEqual(selectedIndex.getProperty("value"), "0", "The selectedIndex is correct.");
146+
assert.strictEqual(eventCounter.getProperty("value"), "6", "The navigate event is fired.");
147+
148+
carousel.keys("ArrowLeft");
149+
assert.strictEqual(selectedIndex.getProperty("value"), "0", "The selectedIndex is correct.");
150+
assert.strictEqual(eventCounter.getProperty("value"), "6", "The navigate event is not fired as no previous item.");
151+
});
113152
});

0 commit comments

Comments
 (0)