packages/ui/src/lib/utility/login-form/login-form.component.ts
Login Form Component with validation. Fires success event with credentials on submit.
selector | ec-login-form |
templateUrl | ./login-form.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
constructor(fb: FormBuilder, symbol: SymbolService)
|
|||||||||
Injects the FormBuilder
Parameters :
|
buttonClasses | |
Default value : ''
|
|
Additional Button classes |
buttonLabel | |
Default value : this.symbol.resolve('login.button.label')
|
|
The Label of the submit button. Defaults to Login |
emailLabel | |
Default value : this.symbol.resolve('login.email.label')
|
|
The Label of the Mail field |
emailPlaceholder | |
Default value : this.symbol.resolve('login.email.placeholder')
|
|
The Placeholder of the mail Field |
loader | |
Type : LoaderComponent
|
|
The loader that should be shown during login |
passwordLabel | |
Default value : this.symbol.resolve('login.password.label')
|
|
The Label of the password field. |
passwordPlaceholder | |
Default value : this.symbol.resolve('login.password.placeholder')
|
|
The Placeholder of the password field |
showLabels | |
Default value : true
|
|
If true, email and password wont have labels |
error | |
Type : EventEmitter<any>
|
|
Event that emits when calling showError. |
success | |
Type : EventEmitter<any>
|
|
Event that emits on succesful submit of the form, passing the login credentials. |
login | ||||
login(value)
|
||||
Method that is meant to be overwritten by a subclass to communicate with an API.
Parameters :
Returns :
any
|
ngOnInit |
ngOnInit()
|
Initializes the form
Returns :
void
|
onSubmit |
onSubmit()
|
Is called when the form has been successfully submitted. Calls login and resets the form after.
Returns :
void
|
showError | ||||
showError(err)
|
||||
Shows the given error in the form. Clears the password field and emits the error event.
Parameters :
Returns :
any
|
Public errorMessage |
Type : string
|
Contains possible error messages. |
Public form |
Type : FormGroup
|
The login's form group. |
notifications |
Type : Notification[]
|
Recent error Notifications |
Protected submitted |
Type : boolean
|
Flips true when submitted. |
Public symbol |
Type : SymbolService
|
The Login Form UI component is meant to be extended by a component that communicates with an API.
<ec-login-form (success)="yourApi.login($event)"></ec-login>
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { throwError } from 'rxjs';
import { LoaderComponent } from '../../loader/loader.component';
import { WithLoader } from '../../loader/with-loader.interface';
import { SymbolService } from '../../symbol/symbol.service';
import { FieldValidators } from '../validators/field-validators';
/** Login Form Component with validation. Fires success event with credentials on submit.
* <example-url>https://components.entrecode.de/ui/login?e=1</example-url>
*/
@Component({
selector: 'ec-login-form',
templateUrl: './login-form.component.html',
})
export class LoginFormComponent implements OnInit, WithLoader {
/** The login's form group.*/
public form: FormGroup;
/** Flips true when submitted. */
protected submitted: boolean;
/** Contains possible error messages. */
public errorMessage: string;
/** Event that emits on succesful submit of the form, passing the login credentials. */
@Output() success: EventEmitter<any> = new EventEmitter();
/** Event that emits when calling showError. */
@Output() error: EventEmitter<any> = new EventEmitter();
/** The loader that should be shown during login */
@Input() loader: LoaderComponent;
/** If true, email and password wont have labels */
@Input() showLabels = true;
/** The Label of the Mail field */
@Input() emailLabel = this.symbol.resolve('login.email.label');
/** The Placeholder of the mail Field */
@Input() emailPlaceholder = this.symbol.resolve('login.email.placeholder');
/** The Label of the password field. */
@Input() passwordLabel = this.symbol.resolve('login.password.label');
/** The Placeholder of the password field */
@Input() passwordPlaceholder = this.symbol.resolve('login.password.placeholder');
/** The Label of the submit button. Defaults to Login */
@Input() buttonLabel = this.symbol.resolve('login.button.label');
/** Additional Button classes */
@Input() buttonClasses = '';
/** Recent error Notifications */
notifications: Notification[];
/** Injects the FormBuilder*/
constructor(protected fb: FormBuilder, public symbol: SymbolService) {}
/** Initializes the form */
ngOnInit() {
this.form = this.fb.group({
email: ['', [Validators.required, FieldValidators.email]], // emailAvailable?
password: ['', [Validators.required]],
});
}
/** Shows the given error in the form. Clears the password field and emits the error event. */
showError(err): any {
this.errorMessage = err.message;
this.form.get('password').setValue('');
this.error.emit(err);
throwError(err);
}
/** Method that is meant to be overwritten by a subclass to communicate with an API. */
login(value) {
// meant to be overridden
return Promise.resolve(value);
}
/** Is called when the form has been successfully submitted. Calls login and resets the form after. */
onSubmit() {
this.submitted = true;
delete this.errorMessage;
if (!this.form.valid) {
return;
}
const login = this.login(this.form.value).then((res) => {
this.form.reset();
this.success.emit(res);
});
if (this.loader) {
this.loader.wait(login);
}
}
}
<form novalidate [formGroup]="form" (ngSubmit)="onSubmit()" class="ec-auth ec-auth_login">
<p class="error" *ngIf="errorMessage">
{{ errorMessage }}
</p>
<div class="field-group ec-auth__email">
<label *ngIf="showLabels" for="ecAuthEmail" class="field-group__label">{{ emailLabel }}</label>
<input type="email" id="ecAuthEmail" formControlName="email" class="input" [placeholder]="emailPlaceholder" />
<ec-input-errors [control]="form.get('email')"></ec-input-errors>
</div>
<div class="field-group ec-auth__password">
<label *ngIf="showLabels" for="ecAuthPassword" class="field-group__label">Passwort</label>
<input type="password" id="ecAuthPassword" formControlName="password" class="input" [placeholder]="passwordPlaceholder" />
<ec-input-errors [control]="form.get('password')"></ec-input-errors>
</div>
<button
type="submit"
[disabled]="form.invalid"
[class.is-disabled]="form.invalid"
class="btn ec-auth__btn"
[ngClass]="buttonClasses"
>
{{ buttonLabel }}
</button>
</form>