forked from huimiu/tab-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.tsx
More file actions
103 lines (93 loc) · 2.95 KB
/
Copy pathDashboard.tsx
File metadata and controls
103 lines (93 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { Component } from "react";
import { dashboardStyles } from "./Dashboard.styles";
interface IDashboardState {
isMobile?: boolean;
observer?: ResizeObserver;
}
/**
* The dashboard class which is the base class for all dashboard components.
*/
export class Dashboard extends Component<{}, IDashboardState> {
private ref: React.RefObject<HTMLDivElement>;
/**
* Constructor for the dashboard class.
* Initializes the dashboard state.
* @param props The properties for the dashboard.
*/
constructor(props: any) {
super(props);
this.state = {
isMobile: undefined,
observer: undefined,
};
this.ref = React.createRef<HTMLDivElement>();
}
/**
* This method is invoked immediately after a component is mounted.
* It's a good place to fetch data from server.
* For more information about react lifecycle, please refer to https://reactjs.org/docs/react-component.html#componentdidmount
*/
componentDidMount(): void {
// Observe the dashboard div for resize events
const observer = new ResizeObserver((entries) => {
for (let entry of entries) {
if (entry.target === this.ref.current) {
const { width } = entry.contentRect;
this.setState({ isMobile: width < 600 });
}
}
});
observer.observe(this.ref.current!);
}
/**
* This method is invoked immediately when a component will be unmounted.
* It's a good place to clean up the resources.
*/
componentWillUnmount(): void {
// Unobserve the dashboard div for resize events
if (this.state.observer && this.ref.current) {
this.state.observer.unobserve(this.ref.current);
}
}
/**
* Define thie dashboard default layout, you can edit the code here to customize your dashboard layout.
*/
render() {
return (
<>
<div
ref={this.ref}
style={dashboardStyles(
this.state.isMobile,
this.rowHeights(),
this.columnWidths()
)}
>
{this.dashboardLayout()}
</div>
</>
);
}
/**
* Implement this method to define the row heights of the dashboard.
* For example, if you want to have 3 rows, and the height of the first row is 100px, the height of the second row is 200px, and the height of the third row is 300px, you can return "100px 200px 300px".
* @returns The row heights of the dashboard.
*/
protected rowHeights(): string | undefined {
return undefined;
}
/**
* Implement this method to define the column widths of the dashboard.
* For example, if you want to have 3 columns, and each column occupies 1/3 of the full width, you can return "1fr 1fr 1fr".
* @returns The column widths of the dashboard.
*/
protected columnWidths(): string | undefined {
return undefined;
}
/**
* Implement this method to define the dashboard layout.
*/
protected dashboardLayout(): JSX.Element | undefined {
return undefined;
}
}