1-
21import React , { useState } from 'react' ;
3- import { View , Text , TouchableOpacity , StyleSheet , Image } from 'react-native' ;
2+ import { View , Text , TouchableOpacity , StyleSheet , Image } from 'react-native' ;
43import { format , addMonths , subMonths , startOfMonth , endOfMonth , startOfWeek , endOfWeek , eachDayOfInterval , isSameDay } from 'date-fns' ;
54import { useFonts } from 'expo-font' ;
65
7- const CalendarScreen : React . FC = ( ) => {
6+ const CalendarScreen : React . FC < { onDateChange : ( date : string ) => void } > = ( { onDateChange } ) => {
87 const [ currentDate , setCurrentDate ] = useState ( new Date ( ) ) ;
98 const [ selectedDate , setSelectedDate ] = useState < Date | null > ( new Date ( ) ) ;
109
11- const [ fontLoaded ] = useFonts ( {
12- 'UrbanistBold' :require ( '../../../assets/fonts/Urbanist-Bold.ttf' ) ,
13- 'UrbanistMedium' :require ( "../../../assets/fonts/Urbanist-Medium.ttf" ) ,
14- 'Urbanist-SemiBold' :require ( "../../../assets/fonts/Urbanist-SemiBold.ttf" )
15- } )
10+ const [ fontLoaded ] = useFonts ( {
11+ 'UrbanistBold' : require ( '../../../assets/fonts/Urbanist-Bold.ttf' ) ,
12+ 'UrbanistMedium' : require ( "../../../assets/fonts/Urbanist-Medium.ttf" ) ,
13+ 'Urbanist-SemiBold' : require ( "../../../assets/fonts/Urbanist-SemiBold.ttf" )
14+ } ) ;
1615
1716 const handlePrevMonth = ( ) => {
1817 setCurrentDate ( prevDate => subMonths ( prevDate , 1 ) ) ;
@@ -23,24 +22,25 @@ const CalendarScreen: React.FC = () => {
2322 } ;
2423
2524 const handleDayPress = ( day : Date ) => {
26- setSelectedDate ( prevSelectedDate =>
27- isSameDay ( prevSelectedDate || new Date ( ) , day ) ? null : day
28- ) ;
25+ const newSelectedDate = isSameDay ( selectedDate || new Date ( ) , day ) ? null : day ;
26+ setSelectedDate ( newSelectedDate ) ;
27+ if ( newSelectedDate ) {
28+ onDateChange ( format ( newSelectedDate , 'yyyy-MM-dd' ) ) ;
29+ }
2930 } ;
30-
3131
3232 const renderHeader = ( ) => {
3333 return (
3434 < View style = { styles . header } >
3535 < Text style = { styles . headerText } > { format ( currentDate , 'MMMM yyyy' ) } </ Text >
36- < View className = 'flex flex-row gap-1' >
37- < TouchableOpacity onPress = { handlePrevMonth } >
38- < Image source = { require ( "../../../assets/appointmentIcon/Arcl1.png" ) } />
39- </ TouchableOpacity >
40- < TouchableOpacity onPress = { handleNextMonth } >
41- < Image source = { require ( "../../../assets/appointmentIcon/Arcal.png" ) } />
42- </ TouchableOpacity >
43- </ View >
36+ < View className = 'flex flex-row gap-1' >
37+ < TouchableOpacity onPress = { handlePrevMonth } >
38+ < Image source = { require ( "../../../assets/appointmentIcon/Arcl1.png" ) } />
39+ </ TouchableOpacity >
40+ < TouchableOpacity onPress = { handleNextMonth } >
41+ < Image source = { require ( "../../../assets/appointmentIcon/Arcal.png" ) } />
42+ </ TouchableOpacity >
43+ </ View >
4444 </ View >
4545 ) ;
4646 } ;
@@ -50,7 +50,7 @@ const CalendarScreen: React.FC = () => {
5050 return (
5151 < View style = { styles . daysOfWeek } >
5252 { daysOfWeek . map ( day => (
53- < Text key = { day } className = 'font-UrbanistBold' style = { styles . dayOfWeekText } > { day } </ Text >
53+ < Text key = { day } style = { styles . dayOfWeekText } > { day } </ Text >
5454 ) ) }
5555 </ View >
5656 ) ;
@@ -69,10 +69,10 @@ const CalendarScreen: React.FC = () => {
6969 onPress = { ( ) => handleDayPress ( day ) }
7070 style = { [
7171 styles . dayButton ,
72- isSameDay ( selectedDate || new Date ( ) , day ) && styles . selectedDayButton ,
72+ selectedDate && isSameDay ( selectedDate , day ) && styles . selectedDayButton ,
7373 ] }
7474 >
75- < Text style = { [ styles . dayText , isSameDay ( selectedDate || new Date ( ) , day ) && styles . selectedDayText ] } >
75+ < Text style = { [ styles . dayText , selectedDate && isSameDay ( selectedDate , day ) && styles . selectedDayText ] } >
7676 { format ( day , 'd' ) }
7777 </ Text >
7878 </ TouchableOpacity >
@@ -81,9 +81,10 @@ const CalendarScreen: React.FC = () => {
8181 ) ;
8282 } ;
8383
84- if ( ! fontLoaded ) {
85- return null
86- }
84+ if ( ! fontLoaded ) {
85+ return null ;
86+ }
87+
8788 return (
8889 < View style = { styles . container } >
8990 { renderHeader ( ) }
@@ -96,33 +97,33 @@ const CalendarScreen: React.FC = () => {
9697const styles = StyleSheet . create ( {
9798 container : {
9899 padding : 20 ,
99- backgroundColor :'#d3dae7' ,
100- borderRadius :20 ,
101- display :'flex' ,
102- justifyContent :'center' ,
103- alignItems :'center'
100+ backgroundColor : '#d3dae7' ,
101+ borderRadius : 20 ,
102+ display : 'flex' ,
103+ justifyContent : 'center' ,
104+ alignItems : 'center'
104105 } ,
105106 header : {
106107 flexDirection : 'row' ,
107108 justifyContent : 'space-between' ,
108109 alignItems : 'center' ,
109- marginBottom :5 ,
110- width :315
110+ marginBottom : 5 ,
111+ width : 315
111112 } ,
112- navButton : {
113- fontSize : 16 ,
114- fontWeight : 'bold' ,
113+ navButtons : {
114+ flexDirection : 'row' ,
115+ gap : 1 ,
115116 } ,
116117 headerText : {
117118 fontSize : 18 ,
118- fontFamily :'UrbanistBold'
119+ fontFamily : 'UrbanistBold'
119120 } ,
120121 daysOfWeek : {
121122 flexDirection : 'row' ,
122123 flexWrap : 'wrap' ,
123- marginTop :10 ,
124- gap :16 ,
125- marginBottom :10
124+ marginTop : 10 ,
125+ gap : 16 ,
126+ marginBottom : 10
126127 } ,
127128 dayOfWeekText : {
128129 width : 32 ,
@@ -131,8 +132,8 @@ const styles = StyleSheet.create({
131132 days : {
132133 flexDirection : 'row' ,
133134 flexWrap : 'wrap' ,
134- gap :16 ,
135- paddingLeft :5
135+ gap : 16 ,
136+ paddingLeft : 5
136137 } ,
137138 dayButton : {
138139 width : 32 ,
@@ -143,21 +144,19 @@ const styles = StyleSheet.create({
143144 } ,
144145 selectedDayButton : {
145146 backgroundColor : '#246bfd' ,
146- display :'flex' ,
147- justifyContent :'center' ,
148- alignItems :'center'
147+ display : 'flex' ,
148+ justifyContent : 'center' ,
149+ alignItems : 'center'
149150 } ,
150151 dayText : {
151152 width : 60 ,
152153 textAlign : 'center' ,
153- fontFamily :'UrbanistMedium' ,
154- color :'#424242'
154+ fontFamily : 'UrbanistMedium' ,
155+ color : '#424242'
155156 } ,
156157 selectedDayText : {
157158 color : 'white' ,
158159 } ,
159160} ) ;
160161
161162export default CalendarScreen ;
162-
163-
0 commit comments