blob: bc6b863c72a136dbfa8ed6452ca22aa1ca70d506 (
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import { Component, forwardRef, Input, OnChanges } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import { SelectOptionsItem } from '../../../../types/select-options-item.model'
@Component({
selector: 'my-select-custom-value',
styleUrls: [ './select-shared.component.scss' ],
templateUrl: './select-custom-value.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectCustomValueComponent),
multi: true
}
]
})
export class SelectCustomValueComponent implements ControlValueAccessor, OnChanges {
@Input() items: SelectOptionsItem[] = []
@Input() clearable = false
@Input() searchable = false
@Input() groupBy: string
@Input() labelForId: string
@Input() inputSuffix: string
@Input() inputType = 'text'
customValue: number | string = ''
selectedId: number | string
itemsWithCustom: SelectOptionsItem[] = []
ngOnChanges () {
this.itemsWithCustom = this.getItems()
}
propagateChange = (_: any) => { /* empty */ }
writeValue (id: number | string) {
this.selectedId = id
if (this.isSelectedIdInItems() !== true) {
this.selectedId = 'other'
this.customValue = id
}
}
registerOnChange (fn: (_: any) => void) {
this.propagateChange = fn
}
registerOnTouched () {
// Unused
}
onModelChange () {
if (this.selectedId === 'other') {
return this.propagateChange(this.customValue)
}
return this.propagateChange(this.selectedId)
}
isSelectedIdInItems () {
return !!this.items.find(i => i.id === this.selectedId)
}
getItems () {
const other: SelectOptionsItem = {
id: 'other',
label: $localize`Custom value...`
}
return this.items.concat([ other ])
}
isCustomValue () {
return this.selectedId === 'other'
}
}
|