This repository was archived by the owner on Sep 2, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdate-picker-month.tsx
More file actions
92 lines (85 loc) · 2.7 KB
/
Copy pathdate-picker-month.tsx
File metadata and controls
92 lines (85 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { h, FunctionalComponent } from "@stencil/core"
import { DuetLocalizedText } from "./date-localization"
import { DatePickerDay, DatePickerDayProps } from "./date-picker-day"
import { getViewOfMonth, inRange, DaysOfWeek, isInList } from "./date-utils"
import { DateDisabledPredicate } from "./duet-date-picker"
function chunk<T>(array: T[], chunkSize: number): T[][] {
const result = []
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize))
}
return result
}
function mapWithOffset<T, U>(array: T[], startingOffset: number, mapFn: (item: T) => U): U[] {
return array.map((_, i) => {
const adjustedIndex = (i + startingOffset) % array.length
return mapFn(array[adjustedIndex])
})
}
type DatePickerMonthProps = {
selectedDates: Date[]
focusedDate: Date
labelledById: string
localization: DuetLocalizedText
firstDayOfWeek: DaysOfWeek
min?: Date
max?: Date
dateFormatter: Intl.DateTimeFormat
isDateDisabled: DateDisabledPredicate
onDateSelect: DatePickerDayProps["onDaySelect"]
onKeyboardNavigation: DatePickerDayProps["onKeyboardNavigation"]
focusedDayRef: (element: HTMLElement) => void
}
export const DatePickerMonth: FunctionalComponent<DatePickerMonthProps> = ({
selectedDates,
focusedDate,
labelledById,
localization,
firstDayOfWeek,
min,
max,
dateFormatter,
isDateDisabled,
onDateSelect,
onKeyboardNavigation,
focusedDayRef,
}) => {
const today = new Date()
const days = getViewOfMonth(focusedDate, firstDayOfWeek)
return (
<table class="duet-date__table" aria-labelledby={labelledById}>
<thead>
<tr>
{mapWithOffset(localization.dayNames, firstDayOfWeek, dayName => (
<th class="duet-date__table-header" scope="col">
<span aria-hidden="true">{dayName.substr(0, 2)}</span>
<span class="duet-date__vhidden">{dayName}</span>
</th>
))}
</tr>
</thead>
<tbody>
{chunk(days, 7).map(week => (
<tr class="duet-date__row">
{week.map(day => (
<td class="duet-date__cell">
<DatePickerDay
day={day}
today={today}
focusedDay={focusedDate}
isSelected={isInList(day, selectedDates)}
disabled={isDateDisabled(day)}
inRange={inRange(day, min, max)}
onDaySelect={onDateSelect}
dateFormatter={dateFormatter}
onKeyboardNavigation={onKeyboardNavigation}
focusedDayRef={focusedDayRef}
/>
</td>
))}
</tr>
))}
</tbody>
</table>
)
}