src/app/shared/components/name-input/name-input.component.ts
Component for inputting the researcher's name
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-name-input |
styleUrls | ./name-input.component.scss |
templateUrl | ./name-input.component.html |
Properties |
Methods |
Inputs |
Outputs |
HostBindings |
Accessors |
constructor(ga: GoogleAnalyticsService)
|
||||||||
Creates an instance of name input component.
Parameters :
|
name | |
Type : UserName
|
|
Current user name |
nameChange | |
Type : EventEmitter
|
|
Emits a UserName object |
class |
Type : "ccf-name-input"
|
Default value : 'ccf-name-input'
|
HTML class name |
updateName | ||||||||||||
updateName(input: Event, key: string)
|
||||||||||||
Updates username with a new entry and emits the UserName object
Parameters :
Returns :
void
|
Readonly clsName |
Type : string
|
Default value : 'ccf-name-input'
|
Decorators :
@HostBinding('class')
|
HTML class name |
firstNameValidator |
Default value : new UntypedFormControl('', [Validators.required])
|
Used to validate the first name input field. |
lastNameValidator |
Default value : new UntypedFormControl('', [Validators.required])
|
Used to validate the last name input field. |
middleNameValidator |
Default value : new UntypedFormControl('', [Validators.required])
|
Used to validate the middle name input field. |
name | ||||||
getname()
|
||||||
Current user name
Returns :
UserName
|
||||||
setname(value: UserName)
|
||||||
Parameters :
Returns :
void
|
import { ChangeDetectionStrategy, Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { UntypedFormControl, Validators } from '@angular/forms';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
/**
* User name data
*/
export interface UserName {
/**
* User's first name
*/
firstName: string;
/**
* User's middle name
*/
middleName: string;
/**
* User's last name
*/
lastName: string;
}
/**
* Component for inputting the researcher's name
*/
@Component({
selector: 'ccf-name-input',
templateUrl: './name-input.component.html',
styleUrls: ['./name-input.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NameInputComponent {
/**
* HTML class name
*/
@HostBinding('class') readonly clsName = 'ccf-name-input';
/**
* Used to validate the first name input field.
*/
firstNameValidator = new UntypedFormControl('', [Validators.required]);
/**
* Used to validate the last name input field.
*/
lastNameValidator = new UntypedFormControl('', [Validators.required]);
/**
* Used to validate the middle name input field.
*/
middleNameValidator = new UntypedFormControl('', [Validators.required]);
/**
* Current user name
*/
@Input()
get name(): UserName {
return this._name;
}
set name(value: UserName) {
this._name = value;
this.firstNameValidator.setValue(value?.firstName || '');
this.lastNameValidator.setValue(value?.lastName || '');
this.middleNameValidator.setValue(value?.middleName || '');
}
private _name: UserName = {
firstName: '',
middleName: '',
lastName: '',
};
/**
* Emits a UserName object
*/
@Output() readonly nameChange = new EventEmitter<UserName>();
/**
* Creates an instance of name input component.
*
* @param ga Analytics service
*/
constructor(private readonly ga: GoogleAnalyticsService) {}
/**
* Updates username with a new entry and emits the UserName object
*
* @param input InputEvent from the input element which contains the new value
* @param key firstName or lastName
*/
updateName(input: Event, key: string): void {
const inputTarget = input.target as HTMLInputElement;
this.name = { ...this.name, [key]: inputTarget.value };
this.ga.event('name_updated', 'name_input', key);
this.nameChange.emit(this.name);
}
}
<mat-form-field class="field" subscriptSizing="dynamic">
<mat-label class="text form-input-label">First Name </mat-label>
<input
matInput
class="input"
[value]="firstNameValidator.value"
(input)="updateName($event, 'firstName')"
matTooltip="Add your first name."
required
title=""
/>
</mat-form-field>
<mat-form-field class="field" subscriptSizing="dynamic">
<mat-label class="text form-input-label">Middle Name </mat-label>
<input
matInput
class="input"
[value]="middleNameValidator.value"
(input)="updateName($event, 'middleName')"
matTooltip="Add your middle name."
/>
</mat-form-field>
<mat-form-field class="field" subscriptSizing="dynamic">
<mat-label class="text form-input-label">Last Name </mat-label>
<input
matInput
class="input"
[value]="lastNameValidator.value"
(input)="updateName($event, 'lastName')"
matTooltip="Add your last name."
required
title=""
/>
</mat-form-field>
./name-input.component.scss
:host {
display: flex;
.field {
&:not(:last-child) {
padding-right: 0.75rem;
}
.input {
text-align: left;
width: 100%;
font-size: 1rem;
}
::ng-deep .mdc-text-field {
padding: 0;
}
}
}