Fix selection in calendar

This commit is contained in:
2026-06-14 22:42:28 +02:00
parent cb5517a387
commit 3ca1418d94
3 changed files with 41 additions and 1 deletions
+31
View File
@@ -10,6 +10,9 @@ vi.mock('@/context', () => ({
vi.mock('@/hooks', () => ({
usePlannedDays: vi.fn(),
useShowWeekNumbers: vi.fn(() => [false]),
getLastLeaveType: vi.fn(() => 'vacation'),
setLastLeaveType: vi.fn(),
}))
vi.mock('@/components/layout/Layout', () => ({
@@ -143,4 +146,32 @@ describe('Calendar', () => {
expect(screen.getByTestId('calendar')).toBeInTheDocument()
})
it('highlights date range live while dragging, before pointerup', () => {
;(useWorkspace as any).mockReturnValue(mockWorkspace)
;(usePlannedDays as any).mockReturnValue(mockPlannedDays)
render(<Calendar />)
const calendarDays = screen.getAllByTestId('calendar-day')
const currentMonthDays = calendarDays.filter((el) => !el.classList.contains('calendar-day--disabled'))
expect(currentMonthDays.length).toBeGreaterThanOrEqual(2)
const startDay = currentMonthDays[0] as HTMLElement
const endDay = currentMonthDays[3] as HTMLElement
// Fire pointerdown on first day
fireEvent.pointerDown(startDay)
// Fire pointerenter on later day WITHOUT firing pointerup
fireEvent.pointerEnter(endDay)
// The range should now have the --selected class before pointerup fires
const selectedDaysInRange = Array.from(calendarDays).filter((el) =>
el.classList.contains('calendar-day--selected')
)
expect(selectedDaysInRange.length).toBeGreaterThan(0)
})
})
+8 -1
View File
@@ -184,6 +184,13 @@ export function Calendar(): ReactNode {
const defaultLeaveType = getLastLeaveType() ?? 'vacation'
const firstSelectedDate = Array.from(selectedDates).sort()[0]
// Dates to highlight in the grid. While dragging, show a live preview of the
// range under the cursor; otherwise show the committed selection.
const highlightedDates =
isDragging && dragAnchor && dragCurrent
? new Set(datesInRange(dragAnchor, dragCurrent))
: selectedDates
return (
<Layout>
<div data-testid="calendar" className="calendar">
@@ -215,7 +222,7 @@ export function Calendar(): ReactNode {
key={weekIdx}
weekDates={week}
plannedDays={plannedDays ?? []}
selectedDates={selectedDates}
selectedDates={highlightedDates}
parentName={parentName ?? 'Du'}
personalDataUuid={personalDataUuid ?? ''}
isCurrentMonth={weekMonth === month}
+2
View File
@@ -62,6 +62,8 @@
flex-direction: column;
transition: all 0.2s;
position: relative;
user-select: none;
-webkit-user-select: none; /* Safari */
}
.calendar-day:hover {