jattac.libs.web.responsive-table 0.1.6 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,381 +1,532 @@
1
- import React, { CSSProperties, Component, ReactNode } from 'react';
2
- import styles from '../Styles/ResponsiveTable.module.css';
3
- import IResponsiveTableColumnDefinition from '../Data/IResponsiveTableColumnDefinition';
4
- import IFooterRowDefinition from '../Data/IFooterRowDefinition';
5
-
6
- export type ColumnDefinition<TData> =
7
- | IResponsiveTableColumnDefinition<TData>
8
- | ((data: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
9
- interface IProps<TData> {
10
- columnDefinitions: ColumnDefinition<TData>[];
11
- data: TData[];
12
- noDataComponent?: ReactNode;
13
- maxHeight?: string;
14
- onRowClick?: (item: TData) => void;
15
- footerRows?: IFooterRowDefinition[];
16
- mobileBreakpoint?: number;
17
- isLoading?: boolean;
18
- animateOnLoad?: boolean;
19
- }
20
-
21
- interface IState {
22
- isMobile: boolean;
23
- }
24
-
25
- // Class component
26
- class ResponsiveTable<TData> extends Component<IProps<TData>, IState> {
27
- private debouncedResize: () => void;
28
-
29
- constructor(props: IProps<TData>) {
30
- super(props);
31
- this.state = {
32
- isMobile: false,
33
- };
34
-
35
- this.debouncedResize = this.debounce(this.handleResize, 200);
36
- }
37
-
38
- private get mobileBreakpoint(): number {
39
- return this.props.mobileBreakpoint || 600;
40
- }
41
-
42
- private debounce(func: () => void, delay: number): () => void {
43
- let timeout: NodeJS.Timeout;
44
- return () => {
45
- clearTimeout(timeout);
46
- timeout = setTimeout(() => func(), delay);
47
- };
48
- }
49
-
50
- private get data(): TData[] {
51
- if (Array.isArray(this.props.data) && this.props.data.length > 0) {
52
- return this.props.data;
53
- } else {
54
- return [];
55
- }
56
- }
57
-
58
- private get noDataSvg(): ReactNode {
59
- return (
60
- <svg xmlns="http://www.w3.org/2000/svg" fill="#ccc" height="40" width="40" viewBox="0 0 24 24">
61
- <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-14h2v6h-2zm0 8h2v2h-2z" />
62
- </svg>
63
- );
64
- }
65
-
66
- private get hasData(): boolean {
67
- return this.data.length > 0;
68
- }
69
-
70
- private get noDataComponent(): ReactNode {
71
- return (
72
- this.props.noDataComponent || (
73
- <div className={styles.noDataWrapper}>
74
- {this.noDataSvg}
75
- <div className={styles.noData}>No data</div>
76
- </div>
77
- )
78
- );
79
- }
80
-
81
- componentDidMount(): void {
82
- this.handleResize(); // Initial check
83
- window.addEventListener('resize', this.debouncedResize);
84
- }
85
-
86
- componentWillUnmount(): void {
87
- window.removeEventListener('resize', this.debouncedResize);
88
- }
89
-
90
- handleResize = (): void => {
91
- this.setState({
92
- isMobile: window.innerWidth <= this.mobileBreakpoint,
93
- });
94
- };
95
-
96
- private getColumnDefinition(
97
- columnDefinition: ColumnDefinition<TData>,
98
- rowIndex: number,
99
- ): IResponsiveTableColumnDefinition<TData> {
100
- if (!this.hasData) {
101
- return { displayLabel: '', cellRenderer: () => '' };
102
- }
103
- return columnDefinition instanceof Function ? columnDefinition(this.data[0], rowIndex) : columnDefinition;
104
- }
105
-
106
- private getRawColumnDefinition(columnDefinition: ColumnDefinition<TData>): IResponsiveTableColumnDefinition<TData> {
107
- let rawColumnDefinition: IResponsiveTableColumnDefinition<TData> = {} as IResponsiveTableColumnDefinition<TData>;
108
- if (columnDefinition instanceof Function) {
109
- rawColumnDefinition = columnDefinition(this.data[0], 0);
110
- } else {
111
- rawColumnDefinition = columnDefinition as IResponsiveTableColumnDefinition<TData>;
112
- }
113
- return rawColumnDefinition;
114
- }
115
-
116
- private onHeaderClickCallback(columnDefinition: ColumnDefinition<TData>): ((id: string) => void) | undefined {
117
- const rawColumnDefinition = this.getRawColumnDefinition(columnDefinition);
118
- if (rawColumnDefinition.interactivity && rawColumnDefinition.interactivity.onHeaderClick) {
119
- return rawColumnDefinition.interactivity.onHeaderClick;
120
- } else {
121
- return undefined;
122
- }
123
- }
124
-
125
- private getClickableHeaderClassName(
126
- onHeaderClickCallback: ((id: string) => void) | undefined,
127
- colDef: ColumnDefinition<TData>,
128
- ): string {
129
- const rawColumnDefinition = this.getRawColumnDefinition(colDef);
130
- const clickableHeaderClassName = onHeaderClickCallback
131
- ? rawColumnDefinition.interactivity!.className || styles.clickableHeader
132
- : '';
133
- return clickableHeaderClassName;
134
- }
135
-
136
- private get rowClickFunction(): (item: TData) => void {
137
- if (this.props.onRowClick) {
138
- return this.props.onRowClick;
139
- } else {
140
- return () => {};
141
- }
142
- }
143
-
144
- private get rowClickStyle(): CSSProperties {
145
- if (this.props.onRowClick) {
146
- return { cursor: 'pointer' } as CSSProperties;
147
- } else {
148
- return {};
149
- }
150
- }
151
-
152
- private get tableFooter(): ReactNode {
153
- if (!this.props.footerRows || this.props.footerRows.length === 0) {
154
- return null;
155
- }
156
-
157
- return (
158
- <tfoot>
159
- {this.props.footerRows.map((row, rowIndex) => (
160
- <tr key={rowIndex}>
161
- {row.columns.map((col, colIndex) => (
162
- <td
163
- key={colIndex}
164
- colSpan={col.colSpan}
165
- className={`${styles.footerCell} ${col.className || ''} ${col.onCellClick ? styles.clickableFooterCell : ''}`}
166
- onClick={col.onCellClick}
167
- >
168
- {col.cellRenderer()}
169
- </td>
170
- ))}
171
- </tr>
172
- ))}
173
- </tfoot>
174
- );
175
- }
176
-
177
- private get mobileFooter(): ReactNode {
178
- if (!this.props.footerRows || this.props.footerRows.length === 0) {
179
- return null;
180
- }
181
-
182
- return (
183
- <div className={styles.footerCard}>
184
- <div className={styles['footer-card-body']}>
185
- {this.props.footerRows.map((row, rowIndex) => {
186
- let currentColumnIndex = 0;
187
- return (
188
- <div key={rowIndex}>
189
- {row.columns.map((col, colIndex) => {
190
- let label = col.displayLabel;
191
- if (!label && col.colSpan === 1) {
192
- const header = this.props.columnDefinitions[currentColumnIndex];
193
- if (header) {
194
- label = this.getRawColumnDefinition(header).displayLabel;
195
- }
196
- }
197
- currentColumnIndex += col.colSpan;
198
- return (
199
- <p
200
- key={colIndex}
201
- className={`${styles['footer-card-row']} ${col.className || ''} ${
202
- col.onCellClick ? styles.clickableFooterCell : ''
203
- }`}
204
- onClick={col.onCellClick}
205
- >
206
- {label && <span className={styles['card-label']}>{label}</span>}
207
- <span className={styles['card-value']}>{col.cellRenderer()}</span>
208
- </p>
209
- );
210
- })}
211
- </div>
212
- );
213
- })}
214
- </div>
215
- </div>
216
- );
217
- }
218
-
219
- private get skeletonView(): ReactNode {
220
- const skeletonRowCount = 5; // Or make this configurable
221
- const columnCount = this.props.columnDefinitions.length;
222
-
223
- if (this.state.isMobile) {
224
- return (
225
- <div>
226
- {[...Array(skeletonRowCount)].map((_, i) => (
227
- <div key={i} className={styles.skeletonCard}>
228
- {[...Array(columnCount)].map((_, j) => (
229
- <div key={j} className={`${styles.skeleton} ${styles.skeletonText}`} style={{ marginBottom: '0.5rem' }} />
230
- ))}
231
- </div>
232
- ))}
233
- </div>
234
- );
235
- }
236
-
237
- return (
238
- <table className={styles.responsiveTable}>
239
- <thead>
240
- <tr>
241
- {[...Array(columnCount)].map((_, i) => (
242
- <th key={i}>
243
- <div className={`${styles.skeleton} ${styles.skeletonText}`} />
244
- </th>
245
- ))}
246
- </tr>
247
- </thead>
248
- <tbody>
249
- {[...Array(skeletonRowCount)].map((_, i) => (
250
- <tr key={i}>
251
- {[...Array(columnCount)].map((_, j) => (
252
- <td key={j}>
253
- <div className={`${styles.skeleton} ${styles.skeletonText}`} />
254
- </td>
255
- ))}
256
- </tr>
257
- ))}
258
- </tbody>
259
- </table>
260
- );
261
- }
262
-
263
- private get mobileView(): ReactNode {
264
- return (
265
- <div>
266
- {this.data.map((row, rowIndex) => (
267
- <div
268
- key={rowIndex}
269
- className={`${styles['card']} ${this.props.animateOnLoad ? styles.animatedRow : ''}`}
270
- style={{ animationDelay: `${rowIndex * 0.05}s` }}
271
- onClick={(e) => {
272
- this.rowClickFunction(row);
273
- e.stopPropagation();
274
- e.preventDefault();
275
- }}
276
- >
277
- <div className={styles['card-header']}> </div>
278
- <div className={styles['card-body']}>
279
- {this.props.columnDefinitions.map((columnDefinition, colIndex) => {
280
- const colDef = this.getColumnDefinition(columnDefinition, rowIndex);
281
- const onHeaderClickCallback = this.onHeaderClickCallback(colDef);
282
- const clickableHeaderClassName = this.getClickableHeaderClassName(onHeaderClickCallback, colDef);
283
- return (
284
- <div key={colIndex} className={styles['card-row']}>
285
- <p>
286
- <span
287
- className={`${styles['card-label']} ${clickableHeaderClassName}`}
288
- onClick={
289
- onHeaderClickCallback ? () => onHeaderClickCallback(colDef.interactivity!.id) : undefined
290
- }
291
- >
292
- {colDef.displayLabel}
293
- </span>
294
- <span className={styles['card-value']}>{colDef.cellRenderer(row)}</span>
295
- </p>
296
- </div>
297
- );
298
- })}
299
- </div>
300
- </div>
301
- ))}
302
- {this.mobileFooter}
303
- </div>
304
- );
305
- }
306
-
307
- private get largeScreenView(): ReactNode {
308
- const useFixedHeaders = this.props.maxHeight ? true : false;
309
-
310
- const fixedHeadersStyle = useFixedHeaders
311
- ? ({ maxHeight: this.props.maxHeight, overflowY: 'auto' } as CSSProperties)
312
- : {};
313
-
314
- return (
315
- <div style={fixedHeadersStyle}>
316
- <table className={styles['responsiveTable']} style={{ zIndex: -1 }}>
317
- <thead>
318
- <tr>
319
- {this.props.columnDefinitions.map((columnDefinition, colIndex) => {
320
- const onHeaderClickCallback = this.onHeaderClickCallback(columnDefinition);
321
- const clickableHeaderClassName = this.getClickableHeaderClassName(
322
- onHeaderClickCallback,
323
- columnDefinition,
324
- );
325
- return (
326
- <th
327
- key={colIndex}
328
- className={`${clickableHeaderClassName}`}
329
- style={{ zIndex: 0 }}
330
- onClick={
331
- onHeaderClickCallback
332
- ? () => onHeaderClickCallback(this.getColumnDefinition(columnDefinition, 0).interactivity!.id)
333
- : undefined
334
- }
335
- >
336
- {this.getColumnDefinition(columnDefinition, 0).displayLabel}
337
- </th>
338
- );
339
- })}
340
- </tr>
341
- </thead>
342
- <tbody>
343
- {this.data.map((row, rowIndex) => (
344
- <tr
345
- key={rowIndex}
346
- className={this.props.animateOnLoad ? styles.animatedRow : ''}
347
- style={{ animationDelay: `${rowIndex * 0.05}s` }}
348
- >
349
- {this.props.columnDefinitions.map((columnDefinition, colIndex) => (
350
- <td onClick={() => this.rowClickFunction(row)} key={colIndex}>
351
- <span style={{ ...this.rowClickStyle }}>
352
- {this.getColumnDefinition(columnDefinition, rowIndex).cellRenderer(row)}
353
- </span>
354
- </td>
355
- ))}
356
- </tr>
357
- ))}
358
- </tbody>
359
- {this.tableFooter}
360
- </table>
361
- </div>
362
- );
363
- }
364
-
365
- render() {
366
- if (this.props.isLoading) {
367
- return this.skeletonView;
368
- }
369
-
370
- if (!this.hasData) {
371
- return this.noDataComponent;
372
- }
373
- if (this.state.isMobile) {
374
- return this.mobileView;
375
- }
376
-
377
- return this.largeScreenView;
378
- }
379
- }
380
-
381
- export default ResponsiveTable;
1
+ import React, { CSSProperties, Component, ReactNode, createRef } from 'react';
2
+ import styles from '../Styles/ResponsiveTable.module.css';
3
+ import IResponsiveTableColumnDefinition from '../Data/IResponsiveTableColumnDefinition';
4
+ import IFooterRowDefinition from '../Data/IFooterRowDefinition';
5
+ import { IResponsiveTablePlugin } from '../Plugins/IResponsiveTablePlugin';
6
+ import { FilterPlugin } from '../Plugins/FilterPlugin';
7
+ import { InfiniteScrollPlugin } from '../Plugins/InfiniteScrollPlugin';
8
+ import { FixedSizeList as List } from 'react-window';
9
+
10
+ export type ColumnDefinition<TData> =
11
+ | IResponsiveTableColumnDefinition<TData>
12
+ | ((data: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
13
+ interface IProps<TData> {
14
+ columnDefinitions: ColumnDefinition<TData>[];
15
+ data: TData[];
16
+ noDataComponent?: ReactNode;
17
+ maxHeight?: string;
18
+ onRowClick?: (item: TData) => void;
19
+ footerRows?: IFooterRowDefinition[];
20
+ mobileBreakpoint?: number;
21
+ plugins?: IResponsiveTablePlugin<TData>[];
22
+ infiniteScrollProps?: {
23
+ enableInfiniteScroll?: boolean;
24
+ onLoadMore?: (currentData: TData[]) => Promise<TData[] | null>;
25
+ hasMore?: boolean;
26
+ loadingMoreComponent?: ReactNode;
27
+ noMoreDataComponent?: ReactNode;
28
+ };
29
+ filterProps?: {
30
+ showFilter?: boolean;
31
+ filterPlaceholder?: string;
32
+ };
33
+ animationProps?: {
34
+ isLoading?: boolean;
35
+ animateOnLoad?: boolean;
36
+ };
37
+ }
38
+
39
+ interface IState<TData> {
40
+ isMobile: boolean;
41
+ processedData: TData[];
42
+ isLoadingMore: boolean;
43
+ }
44
+
45
+ // Class component
46
+ class ResponsiveTable<TData> extends Component<IProps<TData>, IState<TData>> {
47
+ private debouncedResize: () => void;
48
+ private tableContainerRef = createRef<HTMLDivElement>();
49
+
50
+ constructor(props: IProps<TData>) {
51
+ super(props);
52
+ this.state = {
53
+ isMobile: false,
54
+ processedData: props.data,
55
+ isLoadingMore: false,
56
+ };
57
+
58
+ this.debouncedResize = this.debounce(this.handleResize, 200);
59
+ }
60
+
61
+ private get mobileBreakpoint(): number {
62
+ return this.props.mobileBreakpoint || 600;
63
+ }
64
+
65
+ private debounce(func: () => void, delay: number): () => void {
66
+ let timeout: NodeJS.Timeout;
67
+ return () => {
68
+ clearTimeout(timeout);
69
+ timeout = setTimeout(() => func(), delay);
70
+ };
71
+ }
72
+
73
+ private get data(): TData[] {
74
+ if (Array.isArray(this.state.processedData) && this.state.processedData.length > 0) {
75
+ return this.state.processedData;
76
+ } else {
77
+ return [];
78
+ }
79
+ }
80
+
81
+ private get noDataSvg(): ReactNode {
82
+ return (
83
+ <svg xmlns="http://www.w3.org/2000/svg" fill="#ccc" height="40" width="40" viewBox="0 0 24 24">
84
+ <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-14h2v6h-2zm0 8h2v2h-2z" />
85
+ </svg>
86
+ );
87
+ }
88
+
89
+ private get hasData(): boolean {
90
+ return this.data.length > 0;
91
+ }
92
+
93
+ private get noDataComponent(): ReactNode {
94
+ return (
95
+ this.props.noDataComponent || (
96
+ <div className={styles.noDataWrapper}>
97
+ {this.noDataSvg}
98
+ <div className={styles.noData}>No data</div>
99
+ </div>
100
+ )
101
+ );
102
+ }
103
+
104
+ componentDidMount(): void {
105
+ this.handleResize(); // Initial check
106
+ window.addEventListener('resize', this.debouncedResize);
107
+ this.initializePlugins();
108
+ }
109
+
110
+ componentWillUnmount(): void {
111
+ window.removeEventListener('resize', this.debouncedResize);
112
+ }
113
+
114
+ componentDidUpdate(prevProps: IProps<TData>) {
115
+ if (prevProps.data !== this.props.data) {
116
+ this.processData();
117
+ }
118
+
119
+ // Handle infinite scroll loading
120
+ if (this.props.infiniteScrollProps?.enableInfiniteScroll && this.props.infiniteScrollProps?.hasMore && !this.state.isLoadingMore && this.props.infiniteScrollProps?.onLoadMore) {
121
+ // This condition will be met when the parent component updates `data` and `hasMore`
122
+ // after a successful load, or when `hasMore` becomes true.
123
+ // The actual load trigger is within the InfiniteScrollPlugin via `onItemsRendered`
124
+ // or `handleScroll`.
125
+
126
+ }
127
+ }
128
+
129
+ private initializePlugins() {
130
+ const activePlugins: IResponsiveTablePlugin<TData>[] = [];
131
+
132
+ // Add explicitly provided plugins first
133
+ if (this.props.plugins) {
134
+ activePlugins.push(...this.props.plugins);
135
+ }
136
+
137
+ // Automatically add FilterPlugin if filterProps are provided and not already present
138
+ if (this.props.filterProps?.showFilter && !activePlugins.some(p => p.id === 'filter')) {
139
+ activePlugins.push(new FilterPlugin());
140
+ }
141
+
142
+ // Automatically add InfiniteScrollPlugin if infiniteScrollProps are provided and not already present
143
+ if (this.props.infiniteScrollProps?.enableInfiniteScroll && !activePlugins.some(p => p.id === 'infinite-scroll')) {
144
+ activePlugins.push(new InfiniteScrollPlugin());
145
+ }
146
+
147
+ activePlugins.forEach((plugin) => {
148
+ if (plugin.onPluginInit) {
149
+ plugin.onPluginInit({
150
+ getData: () => this.props.data,
151
+ forceUpdate: () => this.processData(),
152
+ getScrollableElement: () => this.tableContainerRef.current,
153
+ infiniteScrollProps: this.props.infiniteScrollProps,
154
+ filterProps: this.props.filterProps,
155
+ columnDefinitions: this.props.columnDefinitions,
156
+ });
157
+ }
158
+ });
159
+
160
+ // Process data with all active plugins
161
+ let processedData = [...this.props.data];
162
+ activePlugins.forEach((plugin) => {
163
+ if (plugin.processData) {
164
+ processedData = plugin.processData(processedData);
165
+ }
166
+ });
167
+ this.setState({ processedData });
168
+ }
169
+
170
+ private processData() {
171
+ let processedData = [...this.props.data];
172
+ if (this.props.plugins) {
173
+ this.props.plugins.forEach((plugin) => {
174
+ if (plugin.processData) {
175
+ processedData = plugin.processData(processedData);
176
+ }
177
+ });
178
+ }
179
+ this.setState({ processedData });
180
+ }
181
+
182
+ handleResize = (): void => {
183
+ this.setState({
184
+ isMobile: window.innerWidth <= this.mobileBreakpoint,
185
+ });
186
+ };
187
+
188
+ private getColumnDefinition(
189
+ columnDefinition: ColumnDefinition<TData>,
190
+ rowIndex: number,
191
+ ): IResponsiveTableColumnDefinition<TData> {
192
+ if (!this.hasData) {
193
+ return { displayLabel: '', cellRenderer: () => '' };
194
+ }
195
+ return columnDefinition instanceof Function ? columnDefinition(this.data[0], rowIndex) : columnDefinition;
196
+ }
197
+
198
+ private getRawColumnDefinition(columnDefinition: ColumnDefinition<TData>): IResponsiveTableColumnDefinition<TData> {
199
+ let rawColumnDefinition: IResponsiveTableColumnDefinition<TData> = {} as IResponsiveTableColumnDefinition<TData>;
200
+ if (columnDefinition instanceof Function) {
201
+ rawColumnDefinition = columnDefinition(this.data[0], 0);
202
+ } else {
203
+ rawColumnDefinition = columnDefinition as IResponsiveTableColumnDefinition<TData>;
204
+ }
205
+ return rawColumnDefinition;
206
+ }
207
+
208
+ private onHeaderClickCallback(columnDefinition: ColumnDefinition<TData>): ((id: string) => void) | undefined {
209
+ const rawColumnDefinition = this.getRawColumnDefinition(columnDefinition);
210
+ if (rawColumnDefinition.interactivity && rawColumnDefinition.interactivity.onHeaderClick) {
211
+ return rawColumnDefinition.interactivity.onHeaderClick;
212
+ } else {
213
+ return undefined;
214
+ }
215
+ }
216
+
217
+ private getClickableHeaderClassName(
218
+ onHeaderClickCallback: ((id: string) => void) | undefined,
219
+ colDef: ColumnDefinition<TData>,
220
+ ): string {
221
+ const rawColumnDefinition = this.getRawColumnDefinition(colDef);
222
+ const clickableHeaderClassName = onHeaderClickCallback
223
+ ? rawColumnDefinition.interactivity!.className || styles.clickableHeader
224
+ : '';
225
+ return clickableHeaderClassName;
226
+ }
227
+
228
+ private get rowClickFunction(): (item: TData) => void {
229
+ if (this.props.onRowClick) {
230
+ return this.props.onRowClick;
231
+ } else {
232
+ return () => {};
233
+ }
234
+ }
235
+
236
+ private get rowClickStyle(): CSSProperties {
237
+ if (this.props.onRowClick) {
238
+ return { cursor: 'pointer' } as CSSProperties;
239
+ } else {
240
+ return {};
241
+ }
242
+ }
243
+
244
+ private get tableFooter(): ReactNode {
245
+ if (!this.props.footerRows || this.props.footerRows.length === 0) {
246
+ return null;
247
+ }
248
+
249
+ return (
250
+ <tfoot>
251
+ {this.props.footerRows.map((row, rowIndex) => (
252
+ <tr key={rowIndex}>
253
+ {row.columns.map((col, colIndex) => (
254
+ <td
255
+ key={colIndex}
256
+ colSpan={col.colSpan}
257
+ className={`${styles.footerCell} ${col.className || ''} ${col.onCellClick ? styles.clickableFooterCell : ''}`}
258
+ onClick={col.onCellClick}
259
+ >
260
+ {col.cellRenderer()}
261
+ </td>
262
+ ))}
263
+ </tr>
264
+ ))}
265
+ </tfoot>
266
+ );
267
+ }
268
+
269
+ private get mobileFooter(): ReactNode {
270
+ if (!this.props.footerRows || this.props.footerRows.length === 0) {
271
+ return null;
272
+ }
273
+
274
+ return (
275
+ <div className={styles.footerCard}>
276
+ <div className={styles['footer-card-body']}>
277
+ {this.props.footerRows.map((row, rowIndex) => {
278
+ let currentColumnIndex = 0;
279
+ return (
280
+ <div key={rowIndex}>
281
+ {row.columns.map((col, colIndex) => {
282
+ let label = col.displayLabel;
283
+ if (!label && col.colSpan === 1) {
284
+ const header = this.props.columnDefinitions[currentColumnIndex];
285
+ if (header) {
286
+ label = this.getRawColumnDefinition(header).displayLabel;
287
+ }
288
+ }
289
+ currentColumnIndex += col.colSpan;
290
+ return (
291
+ <p
292
+ key={colIndex}
293
+ className={`${styles['footer-card-row']} ${col.className || ''} ${
294
+ col.onCellClick ? styles.clickableFooterCell : ''
295
+ }`}
296
+ onClick={col.onCellClick}
297
+ >
298
+ {label && <span className={styles['card-label']}>{label}</span>}
299
+ <span className={styles['card-value']}>{col.cellRenderer()}</span>
300
+ </p>
301
+ );
302
+ })}
303
+ </div>
304
+ );
305
+ })}
306
+ </div>
307
+ </div>
308
+ );
309
+ }
310
+
311
+ private get skeletonView(): ReactNode {
312
+ const skeletonRowCount = 5; // Or make this configurable
313
+ const columnCount = this.props.columnDefinitions.length;
314
+
315
+ if (this.state.isMobile) {
316
+ return (
317
+ <div>
318
+ {[...Array(skeletonRowCount)].map((_, i) => (
319
+ <div key={i} className={styles.skeletonCard}>
320
+ {[...Array(columnCount)].map((_, j) => (
321
+ <div key={j} className={`${styles.skeleton} ${styles.skeletonText}`} style={{ marginBottom: '0.5rem' }} />
322
+ ))}
323
+ </div>
324
+ ))}
325
+ </div>
326
+ );
327
+ }
328
+
329
+ return (
330
+ <table className={styles.responsiveTable}>
331
+ <thead>
332
+ <tr>
333
+ {[...Array(columnCount)].map((_, i) => (
334
+ <th key={i}>
335
+ <div className={`${styles.skeleton} ${styles.skeletonText}`} />
336
+ </th>
337
+ ))}
338
+ </tr>
339
+ </thead>
340
+ <tbody>
341
+ {[...Array(skeletonRowCount)].map((_, i) => (
342
+ <tr key={i}>
343
+ {[...Array(columnCount)].map((_, j) => (
344
+ <td key={j}>
345
+ <div className={`${styles.skeleton} ${styles.skeletonText}`} />
346
+ </td>
347
+ ))}
348
+ </tr>
349
+ ))}
350
+ </tbody>
351
+ </table>
352
+ );
353
+ }
354
+
355
+ private get mobileView(): ReactNode {
356
+ return (
357
+ <div>
358
+ {this.data.map((row, rowIndex) => (
359
+ <div
360
+ key={rowIndex}
361
+ className={`${styles['card']} ${this.props.animationProps?.animateOnLoad ? styles.animatedRow : ''}`}
362
+ style={{ animationDelay: `${rowIndex * 0.05}s` }}
363
+ onClick={(e) => {
364
+ this.rowClickFunction(row);
365
+ e.stopPropagation();
366
+ e.preventDefault();
367
+ }}
368
+ >
369
+ <div className={styles['card-header']}> </div>
370
+ <div className={styles['card-body']}>
371
+ {this.props.columnDefinitions.map((columnDefinition, colIndex) => {
372
+ const colDef = this.getColumnDefinition(columnDefinition, rowIndex);
373
+ const onHeaderClickCallback = this.onHeaderClickCallback(colDef);
374
+ const clickableHeaderClassName = this.getClickableHeaderClassName(onHeaderClickCallback, colDef);
375
+ return (
376
+ <div key={colIndex} className={styles['card-row']}>
377
+ <p>
378
+ <span
379
+ className={`${styles['card-label']} ${clickableHeaderClassName}`}
380
+ onClick={
381
+ onHeaderClickCallback ? () => onHeaderClickCallback(colDef.interactivity!.id) : undefined
382
+ }
383
+ >
384
+ {colDef.displayLabel}
385
+ </span>
386
+ <span className={styles['card-value']}>{colDef.cellRenderer(row)}</span>
387
+ </p>
388
+ </div>
389
+ );
390
+ })}
391
+ </div>
392
+ </div>
393
+ ))}
394
+ {this.mobileFooter}
395
+ </div>
396
+ );
397
+ }
398
+
399
+ private get largeScreenView(): ReactNode {
400
+ const useFixedHeaders = this.props.maxHeight ? true : false;
401
+
402
+ const fixedHeadersStyle = useFixedHeaders
403
+ ? ({ maxHeight: this.props.maxHeight, overflowY: 'auto' } as CSSProperties)
404
+ : {};
405
+
406
+ const Row = ({ index, style }: { index: number; style: CSSProperties }) => {
407
+ const row = this.data[index];
408
+ if (!row) return null; // Should not happen with correct item count
409
+
410
+ return (
411
+ <tr
412
+ key={index}
413
+ className={this.props.animationProps?.animateOnLoad ? styles.animatedRow : ''}
414
+ style={{ ...style, animationDelay: `${index * 0.05}s` }}
415
+ >
416
+ {this.props.columnDefinitions.map((columnDefinition, colIndex) => (
417
+ <td onClick={() => this.rowClickFunction(row)} key={colIndex}>
418
+ <span style={{ ...this.rowClickStyle }}>
419
+ {this.getColumnDefinition(columnDefinition, index).cellRenderer(row)}
420
+ </span>
421
+ </td>
422
+ ))}
423
+ </tr>
424
+ );
425
+ };
426
+
427
+ return (
428
+ <div style={fixedHeadersStyle} ref={this.tableContainerRef}>
429
+ <table className={styles['responsiveTable']}>
430
+ <thead>
431
+ <tr>
432
+ {this.props.columnDefinitions.map((columnDefinition, colIndex) => {
433
+ const onHeaderClickCallback = this.onHeaderClickCallback(columnDefinition);
434
+ const clickableHeaderClassName = this.getClickableHeaderClassName(
435
+ onHeaderClickCallback,
436
+ columnDefinition,
437
+ );
438
+ return (
439
+ <th
440
+ key={colIndex}
441
+ className={`${clickableHeaderClassName}`}
442
+ onClick={
443
+ onHeaderClickCallback
444
+ ? () => onHeaderClickCallback(this.getColumnDefinition(columnDefinition, 0).interactivity!.id)
445
+ : undefined
446
+ }
447
+ >
448
+ {this.getColumnDefinition(columnDefinition, 0).displayLabel}
449
+ </th>
450
+ );
451
+ })}
452
+ </tr>
453
+ </thead>
454
+ <tbody>
455
+ {this.props.infiniteScrollProps?.enableInfiniteScroll ? (
456
+ <List
457
+ height={fixedHeadersStyle.maxHeight ? (typeof fixedHeadersStyle.maxHeight === 'string' ? parseFloat(fixedHeadersStyle.maxHeight) : fixedHeadersStyle.maxHeight) : 500} // Default height if not provided
458
+ itemCount={this.data.length}
459
+ itemSize={50} // Average row height, can be made configurable
460
+ width={'100%'}
461
+ outerRef={this.tableContainerRef} // Pass ref to outer element for scroll events
462
+ >
463
+ {Row}
464
+ </List>
465
+ ) : (
466
+ this.data.map((row, rowIndex) => (
467
+ <tr
468
+ key={rowIndex}
469
+ className={this.props.animationProps?.animateOnLoad ? styles.animatedRow : ''}
470
+ style={{ animationDelay: `${rowIndex * 0.05}s` }}
471
+ >
472
+ {this.props.columnDefinitions.map((columnDefinition, colIndex) => (
473
+ <td onClick={() => this.rowClickFunction(row)} key={colIndex}>
474
+ <span style={{ ...this.rowClickStyle }}>
475
+ {this.getColumnDefinition(columnDefinition, rowIndex).cellRenderer(row)}
476
+ </span>
477
+ </td>
478
+ ))}
479
+ </tr>
480
+ ))
481
+ )}
482
+ </tbody>
483
+ {this.tableFooter}
484
+ </table>
485
+ {this.renderPluginFooters()}
486
+ </div>
487
+ );
488
+ }
489
+
490
+ private renderPluginHeaders() {
491
+ if (!this.props.plugins) {
492
+ return null;
493
+ }
494
+
495
+ return this.props.plugins.map((plugin) => {
496
+ if (plugin.renderHeader) {
497
+ return <div key={plugin.id}>{plugin.renderHeader()}</div>;
498
+ }
499
+ return null;
500
+ });
501
+ }
502
+
503
+ private renderPluginFooters() {
504
+ if (!this.props.plugins) {
505
+ return null;
506
+ }
507
+
508
+ return this.props.plugins.map((plugin) => {
509
+ if (plugin.renderFooter) {
510
+ return <div key={plugin.id + '-footer'}>{plugin.renderFooter()}</div>;
511
+ }
512
+ return null;
513
+ });
514
+ }
515
+
516
+ render() {
517
+ if (this.props.animationProps?.isLoading) {
518
+ return this.skeletonView;
519
+ }
520
+
521
+ return (
522
+ <div>
523
+ {this.renderPluginHeaders()}
524
+ {!this.hasData && this.noDataComponent}
525
+ {this.hasData && this.state.isMobile && this.mobileView}
526
+ {this.hasData && !this.state.isMobile && this.largeScreenView}
527
+ </div>
528
+ );
529
+ }
530
+ }
531
+
532
+ export default ResponsiveTable;