117 lines
5.1 KiB
JavaScript
117 lines
5.1 KiB
JavaScript
// Rôle : rend le sélecteur de date mensuel du formulaire calendrier.
|
|
import { Icon } from "../../../components/Icon.jsx";
|
|
import {
|
|
addMonths,
|
|
formatLocalDateKey,
|
|
getLocalDayIndex,
|
|
getMonthGridDays,
|
|
getMonthStart,
|
|
pad2,
|
|
parseLocalDateKey
|
|
} from "./calendarUtils.js";
|
|
import { WEEKDAY_LABELS } from "./calendarLayout.js";
|
|
|
|
function normalizeDraftDateKey(value) {
|
|
const date = parseLocalDateKey(value);
|
|
return date ? formatLocalDateKey(date) : "";
|
|
}
|
|
|
|
function formatMonthDateLabel(dateKey, mode) {
|
|
const date = parseLocalDateKey(dateKey);
|
|
if (!date) return "";
|
|
const weekday = WEEKDAY_LABELS[getLocalDayIndex(date)];
|
|
const dayLabel = date.toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" });
|
|
if (mode !== "weekday") return dayLabel;
|
|
const ordinal = Math.floor((date.getDate() - 1) / 7) + 1;
|
|
return `S${pad2(ordinal)} - ${weekday}`;
|
|
}
|
|
|
|
function formatMonthTitle(dateLike) {
|
|
return new Date(dateLike).toLocaleDateString(undefined, { month: "long", year: "numeric" });
|
|
}
|
|
|
|
function getWeekdayPickerRows(monthDate) {
|
|
const monthGridDays = getMonthGridDays(monthDate).filter((day) => day.inMonth);
|
|
return Array.from({ length: 5 }, (_, rowIndex) => {
|
|
const ordinal = rowIndex + 1;
|
|
return {
|
|
ordinal,
|
|
days: WEEKDAY_LABELS.map((_, dayIndex) => {
|
|
const matches = monthGridDays.filter((day) => day.dayIndex === dayIndex);
|
|
return matches[rowIndex] || null;
|
|
})
|
|
};
|
|
});
|
|
}
|
|
|
|
export function MonthDateDropdown({ value, displayedMonth, minDate = "", mode, textContent, label, align = "start", onChange, onMonthChange }) {
|
|
const safeValue = normalizeDraftDateKey(value) || formatLocalDateKey(displayedMonth);
|
|
return (
|
|
<div className={`calendar-month-date-dropdown ${align === "end" ? "is-aligned-end" : ""}`}>
|
|
<button className="calendar-month-date-trigger nds-date-trigger" data-size="sm" type="button" aria-label={label}>
|
|
<Icon name={mode === "weekday" ? "day-name" : "day-number"} />
|
|
<span>{formatMonthDateLabel(safeValue, mode)}</span>
|
|
<Icon name="chevron-down" />
|
|
</button>
|
|
<MonthDatePicker value={safeValue} displayedMonth={displayedMonth} minDate={minDate} mode={mode} textContent={textContent} onChange={onChange} onMonthChange={onMonthChange} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function MonthDatePicker({ value, displayedMonth, minDate, mode, textContent, onChange, onMonthChange }) {
|
|
const days = getMonthGridDays(displayedMonth);
|
|
const weekdayRows = getWeekdayPickerRows(displayedMonth);
|
|
const minDateKey = normalizeDraftDateKey(minDate);
|
|
return (
|
|
<div className={`calendar-month-date-picker ${mode === "weekday" ? "is-weekday-mode" : ""}`} role="group" aria-label={textContent.monthDatePickerLabel || "Choisir une date"}>
|
|
{mode === "weekday" ? (
|
|
<>
|
|
{weekdayRows.flatMap((row) => [
|
|
<span key={`label-${row.ordinal}`} className="calendar-month-date-picker-week">{`S${pad2(row.ordinal)}`}</span>,
|
|
...row.days.map((day, dayIndex) => {
|
|
const dateKey = day ? formatLocalDateKey(day.date) : "";
|
|
const disabled = !day || (minDateKey && dateKey < minDateKey);
|
|
return (
|
|
<button
|
|
key={`${row.ordinal}-${dayIndex}`}
|
|
className={`${value === dateKey ? "active" : ""} ${!day ? "is-outside" : ""}`}
|
|
type="button"
|
|
disabled={disabled}
|
|
onClick={() => onChange(dateKey)}
|
|
aria-pressed={value === dateKey}
|
|
>
|
|
{WEEKDAY_LABELS[dayIndex]}
|
|
</button>
|
|
);
|
|
})
|
|
])}
|
|
</>
|
|
) : (
|
|
<>
|
|
<div className="calendar-month-date-picker-header">
|
|
<button type="button" onClick={() => onMonthChange(getMonthStart(addMonths(displayedMonth, -1)))} aria-label={textContent.previousMonthTitle || "Mois précédent"} title={textContent.previousMonthTitle || "Mois précédent"}>{textContent.previousShortLabel || "Prec."}</button>
|
|
<strong>{formatMonthTitle(displayedMonth)}</strong>
|
|
<button type="button" onClick={() => onMonthChange(getMonthStart(addMonths(displayedMonth, 1)))} aria-label={textContent.nextMonthTitle || "Mois suivant"} title={textContent.nextMonthTitle || "Mois suivant"}>{textContent.nextShortLabel || "Suiv."}</button>
|
|
</div>
|
|
{WEEKDAY_LABELS.map((day) => <span key={day} className="calendar-month-date-picker-day">{day.slice(0, 3)}</span>)}
|
|
{days.map((day) => {
|
|
const dateKey = formatLocalDateKey(day.date);
|
|
const disabled = !day.inMonth || (minDateKey && dateKey < minDateKey);
|
|
return (
|
|
<button
|
|
key={dateKey}
|
|
className={`${value === dateKey ? "active" : ""} ${!day.inMonth ? "is-outside" : ""}`}
|
|
type="button"
|
|
disabled={disabled}
|
|
onClick={() => onChange(dateKey)}
|
|
aria-pressed={value === dateKey}
|
|
>
|
|
{day.dayNumber}
|
|
</button>
|
|
);
|
|
})}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|