aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/input-switch.component.ts
blob: e019029c5430d2b2ee0032d8d92b02e7e9f38578 (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
import { Component, forwardRef, Input } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'

@Component({
  selector: 'my-input-switch',
  styleUrls: [ './input-switch.component.scss' ],
  templateUrl: './input-switch.component.html',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => InputSwitchComponent),
      multi: true
    }
  ]
})
export class InputSwitchComponent implements ControlValueAccessor {
  @Input() checked = false
  @Input() inputName: string
  @Input() preventUpdate = false

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

  writeValue (checked: boolean) {
    this.checked = checked
  }

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

  registerOnTouched () {
    // Unused
  }

  update () {
    this.checked = !this.checked
    this.propagateChange(this.checked)
  }
}