mirror of https://github.com/abpframework/abp
commit
4906363ff6
|
After Width: | Height: | Size: 6.3 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 43 KiB |
@ -0,0 +1,74 @@
|
||||
import {
|
||||
Directive,
|
||||
ElementRef,
|
||||
OnDestroy,
|
||||
OnInit,
|
||||
Self,
|
||||
ChangeDetectorRef,
|
||||
HostBinding,
|
||||
Input,
|
||||
Output,
|
||||
EventEmitter,
|
||||
} from '@angular/core';
|
||||
import { FormGroupDirective, FormGroup, FormControl, ɵNgNoValidate } from '@angular/forms';
|
||||
import { fromEvent } from 'rxjs';
|
||||
import { takeUntilDestroy } from '../utils';
|
||||
import { debounceTime, filter } from 'rxjs/operators';
|
||||
|
||||
type Controls = { [key: string]: FormControl } | FormGroup[];
|
||||
|
||||
@Directive({
|
||||
selector: 'form[ngSubmit][formGroup]',
|
||||
})
|
||||
export class FormSubmitDirective implements OnInit, OnDestroy {
|
||||
@Input()
|
||||
notValidateOnSubmit: string | boolean;
|
||||
|
||||
constructor(
|
||||
@Self() private formGroupDirective: FormGroupDirective,
|
||||
private host: ElementRef<HTMLFormElement>,
|
||||
private cdRef: ChangeDetectorRef,
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
fromEvent(this.host.nativeElement as HTMLElement, 'keyup')
|
||||
.pipe(
|
||||
debounceTime(200),
|
||||
filter((key: KeyboardEvent) => key && key.key === 'Enter'),
|
||||
takeUntilDestroy(this),
|
||||
)
|
||||
.subscribe(() => {
|
||||
this.host.nativeElement.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
|
||||
});
|
||||
|
||||
fromEvent(this.host.nativeElement, 'submit')
|
||||
.pipe(
|
||||
takeUntilDestroy(this),
|
||||
filter(() => !this.notValidateOnSubmit && typeof this.notValidateOnSubmit !== 'string'),
|
||||
)
|
||||
.subscribe(() => {
|
||||
const { form } = this.formGroupDirective;
|
||||
|
||||
setDirty(form.controls as { [key: string]: FormControl });
|
||||
form.markAsDirty();
|
||||
|
||||
this.cdRef.detectChanges();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {}
|
||||
}
|
||||
|
||||
function setDirty(controls: Controls) {
|
||||
if (Array.isArray(controls)) {
|
||||
controls.forEach(group => {
|
||||
setDirty(group.controls as { [key: string]: FormControl });
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Object.keys(controls).forEach(key => {
|
||||
controls[key].markAsDirty();
|
||||
controls[key].updateValueAndValidity();
|
||||
});
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
export * from './autofocus.directive';
|
||||
export * from './ellipsis.directive';
|
||||
export * from './form-submit.directive';
|
||||
export * from './permission.directive';
|
||||
export * from './visibility.directive';
|
||||
|
||||
Loading…
Reference in new issue