File

src/app/shared/components/name-input/name-input.component.ts

Description

Component for inputting the researcher's name

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Constructor

constructor(ga: GoogleAnalyticsService)

Creates an instance of name input component.

Parameters :
Name Type Optional Description
ga GoogleAnalyticsService No

Analytics service

Inputs

name
Type : UserName

Current user name

Outputs

nameChange
Type : EventEmitter

Emits a UserName object

HostBindings

class
Type : "ccf-name-input"
Default value : 'ccf-name-input'

HTML class name

Methods

updateName
updateName(input: Event, key: string)

Updates username with a new entry and emits the UserName object

Parameters :
Name Type Optional Description
input Event No

InputEvent from the input element which contains the new value

key string No

firstName or lastName

Returns : void

Properties

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.

Accessors

name
getname()

Current user name

Returns : UserName
setname(value: UserName)
Parameters :
Name Type Optional
value UserName No
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;
    }
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""