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

Field with Array of objects (Unified Mode)

Open bvallant opened this issue 8 years ago • 10 comments

I'm having a field tags which is supposed to be an array of objects. Using react-select with multi I'm able to add tag objects (with an id and a name) to the array (Note that that id and name are not editable, you can just add/remove objects).

Starting with an empty array this works flawlessly, but when initializing the form with an object which has already some tags the field's value always returns the inital array. I did a little bit of debugging and discovered that when I start with an emtpy array field.hasNestedFields is false while when starting with a pre-populated one it changes to true.

export const fields = [
  //... some other fields...
  {
    name: 'tags',
    label: 'Tags',
    value: []
  }
]

// .....
const form = new EditForm({fields}, {plugins});

//... load some item
form.update(item);

// the field component
class TagInput extends React.Component {
  render() {
    return (		
                <Select.AsyncCreatable
		    {...field.bind()}
		    multi
		    valueKey="id"
		    labelKey="name"
		    resetValue={[]}
		    searchable={true}
		    clearable={true}
		    loadOptions={this.getTags}
		/>
    )
  }
}

bvallant avatar May 10 '17 14:05 bvallant

You get hasNestedFields: false when definining the fields value because if the prop value is an object it is treated just as the field value, and not values for nested fields (which are not defined as strcuture into the definition because you are using the Unified Mode). Differently is the use of the update method which its purpose is to recreate a nested fields tree providing an object in input (where each key is a field key).

foxhound87 avatar May 10 '17 20:05 foxhound87

Anyway consider to use add() instead of update() to deal with arrays:

    this.$('tags').add({
      id: 'x',
      name: 'y',
    });

or use the Separated Mode which supports complex arrays structures.

foxhound87 avatar May 10 '17 21:05 foxhound87

related to #197

foxhound87 avatar May 10 '17 21:05 foxhound87

So I tried to switch to Seperated Mode:

const fields = ['tags', 'tags[]', 'tags[].id', 'tags[].name']

But this doesnt work for me at all (and hasNestedFields being always true now). Anything else to consider?

Besides thanks for your help and I think that this is still a very useful library though the documentation on these modes could be a bit better (it's pretty hard to find out why they exist in the first place and when to use which...).

bvallant avatar May 11 '17 09:05 bvallant

Can you post the whole code of the form using the separated mode?

foxhound87 avatar May 11 '17 13:05 foxhound87

I've created a Gist with the relevant code. Note that loading data works as expected and gets displayed the right way but modifying it isnt working. I'm wondering why the Mulitselect in the Company Simple example works flawlessly though...

bvallant avatar May 11 '17 14:05 bvallant

The gist seems ok, the issue could be in the onChange event handler, the default one cannot be enough so you should provide a custom one to manipulate the data edited by the react-select component.

foxhound87 avatar May 11 '17 16:05 foxhound87

Did you see this example for the custom onChange?

https://github.com/foxhound87/mobx-react-form-demo/blob/master/src/components/forms/FormDynamicFieldsSelect.jsx#L10

foxhound87 avatar May 11 '17 16:05 foxhound87

Thanks for your help again, after some poking around I came up with this onChange handler:

handleChange(e) {
  const {field} = this.props
  // clear all "old" values 
  // field.clear() / .reset() didnt work for me 
  // so clearing the mobx array myself
  field.fields.clear()

   // add the currently selected values
   e.map(v => field.add(v));
}

This basically works now for me, thanks for poinint me to the onChange thing.

bvallant avatar May 12 '17 10:05 bvallant

I've updated the package introducing the struct prop.

Take a look here: https://github.com/foxhound87/mobx-react-form/blob/master/tests/data/forms/fixes/form.q1.js

foxhound87 avatar Apr 17 '18 17:04 foxhound87