Claude Code के साथ Calendar Component कैसे बनाएं
Claude Code का उपयोग करके calendar component बनाना सीखें। Practical code examples और step-by-step guidance शामिल है।
Calendar Component की Demand
Booking systems, schedule management, date selection आदि में calendar UI कई applications में ज़रूरी है। External libraries पर depend कर सकते हैं, लेकिन design और functionality की freedom चाहिए तो custom बनाना best है। Claude Code से flexible calendar component quickly बनाया जा सकता है।
Monthly View Calendar की Basic Implementation
> date-fns use करके monthly view calendar component बनाओ।
> Month switching और date selection support करो।
import { useState } from 'react';
import {
startOfMonth, endOfMonth, startOfWeek, endOfWeek,
eachDayOfInterval, format, addMonths, subMonths, isSameMonth, isSameDay, isToday,
} from 'date-fns';
interface CalendarProps {
selected?: Date;
onSelect: (date: Date) => void;
events?: { date: Date; title: string }[];
}
function Calendar({ selected, onSelect, events = [] }: CalendarProps) {
const [currentMonth, setCurrentMonth] = useState(new Date());
const monthStart = startOfMonth(currentMonth);
const monthEnd = endOfMonth(currentMonth);
const calendarStart = startOfWeek(monthStart);
const calendarEnd = endOfWeek(monthEnd);
const days = eachDayOfInterval({ start: calendarStart, end: calendarEnd });
const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const getEventsForDay = (day: Date) =>
events.filter((e) => isSameDay(e.date, day));
return (
<div className="w-full max-w-md mx-auto" role="application" aria-label="calendar">
<div className="flex items-center justify-between mb-4">
<button onClick={() => setCurrentMonth(subMonths(currentMonth, 1))} aria-label="Previous month">
←
</button>
<h2 className="text-lg font-bold" aria-live="polite">
{format(currentMonth, 'MMMM yyyy')}
</h2>
<button onClick={() => setCurrentMonth(addMonths(currentMonth, 1))} aria-label="Next month">
→
</button>
</div>
<div className="grid grid-cols-7 gap-px" role="grid">
{weekDays.map((day) => (
<div key={day} className="text-center text-sm font-medium text-gray-500 py-2" role="columnheader">
{day}
</div>
))}
{days.map((day) => {
const dayEvents = getEventsForDay(day);
const isSelected = selected && isSameDay(day, selected);
const inMonth = isSameMonth(day, currentMonth);
return (
<button
key={day.toISOString()}
onClick={() => onSelect(day)}
role="gridcell"
aria-selected={isSelected}
className={`p-2 text-center rounded-lg relative ${
!inMonth ? 'text-gray-300' :
isSelected ? 'bg-blue-600 text-white' :
isToday(day) ? 'bg-blue-100 font-bold' :
'hover:bg-gray-100'
}`}
>
{format(day, 'd')}
{dayEvents.length > 0 && (
<span className="absolute bottom-1 left-1/2 -translate-x-1/2 w-1 h-1 rounded-full bg-blue-500" />
)}
</button>
);
})}
</div>
</div>
);
}
Date Range Selection
> Check-in/check-out जैसी date range selection support करो।
function useDateRange() {
const [range, setRange] = useState<{ start: Date | null; end: Date | null }>({
start: null,
end: null,
});
const handleSelect = (date: Date) => {
if (!range.start || (range.start && range.end)) {
setRange({ start: date, end: null });
} else if (date < range.start) {
setRange({ start: date, end: range.start });
} else {
setRange({ start: range.start, end: date });
}
};
const isInRange = (date: Date) => {
if (!range.start || !range.end) return false;
return date >= range.start && date <= range.end;
};
return { range, handleSelect, isInRange };
}
Event Display Calendar
function EventCalendar({ events }: { events: CalendarEvent[] }) {
const [selectedDate, setSelectedDate] = useState<Date>(new Date());
const dayEvents = events.filter((e) => isSameDay(e.date, selectedDate));
return (
<div className="flex gap-4">
<Calendar selected={selectedDate} onSelect={setSelectedDate} events={events} />
<div className="flex-1">
<h3 className="font-bold mb-2">
{format(selectedDate, 'MMM d')} के events
</h3>
{dayEvents.length === 0 ? (
<p className="text-gray-500">कोई event नहीं है</p>
) : (
<ul className="space-y-2">
{dayEvents.map((event) => (
<li key={event.id} className="p-3 rounded-lg border">
<span className="font-medium">{event.title}</span>
<span className="text-sm text-gray-500 ml-2">
{format(event.date, 'HH:mm')}
</span>
</li>
))}
</ul>
)}
</div>
</div>
);
}
Summary
Claude Code से monthly view, date range selection, event display जैसे use case के अनुसार flexible calendar component बनाया जा सकता है। Table display के साथ combination के लिए Table Component देखें, responsive support के लिए Responsive Design देखें।
date-fns की details के लिए date-fns Official Site देखें।
Related Posts
Claude Code से Productivity 3 गुना बढ़ाने की 10 Tips
Claude Code से ज़्यादा पाने की 10 practical tips जानें। Prompt strategies से workflow shortcuts तक, ये techniques आज से ही आपकी efficiency boost करेंगी।
Claude Code के साथ Canvas/WebGL Optimization
Claude Code का उपयोग करके Canvas/WebGL optimization के बारे में जानें। Practical tips और code examples शामिल हैं।
Claude Code के साथ Markdown Implementation
Claude Code का उपयोग करके markdown implementation सीखें। Practical tips और code examples शामिल हैं।