Skip to content

Commit f9660d5

Browse files
committed
Added some features for resolving the issue #27
1 parent 5b3d573 commit f9660d5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

angular-code-input/src/lib/code-input.component.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, Aft
118118
* Methods
119119
*/
120120

121+
reset(isChangesEmitting = false): void {
122+
// resetting the code to its initial value or to an empty value
123+
this.onInputCodeChanges();
124+
125+
if (this.state.isInitialFocusFieldEnabled) {
126+
// tslint:disable-next-line:no-non-null-assertion
127+
this.focusOnField(this.initialFocusField!);
128+
}
129+
130+
if (isChangesEmitting) {
131+
this.emitChanges();
132+
}
133+
}
134+
135+
focusOnField(index: number): void {
136+
if (index >= this._codeLength) {
137+
throw new Error('The index of the focusing input box should be less than the codeLength.');
138+
}
139+
140+
this.inputs[index].focus();
141+
}
142+
121143
onClick(e: any): void {
122144
// handle click events only if the the prop is enabled
123145
if (!this.isFocusingOnLastByClickIfFilled) {
@@ -283,7 +305,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, Aft
283305
}
284306

285307
// tslint:disable-next-line:no-non-null-assertion
286-
this.inputs[this.initialFocusField!].focus();
308+
this.focusOnField(this.initialFocusField!);
287309
// tslint:disable-next-line:no-non-null-assertion
288310
this.state.isFocusingAfterAppearingCompleted = document.activeElement === this.inputs[this.initialFocusField!];
289311
}

0 commit comments

Comments
 (0)