Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaultValue of Inputs inside ArrayInput not working #2489

Closed
andressscamacho opened this issue Oct 31, 2018 · 15 comments · Fixed by #4394
Closed

defaultValue of Inputs inside ArrayInput not working #2489

andressscamacho opened this issue Oct 31, 2018 · 15 comments · Fixed by #4394
Labels

Comments

@andressscamacho
Copy link

andressscamacho commented Oct 31, 2018

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
@fzaninotto fzaninotto added the bug label Nov 5, 2018
@fzaninotto
Copy link
Member

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...

@afilp
Copy link
Contributor

afilp commented Dec 1, 2018

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

@fzaninotto
Copy link
Member

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.

@tusmenko
Copy link

tusmenko commented Jun 12, 2019

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)

@brian-isenpai
Copy link

brian-isenpai commented Aug 1, 2019

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?

@djhi
Copy link
Collaborator

djhi commented Aug 1, 2019

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

@fzaninotto
Copy link
Member

ping @djhi

@nosequeldeebee
Copy link

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

@tusmenko
Copy link

tusmenko commented Oct 1, 2019

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

@gvillo
Copy link
Contributor

gvillo commented Jan 14, 2020

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!

@nkigen
Copy link

nkigen commented Jan 15, 2020

Any solution on this?

@gvillo
Copy link
Contributor

gvillo commented Jan 16, 2020

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!

@nkigen
Copy link

nkigen commented Jan 17, 2020

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>

@JulienMattiussi
Copy link
Contributor

JulienMattiussi commented Mar 4, 2020

Issues are fixed in #4394
(ra 3.2)
(both object Object and defaultValues are ok)

@Jongmun-Park
Copy link

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.