From 7f044309a70bef178368b6efeb762ba217a50328 Mon Sep 17 00:00:00 2001 From: Dale Nguyen Date: Sat, 23 Sep 2023 23:27:58 +0700 Subject: [PATCH] fix(inputNumber): improve maxlength behaviour --- src/app/components/inputnumber/inputnumber.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/app/components/inputnumber/inputnumber.ts b/src/app/components/inputnumber/inputnumber.ts index 015b0c44ed4..fd8a5313948 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,10 @@ 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();