diff options
Diffstat (limited to 'client/src/app/shared/shared-forms/select/select-options.component.ts')
-rw-r--r-- | client/src/app/shared/shared-forms/select/select-options.component.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-forms/select/select-options.component.ts b/client/src/app/shared/shared-forms/select/select-options.component.ts new file mode 100644 index 000000000..3ba24c732 --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-options.component.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | import { Component, Input, forwardRef } from '@angular/core' | ||
2 | import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms' | ||
3 | |||
4 | export type SelectOptionsItem = { | ||
5 | id: string | number | ||
6 | label: string | ||
7 | description?: string | ||
8 | group?: string | ||
9 | groupLabel?: string | ||
10 | } | ||
11 | |||
12 | @Component({ | ||
13 | selector: 'my-select-options', | ||
14 | styleUrls: [ './select-shared.component.scss' ], | ||
15 | templateUrl: './select-options.component.html', | ||
16 | providers: [ | ||
17 | { | ||
18 | provide: NG_VALUE_ACCESSOR, | ||
19 | useExisting: forwardRef(() => SelectOptionsComponent), | ||
20 | multi: true | ||
21 | } | ||
22 | ] | ||
23 | }) | ||
24 | export class SelectOptionsComponent implements ControlValueAccessor { | ||
25 | @Input() items: SelectOptionsItem[] = [] | ||
26 | @Input() clearable = false | ||
27 | @Input() searchable = false | ||
28 | @Input() groupBy: string | ||
29 | |||
30 | selectedId: number | string | ||
31 | |||
32 | propagateChange = (_: any) => { /* empty */ } | ||
33 | |||
34 | writeValue (id: number | string) { | ||
35 | this.selectedId = id | ||
36 | } | ||
37 | |||
38 | registerOnChange (fn: (_: any) => void) { | ||
39 | this.propagateChange = fn | ||
40 | } | ||
41 | |||
42 | registerOnTouched () { | ||
43 | // Unused | ||
44 | } | ||
45 | |||
46 | onModelChange () { | ||
47 | this.propagateChange(this.selectedId) | ||
48 | } | ||
49 | } | ||