Skip to content

Commit

Permalink
fix: 馃悰 form action submit and reset behaviour
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Resetting a form will now clear all the errors and reset the submit
state
  • Loading branch information
shaharkazaz committed Mar 22, 2023
1 parent 02d51c6 commit de2e59c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
21 changes: 5 additions & 16 deletions projects/ngneat/error-tailor/src/lib/form-action.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { SpectatorDirective, createDirectiveFactory } from '@ngneat/spectator';

import { FormActionDirective } from './form-action.directive';
import { NO_ERRORS_SCHEMA } from '@angular/core';

describe('FormActionDirective', () => {
let spectator: SpectatorDirective<FormActionDirective>;
const createDirective = createDirectiveFactory(FormActionDirective);
const createDirective = createDirectiveFactory({
directive: FormActionDirective,
schemas: [NO_ERRORS_SCHEMA]
});

beforeEach(() => {
spectator = createDirective(`
Expand All @@ -21,21 +25,6 @@ describe('FormActionDirective', () => {
expect(spectator.directive.element).toBe(form);
});

it('should emit submit when form is submitted and add class `form-submitted`', () => {
let submitted = false;

spectator.directive.submit$.subscribe({
next: () => (submitted = true)
});

const form = spectator.query<HTMLButtonElement>('form');

spectator.dispatchFakeEvent(form, 'submit');

expect(submitted).toBeTrue();
expect(form.classList.contains('form-submitted')).toBeTrue();
});

it('should emit reset when form is reset and remove class `form-submitted` after submit', () => {
let reset = false;

Expand Down
32 changes: 12 additions & 20 deletions projects/ngneat/error-tailor/src/lib/form-action.directive.ts
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);
}
}
4 changes: 2 additions & 2 deletions src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ body {
color: #dc3545;
}

.on-submit.form-submitted,
.on-submit.ng-submitted,
form:not(.on-submit) {
.ng-touched.ng-invalid {
.error-tailor-has-error .ng-invalid {
border-color: #dc3545;
padding-right: calc(1.5em + 0.75rem);
}
Expand Down

0 comments on commit de2e59c

Please sign in to comment.