Skip to content

Commit

Permalink
fix/InputNumber-with-maxlength-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Nguyen committed Sep 24, 2023
1 parent 396297a commit e9d2e85
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 @@ -917,6 +917,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 @@ -926,6 +932,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 e9d2e85

Please sign in to comment.