1
1
import React , { useEffect , useState } from 'react' ;
2
2
import { View , Text , ViewProps , TextProps , StyleSheet , TouchableOpacity , Dimensions , Platform } from 'react-native' ;
3
- import { color } from 'src/utils' ;
4
3
import Icon from '../Icon' ;
5
4
import Ellipsis from '../Ellipsis' ;
6
5
import { getMonths , getWeeksArray , daysArrProps , getType , getNameLen } from './utils' ;
6
+ import ShowDate from './show' ;
7
7
8
8
export let MainWidth = Dimensions . get ( 'window' ) . width ;
9
+ export let MainHeight = Dimensions . get ( 'window' ) . height ;
9
10
export let newDates = new Date ( ) ;
10
11
export let toYear = newDates . getFullYear ( ) ;
11
12
export let toMonth = newDates . getMonth ( ) ;
12
13
export let toDays = newDates . getDate ( ) ;
13
-
14
14
interface barState {
15
15
title ?: string ;
16
16
barRightText ?: string ;
17
17
barLeftText ?: string ;
18
18
onPressBarLeft ?: ( ) => void ;
19
19
render ?: React . ReactNode ;
20
20
}
21
+
21
22
export interface CalendarProps extends ViewProps {
22
23
// 日历颜色
23
24
color : string ;
24
25
//是否显示农历及假日
25
26
lunarHoliday : boolean ;
26
27
bar ?: barState ;
28
+ //农历详情
29
+ showLunar : boolean ;
27
30
}
31
+
28
32
const Calendar = ( props : CalendarProps ) => {
29
33
const bars = {
30
34
barRightText : 'Menu' ,
@@ -33,15 +37,15 @@ const Calendar = (props: CalendarProps) => {
33
37
onPressBarLeft : undefined ,
34
38
render : undefined ,
35
39
} ;
36
- const { color = '#329BCB' , lunarHoliday = false , bar = bars } = props ;
40
+ const { color = '#329BCB' , lunarHoliday = false , bar = bars , showLunar = false } = props ;
37
41
const { barRightText, title, barLeftText, onPressBarLeft, render } = bar ;
38
-
39
42
const [ currentYear , setCurrentYear ] = useState < number > ( toYear ) ;
40
43
const [ currentMonth , setCurrentMonth ] = useState < number > ( toMonth ) ;
41
44
const [ currentDays , setCurrentDays ] = useState < number > ( toDays ) ;
42
45
const [ dayData , setDayData ] = useState < number [ ] > ( [ ] ) ;
43
46
const [ lastData , setLastData ] = useState < number [ ] > ( [ ] ) ;
44
47
const [ nextData , setNextData ] = useState < number [ ] > ( [ ] ) ;
48
+ const [ lunarName , setLunarName ] = useState ( '' ) ;
45
49
46
50
useEffect ( ( ) => {
47
51
let toMonths = getMonths ( currentYear , currentMonth , currentDays ) ;
@@ -98,9 +102,14 @@ const Calendar = (props: CalendarProps) => {
98
102
) ;
99
103
} ) ;
100
104
} ;
105
+
101
106
const renderDays = ( weekDays : daysArrProps [ ] ) => {
102
107
return weekDays . map ( ( day , index ) => {
103
- let type = getType ( day , currentYear , currentMonth , currentDays , toYear , toMonth , toDays ) ;
108
+ let lunarAll = getType ( day , currentYear , currentMonth , currentDays , toYear , toMonth , toDays ) ;
109
+ if ( lunarAll . lunarShow !== '' && lunarName === '' ) {
110
+ setLunarName ( lunarAll . lunarShow ) ;
111
+ }
112
+
104
113
let nameLen = getNameLen ( day . lunarHolidays ) ;
105
114
let lineHeight =
106
115
lunarHoliday === true && Platform . OS === 'ios' ? 0 : lunarHoliday === true ? 18 : MainWidth / 7 - 4.5 ;
@@ -115,11 +124,11 @@ const Calendar = (props: CalendarProps) => {
115
124
< TouchableOpacity
116
125
key = { index }
117
126
style = {
118
- type === 1
127
+ lunarAll . type === 1
119
128
? styles . otherMonth
120
- : type === 2
129
+ : lunarAll . type === 2
121
130
? [ styles . currentMonth , { backgroundColor : color } ]
122
- : type === 3
131
+ : lunarAll . type === 3
123
132
? [ styles . selectMonth , { borderColor : color } ]
124
133
: styles . day
125
134
}
@@ -129,7 +138,7 @@ const Calendar = (props: CalendarProps) => {
129
138
style = { [
130
139
styles . dayText ,
131
140
{
132
- color : type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : '#000' ,
141
+ color : lunarAll . type === 1 ? '#B5B5B5' : lunarAll . type === 2 ? '#fff' : '#000' ,
133
142
lineHeight : lineHeight ,
134
143
paddingTop : paddingTop ,
135
144
} ,
@@ -142,20 +151,22 @@ const Calendar = (props: CalendarProps) => {
142
151
style = { [
143
152
styles . dayText ,
144
153
{
145
- color : type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : colorType ,
154
+ color : lunarAll . type === 1 ? '#B5B5B5' : lunarAll . type === 2 ? '#fff' : colorType ,
146
155
fontSize : 13 ,
147
156
} ,
148
157
] }
149
158
>
150
- { nameLen > 3 ? < Ellipsis maxLen = { 2 } > { day . lunarHolidays } </ Ellipsis > : day . lunarHolidays }
159
+ { nameLen > 3 ? < Ellipsis maxLen = { 2 } > { day . lunarHolidays } </ Ellipsis > : day . lunarHolidays || day . lunar }
151
160
</ Text >
152
161
) }
153
162
</ TouchableOpacity >
154
163
) ;
155
164
} ) ;
156
165
} ;
157
166
const goSelectDay = ( day : daysArrProps ) => {
167
+ let lunarName = `农历${ day . lunarMonth } ${ day . lunar } ` ;
158
168
if ( day . type === 'current' ) {
169
+ setLunarName ( lunarName ) ;
159
170
setCurrentDays ( day . monthDays ) ;
160
171
} else if ( day . type === 'last' ) {
161
172
setCurrentDays ( day . monthDays ) ;
@@ -203,32 +214,35 @@ const Calendar = (props: CalendarProps) => {
203
214
} ;
204
215
205
216
return (
206
- < View style = { { flex : 1 , backgroundColor : '#fff' } } >
207
- { renderBar }
208
- < View style = { styles . calendarHeader } >
209
- < View style = { styles . calendarHeaderItem } >
210
- < TouchableOpacity onPress = { ( ) => getCurrentYear ( 'last' ) } >
211
- < Icon name = "left" size = { 20 } color = { '#333' } />
212
- </ TouchableOpacity >
213
- < Text style = { styles . calendarHeaderText } > { currentYear } 年</ Text >
214
- < TouchableOpacity onPress = { ( ) => getCurrentYear ( 'next' ) } >
215
- < Icon name = "right" size = { 20 } color = { '#333' } />
216
- </ TouchableOpacity >
217
- </ View >
217
+ < View style = { { flex : 1 , position : 'relative' } } >
218
+ < View style = { { flex : 1 , backgroundColor : '#fff' } } >
219
+ { renderBar }
220
+ < View style = { styles . calendarHeader } >
221
+ < View style = { styles . calendarHeaderItem } >
222
+ < TouchableOpacity onPress = { ( ) => getCurrentYear ( 'last' ) } >
223
+ < Icon name = "left" size = { 20 } color = { '#333' } />
224
+ </ TouchableOpacity >
225
+ < Text style = { styles . calendarHeaderText } > { currentYear } 年</ Text >
226
+ < TouchableOpacity onPress = { ( ) => getCurrentYear ( 'next' ) } >
227
+ < Icon name = "right" size = { 20 } color = { '#333' } />
228
+ </ TouchableOpacity >
229
+ </ View >
218
230
219
- < View style = { styles . calendarHeaderItem } >
220
- < TouchableOpacity onPress = { ( ) => getCurrentMonth ( 'last' ) } >
221
- < Icon name = "left" size = { 20 } color = { '#333' } />
222
- </ TouchableOpacity >
223
- < Text style = { styles . calendarHeaderText } > { currentMonth + 1 } 月</ Text >
224
- < TouchableOpacity onPress = { ( ) => getCurrentMonth ( 'next' ) } >
225
- < Icon name = "right" size = { 20 } color = { '#333' } />
226
- </ TouchableOpacity >
231
+ < View style = { styles . calendarHeaderItem } >
232
+ < TouchableOpacity onPress = { ( ) => getCurrentMonth ( 'last' ) } >
233
+ < Icon name = "left" size = { 20 } color = { '#333' } />
234
+ </ TouchableOpacity >
235
+ < Text style = { styles . calendarHeaderText } > { currentMonth + 1 } 月</ Text >
236
+ < TouchableOpacity onPress = { ( ) => getCurrentMonth ( 'next' ) } >
237
+ < Icon name = "right" size = { 20 } color = { '#333' } />
238
+ </ TouchableOpacity >
239
+ </ View >
227
240
</ View >
228
- </ View >
229
241
230
- < View style = { styles . calendarWeekdays } > { renderWeekDays ( ) } </ View >
231
- < View style = { styles . calendarDays } > { renderWeeks ( ) } </ View >
242
+ < View style = { styles . calendarWeekdays } > { renderWeekDays ( ) } </ View >
243
+ < View style = { styles . calendarDays } > { renderWeeks ( ) } </ View >
244
+ </ View >
245
+ { showLunar === true && < ShowDate iconColor = { color } lunar = { lunarName } /> }
232
246
</ View >
233
247
) ;
234
248
} ;
0 commit comments