4
0

CalendarExample.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <Section
  3. title="Example Calendar"
  4. padding
  5. >
  6. <template #toolbar>
  7. <button @click="goToToday">Today</button>
  8. <button @click="previousMonth">‹</button>
  9. <div class="date-picker-container">
  10. <input
  11. id="goto-date"
  12. type="month"
  13. v-model="selectedDate"
  14. @change="goToSelectedDate"
  15. class="date-picker-input"
  16. />
  17. </div>
  18. <button @click="nextMonth">›</button>
  19. </template>
  20. <Calendar
  21. :section="true"
  22. :events="events"
  23. :loading="loading"
  24. :error="error"
  25. :show-navigation="false"
  26. :current-month="currentMonthIndex"
  27. :current-year="currentYear"
  28. @event-click="handleEventClick"
  29. @date-click="handleDateClick"
  30. @month-change="handleMonthChange"
  31. />
  32. </Section>
  33. </template>
  34. <script setup>
  35. import { ref, onMounted } from 'vue'
  36. import Calendar from '../components/Calendar.vue'
  37. import Section from '../components/Section.vue'
  38. const events = ref([])
  39. const loading = ref(false)
  40. const error = ref(null)
  41. const currentMonth = ref('?')
  42. const currentMonthIndex = ref(new Date().getMonth())
  43. const currentYear = ref(new Date().getFullYear())
  44. // Date picker state - using native HTML5 month input
  45. const selectedDate = ref('')
  46. // Generate dummy events for the current month
  47. function generateDummyEvents() {
  48. const now = new Date()
  49. const currentMonth = now.getMonth()
  50. const currentYear = now.getFullYear()
  51. // Get the number of days in the current month
  52. const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate()
  53. const dummyEvents = []
  54. // Add some recurring events
  55. for (let day = 1; day <= daysInMonth; day++) {
  56. const date = new Date(currentYear, currentMonth, day)
  57. const dayOfWeek = date.getDay()
  58. // Add weekend events (Saturday and Sunday)
  59. if (dayOfWeek === 0 || dayOfWeek === 6) {
  60. dummyEvents.push({
  61. id: `weekend-${day}`,
  62. title: 'Weekend Activity',
  63. date: new Date(currentYear, currentMonth, day, 10, 0),
  64. description: 'Relaxing weekend activity'
  65. })
  66. }
  67. // Add some random events
  68. if (Math.random() > 0.7) {
  69. const eventTypes = [
  70. { title: 'Team Meeting', time: [9, 0], duration: 1 },
  71. { title: 'Client Call', time: [14, 30], duration: 0.5 },
  72. { title: 'Project Review', time: [16, 0], duration: 2 },
  73. { title: 'Lunch Break', time: [12, 0], duration: 1 },
  74. { title: 'Code Review', time: [10, 0], duration: 1.5 }
  75. ]
  76. const eventType = eventTypes[Math.floor(Math.random() * eventTypes.length)]
  77. const [hours, minutes] = eventType.time
  78. dummyEvents.push({
  79. id: `event-${day}-${Math.random().toString(36).substr(2, 9)}`,
  80. title: eventType.title,
  81. date: new Date(currentYear, currentMonth, day, hours, minutes),
  82. description: `Scheduled ${eventType.title.toLowerCase()}`
  83. })
  84. }
  85. }
  86. // Add some multi-day events
  87. dummyEvents.push({
  88. id: 'conference-1',
  89. title: 'Tech Conference',
  90. startDate: new Date(currentYear, currentMonth, 5, 9, 0),
  91. endDate: new Date(currentYear, currentMonth, 7, 17, 0),
  92. description: 'Annual technology conference'
  93. })
  94. dummyEvents.push({
  95. id: 'vacation-1',
  96. title: 'Vacation',
  97. startDate: new Date(currentYear, currentMonth, 15, 0, 0),
  98. endDate: new Date(currentYear, currentMonth, 18, 23, 59),
  99. description: 'Family vacation time'
  100. })
  101. // Add some all-day events
  102. dummyEvents.push({
  103. id: 'holiday-1',
  104. title: 'Public Holiday',
  105. date: new Date(currentYear, currentMonth, 1, 0, 0),
  106. description: 'National holiday - office closed'
  107. })
  108. if (daysInMonth >= 25) {
  109. dummyEvents.push({
  110. id: 'deadline-1',
  111. title: 'Project Deadline',
  112. date: new Date(currentYear, currentMonth, 25, 0, 0),
  113. description: 'Important project deadline'
  114. })
  115. }
  116. return dummyEvents
  117. }
  118. function handleEventClick(event) {
  119. console.log('Event clicked:', event)
  120. alert(`Event: ${event.title}\nDescription: ${event.description || 'No description'}`)
  121. }
  122. function handleDateClick(date) {
  123. console.log('Date clicked:', date)
  124. alert(`Date selected: ${date.toLocaleDateString()}`)
  125. }
  126. // Helper function to update the date picker value
  127. function updateDatePicker(monthIndex, year) {
  128. const month = String(monthIndex + 1).padStart(2, '0')
  129. selectedDate.value = `${year}-${month}`
  130. }
  131. // Navigation functions - using the same fast path as internal calendar navigation
  132. function previousMonth() {
  133. const newMonth = currentMonthIndex.value === 0 ? 11 : currentMonthIndex.value - 1
  134. const newYear = currentMonthIndex.value === 0 ? currentYear.value - 1 : currentYear.value
  135. // Update state directly (fast path)
  136. currentMonthIndex.value = newMonth
  137. currentYear.value = newYear
  138. // Update title
  139. const date = new Date(newYear, newMonth, 1)
  140. currentMonth.value = date.toLocaleDateString('en-US', {
  141. month: 'long',
  142. year: 'numeric'
  143. })
  144. // Update date picker
  145. updateDatePicker(newMonth, newYear)
  146. }
  147. function nextMonth() {
  148. const newMonth = currentMonthIndex.value === 11 ? 0 : currentMonthIndex.value + 1
  149. const newYear = currentMonthIndex.value === 11 ? currentYear.value + 1 : currentYear.value
  150. // Update state directly (fast path)
  151. currentMonthIndex.value = newMonth
  152. currentYear.value = newYear
  153. // Update title
  154. const date = new Date(newYear, newMonth, 1)
  155. currentMonth.value = date.toLocaleDateString('en-US', {
  156. month: 'long',
  157. year: 'numeric'
  158. })
  159. // Update date picker
  160. updateDatePicker(newMonth, newYear)
  161. }
  162. function goToToday() {
  163. const now = new Date()
  164. const newMonth = now.getMonth()
  165. const newYear = now.getFullYear()
  166. // Update state directly (fast path)
  167. currentMonthIndex.value = newMonth
  168. currentYear.value = newYear
  169. // Update title
  170. currentMonth.value = now.toLocaleDateString('en-US', {
  171. month: 'long',
  172. year: 'numeric'
  173. })
  174. // Update date picker
  175. updateDatePicker(newMonth, newYear)
  176. }
  177. function goToSelectedDate() {
  178. if (!selectedDate.value) return
  179. // Parse the YYYY-MM format from the native date picker
  180. const [year, month] = selectedDate.value.split('-').map(Number)
  181. const monthIndex = month - 1 // Convert to 0-based month index
  182. // Update state directly (fast path)
  183. currentMonthIndex.value = monthIndex
  184. currentYear.value = year
  185. // Update title
  186. const date = new Date(year, monthIndex, 1)
  187. currentMonth.value = date.toLocaleDateString('en-US', {
  188. month: 'long',
  189. year: 'numeric'
  190. })
  191. }
  192. function handleMonthChange(month, year) {
  193. console.log('Month changed:', month, year)
  194. // In a real app, you might fetch events for the new month
  195. loading.value = true
  196. // Update current month and year state
  197. currentMonthIndex.value = month
  198. currentYear.value = year
  199. // Use native JavaScript to format month and year
  200. const date = new Date(year, month, 1)
  201. currentMonth.value = date.toLocaleDateString('en-US', {
  202. month: 'long',
  203. year: 'numeric'
  204. })
  205. // Update date picker to reflect the new month
  206. updateDatePicker(month, year)
  207. setTimeout(() => {
  208. // Simulate loading delay
  209. events.value = generateDummyEvents()
  210. loading.value = false
  211. }, 500)
  212. }
  213. onMounted(() => {
  214. // Generate initial events for current month
  215. events.value = generateDummyEvents()
  216. // Set initial month title using native JavaScript
  217. const now = new Date()
  218. currentMonthIndex.value = now.getMonth()
  219. currentYear.value = now.getFullYear()
  220. currentMonth.value = now.toLocaleDateString('en-US', {
  221. month: 'long',
  222. year: 'numeric'
  223. })
  224. // Initialize the native date picker with current month/year
  225. const year = now.getFullYear()
  226. const month = String(now.getMonth() + 1).padStart(2, '0')
  227. selectedDate.value = `${year}-${month}`
  228. })
  229. </script>
  230. <style scoped>
  231. .date-picker-container {
  232. display: flex;
  233. align-items: center;
  234. gap: 0.5rem;
  235. }
  236. .date-picker-label {
  237. font-size: 0.875rem;
  238. font-weight: 500;
  239. color: #374151;
  240. }
  241. .date-picker-input {
  242. padding: 0.5rem;
  243. border: 1px solid #d1d5db;
  244. border-radius: 4px;
  245. font-size: 0.875rem;
  246. background: white;
  247. cursor: pointer;
  248. transition: all 0.2s;
  249. }
  250. .date-picker-input:hover {
  251. border-color: #9ca3af;
  252. }
  253. .date-picker-input:focus {
  254. outline: none;
  255. border-color: #3b82f6;
  256. box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  257. }
  258. /* Dark theme support */
  259. @media (prefers-color-scheme: dark) {
  260. .date-picker-label {
  261. color: #d1d5db;
  262. }
  263. .date-picker-input {
  264. background: #374151;
  265. border-color: #4b5563;
  266. color: #f9fafb;
  267. }
  268. .date-picker-input:hover {
  269. border-color: #6b7280;
  270. }
  271. .date-picker-input:focus {
  272. border-color: #60a5fa;
  273. box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.1);
  274. }
  275. }
  276. </style>