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 in ArrayInput doesn't update #4805

Closed
gpietro opened this issue May 13, 2020 · 6 comments
Closed

defaultValue in ArrayInput doesn't update #4805

gpietro opened this issue May 13, 2020 · 6 comments
Labels

Comments

@gpietro
Copy link

gpietro commented May 13, 2020

What you were expecting:
On ArrayInput for each TextInput of type id, I expect with the following:

<ArrayInput
    source="backlinks"
    defaultValue={backlinksDefaultValue}
    validate={[required()]}
>
    <SimpleFormIterator>
        <TextInput
              disable
              label="id"
              source="id"
              defaultValue={React.useMemo(() => v4(), [])}
        />
        <DateInput source="date" label="date" />
        <TextInput source="url" label="url" />
    </SimpleFormIterator>
</ArrayInput>

to get a different defaultValue for each element.

What happened instead:
All the elements have the same defaultValue

Steps to reproduce:
https://codesandbox.io/s/intelligent-moser-bwvn7

Create a new post -> add Backlinks

@fzaninotto
Copy link
Member

fzaninotto commented May 13, 2020

That's an interesting feature, and SimpleFormIterator doesn't support it indeed, as it clones the Input components with their props, and at that time, the defaultValue is already evaluated.

We probably need support for this feature by the underlying library, react-final-form-arrays, before we can implement it in react-admin though.

In the meantime, you can switch SimpleFormIterator with your own component.

@panfiva
Copy link

panfiva commented May 13, 2020

I think this issue is the same as #4709

I have tested react-final-form-arrays and it supports default values for child inputs; however default values are removed by useInitializeFormWithRecord function, which is called from FormView function at https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/form/FormWithRedirect.tsx

@fzaninotto
Copy link
Member

It's a complementary issue to #4709: even if default values worked inside ArrayInput, there would be no way to have a different default value for each item in the set.

@panfiva
Copy link

panfiva commented May 13, 2020

@fzaninotto, to make sure that default / initial values of array's child elements are supported, we need to somehow exclude array element from function call:

However, we cannot just exclude array elements from the call since such call are needed in many uses cases. As far as I can see, we have 2 options:

  • Option 1: Somehow read form initial values on both array and on child fields, and apply them (array first, then child fields later); however, i do not think final-form and react-final-form provide us with a method to query initial values configured on fields.
  • Option 2: stop using useInitializeFormWithRecord; This will prevent form refreshes from working properly. This can be fixed by forcing form destruction and then rendering a brand new copy of a form: for example, after a user click refresh, FormWithRedirect should return empty div and then we should trigger another form refresh that returns a form again.

@Maybach91
Copy link

Maybach91 commented Mar 10, 2021

In the meantime, you can switch SimpleFormIterator for your own component.

@fzaninotto do you have an example how an alternative / own component could look like to have updated default values for each item?

@fzaninotto
Copy link
Member

With the release of react-admin v5, react-admin v3 has reached its end of life. We won't fix bugs or make any new release on the 3.x branch. We recommend that you switch to a more recent version of react-admin.

So I'm closing this issue as we won't fix it.

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

No branches or pull requests

5 participants