From d3b7e27d370f170f5ab73e3726a0457ff4bf4898 Mon Sep 17 00:00:00 2001 From: Steven Ulmer Date: Thu, 24 Oct 2019 11:25:47 -0400 Subject: [PATCH] Go Select Async Items Adds the ability to use a typeahead with go-select. This allows for things like fetching the results server side from a user searching for results. --- .../go-select/go-select.component.html | 1 + .../go-select/go-select.component.ts | 2 + .../select-docs/select-docs.component.html | 37 ++++++++++++++++++ .../select-docs/select-docs.component.ts | 39 +++++++++++++++++++ 4 files changed, 79 insertions(+) diff --git a/projects/go-lib/src/lib/components/go-select/go-select.component.html b/projects/go-lib/src/lib/components/go-select/go-select.component.html index 59afb637f..dc8e99c4e 100644 --- a/projects/go-lib/src/lib/components/go-select/go-select.component.html +++ b/projects/go-lib/src/lib/components/go-select/go-select.component.html @@ -14,6 +14,7 @@ [loading]="loading" [multiple]="multiple" [formControl]="control" + [typeahead]="typeahead" [placeholder]="placeholder"> diff --git a/projects/go-lib/src/lib/components/go-select/go-select.component.ts b/projects/go-lib/src/lib/components/go-select/go-select.component.ts index eb9880084..8df3dace3 100644 --- a/projects/go-lib/src/lib/components/go-select/go-select.component.ts +++ b/projects/go-lib/src/lib/components/go-select/go-select.component.ts @@ -1,5 +1,6 @@ import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core'; import { FormControl } from '@angular/forms'; +import { Subject } from 'rxjs'; @Component({ encapsulation: ViewEncapsulation.None, @@ -21,6 +22,7 @@ export class GoSelectComponent implements OnInit { @Input() loading: boolean = false; @Input() multiple: boolean = false; @Input() placeholder: string; + @Input() typeahead?: Subject; @Input() theme: 'light' | 'dark' = 'light'; ngOnInit(): void { diff --git a/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.html b/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.html index 9f0506ecc..e5c373a16 100644 --- a/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.html +++ b/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.html @@ -344,4 +344,41 @@

Code

+ + +

Component Typeahead

+
+ +

+ Sometimes we may want to use typeahead functionality in order to get the results from the server based + on a users input. The @Input() typeahead?: Subject<string>; binding. + can be used to pass a typeahead string through to do server side searching using an observable with rxjs. +

+
+
+

View

+ +
+
+

Code

+ + +
+
+
+
diff --git a/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.ts b/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.ts index e173a410a..cbe842168 100644 --- a/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.ts +++ b/projects/go-style-guide/src/app/features/ui-kit/components/form-docs/components/select-docs/select-docs.component.ts @@ -2,11 +2,16 @@ import { Component, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { GoModalService, GoSelectComponent } from 'projects/go-lib/src/public_api'; import { SubNavService } from 'projects/go-style-guide/src/app/shared/components/sub-nav/sub-nav.service'; +import { debounceTime, map } from 'rxjs/operators'; +import { concat, of, Subject } from 'rxjs'; @Component({ templateUrl: './select-docs.component.html' }) export class SelectDocsComponent implements OnInit { + itemInput: Subject = new Subject(); + options$: any; + items: any = [ { value: 1, name: 'Reeses' }, { value: 2, name: 'Mints' } @@ -22,6 +27,7 @@ export class SelectDocsComponent implements OnInit { select8: FormControl = new FormControl(''); select9: FormControl = new FormControl(''); select10: FormControl = new FormControl(''); + select11: FormControl = new FormControl(''); hints: Array = ['please select you favorite candy']; @@ -157,6 +163,31 @@ export class SelectDocsComponent implements OnInit { } `; + select11Code: string = ` + + `; + + select11ComponentCode: string = ` + itemInput: Subject = new Subject(); + options$: any; + + this.options$ = concat( + of([]), + this.itemInput.pipe( + debounceTime(600), // Delay user input + map((input) => [input]) + ) + ); + `; + constructor( private goModalService: GoModalService, private subNavService: SubNavService @@ -176,6 +207,14 @@ export class SelectDocsComponent implements OnInit { } ]); }); + + this.options$ = concat( + of([]), + this.itemInput.pipe( + debounceTime(600), // Delay user input + map((input) => [input]) + ) + ); } openModal(): void {