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] Allow specifying the current year in <MonthCalendar /> #8852

Closed
2 tasks done
githorse opened this issue May 3, 2023 · 5 comments · Fixed by #9260
Closed
2 tasks done

[pickers] Allow specifying the current year in <MonthCalendar /> #8852

githorse opened this issue May 3, 2023 · 5 comments · Fixed by #9260
Labels
component: pickers This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature

Comments

@githorse
Copy link

githorse commented May 3, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

Is there some way to change the year on the MonthCalendar? Without it the component is pretty unusable without extra effort to build a year picker around it. Or maybe I'm unclear on how the component is intended to be used -- could someone provide an example?

Examples 🌈

No response

Motivation 🔦

I'm attempting to build a custom modal date range picker around MUI components, but I can't use the MonthPicker without a way to adjust the year on it.

Order ID 💳 (optional)

46840

@githorse githorse added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 3, 2023
@alexfauquette
Copy link
Member

This issue is similar to #8837

The MonthCalendar would need a new prop similar to currentMonth. Something like that should do the job

--- a/packages/x-date-pickers/src/MonthCalendar/MonthCalendar.tsx
+++ b/packages/x-date-pickers/src/MonthCalendar/MonthCalendar.tsx
@@ -77,6 +77,7 @@ export const MonthCalendar = React.forwardRef(function MonthCalendar<TDate>(
   const {
     className,
     value: valueProp,
+    currentMonth,
     defaultValue,
     disabled,
     disableFuture,
@@ -108,8 +109,8 @@ export const MonthCalendar = React.forwardRef(function MonthCalendar<TDate>(
   const todayMonth = React.useMemo(() => utils.getMonth(now), [utils, now]);
 
   const selectedDateOrStartOfMonth = React.useMemo(
-    () => value ?? utils.startOfMonth(now),
-    [now, utils, value],
+    () => value ?? currentMonth ?? utils.startOfMonth(now),
+    [now, currentMonth, utils, value],
   );
 
   const selectedMonth = React.useMemo(() => {

@alexfauquette alexfauquette added component: pickers This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 4, 2023
@alexfauquette alexfauquette changed the title allow toggling the year of the MonthCalendar [pickers] Allow specifying the current year in <MonthCalendar /> May 4, 2023
@flaviendelangle
Copy link
Member

currentMonth or referenceValue is we go for this comment solution: #8311 (comment)

@githorse
Copy link
Author

githorse commented May 8, 2023

If I'm understanding correctly, currentMonth obviously gets me part of the way there, but I would still have to provide my own dropdown or whatever to select a different year, yes? This seems inconsistent with DateCalendar and YearCalendar, which let the user essentially pick any value via (scrolling, < > buttons, or dropdown pickers) without having to be "refreshed" with a new range from outside.

@alexfauquette
Copy link
Member

The <MonthCalendar /> is only here to pick a month. If you want to have the view management part you should use *

<DateCalendar views={["year", "month"]} />

https://codesandbox.io/s/wonderful-boyd-yu9qse?file=/demo.tsx

@flaviendelangle
Copy link
Member

As @alexfauquette said:

  • MonthCalendar => month
  • YearCalendar => year
  • DateCalendar => year + month + day (uses MonthCalendar and YearCalendar behind the scene, as well as the private DayCalendar)

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! enhancement This is not a bug, nor a new feature
Projects
None yet
3 participants