1
- import React , { useState , useEffect } from 'react'
1
+ import React , { useState , useEffect , useCallback , useMemo } from 'react'
2
2
import clsx from 'clsx'
3
3
import PropTypes from 'prop-types'
4
4
5
- import * as TimeSlotUtils from './utils/TimeSlots'
5
+ import { getSlotMetrics } from './utils/TimeSlots'
6
6
import TimeSlotGroup from './TimeSlotGroup'
7
7
8
+ /**
9
+ * Since the TimeGutter only displays the 'times' of slots in a day, and is separate
10
+ * from the Day Columns themselves, we check to see if the range contains an offset difference
11
+ * and, if so, change the beginning and end 'date' by a day to properly display the slots times
12
+ * used.
13
+ */
14
+ function adjustForDST ( { min, max, localizer } ) {
15
+ if ( localizer . getTimezoneOffset ( min ) !== localizer . getTimezoneOffset ( max ) ) {
16
+ return {
17
+ start : localizer . add ( min , - 1 , 'day' ) ,
18
+ end : localizer . add ( max , - 1 , 'day' ) ,
19
+ }
20
+ }
21
+ return { start : min , end : max }
22
+ }
23
+
8
24
const TimeGutter = ( {
9
25
min,
10
26
max,
@@ -17,10 +33,15 @@ const TimeGutter = ({
17
33
getters,
18
34
gutterRef,
19
35
} ) => {
36
+ const { start, end } = useMemo (
37
+ ( ) => adjustForDST ( { min, max, localizer } ) ,
38
+ // eslint-disable-next-line react-hooks/exhaustive-deps
39
+ [ min ?. toISOString ( ) , max ?. toISOString ( ) , localizer ]
40
+ )
20
41
const [ slotMetrics , setSlotMetrics ] = useState (
21
- TimeSlotUtils . getSlotMetrics ( {
22
- min,
23
- max,
42
+ getSlotMetrics ( {
43
+ min : start ,
44
+ max : end ,
24
45
timeslots,
25
46
step,
26
47
localizer,
@@ -31,8 +52,8 @@ const TimeGutter = ({
31
52
if ( slotMetrics ) {
32
53
setSlotMetrics (
33
54
slotMetrics . update ( {
34
- min,
35
- max,
55
+ min : start ,
56
+ max : end ,
36
57
timeslots,
37
58
step,
38
59
localizer,
@@ -43,18 +64,21 @@ const TimeGutter = ({
43
64
* We don't want this to fire when slotMetrics is updated as it would recursively bomb
44
65
*/
45
66
// eslint-disable-next-line react-hooks/exhaustive-deps
46
- } , [ min , max , timeslots , step ] )
67
+ } , [ start ?. toISOString ( ) , end ?. toISOString ( ) , timeslots , step ] )
47
68
48
- const renderSlot = ( value , idx ) => {
49
- if ( idx ) return null // don't return the first (0) idx
69
+ const renderSlot = useCallback (
70
+ ( value , idx ) => {
71
+ if ( idx ) return null // don't return the first (0) idx
50
72
51
- const isNow = slotMetrics . dateIsInGroup ( getNow ( ) , idx )
52
- return (
53
- < span className = { clsx ( 'rbc-label' , isNow && 'rbc-now' ) } >
54
- { localizer . format ( value , 'timeGutterFormat' ) }
55
- </ span >
56
- )
57
- }
73
+ const isNow = slotMetrics . dateIsInGroup ( getNow ( ) , idx )
74
+ return (
75
+ < span className = { clsx ( 'rbc-label' , isNow && 'rbc-now' ) } >
76
+ { localizer . format ( value , 'timeGutterFormat' ) }
77
+ </ span >
78
+ )
79
+ } ,
80
+ [ slotMetrics , localizer , getNow ]
81
+ )
58
82
59
83
return (
60
84
< div className = "rbc-time-gutter rbc-time-column" ref = { gutterRef } >
0 commit comments