Debian Codegraph @

node/@patternfly/react-core/dist/js/components/CalendarMonth/CalendarMonth.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.CalendarMonth = exports.Weekday = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const TextInput_1 = require("../TextInput");
8 const Button_1 = require("../Button");
9 const Select_1 = require("../Select");
10 const MenuToggle_1 = require("../MenuToggle");
11 const InputGroup_1 = require("../InputGroup");
12 const rh_microns_caret_left_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-left-icon'));
13 const rh_microns_caret_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-right-icon'));
14 const react_styles_1 = require("@patternfly/react-styles");
15 const calendar_month_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/CalendarMonth/calendar-month"));
16 const helpers_1 = require("../../helpers");
17 const datetimeUtils_1 = require("../../helpers/datetimeUtils");
18 var Weekday;
19 (function (Weekday) {
20 Weekday[Weekday["Sunday"] = 0] = "Sunday";
21 Weekday[Weekday["Monday"] = 1] = "Monday";
22 Weekday[Weekday["Tuesday"] = 2] = "Tuesday";
23 Weekday[Weekday["Wednesday"] = 3] = "Wednesday";
24 Weekday[Weekday["Thursday"] = 4] = "Thursday";
25 Weekday[Weekday["Friday"] = 5] = "Friday";
26 Weekday[Weekday["Saturday"] = 6] = "Saturday";
27 })(Weekday || (exports.Weekday = Weekday = {}));
28 const buildCalendar = (year, month, weekStart, validators) => {
29 const defaultDate = new Date(year, month);
30 const firstDayOfWeek = new Date(defaultDate);
31 firstDayOfWeek.setDate(firstDayOfWeek.getDate() - firstDayOfWeek.getDay() + weekStart);
32 // We will show a maximum of 6 weeks like Google calendar
33 // Assume we just want the numbers for now...
34 const calendarWeeks = [];
35 if (firstDayOfWeek.getMonth() === defaultDate.getMonth() && firstDayOfWeek.getDate() !== 1) {
36 firstDayOfWeek.setDate(firstDayOfWeek.getDate() - 7);
37 }
38 for (let i = 0; i < 6; i++) {
39 const week = [];
40 for (let j = 0; j < 7; j++) {
41 const date = new Date(firstDayOfWeek);
42 week.push({
43 date,
44 isValid: validators.every((validator) => validator(date))
45 });
46 firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1);
47 }
48 calendarWeeks.push(week);
49 if (firstDayOfWeek.getMonth() !== defaultDate.getMonth()) {
50 break;
51 }
52 }
53 return calendarWeeks;
54 };
55 const isSameDate = (d1, d2) => d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
56 const today = new Date();
57 /** The main calendar month component. */
58 const CalendarMonth = (_a) => {
59 var { date: dateProp, locale = undefined, monthFormat = (date) => date.toLocaleDateString(locale, { month: 'long' }), weekdayFormat = (date) => date.toLocaleDateString(locale, { weekday: 'narrow' }), longWeekdayFormat = (date) => date.toLocaleDateString(locale, { weekday: 'long' }), dayFormat = (date) => date.getDate(), weekStart = 0, // Use the American Sunday as a default
60 onChange = () => { }, validators = [() => true], className, onSelectToggle = () => { }, onMonthChange = () => { }, rangeStart, prevMonthAriaLabel = 'Previous month', nextMonthAriaLabel = 'Next month', yearInputAriaLabel = 'Select year', cellAriaLabel, isDateFocused = false, inlineProps, monthAppendTo = 'inline' } = _a, props = tslib_1.__rest(_a, ["date", "locale", "monthFormat", "weekdayFormat", "longWeekdayFormat", "dayFormat", "weekStart", "onChange", "validators", "className", "onSelectToggle", "onMonthChange", "rangeStart", "prevMonthAriaLabel", "nextMonthAriaLabel", "yearInputAriaLabel", "cellAriaLabel", "isDateFocused", "inlineProps", "monthAppendTo"]);
61 const longMonths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
62 .map((monthNum) => new Date(1990, monthNum))
63 .map(monthFormat);
64 const [isSelectOpen, setIsSelectOpen] = (0, react_1.useState)(false);
65 const getInitialDate = () => {
66 if ((0, datetimeUtils_1.isValidDate)(dateProp)) {
67 return dateProp;
68 }
69 if ((0, datetimeUtils_1.isValidDate)(rangeStart)) {
70 return rangeStart;
71 }
72 return today;
73 };
74 const initialDate = getInitialDate();
75 const [focusedDate, setFocusedDate] = (0, react_1.useState)(initialDate);
76 // Must be numeric given current header design
77 const yearFormat = (date) => date.getFullYear();
78 //
79 const yearFormatted = yearFormat(focusedDate);
80 const [yearInput, setYearInput] = (0, react_1.useState)(yearFormatted.toString());
81 const [hoveredDate, setHoveredDate] = (0, react_1.useState)(undefined);
82 const focusRef = (0, react_1.useRef)(undefined);
83 const hiddenMonthId = (0, helpers_1.useSSRSafeId)('hidden-month-span');
84 const [shouldFocus, setShouldFocus] = (0, react_1.useState)(false);
85 const isValidated = (date) => validators.every((validator) => validator(date));
86 const focusedDateValidated = isValidated(focusedDate);
87 (0, react_1.useEffect)(() => {
88 if ((0, datetimeUtils_1.isValidDate)(dateProp) && !isSameDate(focusedDate, dateProp)) {
89 setFocusedDate(dateProp);
90 }
91 else if (!dateProp) {
92 setFocusedDate(today);
93 }
94 }, [dateProp]);
95 (0, react_1.useEffect)(() => {
96 // Calendar month should not be focused on page load
97 if ((shouldFocus || isDateFocused) && focusedDateValidated && focusRef.current) {
98 focusRef.current.focus();
99 }
100 }, [focusedDate, isDateFocused, focusedDateValidated, focusRef]);
101 const onMonthClick = (ev, newDate) => {
102 setFocusedDate(newDate);
103 setShouldFocus(false);
104 onMonthChange(ev, newDate);
105 setYearInput(yearFormat(newDate).toString());
106 };
107 const onKeyDown = (ev) => {
108 const newDate = new Date(focusedDate);
109 if (ev.key === 'ArrowUp') {
110 newDate.setDate(newDate.getDate() - 7);
111 }
112 else if (ev.key === 'ArrowRight') {
113 newDate.setDate(newDate.getDate() + 1);
114 }
115 else if (ev.key === 'ArrowDown') {
116 newDate.setDate(newDate.getDate() + 7);
117 }
118 else if (ev.key === 'ArrowLeft') {
119 newDate.setDate(newDate.getDate() - 1);
120 }
121 if (newDate.getTime() !== focusedDate.getTime() && isValidated(newDate)) {
122 ev.preventDefault();
123 setFocusedDate(newDate);
124 setShouldFocus(true);
125 }
126 };
127 const changeYear = (newYear) => changeMonth(focusedDate.getMonth(), newYear);
128 const changeMonth = (newMonth, newYear) => new Date(newYear !== null && newYear !== void 0 ? newYear : focusedDate.getFullYear(), newMonth, 1);
129 const MIN_YEAR = 1900;
130 const MAX_YEAR = 2100;
131 const handleYearInputChange = (event, yearStr) => {
132 if (!/^\d{0,4}$/.test(yearStr)) {
133 return;
134 }
135 setYearInput(yearStr);
136 if (yearStr.length === 4) {
137 const yearNum = Number(yearStr);
138 if (yearNum >= MIN_YEAR && yearNum <= MAX_YEAR) {
139 const newDate = changeYear(yearNum);
140 setFocusedDate(newDate);
141 setShouldFocus(false);
142 // We need to manually focus the year input in FireFox when the scroll buttons are clicked, as FireFox doesn't place focus automatically
143 event.target.focus();
144 onMonthChange(event, newDate);
145 }
146 else {
147 setYearInput(yearFormatted.toString());
148 }
149 }
150 };
151 const addMonth = (toAdd) => {
152 let newMonth = focusedDate.getMonth() + toAdd;
153 let newYear = focusedDate.getFullYear();
154 if (newMonth === -1) {
155 newMonth = 11;
156 newYear--;
157 }
158 else if (newMonth === 12) {
159 newMonth = 0;
160 newYear++;
161 }
162 return changeMonth(newMonth, newYear);
163 };
164 const prevMonth = addMonth(-1);
165 const nextMonth = addMonth(1);
166 const focusedYear = focusedDate.getFullYear();
167 const focusedMonth = focusedDate.getMonth();
168 const calendar = (0, react_1.useMemo)(() => buildCalendar(focusedYear, focusedMonth, weekStart, validators), [focusedYear, focusedMonth, weekStart, validators]);
169 if (!focusedDateValidated) {
170 const toFocus = calendar
171 .reduce((acc, cur) => [...acc, ...cur], [])
172 .filter(({ date, isValid }) => isValid && date.getMonth() === focusedMonth)
173 .map(({ date }) => ({ date, days: Math.abs(focusedDate.getTime() - date.getTime()) }))
174 .sort((o1, o2) => o1.days - o2.days)
175 .map(({ date }) => date)[0];
176 if (toFocus) {
177 setFocusedDate(toFocus);
178 }
179 }
180 const isHoveredDateValid = hoveredDate && isValidated(hoveredDate);
181 const monthFormatted = monthFormat(focusedDate);
182 const calendarToRender = ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(calendar_month_1.default.calendarMonth, className) }, props, { children: [(0, jsx_runtime_1.jsxs)("div", { className: calendar_month_1.default.calendarMonthHeader, children: [(0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(calendar_month_1.default.calendarMonthHeaderNavControl, 'pf-m-prev-month'), children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "plain", "aria-label": prevMonthAriaLabel, onClick: (ev) => onMonthClick(ev, prevMonth), icon: (0, jsx_runtime_1.jsx)(rh_microns_caret_left_icon_1.default, {}) }) }), (0, jsx_runtime_1.jsxs)(InputGroup_1.InputGroup, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { isFill: true, children: (0, jsx_runtime_1.jsxs)("div", { className: calendar_month_1.default.calendarMonthHeaderMonth, children: [(0, jsx_runtime_1.jsx)("span", { id: hiddenMonthId, hidden: true, children: "Month" }), (0, jsx_runtime_1.jsx)(Select_1.Select, { toggle: (toggleRef) => ((0, jsx_runtime_1.jsx)(MenuToggle_1.MenuToggle, { ref: toggleRef, onClick: () => setIsSelectOpen(!isSelectOpen), isExpanded: isSelectOpen, style: { width: '140px' }, children: monthFormatted })), "aria-labelledby": hiddenMonthId, isOpen: isSelectOpen, onOpenChange: (isOpen) => {
183 setIsSelectOpen(isOpen);
184 onSelectToggle(isOpen);
185 }, onSelect: (ev, monthNum) => {
186 setIsSelectOpen(false);
187 onSelectToggle(false);
188 const newDate = changeMonth(Number(monthNum));
189 setFocusedDate(newDate);
190 setShouldFocus(false);
191 onMonthChange(ev, newDate);
192 }, selected: monthFormatted, popperProps: { appendTo: monthAppendTo }, children: (0, jsx_runtime_1.jsx)(Select_1.SelectList, { children: longMonths.map((longMonth, index) => ((0, jsx_runtime_1.jsx)(Select_1.SelectOption, { value: index, isSelected: longMonth === monthFormatted, children: longMonth }, index))) }) })] }) }), (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { children: (0, jsx_runtime_1.jsx)("div", { className: calendar_month_1.default.calendarMonthHeaderYear, children: (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { "aria-label": yearInputAriaLabel, type: "number", value: yearInput, onChange: handleYearInputChange }) }) })] }), (0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(calendar_month_1.default.calendarMonthHeaderNavControl, 'pf-m-next-month'), children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "plain", "aria-label": nextMonthAriaLabel, onClick: (ev) => onMonthClick(ev, nextMonth), icon: (0, jsx_runtime_1.jsx)(rh_microns_caret_right_icon_1.default, {}) }) })] }), (0, jsx_runtime_1.jsxs)("table", { className: calendar_month_1.default.calendarMonthCalendar, onMouseLeave: () => setHoveredDate(undefined), children: [(0, jsx_runtime_1.jsx)("thead", { className: calendar_month_1.default.calendarMonthDays, children: (0, jsx_runtime_1.jsx)("tr", { children: calendar[0].map(({ date }, index) => ((0, jsx_runtime_1.jsxs)("th", { className: calendar_month_1.default.calendarMonthDay, scope: "col", children: [(0, jsx_runtime_1.jsx)("span", { className: "pf-v6-screen-reader", children: longWeekdayFormat(date) }), (0, jsx_runtime_1.jsx)("span", { "aria-hidden": true, children: weekdayFormat(date) })] }, index))) }) }), (0, jsx_runtime_1.jsx)("tbody", { onKeyDown: onKeyDown, children: calendar.map((week, index) => ((0, jsx_runtime_1.jsx)("tr", { className: calendar_month_1.default.calendarMonthDatesRow, children: week.map(({ date, isValid }, index) => {
193 const dayFormatted = dayFormat(date);
194 const isToday = isSameDate(date, today);
195 const isSelected = (0, datetimeUtils_1.isValidDate)(dateProp) && isSameDate(date, dateProp);
196 const isFocused = isSameDate(date, focusedDate);
197 const isAdjacentMonth = date.getMonth() !== focusedDate.getMonth();
198 const isRangeStart = (0, datetimeUtils_1.isValidDate)(rangeStart) && isSameDate(date, rangeStart);
199 let isInRange = false;
200 let isRangeEnd = false;
201 if ((0, datetimeUtils_1.isValidDate)(rangeStart)) {
202 let rangeEndDate;
203 if ((0, datetimeUtils_1.isValidDate)(dateProp)) {
204 rangeEndDate = dateProp;
205 }
206 if (isHoveredDateValid && (!(0, datetimeUtils_1.isValidDate)(dateProp) || hoveredDate > dateProp)) {
207 rangeEndDate = hoveredDate;
208 }
209 if (rangeEndDate) {
210 isInRange = date >= rangeStart && date <= rangeEndDate;
211 isRangeEnd = isSameDate(date, rangeEndDate);
212 }
213 // Core would likely need new styles for "is selected but disabled"
214 }
215 return ((0, jsx_runtime_1.jsx)("td", { className: (0, react_styles_1.css)(calendar_month_1.default.calendarMonthDatesCell, isAdjacentMonth && calendar_month_1.default.modifiers.adjacentMonth, isToday && calendar_month_1.default.modifiers.current, (isSelected || isRangeStart) && calendar_month_1.default.modifiers.selected, !isValid && !(isInRange || isRangeStart || isRangeEnd || isSelected) && calendar_month_1.default.modifiers.disabled, isInRange && calendar_month_1.default.modifiers.inRange, isRangeStart && calendar_month_1.default.modifiers.startRange, isRangeEnd && calendar_month_1.default.modifiers.endRange), children: (0, jsx_runtime_1.jsx)("button", Object.assign({ className: (0, react_styles_1.css)(calendar_month_1.default.calendarMonthDate, isRangeEnd && calendar_month_1.default.modifiers.hover, !isValid && calendar_month_1.default.modifiers.disabled), type: "button", onClick: (event) => onChange(event, date), onMouseOver: () => setHoveredDate(date), tabIndex: isFocused ? 0 : -1, disabled: !isValid, "aria-label": cellAriaLabel
216 ? cellAriaLabel(date)
217 : `${dayFormat(date)} ${monthFormat(date)} ${yearFormat(date)}` }, (isFocused && { ref: focusRef }), { children: dayFormatted })) }, index));
218 }) }, index))) })] })] })));
219 if (inlineProps !== undefined) {
220 const Component = (inlineProps.component ? inlineProps.component : 'article');
221 return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({}, (inlineProps.ariaLabelledby && { 'aria-labelledby': inlineProps.ariaLabelledby }), { children: [inlineProps.title, calendarToRender] })));
222 }
223 return calendarToRender;
224 };
225 exports.CalendarMonth = CalendarMonth;
226 exports.CalendarMonth.displayName = 'CalendarMonth';
227 //# sourceMappingURL=CalendarMonth.js.map