@@ -30,6 +30,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
30
30
@Input ( ) readonly isNonDigitsCode = false ;
31
31
@Input ( ) readonly isCodeHidden = false ;
32
32
@Input ( ) readonly isPrevFocusableAfterClearing = true ;
33
+ @Input ( ) readonly isFocusingOnLastByClickIfFilled = false ;
33
34
@Input ( ) readonly inputType = 'tel' ;
34
35
@Input ( ) readonly code ?: string | number ;
35
36
@@ -72,6 +73,29 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
72
73
* Methods
73
74
*/
74
75
76
+ onClick ( e : any ) : void {
77
+ // handle click events only if the the prop is enabled
78
+ if ( ! this . isFocusingOnLastByClickIfFilled ) {
79
+ return ;
80
+ }
81
+
82
+ const target = e . target ;
83
+ const last = this . inputs [ this . codeLength - 1 ] ;
84
+ // already focused
85
+ if ( target === last ) {
86
+ return ;
87
+ }
88
+
89
+ // check filling
90
+ const isFilled = this . getCurrentFilledCode ( ) . length >= this . codeLength ;
91
+ if ( ! isFilled ) {
92
+ return ;
93
+ }
94
+
95
+ // focusing on the last input if is filled
96
+ setTimeout ( ( ) => last . focus ( ) ) ;
97
+ }
98
+
75
99
onInput ( e : any , i : number ) : void {
76
100
const next = i + 1 ;
77
101
const target = e . target ;
@@ -205,6 +229,16 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
205
229
}
206
230
207
231
private emitCode ( ) : void {
232
+ const code = this . getCurrentFilledCode ( ) ;
233
+
234
+ this . codeChanged . emit ( code ) ;
235
+
236
+ if ( code . length >= this . codeLength ) {
237
+ this . codeCompleted . emit ( code ) ;
238
+ }
239
+ }
240
+
241
+ private getCurrentFilledCode ( ) : string {
208
242
let code = '' ;
209
243
210
244
for ( const input of this . inputs ) {
@@ -213,11 +247,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
213
247
}
214
248
}
215
249
216
- this . codeChanged . emit ( code ) ;
217
-
218
- if ( code . length >= this . codeLength ) {
219
- this . codeCompleted . emit ( code ) ;
220
- }
250
+ return code ;
221
251
}
222
252
223
253
private isBackspaceKey ( e : any ) : Promise < boolean > {
0 commit comments