-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[pickers] Add support for @js-joda/core
#4703
Comments
@js-joda/core
Transfering this issue because it relates to a comment in the code that we should fix. |
Interested in this as well |
I think this util method mergeDateAndTime is being misused throughout this codebase. Often it seems to be being used in an effort to reconcile a previous value with a new value, but from what it is doing it seems to be intended to merge a date value and a time value as the name would imply. I am getting a console error on a similar call in the DesktopTimePicker using AdapterDateFns when the input is cleared. mergeDateAndTime is then called with my previous value and a null new value and it breaks. Maybe this deserves a new bug? Edit: looks like bumping some versions may have helped. My yarn lock now has changes including a date-fns bump and my issue has disappeared...leaving this here in case others find this later... |
Yes the problem here was indeed that we were trying to reconcile a null date with a time. |
If I understand correctly: js-joda is fairly unique in having separate data types for dates, times, and dates+times. The @date-io project doesn't seem to have any specific APIs to handle this (although they do invite suggestions for new methods). MUI-X uses I'm experimenting with the following modified adapter. I've done very little, but it seems to work so far. import AdapterJoda from '@date-io/js-joda';
import { Temporal, TemporalQueries } from '@js-joda/core';
export class MuiAdapterJoda extends AdapterJoda {
mergeDateAndTime(date: Temporal, time: Temporal): Temporal {
const datePart = date.query(TemporalQueries.localDate());
const timePart = time.query(TemporalQueries.localTime());
if (datePart && timePart) {
// This gives the same result as @date-io's implementation's
// "LocalDate.from(date).atTime(LocalTime.from(time))"
return datePart.atTime(timePart);
} else if (!datePart) {
// E.g., if MUI-X's TimePicker wants to preserve the date part, but the
// user just gave a LocalTime.
return time;
} else if (!timePart) {
// E.g., if MUI-X's CalendarPicker wants to preserve the time part, but
// the user just gave a LocalDate:
return date;
} else {
// Fall back to @date-io's implementation, which will throw due to null
// date and time.
return super.mergeDateAndTime(date, time);
}
}
}
const App = () => <LocalizationProvider dateAdapter={MuiAdapterJoda}><Root /></LocalizationProvider>; |
Thanks for your experimentation |
Thanks, @flaviendelangle. As I understand it: js-joda offers several date/time options that other libraries don't (the ability to explicitly work with dates without times, or times without dates; the ability to work in local dates/times, since UTC is not a silver bullet; etc.). This is (almost?) unique among JavaScript date/time libraries, which complicates the task of an abstraction library like @date-io. E.g., to what degree should @date-io offer APIs that allow access to js-joda's more specialized functionality, vs. targeting the lowest common denominator? If @date-io does decide to offer access to more specialized functionality, then what's the best way to ensure that its API is sufficiently generic and abstract, given that js-joda is the only one of its kind and there doesn't seem to be strong demand for accessing this more specialized functionality? Etc. And so I start to think it would be better to not modify @date-io's API until there are broader and more concrete use cases. So, for Since that is an alternate, more specialized API, and since I thought it would be better to limit changes to @date-io's own API, I thought it might be better to extend the @date-io adapter within the mui-x package (similar to what I did here), or to at least discuss here before opening a PR in @date-io. If you'd like me to submit this as for discussion within @date-io, I'm happy to do so. Thanks. |
Right now, we are not advertising for people to create their own adapter, mainly because it would widen the scope of what is a "breaking change". We currently allow ourselves to add methods to adapters and use them on the pickers on a minor release. |
@flaviendelangle Fair point. Then maybe we could do two things:
|
I think we could improve the doc to say that we are willing to help anyone wanting to build an adapter and host it on our repo. There is a cost to maintaining adapters, but I doubt dozens of people will create adapters for an unknown date library. That's basically your 2. |
But even with Joda, it has 1.3% (=3.4/(71+71+24+88+3.4)) of the market share. It feels like we would be better off by spending time on something else, e.g. to build an upload component than efforts to support it. |
I'm biased, because I'm one of the folks hoping for js-joda support. But a few potential considerations:
I'm happy to build and submit an adapter (as I tried with #8438 - I had failed to realize that @mui/x-date-pickers had finished its migration away from date-io, and I haven't had time to revisit), if that's what the project wants. I also understand if the project decides to not support js-joda directly - I'll likely end up creating my own unstable + unsupported interface in that case (which is more or less what I've been doing with the code that #8438 is based off of). I'll share a link to that here if I end up going that route. Thank you. |
See mui/mui-x#4703 (comment) (although note that mui-x no longer uses date-io)
…eAndTime method (#653) * Add support for date-only and time-only to js-joda See mui/mui-x#4703 (comment) (although note that mui-x no longer uses date-io) * Simplify code and address code coverage
Is there a current solution or workaround for using js-joda with DatePickers? It sounds like I am forced to use one of the other adapters, and then convert manually, is that correct? |
It is correct 👍 |
I've pushed an updated version of my js-joda adapter for MUI at #8438. See comments there for current status. |
Thank you @joshkel. I've put this issue back into "Needs grooming" so the team could re-evaluate the potential of adding this adapter into the codebase. 😉 |
Current Behavior 😯
Currently when using a
DatePicker
in MUI v5 with JsJodaAdapter (from @date-io/js-joda) an uncaught exception is thrown when changing date. The exception is:DateTimeException: Unable to obtain LocalTime TemporalAccessor: 2021-10-06, type LocalDate
.Expected Behavior 🤔
The picker does not crash and the new value is passed to the
onChange
function.Steps to Reproduce 🕹
Sandbox link
Context 🔦
The curlpit seems to be on this line, when calling
utils.mergeDateAndTime
Your Environment 🌎
`npx @mui/envinfo`
Don't know why it doesn't get detected, but I'm using Chrome 94.0.4606.71
The text was updated successfully, but these errors were encountered: