-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75affab
commit 322840a
Showing
3 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
apps/antalmanac/src/components/Calendar/calendar-course-event-wrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Box } from '@mui/material'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { EventWrapperProps } from 'react-big-calendar'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
import type { CalendarEvent } from '$components/Calendar/CourseCalendarEvent'; | ||
import { useSelectedEventStore } from '$stores/SelectedEventStore'; | ||
|
||
interface CalendarCourseEventWrapperProps extends EventWrapperProps<CalendarEvent> { | ||
children?: React.ReactNode; | ||
} | ||
|
||
/** | ||
* CalendarCourseEventWrapper allows us to override the default onClick event behavior which problamtically rerenders the entire calendar | ||
*/ | ||
export const CalendarCourseEventWrapper = ({ children, ...props }: CalendarCourseEventWrapperProps) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
const setSelectedEvent = useSelectedEventStore((state) => state.setSelectedEvent, shallow); | ||
|
||
const handleClick = useCallback( | ||
(e: React.MouseEvent) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
setSelectedEvent(e, props.event); | ||
}, | ||
[props.event, setSelectedEvent] | ||
); | ||
|
||
useEffect(() => { | ||
const node = ref.current; | ||
if (!node) { | ||
return; | ||
} | ||
|
||
const rbcEvent = node.querySelector('.rbc-event') as HTMLDivElement; | ||
if (!rbcEvent) { | ||
return; | ||
} | ||
|
||
rbcEvent.onclick = (e) => handleClick(e as unknown as React.MouseEvent); // the native onclick requires a little type hacking | ||
}, [handleClick]); | ||
|
||
return <Box ref={ref}>{children}</Box>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters