feat(carousel): enhance circular layout handling and optimize page creation logic

This commit is contained in:
2026-09-24 15:20:57 -03:00
parent ee1420801d
commit f4959e0250
2 changed files with 24 additions and 4 deletions

View File

@@ -70,6 +70,10 @@ export class CarouselComponent<T> implements AfterViewInit {
);
protected readonly isCircularLayout = computed(() => this.circular() && this.pageCount() > 1);
protected readonly circularPages = computed<CarouselPage<T>[]>(() => {
if (!this.isCircularLayout()) {
return [];
}
const pageCount = this.pageCount();
const currentPage = this.normalizePage(this.currentPage(), pageCount);
@@ -270,14 +274,20 @@ export class CarouselComponent<T> implements AfterViewInit {
}
private createPage(slot: CarouselPage<T>['slot'], page: number): CarouselPage<T> {
const sourceItems = this.items();
// Layout has not necessarily been measured when a computed value is inspected.
const pageSize = Math.min(this.itemsPerPage(), sourceItems.length);
if (pageSize === 0) {
return { slot, items: [] };
}
const pageCount = this.pageCount();
const normalizedPage = this.normalizePage(page, pageCount);
const start = normalizedPage * this.itemsPerPage();
const start = normalizedPage * pageSize;
const items: CarouselPageItem<T>[] = [];
for (let offset = 0; offset < this.itemsPerPage(); offset += 1) {
const index = (start + offset) % this.items().length;
items.push({ item: this.items()[index], index });
for (let offset = 0; offset < pageSize; offset += 1) {
const index = (start + offset) % sourceItems.length;
items.push({ item: sourceItems[index], index });
}
return { slot, items };