File

src/app/search-ui/search-input/search-input.component.ts

Implements

OnInit AfterContentInit

Metadata

selector app-search-input
styleUrls search-input.component.css
templateUrl ./search-input.component.html

Index

Properties
Methods
Outputs

Constructor

constructor(deeplink: DeeplinkService)
Parameters :
Name Type Optional
deeplink DeeplinkService no

Outputs

clear $event type: EventEmitter<void>
query $event type: EventEmitter<string>

Methods

clearValue
clearValue()
Returns : void
isEmptyInput
isEmptyInput()
Returns : boolean
ngAfterContentInit
ngAfterContentInit()

Wait till the inner content of this component has been initialised and renderered, and then set the focus on the input and register the DeepLink service.

Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

inputRef
inputRef: ElementRef
Type : ElementRef
Decorators : ViewChild
searchGroup
searchGroup: FormGroup
Type : FormGroup
import { DeeplinkService } from './../deeplink.service';
import { Component, OnInit, EventEmitter, Output, ViewChild, ElementRef, AfterContentInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { filter, distinctUntilChanged, debounceTime, tap, map } from 'rxjs/operators';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-search-input',
  templateUrl: './search-input.component.html',
  styleUrls: ['./search-input.component.css']
})
export class SearchInputComponent implements OnInit, AfterContentInit {
  @ViewChild('inputRef') inputRef: ElementRef;
  @Output() query: EventEmitter<string>;
  @Output() clear: EventEmitter<void>;

  searchGroup: FormGroup;

  constructor(private deeplink: DeeplinkService) {
    this.query = new EventEmitter();
    this.clear = new EventEmitter();
    this.searchGroup = new FormGroup({
      query: new FormControl('')
    });
  }

  ngOnInit() {
    this.searchGroup.valueChanges
      .pipe(
        // prettier-ignore
        map(event => event.query),
        distinctUntilChanged()
      )
      .subscribe(value => {
        if (value.length === 0) {
          this.clear.emit();
          this.deeplink.syncUrl({
            q: null
          });
        } else {
          this.query.emit(value);
          this.deeplink.syncUrl({
            q: value
          });
        }
      });
  }

  /**
   * Wait till the inner content of this component has been initialised and renderered,
   * and then set the focus on the input and register the DeepLink service.
   */
  ngAfterContentInit() {
    this.inputRef.nativeElement.focus();
    this.deeplink.registerFormGroup(this.searchGroup);
  }

  clearValue() {
    (this.searchGroup.controls.query as FormControl).setValue('');

    this.clear.emit();
  }

  isEmptyInput() {
    return this.searchGroup.controls.query.value === '';
  }
}
<mat-form-field [formGroup]="searchGroup">
  <input #inputRef matInput type="text" placeholder="Search for popular apps..." formControlName="query" />
  <button mat-button *ngIf="!isEmptyInput()" matSuffix mat-icon-button aria-label="Clear" (click)="clearValue()">
    <mat-icon>close</mat-icon>
  </button>
</mat-form-field>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""