Skip to content

Commit 6b5a550

Browse files
fix: fix navigation when cyclic=true
1 parent 1a743db commit 6b5a550

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

packages/main/src/Carousel.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -595,25 +595,45 @@ class Carousel extends UI5Element {
595595
}
596596

597597
async navigateLeft() {
598-
this.focusItemIndex(this._focusedItemIndex - 1);
598+
let newFocusedItemIndex = this._focusedItemIndex - 1;
599+
if (this.cyclic && newFocusedItemIndex < 0) {
600+
newFocusedItemIndex = this.items.length - 1;
601+
}
602+
603+
this.focusItemIndex(newFocusedItemIndex);
599604
await renderFinished();
600605
this.focusItem();
601606
}
602607

603608
async navigateRight() {
604-
this.focusItemIndex(this._focusedItemIndex + 1);
609+
let newFocusedItemIndex = this._focusedItemIndex + 1;
610+
if (this.cyclic && newFocusedItemIndex > this.items.length - 1) {
611+
newFocusedItemIndex = 0;
612+
}
613+
614+
this.focusItemIndex(newFocusedItemIndex);
605615
await renderFinished();
606616
this.focusItem();
607617
}
608618

609619
async navigateArrowRight() {
610-
this.navigateTo(this._currentSlideIndex + 1);
620+
let newCurrentSlideIndex = this._currentSlideIndex + 1;
621+
if (this.cyclic && newCurrentSlideIndex > this.items.length - this.effectiveItemsPerPage) {
622+
newCurrentSlideIndex = 0;
623+
}
624+
625+
this.navigateTo(newCurrentSlideIndex);
611626
await renderFinished();
612627
this.focusItem();
613628
}
614629

615630
async navigateArrowLeft() {
616-
this.navigateTo(this._currentSlideIndex - 1);
631+
let newCurrentSlideIndex = this._currentSlideIndex - 1;
632+
if (this.cyclic && newCurrentSlideIndex < 0) {
633+
newCurrentSlideIndex = this.items.length - 1;
634+
}
635+
636+
this.navigateTo(newCurrentSlideIndex);
617637
await renderFinished();
618638
this.focusItem();
619639
}
@@ -624,18 +644,11 @@ class Carousel extends UI5Element {
624644

625645
_navButtonClick(e: UI5CustomEvent<Icon, "click">) {
626646
const target = e.target as Icon;
627-
if (this._visibleItemsIndexes.length > 1) {
628-
if (target.hasAttribute("data-ui5-arrow-forward")) {
629-
this.navigateArrowRight();
630-
} else {
631-
this.navigateArrowLeft();
632-
}
633-
} else if (this._visibleItemsIndexes.length <= 1) {
634-
if (target.hasAttribute("data-ui5-arrow-forward")) {
635-
this.navigateRight();
636-
} else {
637-
this.navigateLeft();
638-
}
647+
648+
if (target.hasAttribute("data-ui5-arrow-forward")) {
649+
this.navigateArrowRight();
650+
} else {
651+
this.navigateArrowLeft();
639652
}
640653
}
641654

packages/main/test/pages/CarouselPublicMethods.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<ui5-button id="navigateLeft" icon="slim-arrow-left"></ui5-button>
1616
<ui5-button id="navigateRight" icon="slim-arrow-right"></ui5-button>
1717
</div>
18-
<ui5-carousel id="carouselCards" items-per-page="S3 M3 L3 XL3">
18+
<ui5-carousel id="carouselCards" items-per-page="S3 M3 L3 XL3" cyclic>
1919
<ui5-card id="card" class="myCard">
2020
<ui5-card-header slot="header" status="1 of 23" title-text="1" subtitle-text="quick links"
2121
interactive>

0 commit comments

Comments
 (0)