bixi
Version:
企业级中后台前端解决方案
126 lines (114 loc) • 3.34 kB
text/typescript
import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
Input,
OnDestroy,
TemplateRef
} from '@angular/core';
import {
AbstractControl,
FormControlDirective,
FormControlName,
NgControl,
NgModel
} from '@angular/forms';
import { Subscription } from 'rxjs';
import { IFormControlStatus } from './form.type';
export class BixiFormItemComponent implements OnDestroy, AfterContentInit {
private validateChanges: Subscription = Subscription.EMPTY;
constructor(private cdr: ChangeDetectorRef) { }
status: IFormControlStatus | null;
_validateStatus: IFormControlStatus | null;
// label
required = false;
label: string;
noColon = false;
for: string;
// control
set validateStatus(status: IFormControlStatus | null) {
this.status = status;
this._validateStatus = status;
}
get validateStatus() {
return this._validateStatus;
}
hasFeedback = false;
extra?: string | TemplateRef<void>;
successTip?: string | TemplateRef<{ $implicit: AbstractControl | NgModel }>;
warningTip?: string | TemplateRef<{ $implicit: AbstractControl | NgModel }>;
errorTip?: string | TemplateRef<{ $implicit: AbstractControl | NgModel }>;
validatingTip?: string | TemplateRef<{ $implicit: AbstractControl | NgModel }>;
// TODO: 9.0 feature
// @Input() autoTips: Record<string, Record<string, string>> = {};
// @Input() disableAutoTips: boolean | 'default' = 'default';
validateControl?: FormControlName | FormControlDirective;
// layout
ls: number = 6;
cs: number = 18;
// fix zorro detect bug
ngAfterContentInit() {
if (this.validateControl && this.validateControl.statusChanges) {
this.validateChanges = this.validateControl.statusChanges.subscribe((status) => {
if (this.validateStatus) return;
this.status = null;
if (this.validateControl && !this.validateControl.dirty) {
this.cdr.markForCheck();
return;
}
switch (status) {
case 'VALID': {
break;
}
case 'INVALID': {
this.status = 'error';
break;
}
case 'DISABLED': {
break;
}
case 'PENDING': {
this.status = 'validating';
break;
}
}
this.cdr.markForCheck();
});
}
}
ngOnDestroy() {
this.validateChanges.unsubscribe();
}
}