|
1 | 1 | <script> |
2 | 2 | import { createEventDispatcher } from 'svelte'; |
3 | 3 | import { Button, Input } from '@sveltestrap/sveltestrap'; |
| 4 | + import Flatpickr from 'svelte-flatpickr'; |
| 5 | + import 'flatpickr/dist/flatpickr.css'; |
4 | 6 | import { TIME_RANGE_OPTIONS, CUSTOM_DATE_RANGE } from '$lib/helpers/constants'; |
5 | 7 | import { clickoutsideDirective } from '$lib/helpers/directives'; |
6 | 8 |
|
|
25 | 27 | /** @type {string} */ |
26 | 28 | let datePickerTab = 'relative'; |
27 | 29 |
|
| 30 | + // Temporary dates for custom selection (before confirming) |
| 31 | + /** @type {string} */ |
| 32 | + let tempStartDate = ''; |
| 33 | + |
| 34 | + /** @type {string} */ |
| 35 | + let tempEndDate = ''; |
| 36 | +
|
| 37 | + // Flatpickr instance reference |
| 38 | + /** @type {any} */ |
| 39 | + let flatpickrInstance = null; |
| 40 | +
|
| 41 | + // Format date for flatpickr (Date object to YYYY-MM-DD string) |
| 42 | + /** @param {Date} date */ |
| 43 | + function formatDateForFlatpickr(/** @type {Date} */ date) { |
| 44 | + if (!date) return ''; |
| 45 | + const year = date.getFullYear(); |
| 46 | + const month = String(date.getMonth() + 1).padStart(2, '0'); |
| 47 | + const day = String(date.getDate()).padStart(2, '0'); |
| 48 | + return `${year}-${month}-${day}`; |
| 49 | + } |
| 50 | +
|
| 51 | + // Flatpickr options for date range selection |
| 52 | + /** @type {any} */ |
| 53 | + let flatpickrOptions = { |
| 54 | + mode: 'range', |
| 55 | + dateFormat: 'Y-m-d', |
| 56 | + inline: true, |
| 57 | + allowInput: false, |
| 58 | + onChange: (/** @type {Date[]} */ selectedDates, /** @type {string} */ dateStr, /** @type {any} */ instance) => { |
| 59 | + if (selectedDates.length === 1) { |
| 60 | + // Only start date selected |
| 61 | + tempStartDate = formatDateForFlatpickr(selectedDates[0]); |
| 62 | + tempEndDate = ''; |
| 63 | + } else if (selectedDates.length === 2) { |
| 64 | + // Both dates selected |
| 65 | + tempStartDate = formatDateForFlatpickr(selectedDates[0]); |
| 66 | + tempEndDate = formatDateForFlatpickr(selectedDates[1]); |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | +
|
| 71 | + // Handle manual input changes |
| 72 | + function handleStartDateChange() { |
| 73 | + if (tempStartDate && flatpickrInstance) { |
| 74 | + if (tempEndDate) { |
| 75 | + flatpickrInstance.setDate([tempStartDate, tempEndDate], false); |
| 76 | + } else { |
| 77 | + flatpickrInstance.setDate([tempStartDate], false); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + function handleEndDateChange() { |
| 83 | + if (tempStartDate && tempEndDate && flatpickrInstance) { |
| 84 | + flatpickrInstance.setDate([tempStartDate, tempEndDate], false); |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | + // Initialize temp dates when opening custom tab |
| 89 | + function initCustomDates() { |
| 90 | + if (startDate) { |
| 91 | + tempStartDate = startDate; |
| 92 | + } |
| 93 | + if (endDate) { |
| 94 | + tempEndDate = endDate; |
| 95 | + } |
| 96 | + if (!tempStartDate && !tempEndDate) { |
| 97 | + tempStartDate = getYesterdayStr(); |
| 98 | + tempEndDate = getTodayStr(); |
| 99 | + } |
| 100 | + // Update flatpickr with initial dates |
| 101 | + if (flatpickrInstance) { |
| 102 | + if (tempStartDate && tempEndDate) { |
| 103 | + flatpickrInstance.setDate([tempStartDate, tempEndDate], false); |
| 104 | + } else if (tempStartDate) { |
| 105 | + flatpickrInstance.setDate([tempStartDate], false); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +
|
28 | 110 | // Preset time range options |
29 | 111 | const presetTimeRangeOptions = TIME_RANGE_OPTIONS.map(x => ({ |
30 | 112 | label: x.label, |
|
80 | 162 | dispatch('change', { timeRange, startDate, endDate }); |
81 | 163 | } |
82 | 164 |
|
83 | | - function handleCustomConfirm() { |
84 | | - if (startDate) { |
85 | | - // If endDate is not provided, default to startDate |
86 | | - if (!endDate) { |
87 | | - endDate = startDate; |
88 | | - } |
89 | | - // Force reactivity by reassigning |
| 165 | + function handleApply() { |
| 166 | + if (tempStartDate) { |
| 167 | + const finalEndDate = tempEndDate || tempStartDate; |
| 168 | + // Update props through binding (will trigger reactivity) |
| 169 | + startDate = tempStartDate; |
| 170 | + endDate = finalEndDate; |
90 | 171 | timeRange = CUSTOM_DATE_RANGE; |
| 172 | + // Dispatch change event with updated values |
| 173 | + dispatch('change', { |
| 174 | + timeRange: CUSTOM_DATE_RANGE, |
| 175 | + startDate: tempStartDate, |
| 176 | + endDate: finalEndDate |
| 177 | + }); |
91 | 178 | } |
92 | 179 | showDatePicker = false; |
93 | | - dispatch('change', { timeRange, startDate, endDate }); |
94 | 180 | } |
95 | 181 |
|
96 | 182 | function handleCancel() { |
|
107 | 193 | if (showDatePicker) { |
108 | 194 | // If custom date is selected, switch to custom tab; otherwise use relative tab |
109 | 195 | datePickerTab = timeRange === CUSTOM_DATE_RANGE ? 'custom' : 'relative'; |
| 196 | + if (datePickerTab === 'custom') { |
| 197 | + // Delay init to ensure flatpickr is mounted |
| 198 | + setTimeout(() => { |
| 199 | + initCustomDates(); |
| 200 | + }, 0); |
| 201 | + } |
110 | 202 | } |
111 | 203 | }} |
112 | 204 | style="cursor: pointer;" |
|
148 | 240 | style="padding: 0.5rem 0.75rem; {datePickerTab === 'custom' ? 'border-bottom: 2px solid var(--bs-primary) !important; margin-bottom: -1px;' : ''}" |
149 | 241 | on:click={() => { |
150 | 242 | datePickerTab = 'custom'; |
151 | | - // Set default dates to yesterday and today if not already set |
152 | | - if (!startDate && !endDate) { |
153 | | - startDate = getYesterdayStr(); |
154 | | - endDate = getTodayStr(); |
155 | | - } |
| 243 | + // Delay init to ensure flatpickr is mounted |
| 244 | + setTimeout(() => { |
| 245 | + initCustomDates(); |
| 246 | + }, 0); |
156 | 247 | }} |
157 | 248 | > |
158 | 249 | Custom |
|
178 | 269 | {/each} |
179 | 270 | </div> |
180 | 271 | {:else if datePickerTab === 'custom'} |
| 272 | + <!-- Calendar Grid --> |
181 | 273 | <div class="mb-3"> |
182 | | - <label for="start-date-picker" class="form-label small mb-2">From:</label> |
| 274 | + <Flatpickr |
| 275 | + options={flatpickrOptions} |
| 276 | + bind:flatpickr={flatpickrInstance} |
| 277 | + /> |
| 278 | + </div> |
| 279 | + |
| 280 | + <!-- Date Input Fields --> |
| 281 | + <div class="d-flex align-items-center gap-2 mb-3"> |
183 | 282 | <Input |
184 | | - id="start-date-picker" |
185 | 283 | type="date" |
186 | | - bind:value={startDate} |
| 284 | + bind:value={tempStartDate} |
187 | 285 | class="form-control form-control-sm" |
| 286 | + on:change={handleStartDateChange} |
188 | 287 | /> |
189 | | - </div> |
190 | | - <div class="mb-4"> |
191 | | - <label for="end-date-picker" class="form-label small mb-2">To:</label> |
| 288 | + <span class="text-muted small">to</span> |
192 | 289 | <Input |
193 | | - id="end-date-picker" |
194 | 290 | type="date" |
195 | | - bind:value={endDate} |
| 291 | + bind:value={tempEndDate} |
196 | 292 | class="form-control form-control-sm" |
| 293 | + on:change={handleEndDateChange} |
197 | 294 | /> |
198 | 295 | </div> |
| 296 | + |
199 | 297 | <div class="d-flex justify-content-end gap-2 mt-3"> |
200 | 298 | <Button |
201 | 299 | color="secondary" |
|
216 | 314 | on:click={(e) => { |
217 | 315 | e.preventDefault(); |
218 | 316 | e.stopPropagation(); |
219 | | - handleCustomConfirm(); |
| 317 | + handleApply(); |
220 | 318 | }} |
221 | 319 | > |
222 | | - Confirm |
| 320 | + Apply |
223 | 321 | </Button> |
224 | 322 | </div> |
225 | 323 | {/if} |
226 | 324 | </div> |
227 | 325 | </div> |
228 | 326 | {/if} |
229 | 327 | </div> |
| 328 | + |
| 329 | +<style> |
| 330 | + /* Hide flatpickr's default input field when using inline mode */ |
| 331 | + :global(.flatpickr-input) { |
| 332 | + display: none !important; |
| 333 | + } |
| 334 | +</style> |
0 commit comments