Skip to content

Commit 83fd060

Browse files
committed
fix: corrected table scrolling behavior and updated version to 2.0.1
1 parent 1d53669 commit 83fd060

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.0.0",
2+
"version": "2.0.1",
33
"license": "MIT",
44
"main": "dist/antd-weekly-calendar.umd.js",
55
"module": "dist/antd-weekly-calendar.es.js",

src/components/CalendarBody.tsx

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import React, { useEffect, useRef } from 'react';
22
import { Table, Grid } from 'antd';
33

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';
96
import { createDayColumns, SCROLL_TO_ROW } from './columns';
107

118
const ALL_DAY_ROW = 0;
@@ -18,17 +15,30 @@ function Calendar<T extends GenericEvent>({
1815
weekends,
1916
}: CalendarBodyProps<T>) {
2017
const rowRef = useRef<null | HTMLDivElement>(null);
18+
const tableContainerRef = useRef<null | HTMLDivElement>(null);
19+
2120
const screens = useBreakpoint();
2221

2322
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' });
2626
}
27-
}, [rowRef]);
27+
}, [SCROLL_TO_ROW]);
2828

29-
const fontSize = screens.xs ? '12px' : '14px'
29+
const fontSize = screens.xs ? '12px' : '14px';
3030
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+
),
3242
dataIndex: 'hour',
3343
key: 'hour',
3444
width: screens.xs ? 50 : 1,
@@ -37,7 +47,7 @@ function Calendar<T extends GenericEvent>({
3747
props: {
3848
style: {
3949
width: screens.xs ? '30%' : '10%',
40-
fontSize: fontSize
50+
fontSize: fontSize,
4151
},
4252
},
4353
children: SCROLL_TO_ROW === id ? (
@@ -55,7 +65,7 @@ function Calendar<T extends GenericEvent>({
5565
<div
5666
style={{
5767
whiteSpace: 'nowrap',
58-
fontSize: fontSize
68+
fontSize: fontSize,
5969
}}
6070
>
6171
{/* @ts-ignore */}
@@ -67,9 +77,16 @@ function Calendar<T extends GenericEvent>({
6777
const tableColumns = [hourColumn, ...dayColumns];
6878

6979
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+
>
7188
<Table
72-
rowKey={record => record.id}
89+
rowKey={(record) => record.id}
7390
dataSource={getDayHoursEvents(weekDatesRange, getDayEvents)}
7491
columns={tableColumns}
7592
pagination={false}
@@ -81,26 +98,26 @@ function Calendar<T extends GenericEvent>({
8198
style: {
8299
backgroundColor: 'white',
83100
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,
86103
top: 0,
87104
padding: '8px 0',
88105
},
89106
};
90-
}
107+
}
91108
return {
92109
style: {
93-
padding: '8px 0', // Add padding for each row
110+
padding: '8px 0',
94111
},
95112
};
96113
}}
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+
// }}
101118
/>
102119
</div>
103120
);
104121
}
105122

106-
export default Calendar;
123+
export default Calendar;

src/components/columns.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from './types';
1010
import { EventBlock } from './Event';
1111

12-
export const SCROLL_TO_ROW = 19;
12+
export const SCROLL_TO_ROW = 6;
1313

1414

1515

src/components/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,20 @@ export const sizeEventBox = <T extends GenericEvent>(event: T, hour: Date) => {
205205
: differenceInMinutes(eventStartTime, hour) * HOUR_TO_DECIMAL;
206206
return { boxPosition: boxPosition, boxSize: boxSize };
207207
};
208+
209+
/**
210+
* calculateScrollOffset - A to calculate the scroll offset needed to bring a specific row into view.
211+
*
212+
* @param {HTMLDivElement} container - The container element that is scrollable.
213+
* @param {HTMLDivElement} row - The row element that needs to be scrolled into view.
214+
* @returns {number} - The calculated scroll offset value.
215+
*
216+
* This function calculates how much the container needs to be scrolled to bring the target row into view.
217+
* It determines the difference between the container's top position and the row's top position, and adjusts
218+
* the scroll position accordingly, with an extra offset to position the row appropriately in the view.
219+
*/
220+
export function calculateScrollOffset(container: HTMLDivElement, row: HTMLDivElement): number {
221+
const containerTop = container.getBoundingClientRect().top;
222+
const rowTop = row.getBoundingClientRect().top;
223+
return rowTop - containerTop // Adjust to scroll just enough to show the row
224+
}

0 commit comments

Comments
 (0)