How to Build a Calendar Component with Claude Code
Learn how to build a calendar component using Claude Code. Includes practical code examples and step-by-step guidance.
カレンダーコンポーネントの需要
予約システム、スケジュール管理、日付選択など、カレンダーUIは多くのアプリケーションで必要です。外部ライブラリに頼ることもできますが、デザインや機能の自由度を求めるなら自作が最適です。Claude Codeを使えば、柔軟なカレンダーコンポーネントを素早く構築できます。
月表示カレンダーの基本実装
> date-fnsを使った月表示カレンダーコンポーネントを作って。
> 月の切り替えと日付選択に対応して。
import { useState } from 'react';
import {
startOfMonth, endOfMonth, startOfWeek, endOfWeek,
eachDayOfInterval, format, addMonths, subMonths, isSameMonth, isSameDay, isToday,
} from 'date-fns';
import { ja } from 'date-fns/locale';
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, { locale: ja });
const calendarEnd = endOfWeek(monthEnd, { locale: ja });
const days = eachDayOfInterval({ start: calendarStart, end: calendarEnd });
const weekDays = ['日', '月', '火', '水', '木', '金', '土'];
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="前月">
←
</button>
<h2 className="text-lg font-bold" aria-live="polite">
{format(currentMonth, 'yyyy年M月', { locale: ja })}
</h2>
<button onClick={() => setCurrentMonth(addMonths(currentMonth, 1))} aria-label="翌月">
→
</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}
aria-label={`${format(day, 'M月d日', { locale: ja })}${dayEvents.length > 0 ? ` ${dayEvents.length}件の予定` : ''}`}
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>
);
}
日付範囲選択
> チェックイン・チェックアウトのような日付範囲選択に対応して。
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 };
}
イベント表示付きカレンダー
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, 'M月d日(E)', { locale: ja })}の予定
</h3>
{dayEvents.length === 0 ? (
<p className="text-gray-500">予定はありません</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を使えば、月表示・日付範囲選択・イベント表示など、用途に合ったカレンダーコンポーネントを柔軟に構築できます。テーブル表示との組み合わせはテーブルコンポーネントを、レスポンシブ対応はレスポンシブデザインを参照してください。
date-fnsの詳細はdate-fns公式サイトをご覧ください。
#Claude Code
#calendar
#React
#UI
#date-fns
Related Posts
Tips & Tricks
Tips & Tricks
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Tips & Tricks
Tips & Tricks
Canvas/WebGL Optimization with Claude Code
Learn about canvas/webgl optimization using Claude Code. Practical tips and code examples included.
Tips & Tricks
Tips & Tricks
Markdown Implementation with Claude Code
Learn about markdown implementation using Claude Code. Practical tips and code examples included.