ngneat / forms-manager

🦄 The Foundation for Proper Form Management in Angular
https://www.netbasal.com
MIT License
519 stars 29 forks source link

FormGroup upsert takes value of previous FormGroup #26

Open field123 opened 3 years ago

field123 commented 3 years ago

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[x] Support request
[ ] Other... Please describe:

Current behavior

When upsert is used to replace firstForm FormGroup on 'example' key with a new secondForm FormGroup on 'example' key the store does not take the values from the new secondForm control it instead copies over the values from the old firstForm control into the secondForm control replacing the it's values.


firstForm = new FormGroup({
    id: new FormControl("123")
});

secondForm = new FormGroup({
    id: new FormControl("456")
});

this.manager.upsert("example", this.firstForm);
// this.manager.getControl("example").value = {"id":"123"}

this.manager.upsert("example", this.secondForm);
// this.manager.getControl("example").value = {"id":"123"} <--- old value

Relevant section of the codebase https://github.com/ngneat/forms-manager/blob/0ee5effc99abc54eba979e8e7bb127f47d211382/projects/ngneat/forms-manager/src/lib/forms-manager.ts#L547

Work around at the moment is to perform a clear on the 'example' key, removing the old form group so that when the new one is passed in it wont have an existing FormGroup at the 'example' key to copy data from.


this.manager.upsert("example", this.firstForm);
// this.manager.getControl("example").value = {"id":"123"}

this.manager.clear("example");
this.manager.upsert("example", this.secondForm);
// this.manager.getControl("example").value = {"id":"456"} <--- new value

Expected behavior

The secondForm values replace the store values.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/github-mt9mwh?file=src/app/example.component.ts

What is the motivation / use case for changing the behavior?

It feels counterintuitive that an upsert method would change the values on the FormGroup being passed in to match the store. I'm aware this may in fact be me miss using this feature so any clarification would be appreciated.

Environment

https://stackblitz.com/edit/github-mt9mwh?file=src/app/example.component.ts Angular version: 9.1.13

Browser: