@utrecht/calendar-react 1.0.17 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css.mjs +31 -6
- package/dist/css.mjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +31 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/index.test.tsx +40 -0
- package/src/index.tsx +31 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@utrecht/calendar-react",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Calendar component for the Municipality of Utrecht based on the NL Design System architecture",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nl-design-system"
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
"clsx": "2.1.1",
|
|
57
57
|
"date-fns": "2.30.0",
|
|
58
58
|
"lodash-es": "4.17.23",
|
|
59
|
-
"@utrecht/
|
|
60
|
-
"@utrecht/
|
|
59
|
+
"@utrecht/calendar-css": "2.0.1",
|
|
60
|
+
"@utrecht/button-react": "3.0.1"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@testing-library/dom": "8.20.1",
|
package/src/index.test.tsx
CHANGED
|
@@ -95,6 +95,28 @@ describe('Calendar', () => {
|
|
|
95
95
|
expect(selectedEventButton).toHaveClass('utrecht-calendar__table-days-item-day--selected');
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
it('hides weekends', () => {
|
|
99
|
+
const currentDate = new Date(2023, 5, 15); // 15 June 2023 (Thu)
|
|
100
|
+
|
|
101
|
+
render(<Calendar onCalendarClick={() => {}} locale={nl} currentDate={currentDate} displayWeekend={false} />);
|
|
102
|
+
|
|
103
|
+
const currentDayButton = screen.getByRole('button', {
|
|
104
|
+
name: 'donderdag 15 juni 2023',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
expect(currentDayButton).not.toBeDisabled();
|
|
108
|
+
|
|
109
|
+
expect(screen.queryByRole('button', { name: 'zaterdag 03 juni 2023' })).not.toBeInTheDocument();
|
|
110
|
+
expect(screen.queryByRole('button', { name: 'zaterdag 10 juni 2023' })).not.toBeInTheDocument();
|
|
111
|
+
expect(screen.queryByRole('button', { name: 'zaterdag 17 juni 2023' })).not.toBeInTheDocument();
|
|
112
|
+
expect(screen.queryByRole('button', { name: 'zaterdag 24 juni 2023' })).not.toBeInTheDocument();
|
|
113
|
+
|
|
114
|
+
expect(screen.queryByRole('button', { name: 'zondag 04 juni 2023' })).not.toBeInTheDocument();
|
|
115
|
+
expect(screen.queryByRole('button', { name: 'zondag 11 juni 2023' })).not.toBeInTheDocument();
|
|
116
|
+
expect(screen.queryByRole('button', { name: 'zondag 18 juni 2023' })).not.toBeInTheDocument();
|
|
117
|
+
expect(screen.queryByRole('button', { name: 'zondag 25 juni 2023' })).not.toBeInTheDocument();
|
|
118
|
+
});
|
|
119
|
+
|
|
98
120
|
it('navigates to previous year', () => {
|
|
99
121
|
const currentDate = new Date(2023, 2, 1);
|
|
100
122
|
const { container } = render(<Calendar onCalendarClick={() => {}} locale={nl} currentDate={currentDate} />);
|
|
@@ -119,6 +141,24 @@ describe('Calendar', () => {
|
|
|
119
141
|
expect(currentDateLabel).toContainHTML('maart 2024');
|
|
120
142
|
});
|
|
121
143
|
|
|
144
|
+
it('renders year navigation buttons by default', () => {
|
|
145
|
+
const currentDate = new Date(2023, 5, 15);
|
|
146
|
+
|
|
147
|
+
render(<Calendar onCalendarClick={() => {}} locale={nl} currentDate={currentDate} />);
|
|
148
|
+
|
|
149
|
+
expect(screen.queryByRole('button', { name: 'vorig jaar' })).toBeInTheDocument();
|
|
150
|
+
expect(screen.queryByRole('button', { name: 'volgend jaar' })).toBeInTheDocument();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('does not render year navigation buttons', () => {
|
|
154
|
+
const currentDate = new Date(2023, 5, 15);
|
|
155
|
+
|
|
156
|
+
render(<Calendar onCalendarClick={() => {}} locale={nl} currentDate={currentDate} displayYearNavigation={false} />);
|
|
157
|
+
|
|
158
|
+
expect(screen.queryByRole('button', { name: 'vorig jaar' })).not.toBeInTheDocument();
|
|
159
|
+
expect(screen.queryByRole('button', { name: 'volgend jaar' })).not.toBeInTheDocument();
|
|
160
|
+
});
|
|
161
|
+
|
|
122
162
|
it('navigates to previous month', () => {
|
|
123
163
|
const currentDate = new Date(2023, 2, 1);
|
|
124
164
|
const { container } = render(<Calendar onCalendarClick={() => {}} locale={nl} currentDate={currentDate} />);
|
package/src/index.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
isBefore,
|
|
13
13
|
isSameDay,
|
|
14
14
|
isSameMonth,
|
|
15
|
+
isWeekend,
|
|
15
16
|
Locale,
|
|
16
17
|
parseISO,
|
|
17
18
|
setMonth,
|
|
@@ -36,14 +37,14 @@ import { IconArrowLeftDouble } from './IconArrowLeftDouble';
|
|
|
36
37
|
import { IconArrowRight } from './IconArrowRight';
|
|
37
38
|
import { IconArrowRightDouble } from './IconArrowRightDouble';
|
|
38
39
|
|
|
39
|
-
function createCalendar(today: Date): Date[] {
|
|
40
|
+
function createCalendar(today: Date, displayWeekend: boolean): Date[] {
|
|
40
41
|
const start = startOfWeek(startOfMonth(today), {
|
|
41
42
|
weekStartsOn: 1 /* Monday */,
|
|
42
43
|
});
|
|
43
44
|
const end = endOfWeek(addWeeks(start, 5), {
|
|
44
45
|
weekStartsOn: 1 /* Monday */,
|
|
45
46
|
});
|
|
46
|
-
return eachDayOfInterval({ start, end });
|
|
47
|
+
return eachDayOfInterval({ start, end }).filter((date) => displayWeekend || !isWeekend(date));
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
export type Events = {
|
|
@@ -81,6 +82,8 @@ export interface CalendarProps {
|
|
|
81
82
|
nextMonthButtonTitle?: string;
|
|
82
83
|
minDate?: Date;
|
|
83
84
|
maxDate?: Date;
|
|
85
|
+
displayWeekend?: boolean;
|
|
86
|
+
displayYearNavigation?: boolean;
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
/**
|
|
@@ -97,16 +100,18 @@ export const Calendar = ({
|
|
|
97
100
|
nextYearButtonTitle = 'Next year',
|
|
98
101
|
previousMonthButtonTitle = 'Previous month',
|
|
99
102
|
nextMonthButtonTitle = 'Next month',
|
|
103
|
+
displayWeekend = true,
|
|
104
|
+
displayYearNavigation = true,
|
|
100
105
|
minDate,
|
|
101
106
|
maxDate,
|
|
102
107
|
}: CalendarProps) => {
|
|
103
108
|
const [visibleMonth, setVisibleMonth] = useState(currentDate || new Date());
|
|
104
109
|
const [selectedDate, setSelectedDate] = useState(currentDate);
|
|
105
|
-
const calendar = createCalendar(visibleMonth);
|
|
110
|
+
const calendar = createCalendar(visibleMonth, displayWeekend);
|
|
106
111
|
const start = startOfWeek(visibleMonth, { weekStartsOn: 1 });
|
|
107
112
|
const end = endOfWeek(visibleMonth, { weekStartsOn: 1 });
|
|
108
113
|
|
|
109
|
-
const currentWeek = eachDayOfInterval({ start, end }).
|
|
114
|
+
const currentWeek = eachDayOfInterval({ start, end }).filter((day) => displayWeekend || !isWeekend(day));
|
|
110
115
|
const chunksWeeks = chunk(calendar, calendar.length / 6);
|
|
111
116
|
|
|
112
117
|
const weeks = chunksWeeks.map((week) =>
|
|
@@ -131,26 +136,34 @@ export const Calendar = ({
|
|
|
131
136
|
}),
|
|
132
137
|
);
|
|
133
138
|
|
|
139
|
+
const monthNavigation = (
|
|
140
|
+
<CalendarNavigationButtons
|
|
141
|
+
previousIcon={<IconArrowLeft title={previousMonthButtonTitle} />}
|
|
142
|
+
nextIcon={<IconArrowRight title={nextMonthButtonTitle} />}
|
|
143
|
+
onPreviousClick={() => setVisibleMonth(setMonth(visibleMonth, visibleMonth.getMonth() - 1))}
|
|
144
|
+
onNextClick={() => setVisibleMonth(addMonths(visibleMonth, 1))}
|
|
145
|
+
>
|
|
146
|
+
<CalendarNavigationLabel dateTime={format(visibleMonth, 'yyyy-mm')}>
|
|
147
|
+
{format(visibleMonth, 'LLLL y', { locale })}
|
|
148
|
+
</CalendarNavigationLabel>
|
|
149
|
+
</CalendarNavigationButtons>
|
|
150
|
+
);
|
|
151
|
+
|
|
134
152
|
return (
|
|
135
153
|
<div className="utrecht-calendar">
|
|
136
154
|
<CalendarNavigation>
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
onPreviousClick={() => setVisibleMonth(setYear(visibleMonth, getYear(visibleMonth) - 1))}
|
|
141
|
-
onNextClick={() => setVisibleMonth(addYears(visibleMonth, 1))}
|
|
142
|
-
>
|
|
155
|
+
{!displayYearNavigation ? (
|
|
156
|
+
monthNavigation
|
|
157
|
+
) : (
|
|
143
158
|
<CalendarNavigationButtons
|
|
144
|
-
previousIcon={<
|
|
145
|
-
nextIcon={<
|
|
146
|
-
onPreviousClick={() => setVisibleMonth(
|
|
147
|
-
onNextClick={() => setVisibleMonth(
|
|
159
|
+
previousIcon={<IconArrowLeftDouble title={previousYearButtonTitle} />}
|
|
160
|
+
nextIcon={<IconArrowRightDouble title={nextYearButtonTitle} />}
|
|
161
|
+
onPreviousClick={() => setVisibleMonth(setYear(visibleMonth, getYear(visibleMonth) - 1))}
|
|
162
|
+
onNextClick={() => setVisibleMonth(addYears(visibleMonth, 1))}
|
|
148
163
|
>
|
|
149
|
-
|
|
150
|
-
{format(visibleMonth, 'LLLL y', { locale })}
|
|
151
|
-
</CalendarNavigationLabel>
|
|
164
|
+
{monthNavigation}
|
|
152
165
|
</CalendarNavigationButtons>
|
|
153
|
-
|
|
166
|
+
)}
|
|
154
167
|
</CalendarNavigation>
|
|
155
168
|
<table className="utrecht-calendar__table" role="grid">
|
|
156
169
|
<CalendarTableWeeksContainer>
|