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

[pickers] Fix AdapterMomentJalaali regression #13144

Merged
merged 8 commits into from
May 16, 2024

Conversation

LukasTy
Copy link
Member

@LukasTy LukasTy commented May 15, 2024

Fixes #12794

Add a docs example with moment-jalaali.

The isSameYear and especially the isSameMonth methods seemed incorrect.
Replaced with this approach:
https://github.com/dmtrKovalenko/date-io/blob/634ae738fa70545ad5fa954861a5e5498b4affc6/packages/jalaali/src/jalaali-utils.ts#L229-L235

The culprit of the regression is: https://github.com/mui/mui-x/pull/8789/files#diff-de14e1bc65d9c2f3084b3e07420b775d6bdcd1fb6b2538dc02745ab862b4a998R185-R189

@LukasTy LukasTy added regression A bug, but worse l10n localization component: pickers This is the name of the generic UI component, not the React module! labels May 15, 2024
@LukasTy LukasTy self-assigned this May 15, 2024
@@ -766,6 +766,17 @@ async function initializeEnvironment(
// expect(await status.isVisible()).to.equal(true);
// expect(await status.textContent()).to.equal('Submitted: 04/17/2022');
// });

it('should correctly select a day in a calendar with "AdapterMomentJalaali"', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to make a unit test work in a reasonable amount of time. 🙈

// `isSame` seems to mutate the date on `moment-jalaali`
// @ts-ignore
return value.clone().isSame(comparing, 'jMonth');
return value.jYear() === comparing.jYear() && value.jMonth() === comparing.jMonth();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main problem that was causing the behavior, because it is called here:

const needMonthSwitch =
action.focusedDay != null &&
!disableSwitchToMonthOnDayFocus &&
!utils.isSameMonth(state.currentMonth, action.focusedDay);

And it caused the focus to be changed to the first day of the current month.

@LukasTy LukasTy added the needs cherry-pick The PR should be cherry-picked to master after merge label May 15, 2024
@mui-bot
Copy link

mui-bot commented May 15, 2024

Copy link
Member

@flaviendelangle flaviendelangle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With those methods I would be very careful with timezone
Could you check if we have tests on those adapter methods for edge cases where you are in the same year when converted to the same timezone but different years in each date's original timezone?

@LukasTy
Copy link
Member Author

LukasTy commented May 15, 2024

With those methods I would be very careful with timezone
Could you check if we have tests on those adapter methods for edge cases where you are in the same year when converted to the same timezone but different years in each date's original timezone?

@flaviendelangle This particular adapter is not timezone compatible. 🤔

@flaviendelangle
Copy link
Member

Right...
I'm slightly uncomfortable to add a behavior that is not timezone compatible (even if it's unlikely that we will ever add timezone support on it, I don't think we could use moment-timezone and momentj-lalaali together) and to diverge from the AdapterMoment logic (which uses value.isSame)

But that's probably not important indeed 😆

<LocalizationProvider dateAdapter={AdapterMomentJalaali} adapterLocale="fa">
<DateTimePicker
label="AdapterMomentJalaali"
defaultValue={moment('2022-02-01T12:00:00')}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date-fns-jalaali defaults to 12hr clock, whereas moment-jalaali to 24hr clock.
I set both to mid-day to at least have them show 12:00.
Do you have better suggestions?
Maybe force ampm to false or true on both? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if adding an exception to our AMPM management to fix this inconsistency is worth it
I think your test is nice and we can keep the behavior as it is

@LukasTy LukasTy requested review from flaviendelangle, michelengelen and a team May 15, 2024 16:51
@LukasTy
Copy link
Member Author

LukasTy commented May 15, 2024

The global moment locale is causing a headache in docs...
Calling moment.loadPersian in the added demo causes other demos using moment to receive the Persian locale.
Screenshot 2024-05-15 at 20 00 27

There are a couple of options:

  1. Don't call moment.loadPersian, which would change the demo to use Farsi symbols:
    Screenshot 2024-05-15 at 20 03 47
  2. Call moment.locale('en') in MomentTimezone and MomentUTC demos to avoid the unwanted locale.

Both methods are not the prettiest and I'm really not sure which one would be the least offensive. 🤔
However, I'm leaning towards the 1st option.

Edit

Forget option 1, the global moment locale does still bleed into other demos... 😱 🤯
I've gone with 2nd option, let me know if you have better suggestions. 🙏

@flaviendelangle
Copy link
Member

Call moment.locale('en') in MomentTimezone and MomentUTC demos to avoid the unwanted locale.

Does it really work if the component re-renders?

@LukasTy
Copy link
Member Author

LukasTy commented May 16, 2024

Does it really work if the component re-renders?

@flaviendelangle I've tried going back and forth multiple times between the demo pages (jalali and Moment UTC) and changing the demos to trigger some issue, but I couldn't do it... 🤔 🤷
Were you able to produce an issue? 🤔

I would expect problems if those demos were on the same page, but it seems "fine" in the current state. 🙈 🤷

P.S. Only after ca1e423 commit argos (test:regressions) is finally happy and no longer detects those previously seen problems. 😉

@flaviendelangle
Copy link
Member

Were you able to produce an issue? 🤔

No
It's quite fragile and might not work fine if we decide to merge some doc pages, but I don't have a better solution :/

@LukasTy
Copy link
Member Author

LukasTy commented May 16, 2024

It's quite fragile and might not work fine if we decide to merge some doc pages, but I don't have a better solution :/

I'm quite unhappy with the solution as well, but on the other hand, if we have problems—we can reconsider having this demo or moving it somewhere else.
It could also become a live example with code preview or something like that. 🤔

@LukasTy LukasTy merged commit 40bfc8d into mui:master May 16, 2024
17 checks passed
@LukasTy LukasTy deleted the fix-moment-jalaali-adapter branch May 16, 2024 09:06
arthurbalduini pushed a commit to arthurbalduini/mui-x that referenced this pull request May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: pickers This is the name of the generic UI component, not the React module! l10n localization needs cherry-pick The PR should be cherry-picked to master after merge regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pickers] AdapterMomentJalaali breaks when selecting date
3 participants