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

DateField: initialValue not working when value is null #7132

Closed
thdk opened this issue Jan 24, 2022 · 3 comments · Fixed by #7139
Closed

DateField: initialValue not working when value is null #7132

thdk opened this issue Jan 24, 2022 · 3 comments · Fixed by #7139

Comments

@thdk
Copy link
Contributor

thdk commented Jan 24, 2022

What you were expecting:
The DateInput component should use the value provided by the initialValue prop when the value is null or undefined.

As state in the docs for the DateInput component:

Prop Required Type Default Description
initialValue Optional mixed - Value to be set when the property is null or undefined

What happened instead:
When the value is null the DateInput ignores the value provided by the initialValue prop.
It does work however when the value is undefined the DateInput component correctly shows the value provided by the initialValue prop.

Steps to reproduce:

Related code:
I have forked the example sandbox and modified the data of the first two posts.
The first post now has null for date published.
The second post now has undefined for date published.

Next, I have adjusted the edit form for a post so that the published_at value now has an initialValue prop with a date.

<DateInput
            source="published_at"
            options={{ locale: "pt" }}
            initialValue={new Date(2100, 0, 1)}
 />

See the sandbox: https://codesandbox.io/s/compassionate-bush-s772t
Click the first post and open the misc. tab. See the value of the published field.
Expected: 01/01/2100
Actual: mm/dd/yyy

Click the second post and open the misc. tab. See the value of the published field.
Expected: 01/01/2100
Actual: 01/01/2100

Other information:

Environment

  • React-admin version: 3.19.7
  • Last version that did not exhibit the issue (if applicable): I believe 3.19.5
  • React version: 17.0.0
  • Browser: Version 97.0.4692.71 (Official Build) (64-bit)
  • Stack trace (in case of a JS error): n/a

I have traced down the source code to the following line:

initialValue: get(record, source, initialValue),

Where lodash get method is used to get the value for a given property from the record object. However, this only returns the defaultValue if the actual value is 'undefined'. See docs for lodash get method.

@fzaninotto
Copy link
Member

null and undefined don't mean the same thing in JS. We should only apply defaultValue if the value is undefined.

@WiXSL
Copy link
Contributor

WiXSL commented Jan 25, 2022

null and undefined don't mean the same thing in JS. We should only apply defaultValue if the value is undefined.

Of course, but as the bug report suggests, the documentation explicitly says Value to be set when the property is null or undefined
Maybe this can be taken as a documentation issue.

@fzaninotto
Copy link
Member

yes, this is a documentation issue.

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