]>
Commit | Line | Data |
---|---|---|
02c01341 RK |
1 | import { Component, Input, forwardRef } from '@angular/core' |
2 | import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms' | |
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 RK |
28 | @Input() groupBy: string |
29 | ||
30 | selectedId: number | string | |
31 | ||
02c01341 RK |
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 | } |