@@ -9,69 +9,39 @@ import { navigate } from './utils/constants'
9
9
import { inRange } from './utils/eventLevels'
10
10
import { isSelected } from './utils/selection'
11
11
12
- class Agenda extends React . Component {
13
- componentDidMount ( ) {
14
- this . _adjustHeader ( )
15
- }
16
-
17
- componentDidUpdate ( ) {
18
- this . _adjustHeader ( )
19
- }
20
-
21
- render ( ) {
22
- let { length, date, events, accessors, localizer } = this . props
23
- let { messages } = localizer
24
- let end = dates . add ( date , length , 'day' )
25
-
26
- let range = dates . range ( date , end , 'day' )
27
-
28
- events = events . filter ( event => inRange ( event , date , end , accessors ) )
29
-
30
- events . sort ( ( a , b ) => + accessors . start ( a ) - + accessors . start ( b ) )
31
-
32
- return (
33
- < div className = "rbc-agenda-view" >
34
- { events . length !== 0 ? (
35
- < React . Fragment >
36
- < table ref = "header" className = "rbc-agenda-table" >
37
- < thead >
38
- < tr >
39
- < th className = "rbc-header" ref = "dateCol" >
40
- { messages . date }
41
- </ th >
42
- < th className = "rbc-header" ref = "timeCol" >
43
- { messages . time }
44
- </ th >
45
- < th className = "rbc-header" > { messages . event } </ th >
46
- </ tr >
47
- </ thead >
48
- </ table >
49
- < div className = "rbc-agenda-content" ref = "content" >
50
- < table className = "rbc-agenda-table" >
51
- < tbody ref = "tbody" >
52
- { range . map ( ( day , idx ) => this . renderDay ( day , events , idx ) ) }
53
- </ tbody >
54
- </ table >
55
- </ div >
56
- </ React . Fragment >
57
- ) : (
58
- < span className = "rbc-agenda-empty" > { messages . noEventsInRange } </ span >
59
- ) }
60
- </ div >
61
- )
62
- }
63
-
64
- renderDay = ( day , events , dayKey ) => {
65
- let {
66
- selected,
67
- getters,
68
- accessors,
69
- localizer,
70
- components : { event : Event , date : AgendaDate } ,
71
- } = this . props
12
+ function Agenda ( {
13
+ accessors,
14
+ components,
15
+ date,
16
+ events,
17
+ getters,
18
+ length,
19
+ localizer,
20
+ onDoubleClickEvent,
21
+ onSelectEvent,
22
+ selected,
23
+ } ) {
24
+ const headerRef = useRef ( null )
25
+ const dateColRef = useRef ( null )
26
+ const timeColRef = useRef ( null )
27
+ const contentRef = useRef ( null )
28
+ const tbodyRef = useRef ( null )
29
+
30
+ useEffect ( ( ) => {
31
+ _adjustHeader ( )
32
+ } )
33
+
34
+ const renderDay = ( day , events , dayKey ) => {
35
+ const { event : Event , date : AgendaDate } = components
72
36
73
37
events = events . filter ( e =>
74
- inRange ( e , dates . startOf ( day , 'day' ) , dates . endOf ( day , 'day' ) , accessors )
38
+ inRange (
39
+ e ,
40
+ localizer . startOf ( day , 'day' ) ,
41
+ localizer . endOf ( day , 'day' ) ,
42
+ accessors ,
43
+ localizer
44
+ )
75
45
)
76
46
77
47
return events . map ( ( event , idx ) => {
@@ -107,20 +77,22 @@ class Agenda extends React.Component {
107
77
style = { userProps . style }
108
78
>
109
79
{ first }
110
- < td className = "rbc-agenda-time-cell" >
111
- { this . timeRangeLabel ( day , event ) }
112
- </ td >
113
- < td className = "rbc-agenda-event-cell" >
80
+ < td className = "rbc-agenda-time-cell" > { timeRangeLabel ( day , event ) } </ td >
81
+ < td
82
+ className = "rbc-agenda-event-cell"
83
+ onClick = { e => onSelectEvent && onSelectEvent ( event , e ) }
84
+ onDoubleClick = { e =>
85
+ onDoubleClickEvent && onDoubleClickEvent ( event , e )
86
+ }
87
+ >
114
88
{ Event ? < Event event = { event } title = { title } /> : title }
115
89
</ td >
116
90
</ tr >
117
91
)
118
92
} , [ ] )
119
93
}
120
94
121
- timeRangeLabel = ( day , event ) => {
122
- let { accessors, localizer, components } = this . props
123
-
95
+ const timeRangeLabel = ( day , event ) => {
124
96
let labelClass = '' ,
125
97
TimeComponent = components . time ,
126
98
label = localizer . messages . allDay
@@ -129,17 +101,19 @@ class Agenda extends React.Component {
129
101
let start = accessors . start ( event )
130
102
131
103
if ( ! accessors . allDay ( event ) ) {
132
- if ( dates . eq ( start , end , 'day' ) ) {
104
+ if ( localizer . eq ( start , end ) ) {
105
+ label = localizer . format ( start , 'agendaTimeFormat' )
106
+ } else if ( localizer . isSameDate ( start , end ) ) {
133
107
label = localizer . format ( { start, end } , 'agendaTimeRangeFormat' )
134
- } else if ( dates . eq ( day , start , 'day' ) ) {
108
+ } else if ( localizer . isSameDate ( day , start ) ) {
135
109
label = localizer . format ( start , 'agendaTimeFormat' )
136
- } else if ( dates . eq ( day , end , 'day' ) ) {
110
+ } else if ( localizer . isSameDate ( day , end ) ) {
137
111
label = localizer . format ( end , 'agendaTimeFormat' )
138
112
}
139
113
}
140
114
141
- if ( dates . gt ( day , start , 'day' ) ) labelClass = 'rbc-continues-prior'
142
- if ( dates . lt ( day , end , 'day' ) ) labelClass += ' rbc-continues-after'
115
+ if ( localizer . gt ( day , start , 'day' ) ) labelClass = 'rbc-continues-prior'
116
+ if ( localizer . lt ( day , end , 'day' ) ) labelClass += ' rbc-continues-after'
143
117
144
118
return (
145
119
< span className = { labelClass . trim ( ) } >
@@ -152,74 +126,125 @@ class Agenda extends React.Component {
152
126
)
153
127
}
154
128
155
- _adjustHeader = ( ) => {
156
- if ( ! this . refs . tbody ) return
129
+ const _adjustHeader = ( ) => {
130
+ if ( ! tbodyRef . current ) return
157
131
158
- let header = this . refs . header
159
- let firstRow = this . refs . tbody . firstChild
132
+ let header = headerRef . current
133
+ let firstRow = tbodyRef . current . firstChild
160
134
161
135
if ( ! firstRow ) return
162
136
163
137
let isOverflowing =
164
- this . refs . content . scrollHeight > this . refs . content . clientHeight
165
- let widths = this . _widths || [ ]
138
+ contentRef . current . scrollHeight > contentRef . current . clientHeight
139
+
140
+ let _widths = [ ]
141
+ let widths = _widths
166
142
167
- this . _widths = [
168
- getWidth ( firstRow . children [ 0 ] ) ,
169
- getWidth ( firstRow . children [ 1 ] ) ,
170
- ]
143
+ _widths = [ getWidth ( firstRow . children [ 0 ] ) , getWidth ( firstRow . children [ 1 ] ) ]
171
144
172
- if ( widths [ 0 ] !== this . _widths [ 0 ] || widths [ 1 ] !== this . _widths [ 1 ] ) {
173
- this . refs . dateCol . style . width = this . _widths [ 0 ] + 'px'
174
- this . refs . timeCol . style . width = this . _widths [ 1 ] + 'px'
145
+ if ( widths [ 0 ] !== _widths [ 0 ] || widths [ 1 ] !== _widths [ 1 ] ) {
146
+ dateColRef . current . style . width = _widths [ 0 ] + 'px'
147
+ timeColRef . current . style . width = _widths [ 1 ] + 'px'
175
148
}
176
149
177
150
if ( isOverflowing ) {
178
- classes . addClass ( header , 'rbc-header-overflowing' )
151
+ addClass ( header , 'rbc-header-overflowing' )
179
152
header . style . marginRight = scrollbarSize ( ) + 'px'
180
153
} else {
181
- classes . removeClass ( header , 'rbc-header-overflowing' )
154
+ removeClass ( header , 'rbc-header-overflowing' )
182
155
}
183
156
}
184
- }
185
157
186
- Agenda . propTypes = {
187
- events : PropTypes . array ,
188
- date : PropTypes . instanceOf ( Date ) ,
189
- length : PropTypes . number . isRequired ,
158
+ let { messages } = localizer
159
+ let end = localizer . add ( date , length , 'day' )
190
160
191
- selected : PropTypes . object ,
161
+ let range = localizer . range ( date , end , 'day' )
162
+
163
+ events = events . filter ( event =>
164
+ inRange (
165
+ event ,
166
+ localizer . startOf ( date , 'day' ) ,
167
+ localizer . endOf ( end , 'day' ) ,
168
+ accessors ,
169
+ localizer
170
+ )
171
+ )
172
+
173
+ events . sort ( ( a , b ) => + accessors . start ( a ) - + accessors . start ( b ) )
174
+
175
+ return (
176
+ < div className = "rbc-agenda-view" >
177
+ { events . length !== 0 ? (
178
+ < React . Fragment >
179
+ < table ref = { headerRef } className = "rbc-agenda-table" >
180
+ < thead >
181
+ < tr >
182
+ < th className = "rbc-header" ref = { dateColRef } >
183
+ { messages . date }
184
+ </ th >
185
+ < th className = "rbc-header" ref = { timeColRef } >
186
+ { messages . time }
187
+ </ th >
188
+ < th className = "rbc-header" > { messages . event } </ th >
189
+ </ tr >
190
+ </ thead >
191
+ </ table >
192
+ < div className = "rbc-agenda-content" ref = { contentRef } >
193
+ < table className = "rbc-agenda-table" >
194
+ < tbody ref = { tbodyRef } >
195
+ { range . map ( ( day , idx ) => renderDay ( day , events , idx ) ) }
196
+ </ tbody >
197
+ </ table >
198
+ </ div >
199
+ </ React . Fragment >
200
+ ) : (
201
+ < span className = "rbc-agenda-empty" > { messages . noEventsInRange } </ span >
202
+ ) }
203
+ </ div >
204
+ )
205
+ }
192
206
207
+ Agenda . propTypes = {
193
208
accessors : PropTypes . object . isRequired ,
194
209
components : PropTypes . object . isRequired ,
210
+ date : PropTypes . instanceOf ( Date ) ,
211
+ events : PropTypes . array ,
195
212
getters : PropTypes . object . isRequired ,
213
+ length : PropTypes . number . isRequired ,
196
214
localizer : PropTypes . object . isRequired ,
215
+ onSelectEvent : PropTypes . func ,
216
+ onDoubleClickEvent : PropTypes . func ,
217
+ selected : PropTypes . object ,
197
218
}
198
219
199
220
Agenda . defaultProps = {
200
221
length : 30 ,
201
222
}
202
223
203
- Agenda . range = ( start , { length = Agenda . defaultProps . length } ) => {
204
- let end = dates . add ( start , length , 'day' )
224
+ Agenda . range = ( start , { length = Agenda . defaultProps . length , localizer } ) => {
225
+ let end = localizer . add ( start , length , 'day' )
205
226
return { start, end }
206
227
}
207
228
208
- Agenda . navigate = ( date , action , { length = Agenda . defaultProps . length } ) => {
229
+ Agenda . navigate = (
230
+ date ,
231
+ action ,
232
+ { length = Agenda . defaultProps . length , localizer }
233
+ ) => {
209
234
switch ( action ) {
210
235
case navigate . PREVIOUS :
211
- return dates . add ( date , - length , 'day' )
236
+ return localizer . add ( date , - length , 'day' )
212
237
213
238
case navigate . NEXT :
214
- return dates . add ( date , length , 'day' )
239
+ return localizer . add ( date , length , 'day' )
215
240
216
241
default :
217
242
return date
218
243
}
219
244
}
220
245
221
246
Agenda . title = ( start , { length = Agenda . defaultProps . length , localizer } ) => {
222
- let end = dates . add ( start , length , 'day' )
247
+ let end = localizer . add ( start , length , 'day' )
223
248
return localizer . format ( { start, end } , 'agendaHeaderFormat' )
224
249
}
225
250
0 commit comments