1
1
import React , { useEffect , useRef } from 'react' ;
2
2
import { Table , Grid } from 'antd' ;
3
3
4
- import {
5
- GenericEvent ,
6
- CalendarBodyProps ,
7
- } from './types' ;
8
- import { getDayHoursEvents } from './utils' ;
4
+ import { GenericEvent , CalendarBodyProps } from './types' ;
5
+ import { getDayHoursEvents , calculateScrollOffset } from './utils' ;
9
6
import { createDayColumns , SCROLL_TO_ROW } from './columns' ;
10
7
11
8
const ALL_DAY_ROW = 0 ;
@@ -18,17 +15,30 @@ function Calendar<T extends GenericEvent>({
18
15
weekends,
19
16
} : CalendarBodyProps < T > ) {
20
17
const rowRef = useRef < null | HTMLDivElement > ( null ) ;
18
+ const tableContainerRef = useRef < null | HTMLDivElement > ( null ) ;
19
+
21
20
const screens = useBreakpoint ( ) ;
22
21
23
22
useEffect ( ( ) => {
24
- if ( rowRef . current ) {
25
- rowRef . current . scrollIntoView ( ) ;
23
+ if ( rowRef . current && tableContainerRef . current && 'scrollTo' in tableContainerRef . current ) {
24
+ const scrollOffset = calculateScrollOffset ( tableContainerRef . current , rowRef . current ) ;
25
+ tableContainerRef . current . scrollTo ( { top : scrollOffset , behavior : 'smooth' } ) ;
26
26
}
27
- } , [ rowRef ] ) ;
27
+ } , [ SCROLL_TO_ROW ] ) ;
28
28
29
- const fontSize = screens . xs ? '12px' : '14px'
29
+ const fontSize = screens . xs ? '12px' : '14px' ;
30
30
const hourColumn = {
31
- title : < div style = { { fontSize : screens . xs ? '14px' : '16px' , textAlign : 'center' , padding : '8px 0' } } > Hours</ div > ,
31
+ title : (
32
+ < div
33
+ style = { {
34
+ fontSize : screens . xs ? '14px' : '16px' ,
35
+ textAlign : 'center' ,
36
+ padding : '8px 0' ,
37
+ } }
38
+ >
39
+ Hours
40
+ </ div >
41
+ ) ,
32
42
dataIndex : 'hour' ,
33
43
key : 'hour' ,
34
44
width : screens . xs ? 50 : 1 ,
@@ -37,7 +47,7 @@ function Calendar<T extends GenericEvent>({
37
47
props : {
38
48
style : {
39
49
width : screens . xs ? '30%' : '10%' ,
40
- fontSize : fontSize
50
+ fontSize : fontSize ,
41
51
} ,
42
52
} ,
43
53
children : SCROLL_TO_ROW === id ? (
@@ -55,7 +65,7 @@ function Calendar<T extends GenericEvent>({
55
65
< div
56
66
style = { {
57
67
whiteSpace : 'nowrap' ,
58
- fontSize : fontSize
68
+ fontSize : fontSize ,
59
69
} }
60
70
>
61
71
{ /* @ts -ignore */ }
@@ -67,9 +77,16 @@ function Calendar<T extends GenericEvent>({
67
77
const tableColumns = [ hourColumn , ...dayColumns ] ;
68
78
69
79
return (
70
- < div style = { { overflowX : 'scroll' } } >
80
+ < div
81
+ ref = { tableContainerRef }
82
+
83
+ style = { {
84
+ height : '80vh' , // Set a fixed height for the container
85
+ overflow : 'auto' , // Allow both vertical and horizontal scrolling within the container only
86
+ } }
87
+ >
71
88
< Table
72
- rowKey = { record => record . id }
89
+ rowKey = { ( record ) => record . id }
73
90
dataSource = { getDayHoursEvents ( weekDatesRange , getDayEvents ) }
74
91
columns = { tableColumns }
75
92
pagination = { false }
@@ -81,26 +98,26 @@ function Calendar<T extends GenericEvent>({
81
98
style : {
82
99
backgroundColor : 'white' ,
83
100
position : 'sticky' ,
84
- boxShadow : 'rgba(0, 0, 0, 0.05) -1px 4px 4px ' ,
85
- zIndex : 1 ,
101
+ boxShadow : 'rgba(0, 0, 0, 0.05) -1px 4px 4px' ,
102
+ zIndex : 3 ,
86
103
top : 0 ,
87
104
padding : '8px 0' ,
88
105
} ,
89
106
} ;
90
- }
107
+ }
91
108
return {
92
109
style : {
93
- padding : '8px 0' , // Add padding for each row
110
+ padding : '8px 0' ,
94
111
} ,
95
112
} ;
96
113
} }
97
- scroll = { {
98
- y : screens . xs ? 300 : 1000 ,
99
- x : 'max-content' ,
100
- } }
114
+ // scroll={{
115
+ // y: screens.xs ? 300 : 1000,
116
+ // x: 'max-content',
117
+ // }}
101
118
/>
102
119
</ div >
103
120
) ;
104
121
}
105
122
106
- export default Calendar ;
123
+ export default Calendar ;
0 commit comments