mobx-react-form icon indicating copy to clipboard operation
mobx-react-form copied to clipboard

It's impossible to clear or reset arrays

Open JulianWielga opened this issue 5 years ago • 4 comments

clear - leaves empty elements in arrays. reset - leaves added elements in arrays and isDirty flag. defaults, initials - return modified arrays.

it's visible even on demo page https://foxhound87.github.io/mobx-react-form-demo/demo.html

JulianWielga avatar May 23 '19 17:05 JulianWielga

The clear and reset are behaving correctly. default and initials are correct. please give more explanation.

foxhound87 avatar May 24 '19 14:05 foxhound87

after clear: https://www.dropbox.com/s/r2lri29c6gkgk8g/after%20clear.png?dl=0 clear should remove all fields that could be added manually.

before reset: https://www.dropbox.com/s/oy12gr1c1hwqnqo/before%20reset.png?dl=0 default & initial are set. form was modified by clear and by delete on top array elements.

after reset: https://www.dropbox.com/s/8srhnwmwn7o1eqj/after%20reset.png?dl=0 default & initial are ignored for array. same for adding, there is no option to revert changes on array.

defaults & initials after adding: https://www.dropbox.com/s/zs0z0sc8lvd2fmx/defaults%26initials.png?dl=0

JulianWielga avatar May 26 '19 12:05 JulianWielga

Thank you for the explanation, The behavior has been always like this, I have to figure out if it can be enhanced.

foxhound87 avatar Jun 20 '19 13:06 foxhound87

@JulianWielga @foxhound87 do you have solution for this? I need to reset nested fields array but it does not do that. Actually the exact behavior you described.

Nodios avatar Nov 21 '20 21:11 Nodios

I forgot to mention that update() can be used to reset/clear arrays:

for example we have members[] in fields struct we can remove the elements with:

instance.update({ members: [] })

which (almost) is the same of:

instance.$('members').each((field) => field.del())

reset and clear example (using values):

const members = instance.$('members').values();
instance.update({ members: [] }) // clear
instance.update({ members }) // reset

foxhound87 avatar Aug 08 '23 09:08 foxhound87