(layout/common/search) Implemented the new MatAutocomplete "autoSelectActiveOption" functionality

This commit is contained in:
Sercan Yemen 2022-05-12 23:37:33 +03:00
parent 6aaa355a48
commit b87173b056
3 changed files with 34 additions and 11 deletions

View File

@ -18,11 +18,12 @@
class="w-full h-full px-16 sm:px-18" class="w-full h-full px-16 sm:px-18"
[formControl]="searchControl" [formControl]="searchControl"
[matAutocomplete]="matAutocomplete" [matAutocomplete]="matAutocomplete"
[placeholder]="'Search for a page or a contact'" [placeholder]="'Search...'"
(keydown)="onKeydown($event)" (keydown)="onKeydown($event)"
#barSearchInput> #barSearchInput>
<mat-autocomplete <mat-autocomplete
class="max-h-128 sm:px-2 border-t rounded-b shadow-md" class="max-h-128 sm:px-2 border-t rounded-b shadow-md"
[autoSelectActiveOption]="true"
[disableRipple]="true" [disableRipple]="true"
#matAutocomplete="matAutocomplete"> #matAutocomplete="matAutocomplete">
<mat-option <mat-option
@ -37,7 +38,8 @@
<ng-container *ngFor="let result of resultSet.results; trackBy: trackByFn"> <ng-container *ngFor="let result of resultSet.results; trackBy: trackByFn">
<mat-option <mat-option
class="group relative mb-1 py-0 px-6 text-md rounded-md hover:bg-gray-100 dark:hover:bg-hover" class="group relative mb-1 py-0 px-6 text-md rounded-md hover:bg-gray-100 dark:hover:bg-hover"
[routerLink]="result.link"> [routerLink]="result.link"
[value]="result.value">
<!-- Contacts --> <!-- Contacts -->
<ng-container *ngIf="resultSet.id === 'contacts'"> <ng-container *ngIf="resultSet.id === 'contacts'">
<ng-container *ngTemplateOutlet="contactResult; context: {$implicit: result}"></ng-container> <ng-container *ngTemplateOutlet="contactResult; context: {$implicit: result}"></ng-container>
@ -74,11 +76,12 @@
matInput matInput
[formControl]="searchControl" [formControl]="searchControl"
[matAutocomplete]="matAutocomplete" [matAutocomplete]="matAutocomplete"
[placeholder]="'Search for a page or a contact'" [placeholder]="'Search...'"
(keydown)="onKeydown($event)"> (keydown)="onKeydown($event)">
</mat-form-field> </mat-form-field>
<mat-autocomplete <mat-autocomplete
class="max-h-128 mt-1 rounded" class="max-h-128 mt-1 rounded"
[autoSelectActiveOption]="true"
[disableRipple]="true" [disableRipple]="true"
#matAutocomplete="matAutocomplete"> #matAutocomplete="matAutocomplete">
<mat-option <mat-option
@ -93,7 +96,8 @@
<ng-container *ngFor="let result of resultSet.results; trackBy: trackByFn"> <ng-container *ngFor="let result of resultSet.results; trackBy: trackByFn">
<mat-option <mat-option
class="group relative mb-1 py-0 px-6 text-md rounded-md hover:bg-gray-100 dark:hover:bg-hover" class="group relative mb-1 py-0 px-6 text-md rounded-md hover:bg-gray-100 dark:hover:bg-hover"
[routerLink]="result.link"> [routerLink]="result.link"
[value]="result.value">
<!-- Contacts --> <!-- Contacts -->
<ng-container *ngIf="resultSet.id === 'contacts'"> <ng-container *ngIf="resultSet.id === 'contacts'">
<ng-container *ngTemplateOutlet="contactResult; context: {$implicit: result}"></ng-container> <ng-container *ngTemplateOutlet="contactResult; context: {$implicit: result}"></ng-container>

View File

@ -1,6 +1,7 @@
import { Component, ElementRef, EventEmitter, HostBinding, Input, OnChanges, OnDestroy, OnInit, Output, Renderer2, SimpleChanges, ViewChild, ViewEncapsulation } from '@angular/core'; import { Component, ElementRef, EventEmitter, HostBinding, Input, OnChanges, OnDestroy, OnInit, Output, Renderer2, SimpleChanges, ViewChild, ViewEncapsulation } from '@angular/core';
import { FormControl } from '@angular/forms'; import { FormControl } from '@angular/forms';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { MatAutocomplete } from '@angular/material/autocomplete';
import { debounceTime, filter, map, Subject, takeUntil } from 'rxjs'; import { debounceTime, filter, map, Subject, takeUntil } from 'rxjs';
import { fuseAnimations } from '@fuse/animations/public-api'; import { fuseAnimations } from '@fuse/animations/public-api';
@ -21,6 +22,7 @@ export class SearchComponent implements OnChanges, OnInit, OnDestroy
opened: boolean = false; opened: boolean = false;
resultSets: any[]; resultSets: any[];
searchControl: FormControl = new FormControl(); searchControl: FormControl = new FormControl();
private _matAutocomplete: MatAutocomplete;
private _unsubscribeAll: Subject<any> = new Subject<any>(); private _unsubscribeAll: Subject<any> = new Subject<any>();
/** /**
@ -59,7 +61,7 @@ export class SearchComponent implements OnChanges, OnInit, OnDestroy
set barSearchInput(value: ElementRef) set barSearchInput(value: ElementRef)
{ {
// If the value exists, it means that the search input // If the value exists, it means that the search input
// is now in the DOM and we can focus on the input.. // is now in the DOM, and we can focus on the input..
if ( value ) if ( value )
{ {
// Give Angular time to complete the change detection cycle // Give Angular time to complete the change detection cycle
@ -71,6 +73,17 @@ export class SearchComponent implements OnChanges, OnInit, OnDestroy
} }
} }
/**
* Setter for mat-autocomplete element reference
*
* @param value
*/
@ViewChild('matAutocomplete')
set matAutocomplete(value: MatAutocomplete)
{
this._matAutocomplete = value;
}
// ----------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------
// @ Lifecycle hooks // @ Lifecycle hooks
// ----------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------
@ -152,14 +165,12 @@ export class SearchComponent implements OnChanges, OnInit, OnDestroy
*/ */
onKeydown(event: KeyboardEvent): void onKeydown(event: KeyboardEvent): void
{ {
// Listen for escape to close the search // Escape
// if the appearance is 'bar' if ( event.code === 'Escape' )
if ( this.appearance === 'bar' )
{ {
// Escape // If the appearance is 'bar' and the mat-autocomplete is not open, close the search
if ( event.code === 'Escape' ) if ( this.appearance === 'bar' && !this._matAutocomplete.isOpen )
{ {
// Close the search
this.close(); this.close();
} }
} }

View File

@ -79,6 +79,9 @@ export class SearchMockApi
// Add a link // Add a link
result.link = '/apps/contacts/' + result.id; result.link = '/apps/contacts/' + result.id;
// Add the name as the value
result.value = result.name;
}); });
// Add to the results // Add to the results
@ -95,6 +98,8 @@ export class SearchMockApi
// Normalize the results // Normalize the results
pagesResults.forEach((result: any) => { pagesResults.forEach((result: any) => {
// Add the page title as the value
result.value = result.title;
}); });
// Add to the results // Add to the results
@ -113,6 +118,9 @@ export class SearchMockApi
// Add a link // Add a link
result.link = '/apps/tasks/' + result.id; result.link = '/apps/tasks/' + result.id;
// Add the title as the value
result.value = result.title;
}); });
// Add to the results // Add to the results