Logo

dev-resources.site

for different kinds of informations.

Shadcn Date Picker with custom implementation

Published at
8/4/2024
Categories
webdev
nextjs
react
shadcn
Author
dellboyan
Categories
4 categories in total
webdev
open
nextjs
open
react
open
shadcn
open
Author
9 person written this
dellboyan
open
Shadcn Date Picker with custom implementation

Shadcn Date Picker that comes with this component library definitely does not suck, but there are a couple of use cases where using this component is not ideal, for example, selecting date of birth, let's explore why.

Shadcn implementation

If you look at the example from shadcn/ui date picker, selecting your Date of Birth would mean that you would have to click on the left arrow for ages to get to let's say 1983. It's not possible to select a year nor month. Even if you could, examples of these implementations are not very intuitive for users, from my experience.

shadcn date picker

Solution

Date Picker from shadcn was built using a combination of Popover and the Calendar. Instead of using Calendar I am using Select and ScrollArea components. And that looks like this:

Shadcn Date Picker with Shadcn

As you can see user can select day, month and a year, which in my opinion is faster and easier. You can check out the demo here.

How to use

To use this component just install Select and ScrollArea components (and shadcn obviously). Copy shadcn-date-picker.tsx from here and save it inside /components. Finally, import ShadcnDatePicker in your page, and use it, for example:

 <ShadcnDatePicker
    startYear={1930}
    endYear={2030}
    selected={date}
    onSelect={setDate}
 />
Enter fullscreen mode Exit fullscreen mode

As you can see, you can select start year and end year based on your needs, date picker also has some validation, for example it will throw an error if user selects 31/2.

That's it, hope you enjoyed this post! If you tried this simple component If you enjoyed this post I'd love it if you could give me a follow on Twitter by clicking on the button below! :)
Dellboyan Twitter

shadcn Article's
30 articles in total
Favicon
Made FOSS for simplifying NextJS dev with OAuth And Postgres
Favicon
.gitkeep file in Shadcn/ui source code
Favicon
Introducing the Zod Schema Designer: A Visual Tool for Creating and Editing Zod Schemas
Favicon
Handling Local Storage for Toaster Notifications
Favicon
I created a visually pleasing HTML color viewer
Favicon
AISEKA Color Tool Website
Favicon
Color Palette is served with ShadCn Theme Editor
Favicon
Create React project with Vite and set up Tailwind, shadcn/ui
Favicon
Why Developers Love the Shadcn Sidebar Component
Favicon
A comparison of metadata configurations between Lobechat and Shadcn/ui
Favicon
Forms in seconds with ShardCn forms
Favicon
What is the Difference Between Radix UI and ShadCN?
Favicon
How Shadcn CLI uses error constants to improve code readability
Favicon
Exploring the ShadCN UI Library: A Comprehensive Guide
Favicon
10 Essential Shadcn Components Every Developer Should Know About
Favicon
The beginning of my open source journey
Favicon
Install ShadCN and Tailwind in Vite React Project
Favicon
How to add shadcn to existing project
Favicon
How to Use Shadcn UI with a React Project
Favicon
How to Install Shadcn with Next.js 14: A Step-by-Step Guide
Favicon
Shadcn Date Picker with custom implementation
Favicon
How to Fix Shadcn UI Adding Wrong Folder for Components
Favicon
Material UI vs Shadcn
Favicon
Building UIs with Franken UI, a Shadcn alternative
Favicon
Implement ShadCn form with Validation
Favicon
Top 5 Coolest shadcn/ui Extensions
Favicon
Shadcn UI: Must-Have Tools & Resources
Favicon
shadcn-ui/ui codebase analysis: examples route explained.
Favicon
Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.
Favicon
Creating a User Nav bar in Next.js 14 using Shadcn UI and Tailwindย CSS

Featured ones: