]>
Commit | Line | Data |
---|---|---|
2e257e36 | 1 | import { Component, forwardRef, HostListener, Input } from '@angular/core' |
30b1e106 | 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
21e493d4 | 3 | import { SelectOptionsItem } from '../../../../types/select-options-item.model' |
02c01341 RK |
4 | |
5 | @Component({ | |
6 | selector: 'my-select-options', | |
7 | styleUrls: [ './select-shared.component.scss' ], | |
8 | templateUrl: './select-options.component.html', | |
9 | providers: [ | |
10 | { | |
11 | provide: NG_VALUE_ACCESSOR, | |
12 | useExisting: forwardRef(() => SelectOptionsComponent), | |
13 | multi: true | |
14 | } | |
15 | ] | |
16 | }) | |
17 | export class SelectOptionsComponent implements ControlValueAccessor { | |
18 | @Input() items: SelectOptionsItem[] = [] | |
19 | @Input() clearable = false | |
20 | @Input() searchable = false | |
02c01341 | 21 | @Input() groupBy: string |
30b1e106 | 22 | @Input() labelForId: string |
ead64cdf | 23 | @Input() searchFn: any |
02c01341 RK |
24 | |
25 | selectedId: number | string | |
8d8a037e | 26 | disabled = false |
02c01341 | 27 | |
02c01341 RK |
28 | propagateChange = (_: any) => { /* empty */ } |
29 | ||
2e257e36 C |
30 | // Allow plugins to update our value |
31 | @HostListener('change', [ '$event.target' ]) | |
32 | handleChange (event: any) { | |
33 | this.writeValue(event.value) | |
34 | this.onModelChange() | |
35 | } | |
36 | ||
02c01341 RK |
37 | writeValue (id: number | string) { |
38 | this.selectedId = id | |
39 | } | |
40 | ||
41 | registerOnChange (fn: (_: any) => void) { | |
42 | this.propagateChange = fn | |
43 | } | |
44 | ||
45 | registerOnTouched () { | |
46 | // Unused | |
47 | } | |
48 | ||
49 | onModelChange () { | |
50 | this.propagateChange(this.selectedId) | |
51 | } | |
8d8a037e JB |
52 | |
53 | setDisabledState (isDisabled: boolean) { | |
54 | this.disabled = isDisabled | |
55 | } | |
02c01341 | 56 | } |