Skip to content

Commit

Permalink
Fix: blank date picker
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
probablykasper committed Apr 29, 2023
1 parent ba4d751 commit 2750309
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Next
- Fix blank date picker when no date is selected and current date is outside min/max range

## 2.4.1 - 2023 Apr 29
- Fix browsing not working with `browseWithoutSelecting` when a date is selected

Expand Down
12 changes: 6 additions & 6 deletions src/lib/DatePicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
/** Default Date to use */
const defaultDate = new Date()
/** The date shown in the popup when none is selected */
let browseDate = value ? cloneDate(value) : cloneDate(defaultDate)
$: if (browseDate.getTime() !== value?.getTime() && !browseWithoutSelecting) {
browseDate = value ? cloneDate(value) : browseDate
}
/** The earliest year the user can select */
export let min = new Date(defaultDate.getFullYear() - 20, 0, 1)
/** The latest year the user can select */
Expand All @@ -58,6 +52,12 @@
}
}
/** The date shown in the popup when none is selected */
let browseDate = value ? cloneDate(value) : cloneDate(clamp(defaultDate, min, max))
$: if (browseDate.getTime() !== value?.getTime() && !browseWithoutSelecting) {
browseDate = value ? cloneDate(value) : browseDate
}
let years = getYears(min, max)
$: years = getYears(min, max)
function getYears(min: Date, max: Date) {
Expand Down
27 changes: 27 additions & 0 deletions src/routes/bug/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import DatePicker from '$lib/DatePicker.svelte'
let date: Date | null = null
// let date_string: string | undefined
let is_date_valid: boolean
let max_date = new Date()
// Atleast 14 years old
max_date.setDate(max_date.getDate() - 5110)
$: if (date && is_date_valid) {
date.setHours(12, 0, 0, 0)
date.toISOString()
// date_string = date.toISOString().slice(0, 10)
}
</script>

<!-- <input bind:value={date_string} type="date" class="hidden" id="id_birth_date" name="birth_date" /> -->
<!-- <DateInput
bind:value={date}
bind:valid={is_date_valid}
max={max_date}
format="yyyy-MM-dd"
placeholder="Select a date"
/> -->
<DatePicker bind:value={date} max={max_date} />

0 comments on commit 2750309

Please sign in to comment.