'Move focus to next control on key Enter

I have found some project on Angular 1.x where user can move focus to next control by pressing Enter key.

'use strict';
app.directive('setTabEnter', function () {

    var includeTags = ['INPUT', 'SELECT'];

    function link(scope, element, attrs) {
        element.on('keydown', function (e) {
            if (e.keyCode == 13 && includeTags.indexOf(e.target.tagName) != -1) {
                var focusable = element[0].querySelectorAll('input,select,button,textarea');
                var currentIndex = Array.prototype.indexOf.call(focusable, e.target)
                var nextIndex = currentIndex == focusable.length - 1 ? 0 : currentIndex + 1;

                if (nextIndex >= 0 && nextIndex < focusable.length)
                    focusable[nextIndex].focus();

                return false;
            }
        });
    }

    return {
        restrict: 'A',
        link: link
    };
});

But this does not work for Angular 2. How can I set focus on next control on Enter keypress in Angular 2?



Solution 1:[1]

import { Directive, ElementRef, HostListener, Input } from'@angular/core';
@Directive({
    selector: '[onReturn]'
})
export class OnReturnDirective {
    private el: ElementRef;
    @Input() onReturn: string;
    constructor(private _el: ElementRef) {
        this.el = this._el;
    }
    @HostListener('keydown', ['$event']) onKeyDown(e) {
        if ((e.which == 13 || e.keyCode == 13)) {
            e.preventDefault();
            if (e.srcElement.nextElementSibling) {
                e.srcElement.nextElementSibling.focus();
            }
            else{
                console.log('close keyboard');
            }
            return;
        }

    }

}

Hope it will help you !

Solution 2:[2]

import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
@Directive({
    selector: '[onReturn]'
})
export class OnReturnDirective {    
    private el: ElementRef;   
    @Input() onReturn: string;
    constructor(private _el: ElementRef,public renderer: Renderer) {
        this.el = this._el;
    }  
    @HostListener('keydown', ['$event']) onKeyDown(e:any) {
        if ((e.which == 13 || e.keyCode == 13)) {
            e.preventDefault();
            let control:any;
            control = e.srcElement.nextElementSibling;
            while (true){                
                if (control) {
                  if ((!control.hidden) && 
                     (control.nodeName == 'INPUT' || 
                      control.nodeName == 'SELECT' || 
                      control.nodeName == 'BUTTON' || 
                      control.nodeName == 'TEXTAREA'))
                     {
                            control.focus();
                            return;
                        }else{
                            control = control.nextElementSibling;
                        }                         
                }
                else {
                    console.log('close keyboard');
                    return;
                }            
            }
        }
    } 
}

Solution 3:[3]

import { Directive, ElementRef, HostListener, Input } from "@angular/core";

@Directive({
    selector: "[onReturn]"
})
export class OnReturnDirective {
    constructor() {}
    @HostListener("keydown", ["$event"]) onKeyDown(e: {
        which: number;
        keyCode: number;
        shiftKey: boolean;
        preventDefault: () => void;
        srcElement: {
            nextElementSibling: { focus: () => void };
            previousElementSibling: { focus: () => void };
        };
    }) {
        if (e.which == 13 || e.keyCode == 13) {
            e.preventDefault();
            let control: any;
            if (e.shiftKey == true) {
                control = e.srcElement.previousElementSibling;
                ControlElement(control, true);
            } else {
                control = e.srcElement.nextElementSibling;
                ControlElement(control, false);
            }
        }

        function ControlElement(control: any, IsPrevious: boolean) {
            while (true) {
                if (control) {
                    if (
                        !control.hidden &&
                        (control.nodeName == "INPUT" ||
                            control.nodeName == "SELECT" ||
                            control.nodeName == "BUTTON" ||
                            control.nodeName == "TEXTAREA")
                    ) {
                        control.focus();
                        return;
                    } else {
                        if (IsPrevious == true) {
                            control = control.previousElementSibling;
                        } else {
                            control = control.nextElementSibling;
                        }
                    }
                } else {
                    console.log("close keyboard");
                    return;
                }
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ani5ha
Solution 2
Solution 3 Jigar Patel