-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtable-content.tsx
More file actions
170 lines (156 loc) · 4.99 KB
/
table-content.tsx
File metadata and controls
170 lines (156 loc) · 4.99 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Box, TableSortLabel, Theme } from "@mui/material";
import { makeStyles } from "@mui/styles";
import clsx from "clsx";
import { createContext, ReactNode, useContext, useMemo } from "react";
import { HeaderGroup } from "react-table";
import { SORTING_ARROW_WIDTH } from "@/charts/table/constants";
import { ColumnMeta } from "@/charts/table/table-state";
import { Flex } from "@/components/flex";
import { OpenMetadataPanelWrapper } from "@/components/metadata-panel";
import { Observation } from "@/domain/data";
/** Workaround because react-window can't pass props to inner element */
type TableContentProps = {
headerGroups: HeaderGroup<Observation>[];
tableColumnsMeta: Record<string, ColumnMeta>;
customSortCount: number;
totalColumnsWidth: number;
handleSortClick?: (columnId: string) => void;
manualSortBy?: Array<{ id: string; desc: boolean }>;
};
const TableContentContext = createContext<TableContentProps | undefined>(
undefined
);
export const TableContentProvider = ({
headerGroups,
tableColumnsMeta,
customSortCount,
totalColumnsWidth,
handleSortClick,
manualSortBy,
children,
}: TableContentProps & { children: ReactNode }) => {
const value = useMemo(() => {
return {
headerGroups,
tableColumnsMeta,
customSortCount,
totalColumnsWidth,
handleSortClick,
manualSortBy,
};
}, [
headerGroups,
tableColumnsMeta,
customSortCount,
totalColumnsWidth,
handleSortClick,
manualSortBy,
]);
return (
<TableContentContext.Provider value={value}>
{children}
</TableContentContext.Provider>
);
};
const useStyles = makeStyles((theme: Theme) => ({
headerGroup: {
margin: 0,
padding: `${theme.spacing(2)} ${theme.spacing(3)}`,
borderTop: "1px solid",
borderBottom: "1px solid",
borderTopColor: theme.palette.monochrome[300],
borderBottomColor: theme.palette.monochrome[300],
fontWeight: "bold",
fontSize: "0.875rem",
backgroundColor: theme.palette.background.paper,
color: theme.palette.grey[700],
minHeight: SORTING_ARROW_WIDTH,
alignItems: "center",
justifyContent: "flex-start",
whiteSpace: "nowrap",
},
headerGroupMeasure: {
justifyContent: "flex-end",
},
}));
export const TableContent = ({ children }: { children: ReactNode }) => {
const ctx = useContext(TableContentContext);
const classes = useStyles();
if (!ctx) {
throw Error("Please wrap TableContent in TableContentProvider");
}
const {
headerGroups,
tableColumnsMeta,
customSortCount,
totalColumnsWidth,
handleSortClick,
manualSortBy,
} = ctx;
return (
<>
<Box sx={{ position: "sticky", top: 0, zIndex: 1 }}>
{headerGroups.map((headerGroup) => {
return (
// getHeaderGroupProps() returns props with key
// eslint-disable-next-line react/jsx-key
<Box {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => {
const { dim, columnComponentType } =
tableColumnsMeta[column.id];
// For manual sorting (server-side), check manualSortBy state
const manualSort = manualSortBy?.find(
(s) => s.id === column.id
);
const isManualSorted = !!manualSort;
const manualSortDirection = manualSort?.desc ? "desc" : "asc";
// For react-table sorting (client-side), use existing logic
const isCustomSorted = column.sortedIndex < customSortCount;
const isActive = handleSortClick
? isManualSorted
: isCustomSorted;
const direction = handleSortClick
? manualSortDirection
: column.isSortedDesc
? "desc"
: "asc";
return (
// eslint-disable-next-line react/jsx-key
<Flex
className={clsx(
classes.headerGroup,
columnComponentType === "NumericalMeasure"
? classes.headerGroupMeasure
: undefined
)}
>
<TableSortLabel
active={isActive}
direction={direction}
onClick={() => handleSortClick?.(column.id)}
>
<OpenMetadataPanelWrapper component={dim}>
<span style={{ fontWeight: "bold" }}>
{column.render("Header")}
</span>
</OpenMetadataPanelWrapper>
</TableSortLabel>
</Flex>
);
})}
</Box>
);
})}
</Box>
<Box
sx={{
position: "relative",
minWidth: totalColumnsWidth,
width: "100%",
}}
>
{children}
</Box>
</>
);
};