import { AbstractControl } from '@angular/forms'; import { buildTitleMap } from '../../shared'; import { Component, Input, OnInit } from '@angular/core'; import { JsonSchemaFormService, TitleMapItem } from '../../json-schema-form.service'; // TODO: Change this to use a Selection List instead? // https://material.angular.io/components/list/overview @Component({ // tslint:disable-next-line:component-selector selector: 'material-checkboxes-widget', template: `
`, styles: [` .title { font-weight: bold; } .checkbox-list { list-style-type: none; } .horizontal-list > li { display: inline-block; margin-right: 10px; zoom: 1; } .checkbox-name { white-space: nowrap; } mat-error { font-size: 75%; } `], }) export class MaterialCheckboxesComponent implements OnInit { formControl: AbstractControl; controlName: string; controlValue: any; controlDisabled = false; boundControl = false; options: any; horizontalList = false; formArray: AbstractControl; checkboxList: TitleMapItem[] = []; @Input() layoutNode: any; @Input() layoutIndex: number[]; @Input() dataIndex: number[]; constructor( private jsf: JsonSchemaFormService ) { } ngOnInit() { this.options = this.layoutNode.options || {}; this.horizontalList = this.layoutNode.type === 'checkboxes-inline' || this.layoutNode.type === 'checkboxbuttons'; this.jsf.initializeControl(this); this.checkboxList = buildTitleMap( this.options.titleMap || this.options.enumNames, this.options.enum, true ); if (this.boundControl) { const formArray = this.jsf.getFormControl(this); for (const checkboxItem of this.checkboxList) { checkboxItem.checked = formArray.value.includes(checkboxItem.value); } } } get allChecked(): boolean { return this.checkboxList.filter(t => t.checked).length === this.checkboxList.length; } get someChecked(): boolean { const checkedItems = this.checkboxList.filter(t => t.checked).length; return checkedItems > 0 && checkedItems < this.checkboxList.length; } updateValue() { this.options.showErrors = true; if (this.boundControl) { this.jsf.updateArrayCheckboxList(this, this.checkboxList); } } updateAllValues(event: any) { this.options.showErrors = true; this.checkboxList.forEach(t => t.checked = event.checked); this.updateValue(); } }