Skip to content

Commit 63e2be6

Browse files
author
Bernardo-MG
committed
Reworked waiting cue on login form
1 parent 1354afe commit 63e2be6

4 files changed

Lines changed: 43 additions & 14 deletions

File tree

src/app/core/login/components/login-form/login-form.component.html

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
<!-- Username -->
44
<div class="mb-2">
55
<input placeholder="Username" type="text" id="usernameInput" formControlName="username" class="form-control"
6-
[ngClass]="{ 'is-invalid': isFieldInvalid('username') }" />
6+
[ngClass]="{ 'is-invalid': isInvalid('username') }" [disabled]="!isInputEnabled()"
7+
[attr.aria-disabled]="!isInputEnabled()" />
78
</div>
89
<!-- Password -->
910
<div class="mb-2">
1011
<input placeholder="Password" type="password" id="passwordInput" formControlName="password" class="form-control"
11-
[ngClass]="{ 'is-invalid': isFieldInvalid('password') }" />
12+
[ngClass]="{ 'is-invalid': isInvalid('password') }" [disabled]="!isInputEnabled()"
13+
[attr.aria-disabled]="!isInputEnabled()" />
1214
</div>
1315
<!-- Remember me check -->
1416
<div class="row mb-2">
@@ -19,11 +21,19 @@
1921
</div>
2022
</div>
2123
</div>
22-
<!-- Login button -->
2324
<div class="d-grid gap-2">
24-
<button [disabled]="!isLoginEnabled()" class="btn btn-primary" [attr.aria-disabled]="!isLoginEnabled()"
25-
aria-label="Login">
26-
<icon-waiting *ngIf="loading"></icon-waiting> Login
27-
</button>
25+
<div *ngIf="waiting; then waiting_cue else login_button"></div>
26+
<ng-template #waiting_cue>
27+
<!-- Waiting cue -->
28+
<button type="submit" class="btn btn-primary" aria-label="Login"
29+
disabled><icon-waiting></icon-waiting></button>
30+
</ng-template>
31+
<ng-template #login_button>
32+
<!-- Login button -->
33+
<button type="submit" [disabled]="!isLoginEnabled()" class="btn btn-primary"
34+
[attr.aria-disabled]="!isLoginEnabled()" aria-label="Login">
35+
<icon-waiting *ngIf="waiting"></icon-waiting> Login
36+
</button>
37+
</ng-template>
2838
</div>
2939
</form>

src/app/core/login/components/login-form/login-form.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ describe('LoginFormComponent', () => {
7070
it('should disable the login button when the form is valid but it is loading', () => {
7171
component.form.controls['username'].setValue('username');
7272
component.form.controls['password'].setValue('password');
73-
component.loading = true;
73+
component.waiting = true;
7474
fixture.detectChanges();
7575

7676
const button = fixture.nativeElement.querySelector('form button');
7777
expect(button.disabled).toEqual(true);
7878
});
7979

8080
it('should disable the login button when the form is loading', () => {
81-
component.loading = true;
81+
component.waiting = true;
8282
fixture.detectChanges();
8383

8484
const button = fixture.nativeElement.querySelector('form button');

src/app/core/login/components/login-form/login-form.component.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ import { LoginFormUser } from '../../models/login-form-user';
1212
export class LoginFormComponent {
1313

1414
/**
15-
* Loading flag. Shows the loading visual cue and disables the form.
15+
* Waiting flag. Shows the waiting visual cue and disables the form.
1616
*/
17-
@Input() public loading = false;
17+
@Input() public waiting = false;
1818

1919
/**
2020
* Failed login flag. Shows the failure warning.
2121
*/
2222
@Input() public failed = false;
2323

24+
/**
25+
* Disabled form flag.
26+
*/
27+
@Input() public disabled = false;
28+
2429
/**
2530
* Login event. Sent when the user accepts the data in the form.
2631
*/
@@ -76,7 +81,7 @@ export class LoginFormComponent {
7681
* @param field field to check
7782
* @returns true if the form is invalid, false otherwise
7883
*/
79-
public isFieldInvalid(field: string): boolean {
84+
public isInvalid(field: string): boolean {
8085
let invalid: boolean;
8186

8287
if (this.form.invalid) {
@@ -93,8 +98,22 @@ export class LoginFormComponent {
9398
return invalid;
9499
}
95100

101+
/**
102+
* Returns true if the login button is enabled.
103+
*
104+
* @returns true if the login button is enabled, false otherwise
105+
*/
96106
public isLoginEnabled(): boolean {
97-
return ((this.form.valid) && (!this.loading));
107+
return ((this.form.valid) && (!this.waiting) && (!this.disabled));
108+
}
109+
110+
/**
111+
* Returns true if the inputs are enabled.
112+
*
113+
* @returns true if the inputs are enabled, false otherwise
114+
*/
115+
public isInputEnabled(): boolean {
116+
return ((!this.waiting) && (!this.disabled));
98117
}
99118

100119
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="card">
22
<div class="card-body">
3-
<authentication-login-form [loading]="loading" [failed]="failed" (login)="onLogin($event)"
3+
<authentication-login-form [waiting]="loading" [failed]="failed" (login)="onLogin($event)"
44
(rememberMe)="onRememberMe($event)"></authentication-login-form>
55
</div>
66
</div>

0 commit comments

Comments
 (0)