forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.tsx
54 lines (50 loc) · 1.54 KB
/
calendar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useColorScheme } from 'nativewind';
import React from 'react';
import { ScrollView } from 'react-native';
import { Calendar, LocaleConfig } from '~/components/ui/calendar';
LocaleConfig.defaultLocale = 'en';
export default function CalendarScreen() {
const { colorScheme } = useColorScheme();
const [selectedDate, setSelectedDate] = React.useState('');
const [selectedDates, setSelectedDates] = React.useState<string[]>([]);
return (
<ScrollView contentContainerStyle={{ flex: 1, padding: 16, gap: 32 }}>
<Calendar
onDayPress={(day) => {
setSelectedDate(day.dateString);
}}
markedDates={{
[selectedDate]: {
selected: true,
disableTouchEvent: true,
selectedColor: colorScheme === 'dark' ? '#0ea5e9' : '#0284c7',
},
}}
/>
<Calendar
onDayPress={(day) => {
setSelectedDates((prev) => {
if (prev.includes(day.dateString)) {
return prev.filter((date) => date !== day.dateString);
}
return [...prev, day.dateString];
});
}}
markedDates={getMarkedDates(selectedDates)}
theme={{
todayTextColor: 'orange',
arrowColor: 'orange',
}}
/>
</ScrollView>
);
}
function getMarkedDates(dates: string[]) {
return dates.reduce((acc, date) => {
acc[date] = {
selected: true,
selectedColor: 'orange',
};
return acc;
}, {} as Record<string, { selected: boolean; selectedColor: string }>);
}