Skip to content

Commit 4d20955

Browse files
committed
add generic to specify return type
1 parent 85319f0 commit 4d20955

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

projects/fusio-sdk/src/lib/abstract/object-selector.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Component, EventEmitter, Input, Output} from '@angular/core';
22
import {catchError, debounceTime, distinctUntilChanged, map, merge, Observable, of, OperatorFunction, Subject, switchMap, tap} from "rxjs";
33
import {fromPromise} from "rxjs/internal/observable/innerFrom";
4-
import {ModelId} from "./query";
54
import {CommonCollection} from "fusio-sdk/dist/CommonCollection";
65

76
/**
@@ -10,12 +9,12 @@ import {CommonCollection} from "fusio-sdk/dist/CommonCollection";
109
@Component({
1110
template: '',
1211
})
13-
export abstract class ObjectSelector<T extends ModelId> {
12+
export abstract class ObjectSelector<T, R> {
1413

1514
@Input() name!: string;
1615
@Input() disabled: boolean = false;
17-
@Input() data?: number = undefined;
18-
@Output() dataChange = new EventEmitter<number>();
16+
@Input() data?: R = undefined;
17+
@Output() dataChange = new EventEmitter<R>();
1918

2019
focus$ = new Subject<string>();
2120

@@ -25,8 +24,14 @@ export abstract class ObjectSelector<T extends ModelId> {
2524
selected?: T
2625

2726
objectFormatter = (object: T): string => {
28-
if (typeof object === 'object' && object.hasOwnProperty(this.getNameKey())) {
29-
return (object as any)[this.getNameKey()];
27+
const name = this.getNameProperty(object);
28+
if (name) {
29+
return name;
30+
}
31+
32+
const id = this.getIdProperty(object);
33+
if (id) {
34+
return '' + id;
3035
}
3136

3237
return '-';
@@ -61,16 +66,21 @@ export abstract class ObjectSelector<T extends ModelId> {
6166
}
6267

6368
changeValue() {
64-
if (this.disabled) {
69+
if (this.disabled || !this.selected) {
6570
return;
6671
}
6772

68-
const id = this.selected?.id;
73+
const id = this.getIdProperty(this.selected);
6974
if (!id) {
7075
return;
7176
}
7277

73-
this.dataChange.emit(parseInt('' + id));
78+
this.dataChange.emit(id);
79+
}
80+
81+
protected getIdKey(): string
82+
{
83+
return 'id';
7484
}
7585

7686
protected getNameKey(): string
@@ -82,4 +92,22 @@ export abstract class ObjectSelector<T extends ModelId> {
8292

8393
protected abstract get(id: string): Promise<T>;
8494

95+
private getIdProperty(object: T): R|undefined
96+
{
97+
if (typeof object === 'object' && object && object.hasOwnProperty(this.getIdKey())) {
98+
return (object as any)[this.getIdKey()];
99+
}
100+
101+
return undefined;
102+
}
103+
104+
private getNameProperty(object: T): string|undefined
105+
{
106+
if (typeof object === 'object' && object && object.hasOwnProperty(this.getNameKey())) {
107+
return (object as any)[this.getNameKey()];
108+
}
109+
110+
return undefined;
111+
}
112+
85113
}

0 commit comments

Comments
 (0)