Skip to content

Commit

Permalink
fix(pagination): fix issues with pagination playground story (#10883)
Browse files Browse the repository at this point in the history
* fix(pagination): fix issues with pagination playground story

* fix(pagination): avoid division by 0
  • Loading branch information
m4olivei committed Aug 30, 2023
1 parent c29a30d commit 082b467
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const Playground = (args) => {
itemsPerPageText,
page,
pageInputDisabled,
pageSize,
pageSizeInputDisabled,
pagesUnknown,
size,
Expand All @@ -91,8 +92,9 @@ export const Playground = (args) => {
?disabled=${disabled}
forward-text=${forwardText}
?is-last-page=${isLastPage}
items-per-page=${itemsPerPageText}
items-per-page-text=${itemsPerPageText}
page=${page}
page-size=${pageSize}
?page-input-disabled=${pageInputDisabled}
?page-size-input-disabled=${pageSizeInputDisabled}
size=${size}
Expand Down Expand Up @@ -122,9 +124,8 @@ export default {
'Explicitly state that the user is at the last page (is-last-page)',
false
),
itemsPerPage: text('Items per page text', 'Items per page:'),
itemsPerPageText: text('Items per page text', 'Items per page:'),
page: number('The current page', 1),

pageSize: number('Number of rows per page (page-size)', 10),
pageInputDisabled: boolean('Pages input disabled', false),
pageSizeInputDisabled: boolean('Pages size input disabled', false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ class CDSPagination extends FocusMixin(HostListenerMixin(LitElement)) {

if (event.composedPath()[0] === this._pageSizeSelect) {
this.pageSize = parseInt(value);
this.totalPages = Math.ceil(totalItems / pageSize);
// Default pageSize to effectively be 1 when we have a value of 0 to avoid
// division by 0.
this.totalPages =
pageSize > 0 ? Math.ceil(totalItems / pageSize) : totalItems;
this.page = 1;
this.start = 0;
} else {
Expand Down Expand Up @@ -274,7 +277,10 @@ class CDSPagination extends FocusMixin(HostListenerMixin(LitElement)) {
pageSize;
}
if (changedProperties.has('pageSize') || changedProperties.has('start')) {
this.totalPages = Math.ceil(totalItems / pageSize);
// Default pageSize to effectively be 1 when we have a value of 0 to avoid
// division by 0.
this.totalPages =
pageSize > 0 ? Math.ceil(totalItems / pageSize) : totalItems;
(this.shadowRoot!.querySelector(selectorPagesSelect) as CDSSelect).value =
this.page.toString();
}
Expand Down

0 comments on commit 082b467

Please sign in to comment.