Skip to content

Commit b0bb11e

Browse files
Merge branch 'development' into release/2.36.0
2 parents b838b2f + acc7825 commit b0bb11e

File tree

2 files changed

+116
-22
lines changed

2 files changed

+116
-22
lines changed

packages/@sparrow-library/src/forms/date-picker/DatePicker.svelte

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,42 @@
1919
2020
let showCalendar = false;
2121
let currentMonth = new Date();
22-
let selectedDay: number = currentMonth.getDate();
23-
let selectedMonth: number = currentMonth.getMonth();
24-
let selectedYear: number = currentMonth.getFullYear();
22+
let selectedDay: number | null = null;
23+
let selectedMonth: number | null = null;
24+
let selectedYear: number | null = null;
2525
let calendarRef: HTMLDivElement;
2626
let inputRef: HTMLInputElement;
2727
let showAbove = false;
2828
let isInvalid = false;
2929
let internalValue = value; // Track internal value for validation
30+
let isClearing = false; // Add flag to track clearing state
31+
32+
// Track the original/previous date for clear functionality
33+
let originalValue = value;
34+
let hasChanged = false;
35+
36+
// Initialize selected date state when component mounts or value changes
37+
function initializeSelectedDate(dateValue: string) {
38+
const date = parseDate(dateValue);
39+
if (date) {
40+
selectedDay = date.getDate();
41+
selectedMonth = date.getMonth();
42+
selectedYear = date.getFullYear();
43+
currentMonth = new Date(selectedYear, selectedMonth, 1);
44+
} else {
45+
selectedDay = null;
46+
selectedMonth = null;
47+
selectedYear = null;
48+
}
49+
}
50+
51+
// Initialize on mount if value exists
52+
onMount(() => {
53+
if (value) {
54+
initializeSelectedDate(value);
55+
originalValue = value;
56+
}
57+
});
3058
3159
// Process min/max dates
3260
let minDateObj: Date | null = null;
@@ -137,13 +165,21 @@
137165
const target = event.target as HTMLInputElement;
138166
internalValue = target.value;
139167
140-
// If empty, clear the date
168+
// If empty, clear the date and selected state
141169
if (!internalValue.trim()) {
142170
isInvalid = false;
143171
value = "";
172+
selectedDay = null;
173+
selectedMonth = null;
174+
selectedYear = null;
144175
return;
145176
}
146177
178+
// Store original value if this is the first change
179+
if (!hasChanged && value) {
180+
originalValue = value;
181+
}
182+
147183
// Try to parse the date
148184
const date = parseDate(internalValue);
149185
if (date) {
@@ -160,6 +196,7 @@
160196
selectedMonth = date.getMonth();
161197
selectedYear = date.getFullYear();
162198
currentMonth = new Date(selectedYear, selectedMonth, 1);
199+
hasChanged = true;
163200
164201
// Dispatch change event
165202
dispatch("change", value);
@@ -231,18 +268,26 @@
231268
}
232269
}
233270
234-
// Watch for external value changes
271+
// Watch for external value changes and track original value
235272
$: if (value !== internalValue && !isInvalid) {
236273
internalValue = value;
237274
238-
// Parse value to update selected date in calendar
239-
const date = parseDate(value);
240-
if (date) {
241-
selectedDay = date.getDate();
242-
selectedMonth = date.getMonth();
243-
selectedYear = date.getFullYear();
244-
currentMonth = new Date(selectedYear, selectedMonth, 1);
275+
// If this is the first time setting a value (from props), store as original
276+
if (!hasChanged && value && !originalValue) {
277+
originalValue = value;
245278
}
279+
280+
// Initialize selected date state for calendar
281+
initializeSelectedDate(value);
282+
}
283+
284+
// Also watch for direct value prop changes (when parent updates the value)
285+
$: if (
286+
value &&
287+
value === internalValue &&
288+
(selectedDay === null || selectedMonth === null || selectedYear === null)
289+
) {
290+
initializeSelectedDate(value);
246291
}
247292
248293
function getDaysInMonth(date: Date) {
@@ -300,13 +345,19 @@
300345
return; // Don't select disabled or non-current month days
301346
}
302347
348+
// Store original value if this is the first change
349+
if (!hasChanged && value) {
350+
originalValue = value;
351+
}
352+
303353
selectedDay = dayObj.day;
304354
selectedMonth = currentMonth.getMonth();
305355
selectedYear = currentMonth.getFullYear();
306356
value = formatDate(selectedDay, selectedMonth, selectedYear);
307357
internalValue = value;
308358
isInvalid = false;
309359
showCalendar = false;
360+
hasChanged = true;
310361
311362
// Dispatch change event when date is selected
312363
dispatch("change", value);
@@ -327,6 +378,11 @@
327378
return;
328379
}
329380
381+
// Store original value if this is the first change
382+
if (!hasChanged && value) {
383+
originalValue = value;
384+
}
385+
330386
currentMonth = new Date(today.getFullYear(), today.getMonth(), 1);
331387
selectedDay = today.getDate();
332388
selectedMonth = today.getMonth();
@@ -335,17 +391,51 @@
335391
internalValue = value;
336392
isInvalid = false;
337393
showCalendar = false;
394+
hasChanged = true;
338395
// Dispatch change event when Today is clicked
339396
dispatch("change", value);
340397
}
341398
342399
function handleClear() {
343-
value = "";
344-
internalValue = "";
345-
isInvalid = false;
346-
showCalendar = false;
347-
// Dispatch change event when Clear is clicked
348-
dispatch("change", "");
400+
// If user has made changes, revert to the original value
401+
if (hasChanged && originalValue) {
402+
const originalDate = parseDate(originalValue);
403+
if (originalDate) {
404+
selectedDay = originalDate.getDate();
405+
selectedMonth = originalDate.getMonth();
406+
selectedYear = originalDate.getFullYear();
407+
value = originalValue;
408+
internalValue = originalValue;
409+
currentMonth = new Date(selectedYear, selectedMonth, 1);
410+
hasChanged = false; // Reset the change flag
411+
412+
isInvalid = false;
413+
showCalendar = false;
414+
415+
// Dispatch change event with the reverted value
416+
dispatch("change", originalValue);
417+
}
418+
} else if (value && !hasChanged) {
419+
// If there's a date selected but no changes made, keep the current date (do nothing)
420+
showCalendar = false;
421+
// Don't dispatch change event as we're not changing the value
422+
return;
423+
} else {
424+
// No original value and no current value, clear completely
425+
value = "";
426+
internalValue = "";
427+
selectedDay = null;
428+
selectedMonth = null;
429+
selectedYear = null;
430+
originalValue = "";
431+
hasChanged = false;
432+
433+
isInvalid = false;
434+
showCalendar = false;
435+
436+
// Dispatch change event with empty value
437+
dispatch("change", "");
438+
}
349439
}
350440
351441
const toggleCalendar = () => {

packages/@sparrow-workspaces/src/features/testflow-explorer/layout/TestflowExplorer.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,9 +3216,13 @@
32163216
<DocumentRegular size="32px" color="#6b6b6b" />
32173217
</div>
32183218
<div class="empty-message">
3219-
No test data imported yet. Use the <span
3220-
style="font-weight: 700;">Import</span
3221-
> button to upload JSON or CSV files for testing.
3219+
{#if searchQuery.trim() === ""}
3220+
No test data imported yet. Use the <span
3221+
style="font-weight: 700;">Import</span
3222+
> button to upload JSON or CSV files for testing.
3223+
{:else}
3224+
No result found
3225+
{/if}
32223226
</div>
32233227
</div>
32243228
{/if}
@@ -3815,7 +3819,7 @@
38153819
width: 100%;
38163820
}
38173821
.empty-icon {
3818-
margin-bottom: 18px;
3822+
margin-bottom: 8px;
38193823
opacity: 0.7;
38203824
}
38213825
.empty-message {

0 commit comments

Comments
 (0)