diff --git a/angular-code-input/src/lib/code-input.component.config.ts b/angular-code-input/src/lib/code-input.component.config.ts index 73f6bcd..b168888 100644 --- a/angular-code-input/src/lib/code-input.component.config.ts +++ b/angular-code-input/src/lib/code-input.component.config.ts @@ -14,6 +14,7 @@ export interface CodeInputComponentConfig { code?: string | number; disabled?: boolean; autocapitalize?: string; + isClearPreviousIfEmpty?: boolean; } export const defaultComponentConfig: CodeInputComponentConfig = { @@ -27,5 +28,6 @@ export const defaultComponentConfig: CodeInputComponentConfig = { isFocusingOnLastByClickIfFilled: false, code: undefined, disabled: false, - autocapitalize: undefined + autocapitalize: undefined, + isClearPreviousIfEmpty: false }; diff --git a/angular-code-input/src/lib/code-input.component.ts b/angular-code-input/src/lib/code-input.component.ts index 96b9c61..b22d6d4 100644 --- a/angular-code-input/src/lib/code-input.component.ts +++ b/angular-code-input/src/lib/code-input.component.ts @@ -50,6 +50,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, OnD @Input() code ?: string | number; @Input() disabled !: boolean; @Input() autocapitalize ?: string; + @Input() isClearPreviousIfEmpty!: boolean; @Output() readonly codeChanged = new EventEmitter(); @Output() readonly codeCompleted = new EventEmitter(); @@ -269,7 +270,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, OnD this.emitChanges(); } - // preventing to focusing on the previous field if it does not exist or the delete key has been pressed + // preventing to focusing/clearing on the previous field if it does not exist or the delete key has been pressed if (prev < 0 || isDeleteKey) { return; } @@ -277,6 +278,11 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, OnD if (isTargetEmpty || this.isPrevFocusableAfterClearing) { this.inputs[prev].focus(); } + + + if (isTargetEmpty && this.isClearPreviousIfEmpty) { + this.setInputValue(this.inputs[prev], null); + } } private onInputCodeChanges(): void {