Skip to content

Commit

Permalink
fix: disable year and month on dropdown if enabledDates is activated. (
Browse files Browse the repository at this point in the history
…#5)

* feat: add disabledDates prop to allow disabling specific dates

* feat: added enabledDates

* fix: error if disalbedDate on array is null

* fixed lint error

* fix: disabled year and month if enabledDates is used

* chore: add changeset
  • Loading branch information
JinIgarashi authored Apr 2, 2024
1 parent 63dfe48 commit 2ac4241
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-cherries-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@undp-data/date-picker-svelte': patch
---

fix: disable year and month on dropdown if enabledDates is activated.
22 changes: 20 additions & 2 deletions src/lib/DatePicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@
}
}
function isDisabledYear(year: number) {
return enabledDates?.length > 0
? enabledDates.filter((day) => {
return day?.getFullYear() === year
}).length === 0
: false
}
function isDisabledMonth(date: Date) {
return enabledDates?.length > 0
? enabledDates.filter((day) => {
return day?.getFullYear() === date.getFullYear() && day?.getMonth() === date.getMonth()
}).length === 0
: false
}
function isDisabledDate(calendarDay: CalendarDay) {
return disabledDates.find((day) => {
return (
Expand Down Expand Up @@ -307,7 +323,9 @@
{#each iLocale.months as monthName, i}
<option
disabled={new Date(browseYear, i, getMonthLength(browseYear, i), 23, 59, 59, 999) <
min || new Date(browseYear, i) > max}
min ||
new Date(browseYear, i) > max ||
isDisabledMonth(new Date(browseYear, i))}
value={i}>{monthName}</option
>
{/each}
Expand Down Expand Up @@ -335,7 +353,7 @@
on:keydown={yearKeydown}
>
{#each years as v}
<option value={v}>{v}</option>
<option disabled={isDisabledYear(v)} value={v}>{v}</option>
{/each}
</select>
<!-- style <select> button without affecting menu popup -->
Expand Down
6 changes: 6 additions & 0 deletions src/routes/DatePicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
const day3 = new Date(day2)
day3.setDate(day3.getDate() + 3)
enabledDates.push(day3)
const day4 = new Date(day2)
day4.setMonth(day4.getMonth() - 1)
enabledDates.push(day4)
const day5 = new Date(day2)
day5.setFullYear(day5.getFullYear() - 1)
enabledDates.push(day5)
</script>

<Split>
Expand Down

0 comments on commit 2ac4241

Please sign in to comment.