]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/select/select-options.component.ts
2890670e5cda5881109cb54a3f31487b47be147c
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / select / select-options.component.ts
1 import { Component, forwardRef, Input } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { SelectOptionsItem } from '../../../../types/select-options-item.model'
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
21 @Input() groupBy: string
22 @Input() labelForId: string
23 @Input() searchFn: any
24
25 selectedId: number | string
26
27 propagateChange = (_: any) => { /* empty */ }
28
29 writeValue (id: number | string) {
30 this.selectedId = id
31 }
32
33 registerOnChange (fn: (_: any) => void) {
34 this.propagateChange = fn
35 }
36
37 registerOnTouched () {
38 // Unused
39 }
40
41 onModelChange () {
42 this.propagateChange(this.selectedId)
43 }
44 }