Last active
July 25, 2020 16:22
-
-
Save jenil94/3610d92b230384a90ce530a446f6f4be to your computer and use it in GitHub Desktop.
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
.... | |
import { | |
format, | |
startOfWeek, | |
addDays, | |
startOfMonth, | |
endOfMonth, | |
endOfWeek, | |
isSameMonth, | |
isSameDay | |
} from "date-fns"; | |
const Calendar = () => { | |
.... | |
const generateDatesForCurrentWeek = (date, selectedDate, activeDate) => { | |
let currentDate = date; | |
const week = []; | |
for (let day = 0; day < 7; day++) { | |
const cloneDate = currentDate; | |
week.push( | |
<div | |
className={`day ${ | |
isSameMonth(currentDate, activeDate) ? "" : "inactiveDay" | |
} ${isSameDay(currentDate, selectedDate) ? "selectedDay" : ""} | |
${isSameDay(currentDate, new Date()) ? "today" : ""}`} | |
onClick={() => { | |
setSelectedDate(cloneDate); | |
}} | |
> | |
{format(currentDate, "d")} | |
</div> | |
); | |
currentDate = addDays(currentDate, 1); | |
} | |
return <>{week}</>; | |
}; | |
const getDates = () => { | |
const startOfTheSelectedMonth = startOfMonth(activeDate); | |
const endOfTheSelectedMonth = endOfMonth(activeDate); | |
const startDate = startOfWeek(startOfTheSelectedMonth); | |
const endDate = endOfWeek(endOfTheSelectedMonth); | |
let currentDate = startDate; | |
const allWeeks = []; | |
while (currentDate <= endDate) { | |
allWeeks.push( | |
generateDatesForCurrentWeek(currentDate, selectedDate, activeDate) | |
); | |
currentDate = addDays(currentDate, 7); | |
} | |
return <div className="weekContainer">{allWeeks}</div>; | |
}; | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment