Skip to content

Commit c14a6fc

Browse files
authored
Merge pull request #11 from codeuniversity/fix-scroll-issue
Fix scroll issue and adjusting weekeends
2 parents eb7c85b + 6970ef6 commit c14a6fc

File tree

10 files changed

+138
-106
lines changed

10 files changed

+138
-106
lines changed

.github/workflows/size.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
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",
@@ -89,7 +89,7 @@
8989
"storybook": "^8.2.9",
9090
"typescript": "^5.5.4",
9191
"vite": "^4.0.0",
92-
"vite-plugin-dts": "^4.0.3",
92+
"vite-plugin-dts": "^4.2.1",
9393
"vitest": "^0.32.2"
9494
},
9595
"eslintConfig": {

src/WeeklyCalendar.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ export function WeeklyCalendar<T extends GenericEvent>({
1313
onSelectDate,
1414
weekends = false,
1515
currentDate,
16+
value
1617
}: CalendarContainerProps<T>) {
17-
const [startWeek, setStartWeek] = useState(startOfWeek(currentDate || new Date(), { weekStartsOn: 0 }));
18+
const dateToUse = currentDate || value;
19+
20+
const [startWeek, setStartWeek] = useState(startOfWeek(dateToUse || new Date(), { weekStartsOn: 0 }));
1821
const weekPeriod = {
1922
startDate: startWeek,
2023
endDate: endOfWeek(startWeek),
2124
};
2225

2326
useEffect(() => {
24-
if (currentDate && startOfWeek(currentDate).getTime() !== startWeek.getTime()) {
25-
setStartWeek(currentDate);
27+
if (dateToUse && startOfWeek(dateToUse).getTime() !== startWeek.getTime()) {
28+
setStartWeek(dateToUse);
2629
}
27-
}, [currentDate]);
30+
}, [dateToUse]);
2831

2932
useEffect(() => {
3033
onSelectDate && onSelectDate(startWeek);

src/components/CalendarBody.tsx

Lines changed: 37 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,23 @@ 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+
101115
/>
102116
</div>
103117
);
104118
}
105119

106-
export default Calendar;
120+
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/types.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export interface BaseCalendarProps<T extends GenericEvent = GenericEvent> {
2020
export interface CalendarContainerProps<T extends GenericEvent = GenericEvent>
2121
extends BaseCalendarProps<T> {
2222
events: T[];
23+
/**
24+
* @deprecated Use `currentDate` instead.
25+
*/
26+
value?: Date;
27+
2328
currentDate?: Date;
2429
}
2530

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+
}

tsconfig.build.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"noEmit": false,
6+
"removeComments": true,
7+
"sourceMap": false
8+
},
9+
"exclude": ["node_modules", "dist", "**/*.stories.*", "**/*.test.*", "**/*.spec.*"]
10+
}
11+

vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import path from 'path';
4+
import dts from 'vite-plugin-dts'
45

56
export default defineConfig({
6-
plugins: [react()],
7+
plugins: [react(), dts({
8+
tsconfigPath: './tsconfig.build.json',
9+
}),],
710
build: {
811
lib: {
912
entry: path.resolve(__dirname, 'src/index.ts'), // This is correct
@@ -20,6 +23,7 @@ export default defineConfig({
2023
},
2124
},
2225
},
26+
2327
emptyOutDir: false, // Prevent Vite from clearing the output directory
2428
},
2529
});

0 commit comments

Comments
 (0)