Skip to content

Commit

Permalink
fix(pagination): forbid defaultCurrentPage values below 1 or above to…
Browse files Browse the repository at this point in the history
…tal pages
  • Loading branch information
Leotheluck authored and dpellier committed Feb 20, 2024
1 parent b7ad13c commit 4fff9d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ describe('e2e:osds-pagination', () => {
const buttonList = await page.findAll('osds-pagination >>> li >>> osds-button');
expect(buttonList.length).toBe(9);
});

it('should not allow for a defaultCurrentPage lower than 1', async() => {
await setup({ attributes: { defaultCurrentPage: -5, totalPages: 5 } });

const current = await el.callMethod('getCurrentPage');
expect(current).toEqual(1);
});

it('should not allow for a defaultCurrentPage higher than total number of pages', async() => {
await setup({ attributes: { defaultCurrentPage: 10, totalPages: 5 } });

const current = await el.callMethod('getCurrentPage');
expect(current).toEqual(5);
});
});

describe('should change page if we click', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ export class OsdsPagination implements OdsPaginationAttribute, OdsPaginationEven

componentWillLoad(): void {
this.itemPerPage = ODS_PAGINATION_PER_PAGE_OPTIONS.includes(this.defaultItemsPerPage) && this.defaultItemsPerPage || ODS_PAGINATION_PER_PAGE_MIN;
this.current = this.defaultCurrentPage || this.current;

if (this.totalItems) {
this.actualTotalPages = this.controller.computeActualTotalPages(this.itemPerPage);
} else {
this.actualTotalPages = this.totalPages;
}

if (this.defaultCurrentPage > this.actualTotalPages) {
this.current = this.actualTotalPages;
} else if (this.defaultCurrentPage < 1) {
this.current = 1;
} else {
this.current = this.defaultCurrentPage || this.current;
}

this.updatePageList();
this.isFirstLoad = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/pagination/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h3>Total items between 10 and 300</h3>

<h3>Total items above 300</h3>
<div class="row">
<osds-pagination id="paginationAddLabel" default-current-page=10 total-items=500 default-items-per-page=50>
<osds-pagination id="paginationAddLabel" default-current-page=8 total-items=500 default-items-per-page=50>
<span slot="before-total-items">of&nbsp;</span>
<span slot="after-total-items">&nbsp;results</span>
</osds-pagination>
Expand Down

0 comments on commit 4fff9d1

Please sign in to comment.