react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

ArrayInput usage with a flat array of strings

Open tmarcinkowski-logitech opened this issue 4 years ago • 3 comments

My API returns an array of strings instead of objects. ArrayInput misses parse and transform props, so I figured out that I'd wrap ArrayInput and prepare the record beforehand. It generally works but reveals either buggy or undocumented behaviour of the component.

What you were expecting: ArrayInput, after giving the record in form of

{
  groups: [{ value: 'group1' }]
}

would store the value in the same form when obtained by e.g. FormDataConsumer, a form validator or data provider.

What happened instead: The value obtained from ArrayInput looks like this:

{
  groups: [{
    "0": "g",
    "1": "r",
    "2": "o",
    "3": "u",
    "4": "p",
    "5": "1",
    "value": "group1"
  }]
}

Steps to reproduce:

Wrap ArrayInput with a component and modify record prop before passing it further. Provide a non-empty record with a flat array of strings under the source key.

BTW only modifying the prop with useEffect works! I've tried copying the record via useState or useMemo and ArrayInput does not display the data (even though the record passed to the component looks properly as one can observe in React DevTools). Seems like a totally separate bug.

Related code:

https://codesandbox.io/s/jolly-stonebraker-qq75u?file=/src/users/FlatArrayInput.js

Enter a list of users, click the first one to edit it and press "Next".

Other information:

The values scattered over the object (0: "g", 1: "r", ....) are initial values. When you change the value in the form, it'll be put into value key, but the other keys will remain.

Environment

  • React-admin version: 3.19.3
  • Last version that did not exhibit the issue (if applicable):
  • React version: 17.0.2
  • Browser:
  • Stack trace (in case of a JS error):

tmarcinkowski-logitech avatar Jan 20 '22 13:01 tmarcinkowski-logitech

I cannot reproduce the bug in your code sandbox if I upgrade to the latest version of react / react-admin (3.19.7)

bug

Please confirm if upgrading solves it for you

WiXSL avatar Jan 25 '22 14:01 WiXSL

Apologies for not replying for so long. I've tested this right away and can confirm that updating to 3.19.7 solved my issue.

The issue regarding passing a copy of the record still stands though.

tmarcinkowski-logitech avatar Feb 03 '22 16:02 tmarcinkowski-logitech

Ok. Please, for a secondary problem, open a new issue following the issue template.

WiXSL avatar Feb 03 '22 16:02 WiXSL