aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/select-tags.component.ts
blob: 2e07d7e8f968c7535881540177ec93209739335c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Component, Input, forwardRef } from '@angular/core'
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'

@Component({
  selector: 'my-select-tags',
  styleUrls: [ './select-shared.component.scss', './select-tags.component.scss' ],
  templateUrl: './select-tags.component.html',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => SelectTagsComponent),
      multi: true
    }
  ]
})
export class SelectTagsComponent implements ControlValueAccessor {
  @Input() items: string[] = []
  @Input() _items: string[] = []

  propagateChange = (_: any) => { /* empty */ }

  writeValue (items: string[]) {
    this._items = items
    this.propagateChange(this._items)
  }

  registerOnChange (fn: (_: any) => void) {
    this.propagateChange = fn
  }

  registerOnTouched () {
    // Unused
  }

  onModelChange () {
    this.propagateChange(this._items)
  }
}