From e9d2e855392f2b147cecad350a4121f5b054dfb5 Mon Sep 17 00:00:00 2001 From: Dale Nguyen Date: Sun, 24 Sep 2023 16:37:59 +0000 Subject: [PATCH] fix/InputNumber-with-maxlength-attribute --- src/app/components/inputnumber/inputnumber.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/components/inputnumber/inputnumber.ts b/src/app/components/inputnumber/inputnumber.ts index 015b0c44ed4..f7655748536 100644 --- a/src/app/components/inputnumber/inputnumber.ts +++ b/src/app/components/inputnumber/inputnumber.ts @@ -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; } @@ -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();