import { OnDestroy, SimpleChanges, OnChanges, Optional, Injector } from '@angular/core'; /* * @Author: 胡玉洋(Yuyang) * @Date: 2019-08-29 * @LastEditors: 胡玉洋(Yuyang) * @LastEditTime: 2019-08-29 * @QQ: 664927877 * @Version: v0.0.8 */ import { Component, Input, forwardRef, OnInit, Output, EventEmitter } from '@angular/core'; import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'; import { IdService, CommonUtils } from '@farris/ui-common'; import { HttpClient } from '@angular/common/http'; import { Observable, Subscription } from 'rxjs'; @Component({ selector: 'farris-checkboxgroup', templateUrl: './checkboxgroup.component.html', providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => FarrisCheckboxGroupComponent), multi: true, }], exportAs: 'FarrisCheckboxGroupComponent' }) export class FarrisCheckboxGroupComponent implements ControlValueAccessor, OnInit, OnDestroy, OnChanges { @Input('tab-index') tabIndex: number; /* radio 数组 */ @Input() data: any[] = []; /* checkbox 类型 原生或者按钮类型*/ @Input() type: string; /* 复选框name */ @Input() name: string; /* 是否水平分布 */ @Input() horizontal: boolean; /* 分隔符 默认逗号*/ @Input() separator = ','; /* 禁用 */ @Input() disable: boolean; /* 复选框组的值 */ @Input() value: any; /* 值类型是否是string */ @Input() isStringValue = true; @Input() textField = 'name'; @Input() valueField = 'value'; /** 加载数据事件 */ @Input() loadData: (emptyData?: any[]) => Observable; loadDataSubscription: Subscription; @Output() changeValue = new EventEmitter(); /* checkbox赋值事件 */ @Output() setCheckboxData = new EventEmitter(); private commonUtils: CommonUtils; constructor(private idSer: IdService, @Optional() private inject: Injector) { if (this.inject) { this.commonUtils = this.inject.get(CommonUtils); } } ngOnInit() { // 初始化name this.name = this.idSer.generate().replace(/-/g, '_'); this.setCheckboxData.emit(this); // todo 取值可以实现promise if (this.loadData) { this.loadDataSubscription = this.loadData().subscribe(res => this.data = res.data); } } ngOnChanges(changes: SimpleChanges) { } ngOnDestroy() { if (this.loadDataSubscription) { this.loadDataSubscription.unsubscribe(); } } _getText(item) { if (item) { if (this.commonUtils && this.commonUtils.getValue) { return this.commonUtils.getValue(this.textField, item); } else { return item[this.textField]; } } else { return ''; } } _getValue(item) { if (item) { if (this.commonUtils && this.commonUtils.getValue) { return this.commonUtils.getValue(this.valueField, item); } else { return item[this.valueField]; } } else { return ''; } } /* 选中input值 */ clickHandler(item: any, index: number) { let arrValue = this.transformToArr(this.value) || []; const val = String(this._getValue(item)); if (!arrValue || !arrValue.length) { arrValue.push(val); } else { if (arrValue.some(item => item == val)) { arrValue = arrValue.filter(n => n !== val); } else { arrValue.push(val); } } // 更新value值 this.value = this.transformToStr(arrValue); // 传值formcontrol this.controlChange(this.value); this.controlTouch(this.value); this.changeValue.emit(this.value); } /* 被选中 */ checked(item: any) { const val = String(this._getValue(item)); const checkedValue = this.transformToArr(this.value); // 多值 return checkedValue.some(item => item == val); } /* 值到数组值的转换 */ transformToArr(value: any): string[] { if (!value) { return []; } if (!this.isStringValue) { return value; } return value.split(this.separator); } /* 值到字符串值的转换 */ transformToStr(value: Array) { const allVals = this.data.map(n => this._getValue(n)); const r = []; allVals.forEach(n => { if (value.some(item => item == n)) { r.push(n); } }); if (!this.isStringValue) { return r; } return r.join(this.separator); } writeValue(value: any): void { this.value = value; } /* change操作值变化 方法重写*/ registerOnChange(fn: () => void): void { this.controlChange = fn; } /* blur值变化 方法重写*/ registerOnTouched(fn: () => void): void { this.controlTouch = fn; } private controlChange: (value: any) => void = () => { }; private controlTouch: (value: any) => void = () => { }; }