]>
Commit | Line | Data |
---|---|---|
30b1e106 C |
1 | import { Component, forwardRef, Input } from '@angular/core' |
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | |
02c01341 | 3 | |
52c4976f C |
4 | export type SelectOptionsItem = { |
5 | id: string | number | |
6 | label: string | |
7 | description?: string | |
8 | group?: string | |
9 | groupLabel?: string | |
10 | } | |
02c01341 RK |
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 | |
02c01341 | 28 | @Input() groupBy: string |
30b1e106 | 29 | @Input() labelForId: string |
02c01341 RK |
30 | |
31 | selectedId: number | string | |
32 | ||
02c01341 RK |
33 | propagateChange = (_: any) => { /* empty */ } |
34 | ||
35 | writeValue (id: number | string) { | |
36 | this.selectedId = id | |
37 | } | |
38 | ||
39 | registerOnChange (fn: (_: any) => void) { | |
40 | this.propagateChange = fn | |
41 | } | |
42 | ||
43 | registerOnTouched () { | |
44 | // Unused | |
45 | } | |
46 | ||
47 | onModelChange () { | |
48 | this.propagateChange(this.selectedId) | |
49 | } | |
50 | } |