From eb23d36275d8d8ef75ac60192ba61d4fda17e654 Mon Sep 17 00:00:00 2001 From: Olivia Guyot Date: Thu, 12 Dec 2024 14:11:30 +0100 Subject: [PATCH] feat(pagination): improve pagination buttons Better reaction to change, style matches mockup --- .../pagination-buttons.component.html | 42 ++++++++++----- .../pagination-buttons.component.spec.ts | 16 ++---- .../pagination-buttons.component.ts | 53 +++++++------------ 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.html b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.html index 115bcf154..237fff434 100644 --- a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.html +++ b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.html @@ -1,22 +1,40 @@
-
+
- + - - - {{ page }} + + +
+ {{ page }} +
+
+ + ... - + {{ page }} @@ -25,9 +43,9 @@ type="light" [disabled]="listComponent.isLastPage" (buttonClick)="listComponent.goToNextPage()" - extraClass="!px-[3px]" + extraClass="text-gray-950" > - +
diff --git a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.spec.ts b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.spec.ts index 9d84597da..dd63fd479 100644 --- a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.spec.ts +++ b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.spec.ts @@ -24,7 +24,6 @@ describe('PaginationButtonsComponent', () => { fixture = TestBed.createComponent(PaginationButtonsComponent) component = fixture.componentInstance component.listComponent = new MockPaginable() - component.calculateVisiblePages() fixture.detectChanges() }) @@ -98,18 +97,11 @@ describe('PaginationButtonsComponent', () => { }) describe('when clicking on page button', () => { beforeEach(() => { - const paginationButtons = - fixture.nativeElement.querySelectorAll('gn-ui-button') - const pageBtnList = [] - paginationButtons.forEach((buttonElement) => { - const ngIcon = buttonElement.querySelector('ng-icon') - if (!ngIcon) { - pageBtnList.push(buttonElement) - } - }) - pageBtnList[1].dispatchEvent(new Event('buttonClick')) + const paginationButton = + fixture.nativeElement.querySelector('[data-test=page-2]') + paginationButton.dispatchEvent(new Event('buttonClick')) }) - it('is should access the requested page', () => { + it('should access the requested page', () => { expect(component.listComponent.goToPage).toHaveBeenCalledWith(2) }) }) diff --git a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.ts b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.ts index 6cfc681b7..d696a1e89 100644 --- a/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.ts +++ b/libs/ui/layout/src/lib/pagination-buttons/pagination-buttons.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnChanges } from '@angular/core' +import { Component, Input } from '@angular/core' import { ButtonComponent } from '@geonetwork-ui/ui/inputs' import { NgIcon, provideIcons } from '@ng-icons/core' import { CommonModule } from '@angular/common' @@ -18,15 +18,10 @@ import { Paginable } from '../paginable.interface' }), ], }) -export class PaginationButtonsComponent implements OnChanges { +export class PaginationButtonsComponent { @Input() listComponent: Paginable - visiblePages: (number | '...')[] = [] - ngOnChanges(): void { - this.calculateVisiblePages() - } - - calculateVisiblePages(): void { + get visiblePages(): (number | '...')[] { const maxVisiblePages = 5 const halfVisible = Math.floor(maxVisiblePages / 2) const startPage = Math.max(this.listComponent.currentPage - halfVisible, 1) @@ -35,33 +30,21 @@ export class PaginationButtonsComponent implements OnChanges { this.listComponent.pagesCount ) - const visiblePages: (number | '...')[] = [] - if (startPage > 1) { - visiblePages.push(1) - if (startPage > 2) { - visiblePages.push('...') + const allPages = new Array(this.listComponent.pagesCount) + .fill(0) + .map((_, i) => i + 1) // pages are 1-based + return allPages.reduce((pages, page) => { + if (page === 1 || page === this.listComponent.pagesCount) { + // first and last page + pages.push(page) + } else if (page >= startPage && page <= endPage) { + // pages around current one + pages.push(page) + } else if (pages[pages.length - 1] !== '...') { + // dots between pages + pages.push('...') } - } - for (let page = startPage; page <= endPage; page++) { - visiblePages.push(page) - } - if (endPage < this.listComponent.pagesCount) { - if (endPage < this.listComponent.pagesCount - 1) { - visiblePages.push('...') - } - visiblePages.push(this.listComponent.pagesCount) - } - - this.visiblePages = visiblePages - } - - changePage(page) { - this.setPage(page) - } - - setPage(newPage) { - if (!Number.isInteger(newPage)) return - this.listComponent.goToPage(newPage) - this.calculateVisiblePages() + return pages + }, []) } }