🐞[BUG]: @ngxs/form-plugin and FormArray
I have a model like this:
{model: {items: []}, dirty: ...,}
then I add an item to items and I expect FormArray to get a new FormGroup/FormControl item also.
The forms with FormControl/FormGroup are usually static. The game is changed completly when FormArray is coming, form becomes dynamic with rows coming and leaving
In this case, I have to append a row to a FormArray, put apropriate data to controls and force syncing FormArray to model. I cannot change form changing ngxs model, I have to modify the underlying FormArray, put values there
I do the strange thing:
// append a new row to items FormArray
this.form.items.push(new this.fb.group({firstName:". lastName: ''}));
// as soon as row is "prepared" it is safe now to update a store
this.store.dispatch(new AppendNewItem({firstName: 'John', lastName: 'Smith'}))
I've played a bit with FormArray and it's working ok for simple cases where you have a FormArray with a bunch of check boxes. Of course you have to first create the FormGroup as just adding an array in the state/model won't create the UI controls.
Yep, I need to update the state model and the form model in sync.
In some form lib I seen the following solution - for every form array there is the factory function provided that generate 'empty' array item
Anyway, for dynamic forms the ngxs forms plugin is of little help.
if you can share a screenshot of your dynamic form, that may help?
I just had the same issue. What I did was initializing the form only AFTER I got the state data.
Demo -https://stackblitz.com/edit/angular-5xyj9d?file=src%2Fapp%2Fapp.component.html