h2qutc / angular-material-components

Angular Material Library provide extra components for every project
https://h2qutc.github.io/angular-material-components/
MIT License
330 stars 166 forks source link

DateTimePicker Luxon adapter impossible? #218

Open mj-jackson opened 3 years ago

mj-jackson commented 3 years ago

Since moment js itself lists points why not to use it anymore we decided to go with luxon for this project I am working on.

I searched but I feel there is no NgxMatLuxonDateModule yet so I tried to write my own luxon adapter based on the luxon adapter of the MatDatePicker. You can implement an abstract class in typescript so I did the following: export class MyDateAdapter extends LuxonDateAdapter implements NgxMatDateAdapter<DateTime> {}

Having all the functionality of the LuxonDateAdapter only to extend it to match the NgxMatDateAdapter. But I am having trouble setting the new time.

Since the NgxMatDateAdapter uses setters which seem to count on a mutable object-reference DATE I cannot update the Luxon DateTime object since it is immutable and the DateTime.set method always returns a newly constructed object.

  setHour(date: DateTime, value: number): void {
    date.set({hour: value}); // returns new object, does not modify the old one

    date = date.set({hour: value}); // new reference? does not work either

    date.hour = value; // ERROR TypeError: setting getter-only property "hour"
  }

Am I missing something?

imario42 commented 3 years ago

I am having the same issue when using js-joda. Seems there is already a patch for it which has not yet been accepted, though. https://github.com/h2qutc/angular-material-components/pull/165

NorseJedi commented 3 years ago

I wrote my own Luxon adapter using the moment.js adapter as a starting point, and it seems to be working as intended. It's not published in npm (yet), but you can find the code here: https://github.com/ensnared/luxon-datetime-adapter

To answer your question though, you can set the values in Luxon but you have to access the internal properties directly, like this:

setHour(date: DateTime, value: number): void {
    date['c'].hour = value;
}

setMinute(date: DateTime, value: number): void {
    date['c'].minute = value;
}

setSecond(date: DateTime, value: number): void {
    date['c'].second = value;
}
alexaka1 commented 1 year ago

Bump. The API expects a mutable date, but Luxon's dates are immutable by design. DateTime reference

Since both the native and Moment adapters are not recommended, this is really hampering the usability of this library.

KelseySheely commented 1 year ago

I forked this a while back to allow for luxon compatibility. It isn't up to date w/ Angular Material 15 yet.

https://github.com/KelseySheely/angular-material-components