fixed calendar dates for the single days
This commit is contained in:
@@ -13,7 +13,7 @@ export const MONTHS = [
|
|||||||
"December",
|
"December",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export type MonthValue = (typeof MONTHS)[number];
|
export type Month = (typeof MONTHS)[number];
|
||||||
|
|
||||||
export const DAYS = [
|
export const DAYS = [
|
||||||
"Monday",
|
"Monday",
|
||||||
@@ -25,4 +25,4 @@ export const DAYS = [
|
|||||||
"Sunday",
|
"Sunday",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export type DayValue = (typeof DAYS)[number];
|
export type Day = (typeof DAYS)[number];
|
||||||
|
|||||||
@@ -1,47 +1,59 @@
|
|||||||
import { Pressable, Text, View } from "react-native";
|
import { Pressable, Text, View } from "react-native";
|
||||||
import { DAYS, MONTHS } from "../Constants";
|
import { DAYS, MONTHS, Month } from "../Constants";
|
||||||
import Header from "../components/Header";
|
import Header from "../components/Header";
|
||||||
import { useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import currentTheme from "../Themes";
|
import currentTheme from "../Themes";
|
||||||
import BaseBackground from "../components/BaseBackground";
|
import BaseBackground from "../components/BaseBackground";
|
||||||
|
|
||||||
// TODO: month selection dropdown menu
|
// TODO: month selection dropdown menu
|
||||||
|
|
||||||
const Calender = () => (
|
const Calendar = () => {
|
||||||
<BaseBackground>
|
|
||||||
<CalenderHeader />
|
|
||||||
<WeekDaysLine />
|
|
||||||
<CalenderGrid />
|
|
||||||
</BaseBackground>
|
|
||||||
);
|
|
||||||
|
|
||||||
const CalenderHeader = () => {
|
|
||||||
const [monthIndex, setMonthIndex] = useState(0);
|
const [monthIndex, setMonthIndex] = useState(0);
|
||||||
const [currentYear, setCurrentYear] = useState(2025);
|
const [currentYear, setCurrentYear] = useState(new Date().getFullYear());
|
||||||
|
|
||||||
const changeMonth = (delta: number) => {
|
const changeMonth = (delta: number) => {
|
||||||
setMonthIndex((prev) => {
|
setMonthIndex((prev) => {
|
||||||
const newIndex = prev + delta;
|
const newIndex = prev + delta;
|
||||||
if (newIndex > 11) {
|
if (newIndex > 11) {
|
||||||
setCurrentYear(currentYear + 1);
|
setCurrentYear((y) => y + 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (newIndex < 0) {
|
if (newIndex < 0) {
|
||||||
setCurrentYear(currentYear - 1);
|
setCurrentYear((y) => y - 1);
|
||||||
return 11;
|
return 11;
|
||||||
}
|
}
|
||||||
return newIndex;
|
return newIndex;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const prevMonth = () => changeMonth(-1);
|
return (
|
||||||
const nextMonth = () => changeMonth(1);
|
<BaseBackground>
|
||||||
|
<CalendarHeader
|
||||||
|
changeMonth={changeMonth}
|
||||||
|
monthIndex={monthIndex}
|
||||||
|
currentYear={currentYear}
|
||||||
|
/>
|
||||||
|
<WeekDaysLine />
|
||||||
|
<CalendarGrid month={MONTHS[monthIndex]} year={currentYear} />
|
||||||
|
</BaseBackground>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
type CalendarHeaderProps = {
|
||||||
|
changeMonth: (delta: number) => void;
|
||||||
|
monthIndex: number;
|
||||||
|
currentYear: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CalendarHeader = (props: CalendarHeaderProps) => {
|
||||||
|
const prevMonth = () => props.changeMonth(-1);
|
||||||
|
const nextMonth = () => props.changeMonth(1);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Header className="flex flex-row items-center justify-between">
|
<Header className="flex flex-row items-center justify-between">
|
||||||
<ChangeMonthButton onPress={prevMonth} title={"<"} />
|
<ChangeMonthButton onPress={prevMonth} title={"<"} />
|
||||||
<Text className="text-4xl">
|
<Text className="text-4xl">
|
||||||
{MONTHS[monthIndex]} {currentYear}
|
{MONTHS[props.monthIndex]} {props.currentYear}
|
||||||
</Text>
|
</Text>
|
||||||
<ChangeMonthButton onPress={nextMonth} title={">"} />
|
<ChangeMonthButton onPress={nextMonth} title={">"} />
|
||||||
</Header>
|
</Header>
|
||||||
@@ -77,42 +89,75 @@ const WeekDaysLine = () => (
|
|||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
|
||||||
const CalenderGrid = () => (
|
type CalendarGridProps = {
|
||||||
<View
|
month: Month;
|
||||||
className="h-full flex-1 flex-col flex-wrap gap-2 p-2"
|
year: number;
|
||||||
style={{
|
};
|
||||||
backgroundColor: currentTheme.calenderBg,
|
|
||||||
}}
|
const CalendarGrid = (props: CalendarGridProps) => {
|
||||||
>
|
const { baseDate, dateOffset } = useMemo(() => {
|
||||||
{Array.from({ length: 6 }).map((_, i) => (
|
const monthIndex = MONTHS.indexOf(props.month);
|
||||||
<View
|
const base = new Date(props.year, monthIndex, 1);
|
||||||
key={i}
|
const offset = base.getDay() === 0 ? 6 : base.getDay() - 1;
|
||||||
className="w-full flex-1 flex-row justify-around items-center gap-2"
|
return { baseDate: base, dateOffset: offset };
|
||||||
>
|
}, [props.month, props.year]);
|
||||||
{/* TODO: correctly retrieve or calculate the date */}
|
|
||||||
{Array.from({ length: 7 }).map((_, j) => (
|
// TODO: create array beforehand in a useMemo
|
||||||
<SingleDay key={j} date={i * 7 + (j+1)} />
|
const createDateFromOffset = (offset: number): Date => {
|
||||||
))}
|
const date = new Date(baseDate);
|
||||||
</View>
|
date.setDate(date.getDate() + offset);
|
||||||
))}
|
return date;
|
||||||
</View>
|
};
|
||||||
);
|
|
||||||
|
return (
|
||||||
|
<View
|
||||||
|
className="h-full flex-1 flex-col flex-wrap gap-2 p-2"
|
||||||
|
style={{
|
||||||
|
backgroundColor: currentTheme.calenderBg,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{Array.from({ length: 6 }).map((_, i) => (
|
||||||
|
<View
|
||||||
|
key={i}
|
||||||
|
className="w-full flex-1 flex-row justify-around items-center gap-2"
|
||||||
|
>
|
||||||
|
{Array.from({ length: 7 }).map((_, j) => (
|
||||||
|
<SingleDay
|
||||||
|
key={j}
|
||||||
|
date={createDateFromOffset(i * 7 + j - dateOffset)}
|
||||||
|
month={props.month}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
type SingleDayProps = {
|
type SingleDayProps = {
|
||||||
date: number,
|
date: Date;
|
||||||
}
|
month: Month;
|
||||||
|
};
|
||||||
|
|
||||||
const SingleDay = (props: SingleDayProps) => (
|
const SingleDay = (props: SingleDayProps) => {
|
||||||
<View
|
const isSameMonth = MONTHS[props.date.getMonth()] === props.month;
|
||||||
className="h-full flex-1 aspect-auto rounded-xl"
|
|
||||||
style={{
|
|
||||||
backgroundColor: currentTheme.primeBg,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Text>
|
|
||||||
{props.date}
|
|
||||||
</Text>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
export default Calender;
|
return (
|
||||||
|
<View
|
||||||
|
className="h-full flex-1 aspect-auto rounded-xl items-center"
|
||||||
|
style={{
|
||||||
|
backgroundColor: currentTheme.primeBg,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Text
|
||||||
|
className={
|
||||||
|
`text-xl ` + (isSameMonth ? "text-black" : "text-black/50")
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{props.date.getDate()}
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Calendar;
|
||||||
|
|||||||
Reference in New Issue
Block a user