import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { ControlValueAccessor } from '@angular/forms'; import { MsgHelper, provideValueAccessor } from 'rh-base/core'; import { RhSafeAny } from 'rh-base/model'; import { noop } from 'rxjs'; @Component({ selector: 'rh-input-select-control', templateUrl: './input-select-control.component.html', styleUrls: ['./input-select-control.component.less'], providers: [provideValueAccessor(RhInputSelectControlComponent)] }) export class RhInputSelectControlComponent implements OnInit, ControlValueAccessor { protected _value = ''; private _oldValue = ''; get value() { return this._value; } set value(value: string) { this._oldValue = this._value; this._value = value; if (this._oldValue !== this._value) { // 当旧的值不等于新值时才触发onchange方法; this.onChange ? this.onChange(value) : noop(); } // if (this.valueHandler) { // this.valueHandler.call(this, this.value); // } } onChange!: (value: string | number) => void; onTouched!: (value: string | number) => void; @Input() rhPlaceholder = ''; // select modal 数据源 @Input() rhDataSource!: RhSafeAny[]; // 默认传入的数据源类型为selectItem // select项显示内容键值 @Input() rhDisplayName = 'Text'; // 控件绑定数据的键值 @Input() rhValue = 'Value'; @Output() rhSelectChange = new EventEmitter(); // displayData: string; displayValue = ''; isShowSelectModal = false; constructor(public cdr: ChangeDetectorRef) {} ngOnInit() { // } openSelectModal() { if (this.rhDataSource.length <= 0) { MsgHelper.ShowWarningMessage(`暂无数据!`); this.isShowSelectModal = false; return; } this.isShowSelectModal = true; } closeSelectModal() { this.isShowSelectModal = false; } onSelectItem(item: RhSafeAny) { this.value = item[this.rhValue]; this.displayValue = item[this.rhDisplayName]; this.rhSelectChange.emit(item); } onModelChange(value: string) { this.value = value; } //#region control value accessor区域 writeValue(obj: string): void { this.value = obj; this.cdr.markForCheck(); } registerOnChange(fn: RhSafeAny): void { this.onChange = fn; } registerOnTouched(fn: RhSafeAny): void { this.onTouched = fn; } }