1 | import { dateLib as defaultDateLib } from "../lib/index.js";
|
2 | import type { DateLib } from "../types/index.js";
|
3 |
|
4 | /**
|
5 | * Represent the day displayed in the calendar.
|
6 | *
|
7 | * In DayPicker, a `Day` is a `Date` that can be displayed in the calendar. It
|
8 | * is used as extension of the native `Date` object to provide additional
|
9 | * information about the day.
|
10 | */
|
11 | export class CalendarDay {
|
12 | constructor(
|
13 | date: Date,
|
14 | displayMonth: Date,
|
15 | /** @ignore */
|
16 | dateLib: DateLib = defaultDateLib
|
17 | ) {
|
18 | this.date = date;
|
19 | this.displayMonth = displayMonth;
|
20 | this.outside = Boolean(
|
21 | displayMonth && !dateLib.isSameMonth(date, displayMonth)
|
22 | );
|
23 | this.dateLib = dateLib;
|
24 | }
|
25 |
|
26 | /**
|
27 | * The utility functions to manipulate dates.
|
28 | *
|
29 | * @private
|
30 | */
|
31 | readonly dateLib: DateLib;
|
32 |
|
33 | /**
|
34 | * Whether the day is not belonging to the displayed month.
|
35 | *
|
36 | * When `outside` is `true`, use `displayMonth` to know to which month the day
|
37 | * belongs.
|
38 | */
|
39 | readonly outside: boolean;
|
40 |
|
41 | /**
|
42 | * The months where the day is displayed.
|
43 | *
|
44 | * In DayPicker, days can fall out the displayed months (e.g. when
|
45 | * `showOutsideDays` is `true`). This property is useful to know if the day is
|
46 | * in the same month of the displayed month.
|
47 | */
|
48 | readonly displayMonth: Date;
|
49 |
|
50 | /** The date represented by this day. */
|
51 | readonly date: Date;
|
52 |
|
53 | /**
|
54 | * Check if the day is the same as the given day: considering if it is in the
|
55 | * same display month.
|
56 | */
|
57 | isEqualTo(day: CalendarDay) {
|
58 | return (
|
59 | this.dateLib.isSameDay(day.date, this.date) &&
|
60 | this.dateLib.isSameMonth(day.displayMonth, this.displayMonth)
|
61 | );
|
62 | }
|
63 | }
|