From 2190cc06e41d2c530b346c9319d2f086d5a2239a Mon Sep 17 00:00:00 2001 From: Hlars Date: Mon, 28 Apr 2025 18:06:49 +0200 Subject: [PATCH] add ability for copy and pasting for currency input --- .../currency-input.component.html | 6 +++--- .../currency-input.component.ts | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/app/currency-input/currency-input.component.html b/src/app/currency-input/currency-input.component.html index f5d50b2..c15afe8 100644 --- a/src/app/currency-input/currency-input.component.html +++ b/src/app/currency-input/currency-input.component.html @@ -1,6 +1,6 @@
+ [attr.aria-describedby]="userAriaDescribedBy" [attr.required]="required" (keyup)="onInputKeyUp($event)" + (input)="onUserInput($event)" (keydown)="onInputKeyDown($event)" (keypress)="onInputKeyPress($event)" + (paste)="onPaste($event)" (focus)="onFocusIn($event)" (blur)="onFocusOut($event)" />
\ No newline at end of file diff --git a/src/app/currency-input/currency-input.component.ts b/src/app/currency-input/currency-input.component.ts index d13000d..28a1888 100644 --- a/src/app/currency-input/currency-input.component.ts +++ b/src/app/currency-input/currency-input.component.ts @@ -160,6 +160,7 @@ export class CurrencyInputComponent implements MatFormFieldControl, Cont @ViewChild('input') input!: ElementRef; isSpecialChar: boolean | null | undefined = undefined; + isMaybePasting: boolean = false; lastValue: string | null | undefined = undefined; min: number = 0; groupChar: string = ''; @@ -269,8 +270,6 @@ export class CurrencyInputComponent implements MatFormFieldControl, Cont return { decimalCharIndex, minusCharIndex }; } - - onUserInput(event: Event) { if (this.disabled) { return; @@ -288,11 +287,16 @@ export class CurrencyInputComponent implements MatFormFieldControl, Cont } this.lastValue = (event.target as HTMLInputElement).value; - if ((event as KeyboardEvent).shiftKey || (event as KeyboardEvent).altKey || event.metaKey || event.ctrlKey) { + if ((event as KeyboardEvent).shiftKey || (event as KeyboardEvent).altKey) { this.isSpecialChar = true; return; } + if (event.metaKey || event.ctrlKey) { + this.isMaybePasting = true; + return; + } + let selectionStart = (event.target as HTMLInputElement).selectionStart as number; let selectionEnd = (event.target as HTMLInputElement).selectionEnd as number; let inputValue = (event.target as HTMLInputElement).value as string; @@ -446,6 +450,11 @@ export class CurrencyInputComponent implements MatFormFieldControl, Cont // this.onKeyDown.emit(event); } + onInputKeyUp(event: KeyboardEvent): void { + if (event.metaKey || event.ctrlKey) { + this.isMaybePasting = false; + } + } onInputKeyPress(event: KeyboardEvent) { if (this.disabled) { @@ -457,6 +466,11 @@ export class CurrencyInputComponent implements MatFormFieldControl, Cont let isDecimalSign = this.isDecimalSign(char); const isMinusSign = this.isMinusSign(char); + // allow pasting or copying in the input field + if (this.isMaybePasting && (event.key === 'v' || event.key === 'c')) { + return; + } + if (code != 13) { event.preventDefault(); }