Skip to content

Commit

Permalink
Merge pull request #13720 from dalenguyen/fix/InputNumber-with-maxlen…
Browse files Browse the repository at this point in the history
…gth-attribute

fix(inputNumber): improve maxlength behaviour
  • Loading branch information
cetincakiroglu authored Mar 22, 2024
2 parents e8220e1 + e9d2e85 commit 0bf7aaf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/app/components/inputnumber/inputnumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,12 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control

const newValue = this.parseValue(this.input.nativeElement.value + char);
const newValueStr = newValue != null ? newValue.toString() : '';

if (this.maxlength && this.getSelectedText()?.length == this.maxlength) {
this.insert(event, char, { isDecimalSign, isMinusSign });
return;
}

if (this.maxlength && newValueStr.length > this.maxlength) {
return;
}
Expand All @@ -987,6 +993,15 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
}
}

private getSelectedText() {
return (
window
?.getSelection()
?.toString()
.replaceAll(/[^0-9']/g, '') || ''
);
}

onPaste(event: ClipboardEvent) {
if (!this.disabled && !this.readonly) {
event.preventDefault();
Expand Down

0 comments on commit 0bf7aaf

Please sign in to comment.