|
1 | 1 | <script lang="ts">
|
2 | 2 | import {
|
3 |
| - startOfDay as startOfDayFunc, |
4 |
| - endOfDay as endOfDayFunc, |
5 |
| - startOfMonth as startOfMonthFunc, |
6 |
| - endOfMonth as endOfMonthFunc, |
7 |
| - addMonths, |
8 |
| - isSameDay, |
9 |
| - isWithinInterval, |
10 |
| - } from 'date-fns'; |
11 |
| - import { type SelectedDate, PeriodType, hasKeyOf } from '@layerstack/utils'; |
12 |
| - import { getMonthDaysByWeek } from '@layerstack/utils/date'; |
| 3 | + type SelectedDate, |
| 4 | + PeriodType, |
| 5 | + hasKeyOf, |
| 6 | + startOfInterval, |
| 7 | + endOfInterval, |
| 8 | + intervalOffset, |
| 9 | + } from '@layerstack/utils'; |
| 10 | + import { getMonthDaysByWeek, isDateWithin, isSameInterval } from '@layerstack/utils/date'; |
13 | 11 |
|
14 | 12 | import { mdiChevronLeft, mdiChevronRight } from '@mdi/js';
|
15 | 13 |
|
|
21 | 19 | export let selected: SelectedDate = undefined;
|
22 | 20 |
|
23 | 21 | export let startOfMonth =
|
24 |
| - (selected instanceof Date && startOfMonthFunc(selected)) || |
25 |
| - (selected instanceof Array && selected.length && startOfMonthFunc(selected[0])) || |
| 22 | + (selected instanceof Date && startOfInterval('month', selected)) || |
| 23 | + (selected instanceof Array && selected.length && startOfInterval('month', selected[0])) || |
26 | 24 | (selected &&
|
27 | 25 | hasKeyOf<{ from: Date }>(selected, 'from') &&
|
28 | 26 | selected.from &&
|
29 |
| - startOfMonthFunc(selected.from)) || |
30 |
| - startOfMonthFunc(new Date()); |
| 27 | + startOfInterval('month', selected.from)) || |
| 28 | + startOfInterval('month', new Date()); |
31 | 29 |
|
32 | 30 | const { format } = getSettings();
|
33 | 31 | $: dateFormat = $format.settings.formats.dates;
|
34 | 32 |
|
35 |
| - $: endOfMonth = endOfMonthFunc(startOfMonth); |
| 33 | + $: endOfMonth = endOfInterval('month', startOfMonth); |
36 | 34 | $: monthDaysByWeek = getMonthDaysByWeek(startOfMonth, dateFormat.weekStartsOn);
|
37 | 35 |
|
38 | 36 | /**
|
|
56 | 54 | return disabledDates instanceof Function
|
57 | 55 | ? disabledDates(date)
|
58 | 56 | : disabledDates instanceof Date
|
59 |
| - ? isSameDay(date, disabledDates) |
| 57 | + ? isSameInterval('day', date, disabledDates) |
60 | 58 | : disabledDates instanceof Array
|
61 |
| - ? disabledDates.some((d) => isSameDay(date, d)) |
| 59 | + ? disabledDates.some((d) => isSameInterval('day', date, d)) |
62 | 60 | : disabledDates instanceof Object
|
63 |
| - ? isWithinInterval(date, { |
64 |
| - start: startOfDayFunc(disabledDates.from), |
65 |
| - end: endOfDayFunc(disabledDates.to || disabledDates.from), |
| 61 | + ? isDateWithin(date, { |
| 62 | + start: startOfInterval('day', disabledDates.from), |
| 63 | + end: endOfInterval('day', disabledDates.to || disabledDates.from), |
66 | 64 | })
|
67 | 65 | : false;
|
68 | 66 | };
|
69 | 67 |
|
70 | 68 | $: isDayHidden = (day: Date) => {
|
71 |
| - const isCurrentMonth = isWithinInterval(day, { |
| 69 | + const isCurrentMonth = isDateWithin(day, { |
72 | 70 | start: startOfMonth,
|
73 | 71 | end: endOfMonth,
|
74 | 72 | });
|
75 | 73 | return !isCurrentMonth && !showOutsideDays;
|
76 | 74 | };
|
77 | 75 |
|
78 | 76 | $: isDayFaded = (day: Date) => {
|
79 |
| - const isCurrentMonth = isWithinInterval(day, { |
| 77 | + const isCurrentMonth = isDateWithin(day, { |
80 | 78 | start: startOfMonth,
|
81 | 79 | end: endOfMonth,
|
82 | 80 | });
|
|
102 | 100 | <Button
|
103 | 101 | icon={mdiChevronLeft}
|
104 | 102 | class="p-2"
|
105 |
| - on:click={() => (startOfMonth = addMonths(startOfMonth, -1))} |
| 103 | + on:click={() => (startOfMonth = intervalOffset('month', startOfMonth, -1))} |
106 | 104 | />
|
107 | 105 |
|
108 | 106 | <div class="flex flex-1 items-center justify-center">
|
|
114 | 112 | <Button
|
115 | 113 | icon={mdiChevronRight}
|
116 | 114 | class="p-2"
|
117 |
| - on:click={() => (startOfMonth = addMonths(startOfMonth, 1))} |
| 115 | + on:click={() => (startOfMonth = intervalOffset('month', startOfMonth, 1))} |
118 | 116 | />
|
119 | 117 | </div>
|
120 | 118 | {/if}
|
|
0 commit comments