diff --git a/angular/src/app/directives/focus-next-input.directive.ts b/angular/src/app/directives/focus-next-input.directive.ts index 3371901..0d9b6b7 100644 --- a/angular/src/app/directives/focus-next-input.directive.ts +++ b/angular/src/app/directives/focus-next-input.directive.ts @@ -9,15 +9,19 @@ export class FocusNextInputDirective { constructor(private renderer: Renderer2) { } ngOnInit() { - this.eventEmitter.subscribe(elementId => { - try { - this.renderer.selectRootElement(elementId).focus(); - this.renderer.selectRootElement(elementId).click(); - } catch (ex) { - (document.activeElement as HTMLElement).blur(); - // If the element doesn't exist or if the element disappears when this called then no need to do anything - } - }); + // TODO: don't need to listen all events + this.eventEmitter.subscribe(elementId => { + try { + const element = this.renderer.selectRootElement(elementId) + setTimeout(() => { + element.focus(); + element.click(); + }, 0); + } catch (ex) { + (document.activeElement as HTMLElement).blur(); + // If the element doesn't exist or if the element disappears when this called then no need to do anything + } + }); } } diff --git a/angular/src/app/presentation-options/default-option/pages/login/login.component.ts b/angular/src/app/presentation-options/default-option/pages/login/login.component.ts index 0c7bf2c..9957811 100644 --- a/angular/src/app/presentation-options/default-option/pages/login/login.component.ts +++ b/angular/src/app/presentation-options/default-option/pages/login/login.component.ts @@ -1,5 +1,3 @@ -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck import { AfterViewInit, Component, @@ -43,47 +41,17 @@ export class LoginComponent implements OnInit, AfterViewInit { private jsonRpcService: JsonrpcService ) {} - ngOnInit(): void { - if ('OTPCredential' in window) { - console.log('have otp credential'); - window.addEventListener('DOMContentLoaded', e => { - const form = document.querySelector('.code-form'); - const ac = new AbortController(); - if (form) { - form.addEventListener('submit', e => { - ac.abort(); - }); - } - navigator.credentials.get({ - otp: { transport:['sms'] }, - signal: ac.signal - }).then(otp => { - console.log(otp) - const input1 = document.getById("field"); - const input2 = document.getById("field1"); - const input3 = document.getById("field2"); - const input4 = document.getById("field3"); - input1.value = otp.code[0]; - input2.value = otp.code[1]; - input3.value = otp.code[2]; - input4.value = otp.code[3]; - if (form) form.submit(); - }).catch(err => { - console.log(err); - }); - }); - } - } + ngOnInit(): void {} ngAfterViewInit() { setTimeout(() => { this.inputFocusEmitter.emit(`#${this.inputIds[0]}`); }, 1000); } - + public inputFocusEmitter = new EventEmitter(); - @HostListener('window:keyup', ['$event']) + @HostListener('window:keydown', ['$event']) HandlKeyEvents(event: any) { if (!event.target.classList.contains('field')) return; const key = event.key.toLocaleLowerCase(); @@ -100,12 +68,11 @@ export class LoginComponent implements OnInit, AfterViewInit { break; default: - elementId = event.target.id; + elementId = event.target.id; const index = this.inputIds.indexOf(elementId); const nextInputIndex = index + 1; - if (event.target.value.length > 1) { - event.target.value = event.target.value.slice(-1); - } + event.target.value = event.key; + if (nextInputIndex > 0 && nextInputIndex <= this.inputIds.length) { this.inputFocusEmitter.emit(`#${this.inputIds[nextInputIndex]}`); }