Skip to content

Commit

Permalink
fix(angular-form): allow to change fielname in select model (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennTractr authored and floross committed Dec 7, 2021
1 parent 204e788 commit e457877
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions libs/angular/components/src/lib/model/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Component,
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
Expand All @@ -15,7 +16,7 @@ import { SelectOptionInterface } from '../../select/interfaces/select.interface'
templateUrl: './select.component.html',
styleUrls: ['./select.component.less'],
})
export class ModelSelectComponent implements OnInit, OnDestroy {
export class ModelSelectComponent implements OnInit, OnDestroy, OnChanges {
protected unsubscribe$: Subject<void> = new Subject<void>();

@Input() idField = 'id';
Expand Down Expand Up @@ -66,9 +67,7 @@ export class ModelSelectComponent implements OnInit, OnDestroy {
options: SelectOptionInterface<Record<string, unknown>>[] = [];

async ngOnInit() {
this.options = this.modelToOption(
await this.getList(),
) as SelectOptionInterface<Record<string, unknown>>[];
this.refreshOptions().catch((err) => console.error(err));

this.valueSubject$
.pipe(
Expand Down Expand Up @@ -100,10 +99,22 @@ export class ModelSelectComponent implements OnInit, OnDestroy {
});
}

ngOnChanges(changes: Record<string, unknown>) {
if (changes.labelField) {
this.refreshOptions().catch((err) => console.error(err));
}
}

ngOnDestroy(): void {
this.unsubscribe$.next();
}

async refreshOptions() {
this.options = this.modelToOption(
await this.getList(),
) as SelectOptionInterface<Record<string, unknown>>[];
}

modelToOption(
model: Record<string, unknown> | Record<string, unknown>[] | undefined,
):
Expand Down

0 comments on commit e457877

Please sign in to comment.