|
|
|
@ -2,14 +2,29 @@ import { |
|
|
|
ChangeDetectionStrategy, |
|
|
|
ChangeDetectorRef, |
|
|
|
Component, |
|
|
|
DestroyRef, |
|
|
|
forwardRef, |
|
|
|
inject, |
|
|
|
InjectionToken, |
|
|
|
InjectionToken, Injector, |
|
|
|
input, |
|
|
|
OnInit, |
|
|
|
} from '@angular/core'; |
|
|
|
import { NgTemplateOutlet } from '@angular/common'; |
|
|
|
import { FormFieldConfig } from '../dynamic-form.models'; |
|
|
|
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|
|
|
import { |
|
|
|
AbstractControl, |
|
|
|
ControlValueAccessor, |
|
|
|
FormControl, |
|
|
|
FormControlName, |
|
|
|
FormGroupDirective, |
|
|
|
FormsModule, |
|
|
|
NG_VALIDATORS, |
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
NgControl, |
|
|
|
ValidationErrors, |
|
|
|
Validators, |
|
|
|
} from '@angular/forms'; |
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|
|
|
|
|
|
|
export const ABP_DYNAMIC_FORM_FIELD = new InjectionToken<DynamicFormFieldComponent>('AbpDynamicFormField'); |
|
|
|
|
|
|
|
@ -22,26 +37,36 @@ const DYNAMIC_FORM_FIELD_CONTROL_VALUE_ACCESSOR = { |
|
|
|
@Component({ |
|
|
|
selector: 'abp-dynamic-form-field', |
|
|
|
templateUrl: './dynamic-form-field.component.html', |
|
|
|
providers: [{ provide: ABP_DYNAMIC_FORM_FIELD, useExisting: DynamicFormFieldComponent }, DYNAMIC_FORM_FIELD_CONTROL_VALUE_ACCESSOR], |
|
|
|
host: { 'class': 'abp-dynamic-form-field' }, |
|
|
|
providers: [ |
|
|
|
{ provide: ABP_DYNAMIC_FORM_FIELD, useExisting: DynamicFormFieldComponent }, |
|
|
|
DYNAMIC_FORM_FIELD_CONTROL_VALUE_ACCESSOR |
|
|
|
], |
|
|
|
host: { class: 'abp-dynamic-form-field' }, |
|
|
|
exportAs: 'abpDynamicFormField', |
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush, |
|
|
|
imports: [ |
|
|
|
FormsModule, |
|
|
|
NgTemplateOutlet |
|
|
|
] |
|
|
|
imports: [FormsModule], |
|
|
|
}) |
|
|
|
|
|
|
|
export class DynamicFormFieldComponent implements ControlValueAccessor { |
|
|
|
export class DynamicFormFieldComponent implements OnInit, ControlValueAccessor { |
|
|
|
field = input.required<FormFieldConfig>(); |
|
|
|
visible = input<boolean>(true); |
|
|
|
disabled = false; |
|
|
|
value: any; |
|
|
|
isFieldInvalid: boolean = false; |
|
|
|
control!: FormControl; |
|
|
|
readonly changeDetectorRef = inject(ChangeDetectorRef); |
|
|
|
readonly destroyRef = inject(DestroyRef); |
|
|
|
private injector = inject(Injector); |
|
|
|
|
|
|
|
ngOnInit() { |
|
|
|
const ngControl = this.injector.get(NgControl, null); |
|
|
|
if (ngControl) { |
|
|
|
this.control = this.injector.get(FormGroupDirective).getControl(ngControl as FormControlName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
onValueChange(value: any) { |
|
|
|
console.log(value); |
|
|
|
this.onChange(value); |
|
|
|
this.changeDetectorRef.markForCheck(); |
|
|
|
} |
|
|
|
|
|
|
|
writeValue(value: any[]): void { |
|
|
|
@ -62,6 +87,13 @@ export class DynamicFormFieldComponent implements ControlValueAccessor { |
|
|
|
this.changeDetectorRef.markForCheck(); |
|
|
|
} |
|
|
|
|
|
|
|
get isValid(): boolean { |
|
|
|
if (this.control) { |
|
|
|
return this.control.invalid && (this.control.dirty || this.control.touched); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private onChange: (value: any) => void = () => {}; |
|
|
|
private onTouched: () => void = () => {}; |
|
|
|
} |
|
|
|
|