-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 馃悰 form action submit and reset behaviour
BREAKING CHANGE: Resetting a form will now clear all the errors and reset the submit state
- Loading branch information
1 parent
02d51c6
commit de2e59c
Showing
3 changed files
with
19 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 12 additions & 20 deletions
32
projects/ngneat/error-tailor/src/lib/form-action.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
import { Directive, ElementRef } from '@angular/core'; | ||
import { fromEvent, Observable } from 'rxjs'; | ||
import { shareReplay, tap } from 'rxjs/operators'; | ||
import { Directive, ElementRef, OnInit } from '@angular/core'; | ||
import { tap, fromEvent, Observable, Subject, takeUntil } from 'rxjs'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: 'form[errorTailor]' | ||
}) | ||
export class FormActionDirective { | ||
submit$: Observable<Event> = fromEvent(this.element, 'submit').pipe( | ||
tap(() => { | ||
if (this.element.classList.contains('form-submitted') === false) { | ||
this.element.classList.add('form-submitted'); | ||
} | ||
}), | ||
shareReplay({ refCount: true, bufferSize: 1 }) | ||
); | ||
export class FormActionDirective implements OnInit { | ||
private submit = new Subject<Event | null>(); | ||
private destroy = new Subject<void>(); | ||
|
||
reset$: Observable<Event> = fromEvent(this.element, 'reset').pipe( | ||
tap(() => { | ||
this.element.classList.remove('form-submitted'); | ||
}), | ||
shareReplay({ refCount: true, bufferSize: 1 }) | ||
); | ||
element = this.host.nativeElement; | ||
submit$ = this.submit.asObservable(); | ||
reset$: Observable<Event> = fromEvent(this.element, 'reset').pipe(tap(() => this.submit.next(null))); | ||
|
||
constructor(private host: ElementRef<HTMLFormElement>) {} | ||
|
||
get element() { | ||
return this.host.nativeElement; | ||
ngOnInit() { | ||
fromEvent(this.element, 'submit') | ||
.pipe(takeUntil(this.destroy)) | ||
.subscribe(this.submit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters