aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/select-options.component.ts
blob: 09f7df53b49ba4745b7b08d9ae441c55805cd345 (plain) (tree)














































                                                                                            
import { Component, Input, forwardRef } from '@angular/core'
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'

export type SelectOptionsItem = { id: number | string, label: string, description?: string }

@Component({
  selector: 'my-select-options',
  styleUrls: [ './select-shared.component.scss' ],
  templateUrl: './select-options.component.html',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => SelectOptionsComponent),
      multi: true
    }
  ]
})
export class SelectOptionsComponent implements ControlValueAccessor {
  @Input() items: SelectOptionsItem[] = []
  @Input() clearable = false
  @Input() searchable = false
  @Input() bindValue = 'id'
  @Input() groupBy: string

  selectedId: number | string

  // ng-select options
  bindLabel = 'label'

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

  writeValue (id: number | string) {
    this.selectedId = id
  }

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

  registerOnTouched () {
    // Unused
  }

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