angular / angular

Deliver web apps with confidence 🚀
https://angular.dev
MIT License
94.71k stars 24.68k forks source link

forms: Emit events for Submit+Reset on forms. #55667

Closed JeanMeche closed 1 week ago

JeanMeche commented 2 weeks ago

This commit add 2 new events sent over the unified event observable.

Fixes #54599

msmallest commented 1 week ago

It would be nice if the Formgroup could have a getter to tell us if it has been submitted, too

Yeah, having existing getters for the types of events captured the with this unified events API has been really nice. For example:

export function touchedEvents$<T>(form: AbstractControl<T>) {
  return form.events.pipe(
    filter(
      (event: ControlEvent): event is TouchedEvent =>
        event instanceof TouchedEvent
    )
  );
}

observableValue$ = this.touchedEvents$(form).pipe(startWith(form.touched))
$signalValue = toSignal(this.observableValue$);

For value/status/pristine/touched events exposed so far. These initial values allow not just observables but also toSignaled things to have a starting value. Having a getter to get the status of the submit at the time of the startWith would be great for things like this, as well as other use cases beyond this.

dylhunn commented 1 week ago

Presubmit looks good: https://fusion2.corp.google.com/presubmit/631188647/OCL:631188647:BASE:631188670:1715031403246:38e97a8c/targets

dylhunn commented 1 week ago

caretaker: g3 presubmit is green (see above)

atscott commented 1 week ago

This PR was merged into the repository by commit fedeaac8ba85002e34dbd6ffa682d7483d39b5fd.