11import PropTypes from 'prop-types'
2- import React from 'react'
2+ import React , { useRef , useEffect } from 'react'
33import addClass from 'dom-helpers/addClass'
44import removeClass from 'dom-helpers/removeClass'
55import getWidth from 'dom-helpers/width'
@@ -10,75 +10,28 @@ import { navigate } from './utils/constants'
1010import { inRange } from './utils/eventLevels'
1111import { isSelected } from './utils/selection'
1212
13- class Agenda extends React . Component {
14- constructor ( props ) {
15- super ( props )
16- this . headerRef = React . createRef ( )
17- this . dateColRef = React . createRef ( )
18- this . timeColRef = React . createRef ( )
19- this . contentRef = React . createRef ( )
20- this . tbodyRef = React . createRef ( )
21- }
22-
23- componentDidMount ( ) {
24- this . _adjustHeader ( )
25- }
26-
27- componentDidUpdate ( ) {
28- this . _adjustHeader ( )
29- }
30-
31- render ( ) {
32- let { length, date, events, accessors, localizer } = this . props
33- let { messages } = localizer
34- let end = dates . add ( date , length , 'day' )
35-
36- let range = dates . range ( date , end , 'day' )
37-
38- events = events . filter ( event => inRange ( event , date , end , accessors ) )
39-
40- events . sort ( ( a , b ) => + accessors . start ( a ) - + accessors . start ( b ) )
41-
42- return (
43- < div className = "rbc-agenda-view" >
44- { events . length !== 0 ? (
45- < React . Fragment >
46- < table ref = { this . headerRef } className = "rbc-agenda-table" >
47- < thead >
48- < tr >
49- < th className = "rbc-header" ref = { this . dateColRef } >
50- { messages . date }
51- </ th >
52- < th className = "rbc-header" ref = { this . timeColRef } >
53- { messages . time }
54- </ th >
55- < th className = "rbc-header" > { messages . event } </ th >
56- </ tr >
57- </ thead >
58- </ table >
59- < div className = "rbc-agenda-content" ref = { this . contentRef } >
60- < table className = "rbc-agenda-table" >
61- < tbody ref = { this . tbodyRef } >
62- { range . map ( ( day , idx ) => this . renderDay ( day , events , idx ) ) }
63- </ tbody >
64- </ table >
65- </ div >
66- </ React . Fragment >
67- ) : (
68- < span className = "rbc-agenda-empty" > { messages . noEventsInRange } </ span >
69- ) }
70- </ div >
71- )
72- }
73-
74- renderDay = ( day , events , dayKey ) => {
75- let {
76- selected,
77- getters,
78- accessors,
79- localizer,
80- components : { event : Event , date : AgendaDate } ,
81- } = this . props
13+ function Agenda ( {
14+ selected,
15+ getters,
16+ accessors,
17+ localizer,
18+ components,
19+ length,
20+ date,
21+ events,
22+ } ) {
23+ const headerRef = useRef ( null )
24+ const dateColRef = useRef ( null )
25+ const timeColRef = useRef ( null )
26+ const contentRef = useRef ( null )
27+ const tbodyRef = useRef ( null )
28+
29+ useEffect ( ( ) => {
30+ _adjustHeader ( )
31+ } )
32+
33+ const renderDay = ( day , events , dayKey ) => {
34+ const { event : Event , date : AgendaDate } = components
8235
8336 events = events . filter ( e =>
8437 inRange ( e , dates . startOf ( day , 'day' ) , dates . endOf ( day , 'day' ) , accessors )
@@ -117,9 +70,7 @@ class Agenda extends React.Component {
11770 style = { userProps . style }
11871 >
11972 { first }
120- < td className = "rbc-agenda-time-cell" >
121- { this . timeRangeLabel ( day , event ) }
122- </ td >
73+ < td className = "rbc-agenda-time-cell" > { timeRangeLabel ( day , event ) } </ td >
12374 < td className = "rbc-agenda-event-cell" >
12475 { Event ? < Event event = { event } title = { title } /> : title }
12576 </ td >
@@ -128,9 +79,7 @@ class Agenda extends React.Component {
12879 } , [ ] )
12980 }
13081
131- timeRangeLabel = ( day , event ) => {
132- let { accessors, localizer, components } = this . props
133-
82+ const timeRangeLabel = ( day , event ) => {
13483 let labelClass = '' ,
13584 TimeComponent = components . time ,
13685 label = localizer . messages . allDay
@@ -164,27 +113,25 @@ class Agenda extends React.Component {
164113 )
165114 }
166115
167- _adjustHeader = ( ) => {
168- if ( ! this . tbodyRef . current ) return
116+ const _adjustHeader = ( ) => {
117+ if ( ! tbodyRef . current ) return
169118
170- let header = this . headerRef . current
171- let firstRow = this . tbodyRef . current . firstChild
119+ let header = headerRef . current
120+ let firstRow = tbodyRef . current . firstChild
172121
173122 if ( ! firstRow ) return
174123
175124 let isOverflowing =
176- this . contentRef . current . scrollHeight >
177- this . contentRef . current . clientHeight
178- let widths = this . _widths || [ ]
179-
180- this . _widths = [
181- getWidth ( firstRow . children [ 0 ] ) ,
182- getWidth ( firstRow . children [ 1 ] ) ,
183- ]
184-
185- if ( widths [ 0 ] !== this . _widths [ 0 ] || widths [ 1 ] !== this . _widths [ 1 ] ) {
186- this . dateColRef . current . style . width = this . _widths [ 0 ] + 'px'
187- this . timeColRef . current . style . width = this . _widths [ 1 ] + 'px'
125+ contentRef . current . scrollHeight > contentRef . current . clientHeight
126+
127+ let _widths = [ ]
128+ let widths = _widths
129+
130+ _widths = [ getWidth ( firstRow . children [ 0 ] ) , getWidth ( firstRow . children [ 1 ] ) ]
131+
132+ if ( widths [ 0 ] !== _widths [ 0 ] || widths [ 1 ] !== _widths [ 1 ] ) {
133+ dateColRef . current . style . width = _widths [ 0 ] + 'px'
134+ timeColRef . current . style . width = _widths [ 1 ] + 'px'
188135 }
189136
190137 if ( isOverflowing ) {
@@ -194,6 +141,46 @@ class Agenda extends React.Component {
194141 removeClass ( header , 'rbc-header-overflowing' )
195142 }
196143 }
144+
145+ let { messages } = localizer
146+ let end = dates . add ( date , length , 'day' )
147+
148+ let range = dates . range ( date , end , 'day' )
149+
150+ events = events . filter ( event => inRange ( event , date , end , accessors ) )
151+
152+ events . sort ( ( a , b ) => + accessors . start ( a ) - + accessors . start ( b ) )
153+
154+ return (
155+ < div className = "rbc-agenda-view" >
156+ { events . length !== 0 ? (
157+ < React . Fragment >
158+ < table ref = { headerRef } className = "rbc-agenda-table" >
159+ < thead >
160+ < tr >
161+ < th className = "rbc-header" ref = { dateColRef } >
162+ { messages . date }
163+ </ th >
164+ < th className = "rbc-header" ref = { timeColRef } >
165+ { messages . time }
166+ </ th >
167+ < th className = "rbc-header" > { messages . event } </ th >
168+ </ tr >
169+ </ thead >
170+ </ table >
171+ < div className = "rbc-agenda-content" ref = { contentRef } >
172+ < table className = "rbc-agenda-table" >
173+ < tbody ref = { tbodyRef } >
174+ { range . map ( ( day , idx ) => renderDay ( day , events , idx ) ) }
175+ </ tbody >
176+ </ table >
177+ </ div >
178+ </ React . Fragment >
179+ ) : (
180+ < span className = "rbc-agenda-empty" > { messages . noEventsInRange } </ span >
181+ ) }
182+ </ div >
183+ )
197184}
198185
199186Agenda . propTypes = {
0 commit comments