From 22a02950c3e89bfb94a498a8cb8a07c292e18d72 Mon Sep 17 00:00:00 2001 From: Guillermo Pages Date: Wed, 16 Jul 2025 16:55:09 +0200 Subject: [PATCH] feat: interactive date range --- src/examples/InteractiveDateRange.tsx | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/examples/InteractiveDateRange.tsx diff --git a/src/examples/InteractiveDateRange.tsx b/src/examples/InteractiveDateRange.tsx new file mode 100644 index 0000000..0b522d8 --- /dev/null +++ b/src/examples/InteractiveDateRange.tsx @@ -0,0 +1,125 @@ +import React, { useState } from 'react'; +import { DateRange } from '../components/calendar/DateRange'; +import { Controls } from '../components/calendar/Controls'; +import { HeaderStyle, DaySize, MonthCutoffType } from '../types/calendar'; +import { format, startOfDay } from 'date-fns'; +import styles from './Examples.module.scss'; + +export const InteractiveDateRange: React.FC = () => { + const [dayHeaderStyle, setDayHeaderStyle] = useState('tiny'); + const [size, setSize] = useState('l'); + const [fontProportion, setFontProportion] = useState(100); + const [included, setIncluded] = useState(true); + const [selected, setSelected] = useState(false); + const [monthCutoff, setMonthCutoff] = useState(undefined); + const [activeDates, setActiveDates] = useState([]); + const [lastClicked, setLastClicked] = useState(''); + + const fromDate = new Date(2025, 0, 10); // January 10, 2025 + const toDate = new Date(2025, 0, 25); // January 25, 2025 + + const handleDateClick = (date: Date) => { + const dateString = format(date, 'MMMM d, yyyy'); + setLastClicked(dateString); + + // Toggle the date in activeDates + setActiveDates(prev => { + const dateTime = startOfDay(date).getTime(); + const existingIndex = prev.findIndex(d => + startOfDay(d).getTime() === dateTime + ); + + if (existingIndex >= 0) { + // Remove if already active + return prev.filter((_, index) => index !== existingIndex); + } else { + // Add if not active + return [...prev, date]; + } + }); + + // Show alert + alert(`Clicked: ${dateString}`); + }; + + return ( +
+

Interactive Date Range

+

+ Click on dates to toggle their active state. Active dates have inverted colors. +

+ +
+ + +
+
+

Last clicked: {lastClicked || 'None'}

+

Active dates: {activeDates.length > 0 + ? activeDates.map(d => format(d, 'MMM d')).join(', ') + : 'None'}

+
+
+ +
+
+
{` {
+    alert(\`Clicked: \${format(date, 'MMMM d, yyyy')}\`);
+    // Toggle active state
+    setActiveDates(prev => {
+      const exists = prev.some(d => d.getTime() === date.getTime());
+      return exists 
+        ? prev.filter(d => d.getTime() !== date.getTime())
+        : [...prev, date];
+    });
+  }}
+  activeDates={activeDates}
+/>`}
+
+
+ ); +}; \ No newline at end of file