1 import { Component, forwardRef, HostListener, Input } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { SelectOptionsItem } from '../../../../types/select-options-item.model'
6 selector: 'my-select-options',
7 styleUrls: [ './select-shared.component.scss' ],
8 templateUrl: './select-options.component.html',
11 provide: NG_VALUE_ACCESSOR,
12 useExisting: forwardRef(() => SelectOptionsComponent),
17 export class SelectOptionsComponent implements ControlValueAccessor {
18 @Input() items: SelectOptionsItem[] = []
19 @Input() clearable = false
20 @Input() searchable = false
21 @Input() groupBy: string
22 @Input() labelForId: string
23 @Input() searchFn: any
25 selectedId: number | string
27 propagateChange = (_: any) => { /* empty */ }
29 // Allow plugins to update our value
30 @HostListener('change', [ '$event.target' ])
31 handleChange (event: any) {
32 this.writeValue(event.value)
36 writeValue (id: number | string) {
40 registerOnChange (fn: (_: any) => void) {
41 this.propagateChange = fn
44 registerOnTouched () {
49 this.propagateChange(this.selectedId)