|
@@ -0,0 +1,59 @@
|
|
|
+import { Component, Attribute, OnInit, Input } from '@angular/core';
|
|
|
+import { ControlContainer, FormGroup, FormControl } from '@angular/forms';
|
|
|
+import { CustomInputComponent } from './../../_abstract/custom-input.component';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-checkbutton-group',
|
|
|
+ templateUrl: './checkbutton-group.component.html',
|
|
|
+ styleUrls: ['./checkbutton-group.component.scss']
|
|
|
+})
|
|
|
+export class CheckbuttonGroupComponent /* extends CustomInputComponent */ implements OnInit {
|
|
|
+
|
|
|
+ @Input()
|
|
|
+ control: FormGroup;
|
|
|
+
|
|
|
+ @Input()
|
|
|
+ meta;
|
|
|
+
|
|
|
+ formGroup: FormGroup;
|
|
|
+ firstControl: FormControl;
|
|
|
+ childMeta: Array<StringMap>;
|
|
|
+ controlNames: Array<string>;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private cc: ControlContainer,
|
|
|
+ @Attribute('firstEnablesRest') private firstEnablesRest
|
|
|
+ ) {
|
|
|
+ // super();
|
|
|
+ this.firstEnablesRest = firstEnablesRest === ''; // True if 'firstEnablesRest' exists as component attribute
|
|
|
+ console.log('firstEnablesRest', this.firstEnablesRest);
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ console.log('************************');
|
|
|
+ this.formGroup = this.control as FormGroup;
|
|
|
+ // this.formGroup = this.cc.control as FormGroup;
|
|
|
+ this.childMeta = Object.values(this.meta.meta); // Metadata array or all controls in group
|
|
|
+ console.log(Array.isArray(this.childMeta));
|
|
|
+ this.controlNames = Object.keys(this.formGroup.controls);
|
|
|
+ // this.controlNames = Object.keys(this.cc.control.value);
|
|
|
+ console.log(this.controlNames);
|
|
|
+ if (this.firstEnablesRest) {
|
|
|
+ this.firstControl = this.formGroup.controls[this.controlNames[0]] as FormControl;
|
|
|
+ this.childMeta[0].isDisabled = false;
|
|
|
+ this.childMeta.slice(1).map(meta => { meta.isDisabled = !this.firstControl.value; return meta; });
|
|
|
+
|
|
|
+ // Observe value changes on first control
|
|
|
+ this.firstControl.valueChanges.subscribe(val => {
|
|
|
+ for (var i = 1; i < this.childMeta.length; i++) {
|
|
|
+ // NOTE: We rassign input to trigger change detection
|
|
|
+ this.childMeta[i] = {
|
|
|
+ ...this.childMeta[1],
|
|
|
+ isDisabled: !val
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|