React-admin: defaultValue of Inputs inside ArrayInput not working

Created on 31 Oct 2018  路  15Comments  路  Source: marmelab/react-admin

What you were expecting:
I set defaultValue of Inputs inside an ArrayInput and expect that when I add a new element it sets the default value. For example, if I have an array of posts each one with and id, title, created_at, description, and set the defaultValue of created_at DateInput to Date.now(), I expect that everytime I add a new post the created_at will be Date.now()

What happened instead:
It doesn't work

Related code:
Forked from simple demo, edit a post and try to add a new backlink in the miscellaneous tab.
https://codesandbox.io/s/k06x14xy8v

Environment

  • React-admin version: 2.4.1
  • React version: ^16.6.0
  • Browser: Chrome
bug

Most helpful comment

It might be fixed in v3 as we migrate from redux-form to react-final-form. I'll check when I'll have the time

All 15 comments

Confirmed and reproduced. That's because defaultValues are currently only set at one level:

// in ra-core/form/getDefaultValues.js
const getDefaultValues = (data = {}, defaultValue = {}, defaultValues = {}) => {
    const globalDefaultValue =
        typeof defaultValue === 'function' ? defaultValue() : defaultValue;
    return { ...globalDefaultValue, ...defaultValues, ...data };
};

Let's say a record has sub elements, as follows:

{
  id: 123,
  foos: [
    { bar: 'baz' }
 ]
}

Given there is an ArrayInput written as:

<ArrayInput>
  <SimpleFormIterator>
    <TextInput source="bar" defaultValue="hello" />
  </SimpleFormIterator>
</ArrayInput>

When a user add a foo by way of ArrayInput, the default value of foos is set to:

[
    { bar: 'baz' },
    { bar: 'hello' }
]

but since foos already has a value, this default value is never used...

@fzaninotto So is this an issue that we should raise in redux-form repo? Or we should investigate how to do this with react-admin code? Maybe iterate on the fileds upon the addition of a new item and add the default values to it too?

react-admin does its own default value voodoo to overcome the redux-form limitations in this field. the solution must be found in react-admin.

One point to add - I found that it's not working on Edit view, but works on Create page. But works only on first element in list)

We're experiencing issues with default value of NumberInput and TextInput inside ArrayInput and SimpleFormIterator, we're on version 2.8.1 of react-admin. The default values work the first time you add a record, but default value doesn't show for subsequent record.

Any updates on when this issue will be resolved?

It might be fixed in v3 as we migrate from redux-form to react-final-form. I'll check when I'll have the time

ping @djhi

can we get a status update on this? I don't currently see this ticket in the v3 roadmap.

@nosequeldeebee I found this fixed till 3.0.0-alpha.2

Still happening to me on v3.1.2

<ArrayInput source="tags">
   <SimpleFormIterator>
      <TextInput label="Tag" />
   </SimpleFormIterator>
</ArrayInput>

is there a way to fix it?, it keeps adding [object Object] as default value (to reproduce it you have to omit source attribute, this is an array of strings, I want ['tag1', 'tag2'] as result)

image

Environment

React-admin version: 3.1.2
React version: 16.12.0
Browser: Firefox developer edition 73.0b2 (64-bit) // Google Chrome 79.0.3945.117 (Official Build) (64-bit)

Thank you!

Any solution on this?

I just did a fix for my issue #4306 , it's related to this bug, but it's not 100% this bug, please give me your feedback and see if we could fix this issue. Thanks!

A simple workaround () for now while awaiting the above fix to be merged is to format the source as below format={v=> { return v instanceof Object ? '' : v;}} . You can the apply it to an inner input component.

<ArrayInput source="subCategories" label='Sub Categories'> <SimpleFormIterator> <TextInput format={v=> { return v instanceof Object ? '' : v;}} /> </SimpleFormIterator> </ArrayInput>

Issues are fixed in https://github.com/marmelab/react-admin/pull/4394
(ra 3.2)
(both object Object and defaultValues are ok)

Still happening to me on v3.1.2

<ArrayInput source="tags">
   <SimpleFormIterator>
      <TextInput label="Tag" />
   </SimpleFormIterator>
</ArrayInput>

is there a way to fix it?, it keeps adding [object Object] as default value (to reproduce it you have to omit source attribute, this is an array of strings, I want ['tag1', 'tag2'] as result)

image

Environment

React-admin version: 3.1.2
React version: 16.12.0
Browser: Firefox developer edition 73.0b2 (64-bit) // Google Chrome 79.0.3945.117 (Official Build) (64-bit)

Thank you!

If you want empty string instead of [object Object],
you can add defaultValue="" in the TextInput.
maybe there is empty string, not [object Object]

Was this page helpful?
0 / 5 - 0 ratings