Skip to content
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

feat(pagination): Add input validation to restrict values between 1 a… #945

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/components/pagination/bl-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ export default class BlPagination extends LitElement {

this._changePage(newPage);
}
_validateInput(event: KeyboardEvent) {
const allowedKeys = ["Backspace", "ArrowLeft", "ArrowRight", "Delete", "Tab", "Enter"];
const isNumberKey = !isNaN(Number(event.key));
const isAllowedKey = allowedKeys.includes(event.key);

// Prevent non-numeric and non-allowed keys
if (!isNumberKey && !isAllowedKey) {
event.preventDefault();
}

// Prevent input that results in a number less than 1 or greater than the last page
const target = event.target as HTMLInputElement | null;
const newValue = target ? target.value + event.key : event.key;

if ((isNumberKey && parseInt(newValue) < 1) || parseInt(newValue) > this._getLastPage()) {
event.preventDefault();
}
}

private _selectHandler(event: CustomEvent) {
this.itemsPerPage = +event?.detail?.value || 100;
Expand Down Expand Up @@ -261,7 +279,11 @@ export default class BlPagination extends LitElement {
const jumperEl = this.hasJumper
? html` <div class="jumper">
<label>${jumperText}</label>
<bl-input .value="${this.currentPage}" @bl-change="${this._inputHandler}"></bl-input>
<bl-input
placeholder="Page"
@bl-change="${this._inputHandler}"
@keydown="${this._validateInput}"
></bl-input>
</div>`
: null;

Expand Down
Loading