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

feat(components): calendar/datepicker with month and year select #1739

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

jhavej
Copy link

@jhavej jhavej commented Oct 13, 2023

This PR implements the missing month/year select in the calendar/datepicker component.

Related PRs: #1680 #320 #546 #509 #880

My approach relies exclusively on the native captionLayout prop offered by React DayPicker, along with additional Tailwind CSS styling for the Calendar component. This approach maximizes the capabilities of React DayPicker, simplifying the solution and reducing the risk of bugs.

Other calendar/datepicker use cases remain unchanged.

Screenshot 2023-10-13 at 16 40 38

Here's the demo:

Screen.Recording.2023-10-13.at.16.21.15.mov

Cheers,
Jakub 👋

@vercel
Copy link

vercel bot commented Oct 13, 2023

@jhavej is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@shadcn shadcn added enhancement New feature or request component: calendar area: roadmap This looks great. We'll add it to the roadmap, review and merge. labels Oct 15, 2023
@vercel
Copy link

vercel bot commented Oct 15, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 12, 2023 7:57am

@iaingymware
Copy link
Contributor

iaingymware commented Oct 15, 2023

Whilst testing this change, it seems fromDate & toDate no longer work.

With the changes made in this PR:

image

With the previous calendar component:

image

In the 1st screenshot, the calendar defaults to October 2023. In the 2nd screenshot the calendar defaults to October 2005. The expected behaviour is the latter.

The following is my implementation of this component (which is static between the test scenarios):

<Calendar captionLayout="dropdown-buttons" fromDate={new Date("1900-01-01")} toDate={eighteenYearsAgo} mode="single" selected={field.value} onSelect={field.onChange} disabled={(date) => date > eighteenYearsAgo || date < new Date("1900-01-01") } initialFocus />

eighteenYearsAgo is a const defined to calculate 18 years from todays date:

const eighteenYearsAgo = moment().subtract(18, "years").toDate();

Maybe the implementation should change, but from the original comment I don't think so.

Happy to make changes to my implementation as per instruction.

Thanks

@shadcn
Copy link
Collaborator

shadcn commented Oct 18, 2023

@jhavej Could this be a duplicate of #1680? What are your thoughts on #1680?

@jhavej
Copy link
Author

jhavej commented Oct 24, 2023

Whilst testing this change, it seems fromDate & toDate no longer work.

With the changes made in this PR:

image

With the previous calendar component:

image

In the 1st screenshot, the calendar defaults to October 2023. In the 2nd screenshot the calendar defaults to October 2005. The expected behaviour is the latter.

The following is my implementation of this component (which is static between the test scenarios):

<Calendar captionLayout="dropdown-buttons" fromDate={new Date("1900-01-01")} toDate={eighteenYearsAgo} mode="single" selected={field.value} onSelect={field.onChange} disabled={(date) => date > eighteenYearsAgo || date < new Date("1900-01-01") } initialFocus />

eighteenYearsAgo is a const defined to calculate 18 years from todays date:

const eighteenYearsAgo = moment().subtract(18, "years").toDate();

Maybe the implementation should change, but from the original comment I don't think so.

Happy to make changes to my implementation as per instruction.

Thanks

Thanks for the catch @iaingymware 👍

It was caused because of the default values for fromYear/toYear (these are mandatory for captionLayout to work - see the official docs for React DayPicker) - these props always overrode fromDate/toDate or fromMonth/toMonth.

I removed the default values and passed fromYear/toYear props to Calendar directly from the DatePickerWithMonthYear - this way it's more explicit and aligns with the React DayPicker official docs.

Tested also with fromDate/toDate & fromMonth/toMonth 🆗

@jhavej
Copy link
Author

jhavej commented Oct 24, 2023

@jhavej Could this be a duplicate of #1680? What are your thoughts on #1680?

Hi @shadcn 👋 Yeah, I noticed this PR, see it in my first comment. It brings unnecessary complexity as it adds a custom dropdown and event handlers - you can see it here.

My solution is purely based on the capabilities of React DayPicker by enabling its captionLayout prop + some needed Tailwind CSS styling.

Just fixed the minor issue above & built the registry so we can finish this soon.

Cheers,
Jakub

@MendyLanda
Copy link

MendyLanda commented Oct 31, 2023

Hi @shadcn 👋 Yeah, I noticed this PR, see it in my first comment. It brings unnecessary complexity as it adds a custom dropdown and event handlers - you can see it here.

I tested both and imo @jhavej implementation is less bloated, and works better.
Thank you @jhavej for making my life ez! it's already in prod ❤️
image

@MendyLanda
Copy link

I made slightly different versions of the dropdown buttons, lmk what you think

  • month is taking all available space with flex-1 to reduce ui shifting
  • added button behaviour (using buttonVariants)

with the outline variant:
image

or with the ghost variant (cursor is hovering over month):
image

@jhavej
Copy link
Author

jhavej commented Nov 17, 2023

Thanks for the approval @pyloverm 👍

Should I merge main to resolve the conflicts? I think it'd discard your approval so maybe it'd speed up the PR merge if you do it from your end.

Let me know and thanks again.

Copy link

@pyloverm pyloverm left a comment

Choose a reason for hiding this comment

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

The addition of the date-picker-with-range and date-picker-with-month-year components appears consistent with the component organization.
The enhancements to the default calendar, including the month and year dropdowns in date-picker-with-month-year, align with the design goals of shadcn/ui . It's great to see improvements in usability.
The documentation for the date-picker-with-month-year component in date-picker.mdx is clear and well-integrated.
The updates to the styles in both default and New York registries show attention to detail.
Overall, this is a solid contribution!👏🚀

@MrLightful
Copy link

@pyloverm @shadcn is it scheduled for release?

@AssadAnabosi
Copy link

@jhavej Could this be a duplicate of #1680? What are your thoughts on #1680?

Hi @shadcn 👋 Yeah, I noticed this PR, see it in my first comment. It brings unnecessary complexity as it adds a custom dropdown and event handlers - you can see it here.

My solution is purely based on the capabilities of React DayPicker by enabling its captionLayout prop + some needed Tailwind CSS styling.

Just fixed the minor issue above & built the registry so we can finish this soon.

Cheers, Jakub

Hi @jhavej I have tried your approach [not gonna lie it is a clean one], however since it uses the default browser dropdown it's not working correctly while in dark mode

image
image

Extra: I have also tried #1680 and yours works better, but is there a way to combine both the clean look of shadcn/ui select component and your approach? (Your approach works much better for range selection since I can change the month from both sides)

image

@cblberlin
Copy link

would it be somehow add month and year picker but not only day picker in the future?

@mateusz-kifner
Copy link

Hello everyone, i have seen this pull request and though that it needs more love, so i put together my spin on dropdown month/year picker. It requires a lot more code to work, but it's probably worth it.
I'm not sure if this is what a minimalist library like shadcn needs, but if so, I'm happy to turn this into a proper PR.

Screenshot_20241016_011646
Screenshot_20241016_011701
Screenshot_20241016_011717

Video.webm

Demo: https://shadcn-calendar-v9-m9mh.vercel.app/
Repo: https://github.com/mateusz-kifner/shadcn-calendar-v9

Many thanks to @Nhollas for providing styling for day-react-picker v9 in this https://github.com/shadcn-ui/ui/pull/4371#issuecomment-2284286079

Feel free to borrow my code it's MIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: roadmap This looks great. We'll add it to the roadmap, review and merge. component: calendar enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants