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

[DateInput] defaultValue does not override value from record #10474

Open
ogroppo opened this issue Jan 26, 2025 · 5 comments
Open

[DateInput] defaultValue does not override value from record #10474

ogroppo opened this issue Jan 26, 2025 · 5 comments

Comments

@ogroppo
Copy link

ogroppo commented Jan 26, 2025

What you were expecting:
if the value of the field from dataprovider is A and defaultValue is B - I expect B to override A

What happened instead:
Value from data provider always wins.

Steps to reproduce:
Follow the examples from jsDoc

 * @example
 * // If the initial value string contains more than a date (e.g. an hour, a timezone),
 * // these details are ignored.
 * <DateInput source="published_at" defaultValue="2021-09-11T20:46:20.000-04:00" />
 * // The input will display '2021-09-11' whatever the browser timezone.
 *
 * @example
 * // If the initial value is a Date object, DateInput converts it to a string
 * // and ignores the timezone.
 * <DateInput source="published_at" defaultValue={new Date("2021-09-11T20:46:20.000-04:00")} />
 * // The input will display '2021-09-11' whatever the browser timezone.
 *

source always wins over defaultValue

CODE
Image
RESULT (data from source)
Image

Environment

  • React-admin version: 5.4.4
  • React version: 18.3.1
  • Browser: Chrome
@ogroppo
Copy link
Author

ogroppo commented Jan 26, 2025

Also, slightly related. The documentation is misleading on page https://marmelab.com/react-admin/DateInput.html

Where it says

Note: This conversion may change the date because of timezones. For example, the date string '2022-04-30T00:00:00.000Z' in Europe may be displayed as '2022-04-29' in Honolulu. If this is not what you want, pass your own [parse](https://marmelab.com/react-admin/Inputs.html#parse) function to <DateInput>.

That should replaced by

 * // If the initial value is a Date object, DateInput converts it to a string
 * // and ignores the timezone.

like the js docs, because the parse prop is for the output not the input

@djhi
Copy link
Collaborator

djhi commented Jan 27, 2025

Hi,

if the value of the field from dataprovider is A and defaultValue is B - I expect B to override A

That's not how it works. Default values are only used when the value from the record is undefined.

Regarding the issue documentation, can you open a PR to fix it?

@ogroppo
Copy link
Author

ogroppo commented Jan 27, 2025

Default values are only used when the value from the record is undefined.

Ok gotcha, what do you recommend to manipulate the value from record? depending on the answer, I can raise a PR for the docs no problem 👍🏻

@djhi
Copy link
Collaborator

djhi commented Jan 27, 2025

It depends on your use case. Can you explain what you're trying to achieve?

@ogroppo
Copy link
Author

ogroppo commented Jan 27, 2025

It's the answer to this question/problem of the docs:

Note: This conversion may change the date because of timezones. For example, the date string '2022-04-30T00:00:00.000Z' in Europe may be displayed as '2022-04-29' in Honolulu.

Currently the answer is

If this is not what you want, pass your own [parse](https://marmelab.com/react-admin/Inputs.html#parse) function to <DateInput>.

But parse is to manipulate the value on change, not the value incoming from the record on initial display.
Which means that any timezone with negative offset (i.e. Honolulu) will see the day before, if the day is stored as midnight UTC (like the example 2022-04-30T00:00:00.000Z).

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

No branches or pull requests

2 participants