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

TextInput defaultValue with a function does not work #5354

Closed
edulix opened this issue Oct 6, 2020 · 2 comments · Fixed by #5387
Closed

TextInput defaultValue with a function does not work #5354

edulix opened this issue Oct 6, 2020 · 2 comments · Fixed by #5387
Assignees

Comments

@edulix
Copy link
Contributor

edulix commented Oct 6, 2020

What you were expecting:
Setting a defaultValue={() => uuid()} to a TextInput should work. I'm creating an UuuidInput field, that should by default provide a new uuid. It works if the SimpleForm sets an initialValues function, but that's not what I need.

What happened instead:
The TextInput appears empty inside the SimpleForm.

Steps to reproduce:
Use a SimpleForm with a TextInput

Related code:
Take a look at https://codesandbox.io/s/intelligent-bartik-km7ed?file=/src/posts/PostCreate.js

I modified the forked default code sandbox so that he create post form has a title TextInput field with a string defaultValue and teaser field has a functional defaultValue property. The title works, the teaser field appears empty:

        <TextInput autoFocus source="title" defaultValue="static value" />
        <TextInput
          source="teaser"
          defaultValue={() => "value from a function"}
        />

Environment

  • React-admin version:3.9.0
  • Last version that did not exhibit the issue (if applicable): -
  • React version: 16.9.0
  • Browser: chrome
  • Stack trace (in case of a JS error): -
@fzaninotto fzaninotto added the bug label Oct 8, 2020
@fzaninotto
Copy link
Member

fzaninotto commented Oct 8, 2020

Reproduced, thanks.

Looking at the react-admin code, this regression seems to be caused by the FormWithRedirect refactoring this regression was caused by the move from redux-form to react-final-form (in 306aef5, released in 3.0, more than a year ago). However, reintroducing this feature (which was introduced by #277 and is documented in CreateEdit.md#per-input-default-value) isn't trivial, and besides it never really worked: we had to inspect Form children, which was never robust.

I think this should be a react-final-form feature, and making it work outside of that lib is too risky.

So I think what we should do here is fix the documentation to remove the ability to pass a custom function as default value.

In your case, you can use the form initialValues to set the initial value for one field at the form level.

@alextaymx

This comment has been minimized.

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

Successfully merging a pull request may close this issue.

3 participants