11import React , { useEffect , useState } from 'react' ;
22import { View , Text , ViewProps , TextProps , StyleSheet , TouchableOpacity , Dimensions , Platform } from 'react-native' ;
3- import { color } from 'src/utils' ;
43import Icon from '../Icon' ;
54import Ellipsis from '../Ellipsis' ;
65import { getMonths , getWeeksArray , daysArrProps , getType , getNameLen } from './utils' ;
6+ import ShowDate from './show' ;
77
88export let MainWidth = Dimensions . get ( 'window' ) . width ;
9+ export let MainHeight = Dimensions . get ( 'window' ) . height ;
910export let newDates = new Date ( ) ;
1011export let toYear = newDates . getFullYear ( ) ;
1112export let toMonth = newDates . getMonth ( ) ;
1213export let toDays = newDates . getDate ( ) ;
13-
1414interface barState {
1515 title ?: string ;
1616 barRightText ?: string ;
1717 barLeftText ?: string ;
1818 onPressBarLeft ?: ( ) => void ;
1919 render ?: React . ReactNode ;
2020}
21+
2122export interface CalendarProps extends ViewProps {
2223 // 日历颜色
2324 color : string ;
2425 //是否显示农历及假日
2526 lunarHoliday : boolean ;
2627 bar ?: barState ;
28+ //农历详情
29+ showLunar : boolean ;
2730}
31+
2832const Calendar = ( props : CalendarProps ) => {
2933 const bars = {
3034 barRightText : 'Menu' ,
@@ -33,15 +37,15 @@ const Calendar = (props: CalendarProps) => {
3337 onPressBarLeft : undefined ,
3438 render : undefined ,
3539 } ;
36- const { color = '#329BCB' , lunarHoliday = false , bar = bars } = props ;
40+ const { color = '#329BCB' , lunarHoliday = false , bar = bars , showLunar = false } = props ;
3741 const { barRightText, title, barLeftText, onPressBarLeft, render } = bar ;
38-
3942 const [ currentYear , setCurrentYear ] = useState < number > ( toYear ) ;
4043 const [ currentMonth , setCurrentMonth ] = useState < number > ( toMonth ) ;
4144 const [ currentDays , setCurrentDays ] = useState < number > ( toDays ) ;
4245 const [ dayData , setDayData ] = useState < number [ ] > ( [ ] ) ;
4346 const [ lastData , setLastData ] = useState < number [ ] > ( [ ] ) ;
4447 const [ nextData , setNextData ] = useState < number [ ] > ( [ ] ) ;
48+ const [ lunarName , setLunarName ] = useState ( '' ) ;
4549
4650 useEffect ( ( ) => {
4751 let toMonths = getMonths ( currentYear , currentMonth , currentDays ) ;
@@ -98,9 +102,14 @@ const Calendar = (props: CalendarProps) => {
98102 ) ;
99103 } ) ;
100104 } ;
105+
101106 const renderDays = ( weekDays : daysArrProps [ ] ) => {
102107 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+
104113 let nameLen = getNameLen ( day . lunarHolidays ) ;
105114 let lineHeight =
106115 lunarHoliday === true && Platform . OS === 'ios' ? 0 : lunarHoliday === true ? 18 : MainWidth / 7 - 4.5 ;
@@ -115,11 +124,11 @@ const Calendar = (props: CalendarProps) => {
115124 < TouchableOpacity
116125 key = { index }
117126 style = {
118- type === 1
127+ lunarAll . type === 1
119128 ? styles . otherMonth
120- : type === 2
129+ : lunarAll . type === 2
121130 ? [ styles . currentMonth , { backgroundColor : color } ]
122- : type === 3
131+ : lunarAll . type === 3
123132 ? [ styles . selectMonth , { borderColor : color } ]
124133 : styles . day
125134 }
@@ -129,7 +138,7 @@ const Calendar = (props: CalendarProps) => {
129138 style = { [
130139 styles . dayText ,
131140 {
132- color : type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : '#000' ,
141+ color : lunarAll . type === 1 ? '#B5B5B5' : lunarAll . type === 2 ? '#fff' : '#000' ,
133142 lineHeight : lineHeight ,
134143 paddingTop : paddingTop ,
135144 } ,
@@ -142,20 +151,22 @@ const Calendar = (props: CalendarProps) => {
142151 style = { [
143152 styles . dayText ,
144153 {
145- color : type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : colorType ,
154+ color : lunarAll . type === 1 ? '#B5B5B5' : lunarAll . type === 2 ? '#fff' : colorType ,
146155 fontSize : 13 ,
147156 } ,
148157 ] }
149158 >
150- { nameLen > 3 ? < Ellipsis maxLen = { 2 } > { day . lunarHolidays } </ Ellipsis > : day . lunarHolidays }
159+ { nameLen > 3 ? < Ellipsis maxLen = { 2 } > { day . lunarHolidays } </ Ellipsis > : day . lunarHolidays || day . lunar }
151160 </ Text >
152161 ) }
153162 </ TouchableOpacity >
154163 ) ;
155164 } ) ;
156165 } ;
157166 const goSelectDay = ( day : daysArrProps ) => {
167+ let lunarName = `农历${ day . lunarMonth } ${ day . lunar } ` ;
158168 if ( day . type === 'current' ) {
169+ setLunarName ( lunarName ) ;
159170 setCurrentDays ( day . monthDays ) ;
160171 } else if ( day . type === 'last' ) {
161172 setCurrentDays ( day . monthDays ) ;
@@ -203,32 +214,35 @@ const Calendar = (props: CalendarProps) => {
203214 } ;
204215
205216 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 >
218230
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 >
227240 </ View >
228- </ View >
229241
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 } /> }
232246 </ View >
233247 ) ;
234248} ;
0 commit comments