Skip to content

Commit 4e7e772

Browse files
authored
Merge pull request #138 from netgrif/NAE-1745
[NAE-1745] Enumeration autocomplete constantly sending value
2 parents 02188ce + 67bd507 commit 4e7e772

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-autocomplete-select-field/abstract-enumeration-autocomplete-select-field.component.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {Component, ElementRef, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
2-
import {FormControl} from '@angular/forms';
1+
import {Component, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
2+
import {FormControl, NgModel} from '@angular/forms';
33
import {Observable, of} from 'rxjs';
44
import {map, startWith} from 'rxjs/operators';
55
import {EnumerationField, EnumerationFieldValidation, EnumerationFieldValue} from '../models/enumeration-field';
66
import {WrappedBoolean} from '../../data-field-template/models/wrapped-boolean';
77
import {TranslateService} from '@ngx-translate/core';
8+
import {MatAutocompleteSelectedEvent} from '@angular/material/autocomplete';
89

910
@Component({
1011
selector: 'ncc-abstract-enumeration-autocomplete-field',
@@ -15,18 +16,28 @@ export abstract class AbstractEnumerationAutocompleteSelectFieldComponent implem
1516
@Input() enumerationField: EnumerationField;
1617
@Input() formControlRef: FormControl;
1718
@Input() showLargeLayout: WrappedBoolean;
18-
@ViewChild('input') text: ElementRef;
19+
@ViewChild('input') text: NgModel;
20+
public tmpValue: string;
1921

2022
filteredOptions: Observable<Array<EnumerationFieldValue>>;
2123

2224
constructor(protected _translate: TranslateService) {
2325
}
2426

2527
ngOnInit() {
28+
this.tmpValue = this.formControlRef.value ?? '';
2629
this.filteredOptions = this.formControlRef.valueChanges.pipe(
2730
startWith(''),
2831
map(value => this._filter(value))
2932
);
33+
this.enumerationField.touch$.subscribe(touch => {
34+
if (touch) {
35+
this.text.control.markAsTouched();
36+
}
37+
});
38+
this.formControlRef.valueChanges.subscribe(it => {
39+
this.tmpValue = it ?? '';
40+
});
3041
}
3142

3243
ngOnDestroy(): void {
@@ -46,11 +57,20 @@ export abstract class AbstractEnumerationAutocompleteSelectFieldComponent implem
4657
}
4758

4859
change() {
49-
if (this.text.nativeElement.value !== undefined) {
50-
this.filteredOptions = of(this._filter(this.text.nativeElement.value));
60+
if (this.text.value !== undefined) {
61+
this.filteredOptions = of(this._filter(this.text.value));
5162
}
5263
}
5364

65+
select(event: MatAutocompleteSelectedEvent) {
66+
this.formControlRef.setValue(event.option.value);
67+
}
68+
69+
70+
isInvalid(): boolean {
71+
return !this.formControlRef.disabled && !this.formControlRef.valid && this.text.control.touched;
72+
}
73+
5474
public renderSelection = (key) => {
5575
if (key !== undefined && key !== '' && key !== null) {
5676
if (this.enumerationField.choices.find(choice => choice.key === key)) {

projects/netgrif-components-core/src/lib/data-fields/models/abstract-data-field.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ export abstract class DataField<T> {
273273
this._touch.next(set);
274274
}
275275

276+
get touch$(): Observable<boolean> {
277+
return this._touch.asObservable();
278+
}
279+
276280
get component(): Component {
277281
return this._component;
278282
}

projects/netgrif-components/src/lib/data-fields/enumeration-field/enumeration-autocomplete-select-field/enumeration-autocomplete-select-field.component.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
<input type="text"
44
aria-label="Autocomplete"
55
matInput
6-
#input
6+
#input="ngModel"
77
[placeholder]="enumerationField.placeholder"
8-
[formControl]="formControlRef"
98
[matAutocomplete]="auto"
9+
[(ngModel)]="tmpValue"
1010
[required]="enumerationField.behavior.required"
11+
[disabled]="formControlRef.disabled"
1112
(focus)="change()"
1213
(keyup)="change()">
13-
<mat-autocomplete [displayWith]="renderSelection" autoActiveFirstOption #auto="matAutocomplete">
14+
<mat-autocomplete [displayWith]="renderSelection" autoActiveFirstOption #auto="matAutocomplete" (optionSelected)="select($event)">
1415
<mat-option [value]="null">---</mat-option>
1516
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.key" (click)="change()">
1617
{{option.value}}
1718
</mat-option>
1819
</mat-autocomplete>
1920
<mat-hint>{{enumerationField.description}}</mat-hint>
20-
<mat-error *ngIf="enumerationField.isInvalid(formControlRef)">{{buildErrorMessage()}}</mat-error>
21+
<mat-error *ngIf="isInvalid() !== undefined">{{buildErrorMessage()}}</mat-error>
2122
</mat-form-field>

0 commit comments

Comments
 (0)