Skip to content

Commit 1cca7ec

Browse files
Fix clamping
1 parent 3c6713a commit 1cca7ec

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/lib/DatePicker.svelte

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
}
2323
function browse(d: Date) {
2424
browseDate = value ? cloneDate(d) : cloneDate(defaultDate)
25+
clamp(min, max)
2526
if (!browseWithoutSelecting) {
26-
setValue(d)
27-
clamp(min, max)
27+
setValue(browseDate)
2828
}
2929
}
3030
@@ -33,20 +33,23 @@
3333
3434
/** The date shown in the popup when none is selected */
3535
let browseDate = value ? cloneDate(value) : cloneDate(defaultDate)
36-
$: if (value) browseDate = cloneDate(value)
3736
3837
/** The earliest year the user can select */
3938
export let min = new Date(defaultDate.getFullYear() - 20, 0, 1)
4039
/** The latest year the user can select */
4140
export let max = new Date(defaultDate.getFullYear(), 11, 31, 23, 59, 59, 999)
4241
function clamp(min: Date, max: Date) {
43-
if (browseDate && browseDate > max) {
44-
setValue(max)
45-
} else if (browseDate && browseDate < min) {
46-
setValue(min)
42+
if (browseDate > max) {
43+
browseDate = cloneDate(max)
44+
} else if (browseDate < min) {
45+
browseDate = cloneDate(min)
4746
}
4847
}
49-
$: if (value) clamp(min, max)
48+
49+
$: if (value) {
50+
clamp(min, max)
51+
browseDate = cloneDate(value)
52+
}
5053
5154
let years = getYears(min, max)
5255
$: years = getYears(min, max)

0 commit comments

Comments
 (0)