Browse Source

Default options added to GfSymbolAutocompleteComponent

pull/4563/head
csehatt741 4 months ago
committed by Thomas Kaul
parent
commit
2bae04e20f
  1. 38
      libs/ui/src/lib/symbol-autocomplete/symbol-autocomplete.component.html
  2. 40
      libs/ui/src/lib/symbol-autocomplete/symbol-autocomplete.component.ts

38
libs/ui/src/lib/symbol-autocomplete/symbol-autocomplete.component.html

@ -1,15 +1,45 @@
<input <input
*ngIf="!isAutocomplete"
autocapitalize="off" autocapitalize="off"
autocomplete="off" autocomplete="off"
matInput matInput
[formControl]="control" [formControl]="inputControl"
[matAutocomplete]="symbolAutocomplete" [matAutocomplete]="symbolAutocomplete"
/> />
<mat-select
*ngIf="isAutocomplete"
[displayWith]="displayFn"
[formControl]="selectControl"
(keydown)="onShowAutocomplete($event)"
(selectionChange)="onSelectUpdateSymbol($event)"
>
@for (lookupItem of defaultLookupItems; track lookupItem) {
<mat-option
class="line-height-1"
[disabled]="lookupItem.dataProviderInfo.isPremium"
[value]="lookupItem"
>
<span class="align-items-center d-flex line-height-1"
><span>{{ lookupItem.name }}</span>
@if (lookupItem.dataProviderInfo.isPremium) {
<gf-premium-indicator class="ml-1" [enableLink]="false" />
}
</span>
<small class="text-muted"
>{{ lookupItem.symbol | gfSymbol }} · {{ lookupItem.currency }}
@if (lookupItem.dataProviderInfo.name) {
· {{ lookupItem.dataProviderInfo.name }}
}
</small>
</mat-option>
}
</mat-select>
<mat-autocomplete <mat-autocomplete
#symbolAutocomplete="matAutocomplete" #symbolAutocomplete
[displayWith]="displayFn" [displayWith]="displayFn"
(optionSelected)="onUpdateSymbol($event)" (optionSelected)="onAutocompleteUpdateSymbol($event)"
> >
@if (!isLoading) { @if (!isLoading) {
@for (lookupItem of filteredLookupItems; track lookupItem) { @for (lookupItem of filteredLookupItems; track lookupItem) {
@ -35,7 +65,7 @@
</small> </small>
</mat-option> </mat-option>
} @empty { } @empty {
@if (control.value?.length > 1) { @if (inputControl.value?.length > 1) {
<mat-option class="line-height-1" disabled="true" i18n <mat-option class="line-height-1" disabled="true" i18n
>Oops! Could not find any assets.</mat-option >Oops! Could not find any assets.</mat-option
> >

40
libs/ui/src/lib/symbol-autocomplete/symbol-autocomplete.component.ts

@ -23,7 +23,6 @@ import {
ReactiveFormsModule ReactiveFormsModule
} from '@angular/forms'; } from '@angular/forms';
import { import {
MatAutocomplete,
MatAutocompleteModule, MatAutocompleteModule,
MatAutocompleteSelectedEvent MatAutocompleteSelectedEvent
} from '@angular/material/autocomplete'; } from '@angular/material/autocomplete';
@ -33,6 +32,7 @@ import {
} from '@angular/material/form-field'; } from '@angular/material/form-field';
import { MatInput, MatInputModule } from '@angular/material/input'; import { MatInput, MatInputModule } from '@angular/material/input';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSelectChange, MatSelectModule } from '@angular/material/select';
import { isString } from 'lodash'; import { isString } from 'lodash';
import { Subject, tap } from 'rxjs'; import { Subject, tap } from 'rxjs';
import { import {
@ -59,6 +59,7 @@ import { GfPremiumIndicatorComponent } from '../premium-indicator';
MatFormFieldModule, MatFormFieldModule,
MatInputModule, MatInputModule,
MatProgressSpinnerModule, MatProgressSpinnerModule,
MatSelectModule,
ReactiveFormsModule ReactiveFormsModule
], ],
providers: [ providers: [
@ -76,14 +77,15 @@ export class GfSymbolAutocompleteComponent
extends AbstractMatFormField<LookupItem> extends AbstractMatFormField<LookupItem>
implements OnInit, OnDestroy implements OnInit, OnDestroy
{ {
@Input() private includeIndices = false;
@Input() public isLoading = false; @Input() public isLoading = false;
@Input() public isAutocomplete = false;
@Input() public defaultLookupItems: LookupItem[] = [];
@ViewChild(MatInput) private input: MatInput; @ViewChild(MatInput) private input: MatInput;
@ViewChild('symbolAutocomplete') public symbolAutocomplete: MatAutocomplete; public selectControl = new FormControl();
public inputControl = new FormControl();
public control = new FormControl();
public filteredLookupItems: (LookupItem & { assetSubClassString: string })[] = public filteredLookupItems: (LookupItem & { assetSubClassString: string })[] =
[]; [];
@ -103,10 +105,11 @@ export class GfSymbolAutocompleteComponent
public ngOnInit() { public ngOnInit() {
if (this.disabled) { if (this.disabled) {
this.control.disable(); this.selectControl.disable();
this.inputControl.disable();
} }
this.control.valueChanges this.inputControl.valueChanges
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe(() => {
if (super.value) { if (super.value) {
@ -114,7 +117,7 @@ export class GfSymbolAutocompleteComponent
} }
}); });
this.control.valueChanges this.inputControl.valueChanges
.pipe( .pipe(
filter((query) => { filter((query) => {
return isString(query) && query.length > 1; return isString(query) && query.length > 1;
@ -129,8 +132,7 @@ export class GfSymbolAutocompleteComponent
takeUntil(this.unsubscribeSubject), takeUntil(this.unsubscribeSubject),
switchMap((query: string) => { switchMap((query: string) => {
return this.dataService.fetchSymbols({ return this.dataService.fetchSymbols({
query, query
includeIndices: this.includeIndices
}); });
}) })
) )
@ -174,7 +176,23 @@ export class GfSymbolAutocompleteComponent
} }
} }
public onUpdateSymbol(event: MatAutocompleteSelectedEvent) { public onShowAutocomplete(event: KeyboardEvent) {
if (event.key.length === 1) {
this.inputControl.setValue(event.key);
this.isAutocomplete = true;
this.changeDetectorRef.markForCheck();
}
}
public onSelectUpdateSymbol(event: MatSelectChange) {
super.value = {
dataSource: event.source.value.dataSource,
symbol: event.source.value.symbol
} as LookupItem;
}
public onAutocompleteUpdateSymbol(event: MatAutocompleteSelectedEvent) {
super.value = { super.value = {
dataSource: event.option.value.dataSource, dataSource: event.option.value.dataSource,
symbol: event.option.value.symbol symbol: event.option.value.symbol
@ -182,7 +200,7 @@ export class GfSymbolAutocompleteComponent
} }
public set value(value: LookupItem) { public set value(value: LookupItem) {
this.control.setValue(value); this.inputControl.setValue(value);
super.value = value; super.value = value;
} }

Loading…
Cancel
Save