|
1 |
| -import { useState } from "react"; |
2 |
| -import { Text, View } from "react-native"; |
3 |
| -import { Calendar, toDateId } from "@marceloterreiro/flash-calendar"; |
| 1 | +import type { CalendarProps } from "@marceloterreiro/flash-calendar"; |
| 2 | +import { View } from "react-native"; |
| 3 | +import { Calendar, useCalendar } from "@marceloterreiro/flash-calendar"; |
4 | 4 |
|
5 |
| -const today = toDateId(new Date()); |
| 5 | +import { Button } from "~/components/ui/button"; |
| 6 | +import { Text } from "~/components/ui/text"; |
| 7 | +import { ChevronLeft } from "~/lib/icons/chevron-left"; |
| 8 | +import { ChevronRight } from "~/lib/icons/chevron-right"; |
| 9 | + |
| 10 | +const DAY_HEIGHT = 25; |
| 11 | +const MONTH_HEADER_HEIGHT = 40; |
| 12 | +const WEEK_DAYS_HEIGHT = 25; |
| 13 | +const FOOTER_HEIGHT = 30; |
| 14 | + |
| 15 | +const BORDER_WIDTH = 1; |
| 16 | + |
| 17 | +interface BasicCalendarProps extends CalendarProps { |
| 18 | + onPreviousMonthPress: () => void; |
| 19 | + onNextMonthPress: () => void; |
| 20 | +} |
| 21 | + |
| 22 | +export function BasicCalendar(props: BasicCalendarProps) { |
| 23 | + const { calendarRowMonth, weekDaysList, weeksList } = useCalendar(props); |
6 | 24 |
|
7 |
| -export function BasicCalendar() { |
8 |
| - const [selectedDate, setSelectedDate] = useState(today); |
9 | 25 | return (
|
10 | 26 | <View>
|
11 |
| - <Text>Selected date: {selectedDate}</Text> |
12 |
| - <Calendar |
13 |
| - calendarActiveDateRanges={[ |
14 |
| - { |
15 |
| - startId: selectedDate, |
16 |
| - endId: selectedDate, |
17 |
| - }, |
18 |
| - ]} |
19 |
| - calendarMonthId={today} |
20 |
| - onCalendarDayPress={setSelectedDate} |
21 |
| - /> |
| 27 | + <Calendar.VStack spacing={props.calendarRowVerticalSpacing}> |
| 28 | + {/* Replaces `Calendar.Row.Month` with a custom implementation */} |
| 29 | + <Calendar.HStack |
| 30 | + alignItems="center" |
| 31 | + justifyContent="space-around" |
| 32 | + width="100%" |
| 33 | + > |
| 34 | + <Button |
| 35 | + onPress={props.onPreviousMonthPress} |
| 36 | + size={"icon"} |
| 37 | + className="bg-transparent active:opacity-50" |
| 38 | + > |
| 39 | + <ChevronLeft className="text-foreground" size={48} /> |
| 40 | + </Button> |
| 41 | + <Text>{calendarRowMonth.split(" ")[0]?.toUpperCase()}</Text> |
| 42 | + <Button |
| 43 | + onPress={props.onNextMonthPress} |
| 44 | + size={"icon"} |
| 45 | + className="bg-transparent active:opacity-50" |
| 46 | + > |
| 47 | + <ChevronRight className="text-foreground" size={48} /> |
| 48 | + </Button> |
| 49 | + </Calendar.HStack> |
| 50 | + |
| 51 | + <Calendar.Row.Week spacing={4}> |
| 52 | + {weekDaysList.map((day, i) => ( |
| 53 | + <Calendar.Item.WeekName height={WEEK_DAYS_HEIGHT} key={i}> |
| 54 | + {day.toUpperCase()} |
| 55 | + </Calendar.Item.WeekName> |
| 56 | + ))} |
| 57 | + <View /> |
| 58 | + </Calendar.Row.Week> |
| 59 | + |
| 60 | + {weeksList.map((week, i) => ( |
| 61 | + <Calendar.Row.Week key={i}> |
| 62 | + {week.map((day) => { |
| 63 | + if (day.isDifferentMonth) { |
| 64 | + return ( |
| 65 | + <Calendar.Item.Day.Container |
| 66 | + dayHeight={DAY_HEIGHT} |
| 67 | + daySpacing={4} |
| 68 | + isStartOfWeek={day.isStartOfWeek} |
| 69 | + key={day.id} |
| 70 | + > |
| 71 | + <Calendar.Item.Empty height={DAY_HEIGHT} /> |
| 72 | + </Calendar.Item.Day.Container> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <Calendar.Item.Day.Container |
| 78 | + dayHeight={DAY_HEIGHT} |
| 79 | + daySpacing={4} |
| 80 | + isStartOfWeek={day.isStartOfWeek} |
| 81 | + key={day.id} |
| 82 | + > |
| 83 | + <Calendar.Item.Day |
| 84 | + height={DAY_HEIGHT} |
| 85 | + metadata={day} |
| 86 | + onPress={props.onCalendarDayPress} |
| 87 | + > |
| 88 | + {day.displayLabel} |
| 89 | + </Calendar.Item.Day> |
| 90 | + </Calendar.Item.Day.Container> |
| 91 | + ); |
| 92 | + })} |
| 93 | + </Calendar.Row.Week> |
| 94 | + ))} |
| 95 | + </Calendar.VStack> |
22 | 96 | </View>
|
23 | 97 | );
|
24 | 98 | }
|
0 commit comments