Search code examples
angularnativescriptangular2-nativescript

Angular2/Nativescript: Submit a form when return key is pressed on soft keyboard


I have a Angular2/Nativescript login component with username and password TextFields... When the password textField is being edited I set the returnKeyType to "done" and expect the login() function to be called when Done is pressed on the soft keyboard. At the moment when done is pressed the keyboard gets dismissed but the login function doesn't get called so I still have to tap the login button after the keyboard is dismissed for the form to be submitted. Is there any way to submit the form when the return key is pressed on a specific TextField in Nativescript? If so How can I go about implementing it? I tried the returnPress event but nothing happens...

my code:

<ActionBar title="Login"></ActionBar>
<StackLayout class="page">
    <GridLayout columns="*, auto" rows="auto">
        <ActivityIndicator class="m-l-10 m-t-10 activity-indicator" [busy]="busy" [visibility]="busy ? 'visible' : 'collapse'" horizontalAlignment="left"></ActivityIndicator>
        <Button row="0" col="1" id="setIPBtn" class=" m-t-20 pull-right font-awesome" text="&#xf0ad; Settings" (tap)="setIP()"></Button>
    </GridLayout>

    <Label class="m-x-auto m-t-20 title h1 text-primary p-x-10" text="Log In" backgroundColor="blue"></Label>
    <StackLayout class="form">
        <StackLayout class="input-field">
            <Label class="body label text-left" text="Enter Username"></Label>
            <TextField class="input input-border" hint="Username" [(ngModel)]="username" autocorrect="false" autocapitalizationType="none" returnKeyType="next"></TextField>
        </StackLayout>
        <StackLayout class="input-field">
            <Label class="body label text-left" text="Enter Username"></Label>
            <TextField class="input input-border" secure="true" hint="Password" [(ngModel)]="password" autocorrect="false" autocapitalizationType="none" returnKeyType="done" returnPress="login()"></TextField>            
        </StackLayout>
        <Button class="btn btn-submit font-awesome bg-primary" [text]="isLoggingIn ? 'Logging in...' : '&#xf090; Login'" (tap)="login()" [isEnabled]="username !== '' && username !== null && password !== '' && password !== null && !isLoggingIn"></Button>
    </StackLayout>      



</StackLayout>

Solution

  • Try this:

    (returnPress)="login()"