-
Notifications
You must be signed in to change notification settings - Fork 125
[ix-pagination] Add option to decide on number of items #2364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d9bd7b3
8a9d677
88e1c5b
0989556
c8d09ce
b1cc1c8
87912a8
09e8ddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@siemens/ix-angular": minor | ||
| "@siemens/ix": minor | ||
| "@siemens/ix-react": minor | ||
| "@siemens/ix-vue": minor | ||
| --- | ||
|
|
||
| Add property **itemCountOptions** to **ix-pagination. | ||
|
|
||
| Fixes #2103 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |
| h, | ||
| Host, | ||
| Prop, | ||
| Watch, | ||
| } from '@stencil/core'; | ||
| import { BaseButton, BaseButtonProps } from '../button/base-button'; | ||
| import { a11yBoolean } from '../utils/a11y'; | ||
|
|
@@ -41,6 +42,9 @@ export class Pagination { | |
| }; | ||
|
|
||
| private readonly maxCountPages = 7; | ||
| private readonly defaultItemCountOptions = [10, 15, 20, 40, 100]; | ||
| private readonly itemCountOptionsPropName = 'itemCountOptions'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be hard coded in the console.warnings |
||
| private verifiedItemCountOptions?: number[]; | ||
|
|
||
| @Element() hostElement!: HTMLIxPaginationElement; | ||
|
|
||
|
|
@@ -60,6 +64,16 @@ export class Pagination { | |
| */ | ||
| @Prop() hideItemCount = false; | ||
|
|
||
| /** | ||
| * Custom item count options for advanced mode. | ||
| * Provide an array of numbers to display in the items per page dropdown. | ||
| * If not provided or empty, defaults to [10, 15, 20, 40, 100]. | ||
| * Only positive integers greater than 0 are valid. Invalid values and duplicates are automatically filtered out. | ||
| * | ||
| * @since 4.3.0 | ||
| */ | ||
| @Prop() itemCountOptions?: number[]; | ||
|
|
||
| /** | ||
| * Total number of pages | ||
| */ | ||
|
|
@@ -118,6 +132,87 @@ export class Pagination { | |
| */ | ||
| @Event() itemCountChanged!: EventEmitter<number>; | ||
|
|
||
| @Watch('itemCountOptions') | ||
| onItemCountOptionsChange() { | ||
| if (this.advanced && !this.hideItemCount) { | ||
| this.verifiedItemCountOptions = this.getValidItemCountOptions(); | ||
| this.verifyEmptyItemCountOptions(); | ||
| this.verifyAllInvalidItemCountOptions(); | ||
| this.verifyItemCountMismatch(); | ||
| } | ||
| } | ||
|
|
||
| componentWillLoad() { | ||
| if (!this.advanced || this.hideItemCount) { | ||
| return; | ||
| } | ||
|
|
||
| this.verifiedItemCountOptions = this.getValidItemCountOptions(); | ||
| this.verifyEmptyItemCountOptions(); | ||
| this.verifyAllInvalidItemCountOptions(); | ||
| this.verifyItemCountMismatch(); | ||
| } | ||
|
Comment on lines
+135
to
+154
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic within |
||
|
|
||
| private verifyEmptyItemCountOptions(): void { | ||
| if (this.itemCountOptions?.length !== 0) { | ||
| return; | ||
| } | ||
|
|
||
| console.warn( | ||
| `[ix-pagination] ${this.itemCountOptionsPropName} is an empty array. Falling back to default options: [${this.defaultItemCountOptions.join(', ')}]` | ||
| ); | ||
| } | ||
|
|
||
| private verifyAllInvalidItemCountOptions(): void { | ||
| if ( | ||
| !this.itemCountOptions?.length || | ||
| (this.verifiedItemCountOptions?.length ?? 0) > 0 | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| console.warn( | ||
| `[ix-pagination] All values in ${this.itemCountOptionsPropName} are invalid. ` + | ||
| `Only positive integers are allowed. Falling back to default options: [${this.defaultItemCountOptions.join(', ')}]` | ||
| ); | ||
| this.verifiedItemCountOptions = this.defaultItemCountOptions; | ||
| } | ||
|
|
||
| private verifyItemCountMismatch(): void { | ||
| if (!this.verifiedItemCountOptions?.length) { | ||
| return; | ||
| } | ||
|
|
||
| if (this.verifiedItemCountOptions.includes(this.itemCount)) { | ||
| return; | ||
| } | ||
|
|
||
| const displayOptions = | ||
| this.verifiedItemCountOptions.length > 5 | ||
| ? `${this.verifiedItemCountOptions.slice(0, 5).join(', ')} ...` | ||
| : this.verifiedItemCountOptions.join(', '); | ||
|
|
||
| console.warn( | ||
| `[ix-pagination] Configuration mismatch: itemCount value "${ | ||
| this.itemCount | ||
| }" is not present in ${this.itemCountOptionsPropName} [${displayOptions}]. ` + | ||
| `This will result in an invalid dropdown state. Please either add ${this.itemCount} to ${this.itemCountOptionsPropName} or set itemCount to one of the available options.` | ||
| ); | ||
| } | ||
|
|
||
| private getValidItemCountOptions(): number[] { | ||
| return Array.from( | ||
| new Set( | ||
| (this.itemCountOptions?.length | ||
| ? this.itemCountOptions | ||
| : this.defaultItemCountOptions | ||
| ) | ||
| .filter((option) => option > 0 && Number.isInteger(option)) | ||
| .sort((current, next) => current - next) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private selectPage(index: number) { | ||
| const oldIndex = this.selectedPage; | ||
|
|
||
|
|
@@ -312,11 +407,14 @@ export class Pagination { | |
| this.itemCountChanged.emit(count); | ||
| }} | ||
| > | ||
| <ix-select-item label="10" value="10"></ix-select-item> | ||
| <ix-select-item label="15" value="15"></ix-select-item> | ||
| <ix-select-item label="20" value="20"></ix-select-item> | ||
| <ix-select-item label="40" value="40"></ix-select-item> | ||
| <ix-select-item label="100" value="100"></ix-select-item> | ||
| {( | ||
| this.verifiedItemCountOptions || this.getValidItemCountOptions() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
| ).map((option) => ( | ||
| <ix-select-item | ||
| label={`${option}`} | ||
| value={`${option}`} | ||
| ></ix-select-item> | ||
| ))} | ||
| </ix-select> | ||
| </span> | ||
| )} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be hard coded in the console.warnings