11import PropTypes from 'prop-types' ;
2- import memoize from 'memoize-one' ;
32import XDate from 'xdate' ;
43
5- import React , { Component } from 'react' ;
4+ import React , { useRef , useMemo , useContext } from 'react' ;
65import { Text , View } from 'react-native' ;
76
87import { Theme } from '../types' ;
9- import { extractComponentProps } from '../componentUpdater ' ;
10- import { formatNumbers , sameMonth } from '../dateutils ' ;
8+ import { formatNumbers } from '../dateutils ' ;
9+ import { getCalendarDateString } from '../services ' ;
1110import Calendar , { CalendarProps } from '../calendar' ;
1211import styleConstructor from './style' ;
13- import { getCalendarDateString } from '../services ' ;
12+ import CalendarContext from '../expandableCalendar/Context ' ;
1413
1514export type CalendarListItemProps = CalendarProps & {
1615 item : any ;
@@ -21,120 +20,100 @@ export type CalendarListItemProps = CalendarProps & {
2120 scrollToMonth ?: ( date : XDate ) => void ;
2221} ;
2322
24- type CalendarListItemState = {
25- hideArrows : boolean ;
26- hideExtraDays : boolean ;
27- } ;
28-
29- class CalendarListItem extends Component < CalendarListItemProps , CalendarListItemState > {
30- static displayName = 'CalendarListItem' ;
31-
32- static propTypes = {
33- ...Calendar . propTypes ,
34- item : PropTypes . any ,
35- calendarWidth : PropTypes . number ,
36- calendarHeight : PropTypes . number ,
37- horizontal : PropTypes . bool
38- } ;
39-
40- static defaultProps = {
41- hideArrows : true ,
42- hideExtraDays : true
43- } ;
44-
45- style : any ;
46-
47- constructor ( props : CalendarListItemProps ) {
48- super ( props ) ;
49-
50- this . style = styleConstructor ( props . theme ) ;
51- }
52-
53- shouldComponentUpdate ( nextProps : CalendarListItemProps ) {
54- const r1 = this . props . item ;
55- const r2 = nextProps . item ;
56-
57- return ! sameMonth ( r1 , r2 ) || ! ! ( r2 . propBump && r2 . propBump !== r1 . propBump ) ;
58- }
59-
60- onPressArrowLeft = ( method : ( ) => void , month : XDate ) => {
61- const { onPressArrowLeft, scrollToMonth} = this . props ;
62- const monthClone = month . clone ( ) ;
63-
64- if ( onPressArrowLeft ) {
65- onPressArrowLeft ( method , monthClone ) ;
66- } else if ( scrollToMonth ) {
67- const currentMonth = monthClone . getMonth ( ) ;
68- monthClone . addMonths ( - 1 ) ;
69-
70- // Make sure we actually get the previous month, not just 30 days before currentMonth.
71- while ( monthClone . getMonth ( ) === currentMonth ) {
72- monthClone . setDate ( monthClone . getDate ( ) - 1 ) ;
23+ const CalendarListItem = ( props : CalendarListItemProps ) => {
24+ const {
25+ theme,
26+ item,
27+ scrollToMonth,
28+ horizontal,
29+ calendarHeight,
30+ calendarWidth,
31+ testID,
32+ style : propsStyle ,
33+ headerStyle,
34+ onPressArrowLeft,
35+ onPressArrowRight
36+ } = props ;
37+ const context = useContext ( CalendarContext ) ;
38+
39+ const style = useRef ( styleConstructor ( theme ) ) ;
40+ const calendarStyle = useMemo ( ( ) => {
41+ return [
42+ {
43+ width : calendarWidth ,
44+ minHeight : calendarHeight
45+ } ,
46+ style . current . calendar ,
47+ propsStyle
48+ ] ;
49+ } , [ calendarWidth , calendarHeight , propsStyle ] ) ;
50+
51+ const _onPressArrowLeft = ( method : ( ) => void , month ?: XDate ) => {
52+ const monthClone = month ?. clone ( ) ;
53+ if ( monthClone ) {
54+ if ( onPressArrowLeft ) {
55+ onPressArrowLeft ( method , monthClone ) ;
56+ } else if ( scrollToMonth ) {
57+ const currentMonth = monthClone . getMonth ( ) ;
58+ monthClone . addMonths ( - 1 ) ;
59+
60+ // Make sure we actually get the previous month, not just 30 days before currentMonth.
61+ while ( monthClone . getMonth ( ) === currentMonth ) {
62+ monthClone . setDate ( monthClone . getDate ( ) - 1 ) ;
63+ }
64+ scrollToMonth ( monthClone ) ;
7365 }
74-
75- scrollToMonth ( monthClone ) ;
7666 }
7767 } ;
7868
79- onPressArrowRight = ( method : ( ) => void , month : XDate ) => {
80- const { onPressArrowRight , scrollToMonth } = this . props ;
81- const monthClone = month . clone ( ) ;
82-
83- if ( onPressArrowRight ) {
84- onPressArrowRight ( method , monthClone ) ;
85- } else if ( scrollToMonth ) {
86- monthClone . addMonths ( 1 ) ;
87- scrollToMonth ( monthClone ) ;
69+ const _onPressArrowRight = ( method : ( ) => void , month ? : XDate ) => {
70+ const monthClone = month ?. clone ( ) ;
71+ if ( monthClone ) {
72+ if ( onPressArrowRight ) {
73+ onPressArrowRight ( method , monthClone ) ;
74+ } else if ( scrollToMonth ) {
75+ monthClone . addMonths ( 1 ) ;
76+ scrollToMonth ( monthClone ) ;
77+ }
8878 }
8979 } ;
9080
91- getCalendarStyle = memoize ( ( width , height , style ) => {
92- return [ { width, minHeight : height } , this . style . calendar , style ] ;
93- } ) ;
94-
95- render ( ) {
96- const {
97- item,
98- horizontal,
99- calendarHeight,
100- calendarWidth,
101- testID,
102- style,
103- headerStyle,
104- onPressArrowLeft,
105- onPressArrowRight,
106- // @ts -expect-error
107- context
108- } = this . props ;
109- const calendarProps = extractComponentProps ( Calendar , this . props ) ;
110- const calStyle = this . getCalendarStyle ( calendarWidth , calendarHeight , style ) ;
111-
112- if ( item . getTime ) {
113- return (
114- < Calendar
115- { ...calendarProps }
116- testID = { testID }
117- current = { getCalendarDateString ( item . toString ( ) ) }
118- style = { calStyle }
119- headerStyle = { horizontal ? headerStyle : undefined }
120- disableMonthChange
121- onPressArrowLeft = { horizontal ? this . onPressArrowLeft : onPressArrowLeft }
122- onPressArrowRight = { horizontal ? this . onPressArrowRight : onPressArrowRight }
123- context = { context }
124- />
125- ) ;
126- } else {
127- const text = formatNumbers ( item . toString ( ) ) ;
128-
129- return (
130- < View style = { [ { height : calendarHeight , width : calendarWidth } , this . style . placeholder ] } >
131- < Text allowFontScaling = { false } style = { this . style . placeholderText } >
132- { text }
133- </ Text >
134- </ View >
135- ) ;
81+ if ( item . getTime ) {
82+ return (
83+ < Calendar
84+ { ...props }
85+ testID = { testID }
86+ current = { getCalendarDateString ( item . toString ( ) ) }
87+ style = { calendarStyle }
88+ headerStyle = { horizontal ? headerStyle : undefined }
89+ disableMonthChange
90+ onPressArrowLeft = { horizontal ? _onPressArrowLeft : onPressArrowLeft }
91+ onPressArrowRight = { horizontal ? _onPressArrowRight : onPressArrowRight }
92+ context = { context } // ???
93+ />
94+ ) ;
95+ } else {
96+ const text = formatNumbers ( item . toString ( ) ) ;
97+ return (
98+ < View style = { [ { height : calendarHeight , width : calendarWidth } , style . current . placeholder ] } >
99+ < Text allowFontScaling = { false } style = { style . current . placeholderText } >
100+ { text }
101+ </ Text >
102+ </ View >
103+ ) ;
136104 }
137- }
138- }
105+ } ;
139106
140107export default CalendarListItem ;
108+ CalendarListItem . displayName = 'CalendarListItem' ;
109+ CalendarListItem . propTypes = {
110+ ...Calendar . propTypes ,
111+ item : PropTypes . any ,
112+ calendarWidth : PropTypes . number ,
113+ calendarHeight : PropTypes . number ,
114+ horizontal : PropTypes . bool
115+ } ;
116+ CalendarListItem . defaultProps = {
117+ hideArrows : true ,
118+ hideExtraDays : true
119+ } ;
0 commit comments