@@ -21,45 +21,35 @@ import {weekDayNames, sameDate, sameMonth} from '../dateutils';
2121import { AGENDA_CALENDAR_KNOB } from '../testIDs' ;
2222import { VelocityTracker } from '../velocityTracker' ;
2323import { DateData } from '../types' ;
24+ import { getCalendarDateString } from '../services' ;
2425import styleConstructor from './style' ;
26+ import { ReservationsType } from '../types' ;
2527import CalendarList , { CalendarListProps } from '../calendar-list' ;
2628import ReservationList , { ReservationListProps } from './reservation-list' ;
2729
2830
2931const HEADER_HEIGHT = 104 ;
3032const KNOB_HEIGHT = 24 ;
3133
32- export type ReservationItemType = {
33- name : string ;
34- height : number ;
35- day : XDate ;
36- } ;
37-
38- export type ReservationsType = {
39- [ date : string ] : ReservationItemType [ ] ;
40- } ;
41-
4234export type AgendaProps = CalendarListProps & ReservationListProps & {
4335 /** the list of items that have to be displayed in agenda. If you want to render item as empty date
4436 the value of date key has to be an empty array []. If there exists no value for date key it is
4537 considered that the date in question is not yet loaded */
46- items : ReservationsType ;
38+ items ? : ReservationsType ;
4739 /** callback that gets called when items for a certain month should be loaded (month became visible) */
48- loadItemsForMonth ?: ( data : any ) => DateData ;
40+ loadItemsForMonth ?: ( data : DateData ) => void ;
4941 /** callback that fires when the calendar is opened or closed */
5042 onCalendarToggled ?: ( enabled : boolean ) => void ;
51- /** callback that gets called on day press */
52- onDayPress ?: ( data : DateData ) => void ;
5343 /** callback that gets called when day changes while scrolling agenda list */
54- onDayChange ?: ( data : any ) => void ;
44+ onDayChange ?: ( data : DateData ) => void ;
5545 /** specify how agenda knob should look like */
5646 renderKnob ?: ( ) => JSX . Element ;
5747 /** initially selected day */
58- selected : boolean ; //TODO: Should be renamed 'selectedDay'
48+ selected ?: string ; //TODO: Should be renamed 'selectedDay'
5949 /** Hide knob button. Default = false */
60- hideKnob : boolean ;
50+ hideKnob ? : boolean ;
6151 /** When `true` and `hideKnob` prop is `false`, the knob will always be visible and the user will be able to drag the knob up and close the calendar. Default = false */
62- showClosingKnob : boolean ;
52+ showClosingKnob ? : boolean ;
6353}
6454
6555type State = {
@@ -94,8 +84,6 @@ export default class Agenda extends Component<AgendaProps, State> {
9484 loadItemsForMonth : PropTypes . func ,
9585 /** callback that fires when the calendar is opened or closed */
9686 onCalendarToggled : PropTypes . func ,
97- /** callback that gets called on day press */
98- onDayPress : PropTypes . func ,
9987 /** callback that gets called when day changes while scrolling agenda list */
10088 onDayChange : PropTypes . func ,
10189 /** specify how agenda knob should look like */
@@ -111,14 +99,14 @@ export default class Agenda extends Component<AgendaProps, State> {
11199 private style : { [ key : string ] : ViewStyle } ;
112100 private viewHeight : number ;
113101 private viewWidth : number ;
114- private scrollTimeout : any ;
102+ private scrollTimeout ?: ReturnType < typeof setTimeout > ;
115103 private headerState : string ;
116104 private currentMonth : XDate ;
117- private knobTracker : any ;
105+ private knobTracker : VelocityTracker ;
118106 private _isMounted : boolean | undefined ;
119107 private scrollPad : React . RefObject < any > = React . createRef ( ) ;
120108 private calendar : React . RefObject < CalendarList > = React . createRef ( ) ;
121- private knob : React . RefObject < any > = React . createRef ( ) ;
109+ private knob : React . RefObject < View > = React . createRef ( ) ;
122110 public list : React . RefObject < ReservationList > = React . createRef ( ) ;
123111
124112 constructor ( props : AgendaProps ) {
@@ -228,11 +216,11 @@ export default class Agenda extends Component<AgendaProps, State> {
228216 }
229217 }
230218
231- chooseDayFromCalendar = ( d : any ) => {
219+ onDayPress = ( d : DateData ) => {
232220 this . chooseDay ( d , ! this . state . calendarScrollable ) ;
233221 } ;
234222
235- chooseDay ( d : any , optimisticScroll : boolean ) {
223+ chooseDay ( d : DateData , optimisticScroll : boolean ) {
236224 const day = parseDate ( d ) ;
237225
238226 this . setState ( {
@@ -255,14 +243,16 @@ export default class Agenda extends Component<AgendaProps, State> {
255243 this . props . onDayPress ?.( xdateToData ( day ) ) ;
256244 }
257245
258- generateMarkings = memoize ( ( selectedDay , markedDates , items = { } ) => {
246+ generateMarkings = memoize ( ( selectedDay , markedDates , items ) => {
259247 if ( ! markedDates ) {
260248 markedDates = { } ;
261- Object . keys ( items ) . forEach ( key => {
262- if ( items [ key ] && items [ key ] . length ) {
263- markedDates [ key ] = { marked : true } ;
264- }
265- } ) ;
249+ if ( items ) {
250+ Object . keys ( items ) . forEach ( key => {
251+ if ( items [ key ] && items [ key ] . length ) {
252+ markedDates [ key ] = { marked : true } ;
253+ }
254+ } ) ;
255+ }
266256 }
267257
268258 const key = toMarkingFormat ( selectedDay ) ;
@@ -326,7 +316,9 @@ export default class Agenda extends Component<AgendaProps, State> {
326316 this . props . onVisibleMonthsChange ?.( months ) ;
327317
328318 if ( this . props . items && ! this . state . firstReservationLoad ) {
329- clearTimeout ( this . scrollTimeout ) ;
319+ if ( this . scrollTimeout ) {
320+ clearTimeout ( this . scrollTimeout ) ;
321+ }
330322
331323 this . scrollTimeout = setTimeout ( ( ) => {
332324 if ( this . _isMounted ) {
@@ -336,16 +328,13 @@ export default class Agenda extends Component<AgendaProps, State> {
336328 }
337329 } ;
338330
339- onDayChange = ( day : any ) => {
340- const newDate = parseDate ( day ) ;
341- const withAnimation = sameMonth ( newDate , this . state . selectedDay ) ;
342-
331+ onDayChange = ( day : XDate ) => {
332+ const withAnimation = sameMonth ( day , this . state . selectedDay ) ;
343333 this . calendar ?. current ?. scrollToDay ( day , this . calendarOffset ( ) , withAnimation ) ;
344- this . setState ( {
345- selectedDay : newDate
346- } ) ;
334+
335+ this . setState ( { selectedDay : day } ) ;
347336
348- this . props . onDayChange ?.( xdateToData ( newDate ) ) ;
337+ this . props . onDayChange ?.( xdateToData ( day ) ) ;
349338 } ;
350339
351340 renderReservations ( ) {
@@ -355,7 +344,7 @@ export default class Agenda extends Component<AgendaProps, State> {
355344 < ReservationList
356345 { ...reservationListProps }
357346 ref = { this . list }
358- reservations = { this . props . items }
347+ items = { this . props . items }
359348 selectedDay = { this . state . selectedDay }
360349 topDay = { this . state . topDay }
361350 onDayChange = { this . onDayChange }
@@ -372,13 +361,13 @@ export default class Agenda extends Component<AgendaProps, State> {
372361 < CalendarList
373362 { ...calendarListProps }
374363 ref = { this . calendar }
375- current = { this . currentMonth }
364+ current = { getCalendarDateString ( this . currentMonth . toString ( ) ) }
376365 markedDates = { this . generateMarkings ( this . state . selectedDay , markedDates , items ) }
377366 calendarWidth = { this . viewWidth }
378367 scrollEnabled = { this . state . calendarScrollable }
379368 hideExtraDays = { shouldHideExtraDays }
380369 onLayout = { this . onCalendarListLayout }
381- onDayPress = { this . chooseDayFromCalendar }
370+ onDayPress = { this . onDayPress }
382371 onVisibleMonthsChange = { this . onVisibleMonthsChange }
383372 />
384373 ) ;
0 commit comments