Date Picker (AD)
shadcn-lexical-editor/date-pickerFreeComponent
A single date picker (Gregorian/AD) with month navigation and a minimum date option.
Install it
npx shadcn@latest add http://www.alishpawn.com.np/r/date-picker.jsonSource
1"use client";23import { useState, useRef, useEffect, useMemo, useCallback } from "react";4import { Calendar, ChevronLeft, ChevronRight } from "lucide-react";5import { cn } from "@/lib/utils";67interface DatePickerProps {8 value: string; // 'YYYY-MM-DD'9 onChange: (value: string) => void;10 placeholder?: string;11 className?: string;12 min?: string; // 'YYYY-MM-DD'13 id?: string;14}1516function toLocalDate(value: string): Date | null {17 if (!value) return null;18 const [y, m, d] = value.split("-").map(Number);19 const date = new Date(y, m - 1, d);20 return isNaN(date.getTime()) ? null : date;21}2223function toValue(date: Date): string {24 const y = date.getFullYear();25 const m = String(date.getMonth() + 1).padStart(2, "0");26 const d = String(date.getDate()).padStart(2, "0");27 return `${y}-${m}-${d}`;28}2930function stripTime(date: Date): Date {31 return new Date(date.getFullYear(), date.getMonth(), date.getDate());32}3334function isSameDay(a: Date, b: Date): boolean {35 return (36 a.getFullYear() === b.getFullYear() &&37 a.getMonth() === b.getMonth() &&38 a.getDate() === b.getDate()39 );40}4142const WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];4344export function DatePicker({45 value,46 onChange,47 placeholder = "Select date",48 className,49 min,50 id,51}: DatePickerProps) {52 const minDate = toLocalDate(min || "");53 const [open, setOpen] = useState(false);54 const selected = toLocalDate(value);55 const [monthNav, setMonthNav] = useState(0);56 const wrapperRef = useRef<HTMLDivElement>(null);57 const buttonRef = useRef<HTMLButtonElement>(null);58 const [dropdownPos, setDropdownPos] = useState<{59 top: number;60 left: number;61 } | null>(null);6263 const updatePosition = useCallback(() => {64 if (buttonRef.current) {65 const rect = buttonRef.current.getBoundingClientRect();66 setDropdownPos({ top: rect.bottom + 6, left: rect.left });67 }68 }, []);6970 const viewDate = useMemo(() => {71 const base = toLocalDate(value) || new Date();72 return new Date(base.getFullYear(), base.getMonth() + monthNav, 1);73 }, [value, monthNav]);7475 useEffect(() => {76 function handleClick(e: MouseEvent) {77 if (78 wrapperRef.current &&79 !wrapperRef.current.contains(e.target as Node)80 ) {81 setOpen(false);82 }83 }84 document.addEventListener("mousedown", handleClick);85 return () => document.removeEventListener("mousedown", handleClick);86 }, []);8788 useEffect(() => {89 if (!open) return;90// … truncated
What it pulls in
npm packages
Files it writes
More from shadcn-lexical-editor
All 6 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Date Picker (BS / Nepali)date-picker-bs | shadcn-lexical-editor | Components | Free | 2 deps · 3 files | |
| Date Range Picker (AD)date-range-picker | shadcn-lexical-editor | Components | Free | 1 dep | |
| Date Range Picker (BS / Nepali)date-range-picker-bs | shadcn-lexical-editor | Components | Free | 2 deps · 2 files | |
| Rich Text Editorrich-text-editor | shadcn-lexical-editor | Components | Free | 15 deps · 52 files |
