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

defaultValue of Inputs inside ArrayInput not working

Open andressscamacho opened this issue 8 years ago • 15 comments

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

andressscamacho avatar Oct 31 '18 01:10 andressscamacho

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 avatar Nov 05 '18 22:11 fzaninotto

@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?

afilp avatar Dec 01 '18 09:12 afilp

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.

fzaninotto avatar Dec 12 '18 17:12 fzaninotto

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)

tusmenko avatar Jun 12 '19 20:06 tusmenko

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?

brian-isenpai avatar Aug 01 '19 12:08 brian-isenpai

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

djhi avatar Aug 01 '19 12:08 djhi

ping @djhi

fzaninotto avatar Sep 17 '19 14:09 fzaninotto

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

nosequeldeebee avatar Oct 01 '19 00:10 nosequeldeebee

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

tusmenko avatar Oct 01 '19 09:10 tusmenko

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!

gvillo avatar Jan 14 '20 20:01 gvillo

Any solution on this?

nkigen avatar Jan 15 '20 12:01 nkigen

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!

gvillo avatar Jan 16 '20 23:01 gvillo

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>

nkigen avatar Jan 17 '20 00:01 nkigen

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

JulienMattiussi avatar Mar 04 '20 16:03 JulienMattiussi

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]

Jongmun-Park avatar Jul 17 '20 06:07 Jongmun-Park