checkbutton-group.component.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Component, Attribute, OnInit } from '@angular/core';
  2. import { FormControl } from '@angular/forms';
  3. import { GroupInputComponent } from './../../_abstract/group-input.component';
  4. @Component({
  5. selector: 'app-checkbutton-group',
  6. templateUrl: './checkbutton-group.component.html',
  7. styleUrls: ['./checkbutton-group.component.scss']
  8. })
  9. export class CheckbuttonGroupComponent extends GroupInputComponent implements OnInit {
  10. firstControl: FormControl;
  11. constructor(
  12. @Attribute('firstEnablesRest') private firstEnablesRest
  13. ) {
  14. super();
  15. this.firstEnablesRest = firstEnablesRest === ''; // True if 'firstEnablesRest' exists as component attribute
  16. }
  17. ngOnInit() {
  18. super.ngOnInit();
  19. if (this.meta.firstEnablesRest) {
  20. this.firstEnablesRest = this.meta.firstEnablesRest;
  21. }
  22. if (this.firstEnablesRest) {
  23. this.firstControl = this.formGroup.controls[this.controlNames[0]] as FormControl;
  24. this.childMeta[0].isDisabled = false;
  25. this.childMeta.slice(1).map(meta => { meta.isDisabled = !this.firstControl.value; return meta; });
  26. // Observe value changes on first control
  27. this.firstControl.valueChanges.subscribe(val => {
  28. for (var i = 1; i < this.childMeta.length; i++) {
  29. // NOTE: We rassign the input (array member) to trigger change detection (otherwise it doesn't run)
  30. // See https://juristr.com/blog/2016/04/angular2-change-detection/
  31. this.childMeta[i] = {
  32. ...this.childMeta[1],
  33. isDisabled: !val
  34. }
  35. }
  36. });
  37. }
  38. }
  39. }