Skip to content

Commit

Permalink
fix(canary-web-components): icpaginationbar itemsperpage fix all opti…
Browse files Browse the repository at this point in the history
…on when the options vary

include the all option when there are fewer items than the available options

fix mi6#2843
  • Loading branch information
dn55533 committed Jan 6, 2025
1 parent a7135b5 commit c7bbf0c
Showing 1 changed file with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class PaginationBar {
* If `true`, the 'All' option will be hidden from the 'items per page' select input.
*/
@Prop() hideAllFromItemsPerPage?: boolean = false;
@Watch("hideAllFromItemsPerPage")
watchHideAllFromItemsPerPageHandler(): void {
this.setPaginationBarContent();
}

/**
* The text which will be used in place of 'Item' on the pagination bar.
Expand Down Expand Up @@ -358,7 +362,7 @@ export class PaginationBar {
this.icPageChange.emit({ value: this.activePage, fromItemsPerPage: true });
};

private setPaginationBarContent = (): void => {
private setItemsPerPageOptions = () => {
const clonedItemsPerPageOptions: {
label: string;
value: string;
Expand All @@ -379,16 +383,39 @@ export class PaginationBar {
{ label: "100", value: "100" },
{ label: "1000", value: "1000" },
]);
!this.hideAllFromItemsPerPage &&
displayedItemsPerPageOptions.push({
label: "All",
value: String(this.totalItems),
});

this.displayedItemsPerPageOptions = displayedItemsPerPageOptions.filter(
({ value }) => this.totalItems >= Number(value)
);

const currentItemsLength = this.displayedItemsPerPageOptions.length;

const addAllOption = () =>
this.displayedItemsPerPageOptions.push({
label: "All",
value: String(this.totalItems),
});

const lastItemsPerPageEqualsTotalItems = () =>
Number(
this.displayedItemsPerPageOptions[currentItemsLength - 1].value
) === this.totalItems;

const relabelLastOptionToAll = () =>
(this.displayedItemsPerPageOptions[currentItemsLength - 1].label = "All");

if (currentItemsLength === 0) {
addAllOption();
} else if (lastItemsPerPageEqualsTotalItems()) {
relabelLastOptionToAll();
} else if (!this.hideAllFromItemsPerPage) {
addAllOption();
}
};

private setPaginationBarContent = (): void => {
this.setItemsPerPageOptions();

let lastOptionValue = 0;
const updated = this.displayedItemsPerPageOptions.some(({ value }) => {
lastOptionValue = Number(value);
Expand Down

0 comments on commit c7bbf0c

Please sign in to comment.