@hpcc-js/dgrid2 3.6.4 → 3.6.5
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.
- package/LICENSE +43 -43
- package/README.md +5 -5
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +1 -1
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +10 -8
- package/src/ErrorBoundary.tsx +56 -56
- package/src/__package__.ts +3 -3
- package/src/hooks.ts +10 -10
- package/src/index.ts +2 -2
- package/src/reactTable.tsx +163 -163
- package/src/table.css +9 -9
- package/src/table.md +57 -57
- package/src/table.ts +106 -106
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hpcc-js/dgrid2",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.5",
|
|
4
4
|
"description": "hpcc-js - DGrid2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.umd.cjs",
|
|
@@ -23,10 +23,12 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"clean": "rimraf --glob lib* types dist *.tsbuildinfo .turbo",
|
|
25
25
|
"bundle": "vite build",
|
|
26
|
-
"bundle-watch": "vite --
|
|
26
|
+
"bundle-watch": "vite build --watch",
|
|
27
|
+
"bundle-serve": "vite --port 5507",
|
|
27
28
|
"gen-types": "tsc --project tsconfig.json",
|
|
28
29
|
"gen-types-watch": "npm run gen-types -- --watch",
|
|
29
30
|
"build": "run-p gen-types bundle",
|
|
31
|
+
"watch": "run-p gen-types-watch bundle-watch",
|
|
30
32
|
"lint": "eslint ./src",
|
|
31
33
|
"lint-fix": "eslint --fix src/**/*.ts",
|
|
32
34
|
"docs": "typedoc --options tdoptions.json .",
|
|
@@ -37,17 +39,17 @@
|
|
|
37
39
|
"update-major": "npx --yes npm-check-updates -u"
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"@hpcc-js/common": "^3.7.
|
|
41
|
-
"@hpcc-js/util": "^3.5.
|
|
42
|
+
"@hpcc-js/common": "^3.7.7",
|
|
43
|
+
"@hpcc-js/util": "^3.5.6"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
44
|
-
"@hpcc-js/esbuild-plugins": "^1.8.
|
|
46
|
+
"@hpcc-js/esbuild-plugins": "^1.8.8",
|
|
45
47
|
"@types/react": "19.2.14",
|
|
46
48
|
"@types/react-dom": "19.2.3",
|
|
47
49
|
"@vitejs/plugin-react": "4.7.0",
|
|
48
|
-
"react": "19.2.
|
|
50
|
+
"react": "19.2.5",
|
|
49
51
|
"react-data-grid": "7.0.0-beta.59",
|
|
50
|
-
"react-dom": "19.2.
|
|
52
|
+
"react-dom": "19.2.5",
|
|
51
53
|
"vite-plugin-html": "3.2.2"
|
|
52
54
|
},
|
|
53
55
|
"repository": {
|
|
@@ -61,5 +63,5 @@
|
|
|
61
63
|
"url": "https://github.com/hpcc-systems/Visualization/issues"
|
|
62
64
|
},
|
|
63
65
|
"homepage": "https://github.com/hpcc-systems/Visualization",
|
|
64
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "630e839917f1cc38f6e3324db5a9ac991234599a"
|
|
65
67
|
}
|
package/src/ErrorBoundary.tsx
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import React, { Component, ErrorInfo, ReactNode } from "react";
|
|
2
|
-
|
|
3
|
-
interface Props {
|
|
4
|
-
children: ReactNode;
|
|
5
|
-
fallback?: ReactNode;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface State {
|
|
9
|
-
hasError: boolean;
|
|
10
|
-
error: Error | null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class ErrorBoundary extends Component<Props, State> {
|
|
14
|
-
public state: State = {
|
|
15
|
-
hasError: false,
|
|
16
|
-
error: null
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
public static getDerivedStateFromError(error: Error): State {
|
|
20
|
-
return { hasError: true, error };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
24
|
-
console.error("Error caught by boundary:", error);
|
|
25
|
-
console.error("Component stack:", errorInfo.componentStack);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private handleReset = () => {
|
|
29
|
-
this.setState({ hasError: false, error: null });
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
public render() {
|
|
33
|
-
if (this.state.hasError) {
|
|
34
|
-
if (this.props.fallback) {
|
|
35
|
-
return this.props.fallback;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<div className="min-h-[200px] flex items-center justify-center p-8 bg-red-50 rounded-lg border border-red-100">
|
|
40
|
-
<div className="text-center">
|
|
41
|
-
<h2 className="text-xl font-semibold text-red-700 mb-2">Something went wrong</h2>
|
|
42
|
-
<p className="text-red-600">{this.state.error?.message}</p>
|
|
43
|
-
<button
|
|
44
|
-
onClick={this.handleReset}
|
|
45
|
-
className="mt-4 px-4 py-2 bg-red-600 text-white rounded-md hover:bg-red-700 transition-colors"
|
|
46
|
-
>
|
|
47
|
-
Try again
|
|
48
|
-
</button>
|
|
49
|
-
</div>
|
|
50
|
-
</div>
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return this.props.children;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
import React, { Component, ErrorInfo, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
interface Props {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
fallback?: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface State {
|
|
9
|
+
hasError: boolean;
|
|
10
|
+
error: Error | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class ErrorBoundary extends Component<Props, State> {
|
|
14
|
+
public state: State = {
|
|
15
|
+
hasError: false,
|
|
16
|
+
error: null
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
public static getDerivedStateFromError(error: Error): State {
|
|
20
|
+
return { hasError: true, error };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
24
|
+
console.error("Error caught by boundary:", error);
|
|
25
|
+
console.error("Component stack:", errorInfo.componentStack);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private handleReset = () => {
|
|
29
|
+
this.setState({ hasError: false, error: null });
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
public render() {
|
|
33
|
+
if (this.state.hasError) {
|
|
34
|
+
if (this.props.fallback) {
|
|
35
|
+
return this.props.fallback;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div className="min-h-[200px] flex items-center justify-center p-8 bg-red-50 rounded-lg border border-red-100">
|
|
40
|
+
<div className="text-center">
|
|
41
|
+
<h2 className="text-xl font-semibold text-red-700 mb-2">Something went wrong</h2>
|
|
42
|
+
<p className="text-red-600">{this.state.error?.message}</p>
|
|
43
|
+
<button
|
|
44
|
+
onClick={this.handleReset}
|
|
45
|
+
className="mt-4 px-4 py-2 bg-red-600 text-white rounded-md hover:bg-red-700 transition-colors"
|
|
46
|
+
>
|
|
47
|
+
Try again
|
|
48
|
+
</button>
|
|
49
|
+
</div>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return this.props.children;
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/__package__.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const PKG_NAME = "__PACKAGE_NAME__";
|
|
2
|
-
export const PKG_VERSION = "__PACKAGE_VERSION__";
|
|
3
|
-
export const BUILD_VERSION = "__BUILD_VERSION__";
|
|
1
|
+
export const PKG_NAME = "__PACKAGE_NAME__";
|
|
2
|
+
export const PKG_VERSION = "__PACKAGE_VERSION__";
|
|
3
|
+
export const BUILD_VERSION = "__BUILD_VERSION__";
|
package/src/hooks.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { Widget } from "@hpcc-js/common";
|
|
3
|
-
|
|
4
|
-
export function useData(widget: Widget): [string[], Array<string | number>[]] {
|
|
5
|
-
const columns: string[] = React.useMemo(() => widget.columns(), [widget, widget.dataChecksum()]);
|
|
6
|
-
const data: Array<string | number>[] = React.useMemo(() => widget.data(), [widget, widget.dataChecksum()]);
|
|
7
|
-
|
|
8
|
-
return [columns, data];
|
|
9
|
-
}
|
|
10
|
-
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Widget } from "@hpcc-js/common";
|
|
3
|
+
|
|
4
|
+
export function useData(widget: Widget): [string[], Array<string | number>[]] {
|
|
5
|
+
const columns: string[] = React.useMemo(() => widget.columns(), [widget, widget.dataChecksum()]);
|
|
6
|
+
const data: Array<string | number>[] = React.useMemo(() => widget.data(), [widget, widget.dataChecksum()]);
|
|
7
|
+
|
|
8
|
+
return [columns, data];
|
|
9
|
+
}
|
|
10
|
+
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./__package__.ts";
|
|
2
|
-
export * from "./table.ts";
|
|
1
|
+
export * from "./__package__.ts";
|
|
2
|
+
export * from "./table.ts";
|
package/src/reactTable.tsx
CHANGED
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
import React, { FunctionComponent, useCallback, useEffect, useState } from "react";
|
|
2
|
-
import { DataGrid, Column, SelectColumn, SortColumn } from "react-data-grid";
|
|
3
|
-
import { format, timeFormat, timeParse } from "@hpcc-js/common";
|
|
4
|
-
import { ErrorBoundary } from "./ErrorBoundary.tsx";
|
|
5
|
-
import { useData } from "./hooks.ts";
|
|
6
|
-
import type { Table } from "./table.ts";
|
|
7
|
-
|
|
8
|
-
import "react-data-grid/lib/styles.css";
|
|
9
|
-
|
|
10
|
-
export type QuerySortItem = { attribute: string, descending: boolean };
|
|
11
|
-
function copyAndSort<T>(items: T[], attribute: string, descending?: boolean): T[] {
|
|
12
|
-
const key = attribute as keyof T;
|
|
13
|
-
return [...items].sort((a: T, b: T) => {
|
|
14
|
-
if (a[key] < b[key]) {
|
|
15
|
-
return descending ? 1 : -1;
|
|
16
|
-
} else if (a[key] > b[key]) {
|
|
17
|
-
return descending ? -1 : 1;
|
|
18
|
-
}
|
|
19
|
-
return 0;
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface EmptyRowsRendererProps {
|
|
24
|
-
message: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const EmptyRowsRenderer: FunctionComponent<EmptyRowsRendererProps> = ({
|
|
28
|
-
message
|
|
29
|
-
}) => {
|
|
30
|
-
|
|
31
|
-
return <div style={{ textAlign: "center", gridColumn: "1/-1" }} >
|
|
32
|
-
{message}
|
|
33
|
-
</div>;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
interface ColumnEx<TRow, TSummaryRow = unknown> extends Column<TRow, TSummaryRow> {
|
|
37
|
-
renderCell?: (props: any) => any;
|
|
38
|
-
__hpcc_pattern?: ReturnType<typeof timeParse>;
|
|
39
|
-
__hpcc_format?: ReturnType<typeof format> | ReturnType<typeof timeFormat>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface ReactTableProps {
|
|
43
|
-
table: Table;
|
|
44
|
-
sort?: QuerySortItem,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const ReactTable: FunctionComponent<ReactTableProps> = ({
|
|
48
|
-
table,
|
|
49
|
-
sort
|
|
50
|
-
}) => {
|
|
51
|
-
const [columns, data] = useData(table);
|
|
52
|
-
const multiSelect = table.multiSelect();
|
|
53
|
-
const columnTypes = table.columnTypes();
|
|
54
|
-
const columnPatterns = table.columnPatterns();
|
|
55
|
-
const columnFormats = table.columnFormats();
|
|
56
|
-
|
|
57
|
-
const [listColumns, setListColumns] = useState<ColumnEx<any[]>[]>([]);
|
|
58
|
-
const [sortColumn, setSortColumn] = useState<SortColumn>();
|
|
59
|
-
const [rows, setRows] = useState<any[]>([]);
|
|
60
|
-
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number>>(new Set());
|
|
61
|
-
|
|
62
|
-
// Columns ---
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
setListColumns([
|
|
65
|
-
...multiSelect ? [SelectColumn as ColumnEx<any[]>] : [],
|
|
66
|
-
...columns.map((column): ColumnEx<any[]> => {
|
|
67
|
-
const type = columnTypes[column] ?? "string";
|
|
68
|
-
let formatter;
|
|
69
|
-
let __hpcc_pattern;
|
|
70
|
-
let __hpcc_format;
|
|
71
|
-
switch (type) {
|
|
72
|
-
case "time":
|
|
73
|
-
__hpcc_pattern = columnPatterns[column] !== undefined ? timeParse(columnPatterns[column]) : undefined;
|
|
74
|
-
__hpcc_format = columnFormats[column] !== undefined ? timeFormat(columnFormats[column]) : undefined;
|
|
75
|
-
break;
|
|
76
|
-
case "number":
|
|
77
|
-
formatter = (props) => {
|
|
78
|
-
return <div style={{ textAlign: "right" }}>{props.row[props.column.key]}</div>;
|
|
79
|
-
};
|
|
80
|
-
// eslint-disable-next-line no-fallthrough
|
|
81
|
-
default:
|
|
82
|
-
__hpcc_format = columnFormats[column] !== undefined ? format(columnFormats[column]) : undefined;
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
key: column,
|
|
86
|
-
name: column,
|
|
87
|
-
resizable: true,
|
|
88
|
-
sortable: true,
|
|
89
|
-
minWidth: 80,
|
|
90
|
-
renderCell: formatter,
|
|
91
|
-
__hpcc_pattern,
|
|
92
|
-
__hpcc_format
|
|
93
|
-
};
|
|
94
|
-
})
|
|
95
|
-
]);
|
|
96
|
-
}, [columnFormats, columnPatterns, columnTypes, columns, multiSelect]);
|
|
97
|
-
|
|
98
|
-
const onSortColumnsChange = useCallback((sortColumns: SortColumn[]) => {
|
|
99
|
-
const futureSortColumn = sortColumns.slice(-1)[0];
|
|
100
|
-
const sorted = futureSortColumn !== undefined;
|
|
101
|
-
const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
|
|
102
|
-
setSortColumn(futureSortColumn);
|
|
103
|
-
setRows(copyAndSort(rows, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
|
|
104
|
-
}, [rows]);
|
|
105
|
-
|
|
106
|
-
const rowKeyGetter = useCallback((row: any) => {
|
|
107
|
-
return row.key;
|
|
108
|
-
}, []);
|
|
109
|
-
|
|
110
|
-
const onSelectedRowsChange = useCallback((selectedRows: Set<any>) => {
|
|
111
|
-
setSelectedRows(selectedRows);
|
|
112
|
-
}, []);
|
|
113
|
-
|
|
114
|
-
const onCellClick = useCallback((row, column) => {
|
|
115
|
-
table.onRowClickCallback(row, column.key);
|
|
116
|
-
}, [table]);
|
|
117
|
-
|
|
118
|
-
// Rows ---
|
|
119
|
-
useEffect(() => {
|
|
120
|
-
let items = data.map((row, index) => {
|
|
121
|
-
const retVal = {
|
|
122
|
-
key: index
|
|
123
|
-
};
|
|
124
|
-
listColumns.forEach((column, index) => {
|
|
125
|
-
let val = row[index] as string;
|
|
126
|
-
if (column.__hpcc_pattern && column.__hpcc_format) {
|
|
127
|
-
val = column.__hpcc_format(column.__hpcc_pattern(val));
|
|
128
|
-
} else if (column.__hpcc_pattern) {
|
|
129
|
-
val = column.__hpcc_pattern(val).toString();
|
|
130
|
-
} else if (column.__hpcc_format) {
|
|
131
|
-
val = column.__hpcc_format(val as any);
|
|
132
|
-
}
|
|
133
|
-
retVal[column.key] = val;
|
|
134
|
-
});
|
|
135
|
-
return retVal;
|
|
136
|
-
});
|
|
137
|
-
if (sort?.attribute) {
|
|
138
|
-
items = copyAndSort(items, sort.attribute, sort.descending);
|
|
139
|
-
}
|
|
140
|
-
setRows(items);
|
|
141
|
-
}, [listColumns, data, sort]);
|
|
142
|
-
|
|
143
|
-
return (
|
|
144
|
-
<ErrorBoundary fallback={<div>Error loading table</div>}>
|
|
145
|
-
<DataGrid
|
|
146
|
-
columns={listColumns}
|
|
147
|
-
headerRowHeight={24}
|
|
148
|
-
rows={rows}
|
|
149
|
-
rowKeyGetter={rowKeyGetter}
|
|
150
|
-
rowHeight={20}
|
|
151
|
-
renderers={{ noRowsFallback: <EmptyRowsRenderer message={table.noDataMessage()} /> as any }}
|
|
152
|
-
className={table.darkMode() ? "rdg-dark" : "rdg-light"}
|
|
153
|
-
sortColumns={sortColumn ? [sortColumn] : []}
|
|
154
|
-
onSortColumnsChange={onSortColumnsChange}
|
|
155
|
-
selectedRows={selectedRows}
|
|
156
|
-
onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
|
|
157
|
-
onCellClick={multiSelect ? undefined : (args, event) => onCellClick(args.row, args.column)}
|
|
158
|
-
style={{ height: "100%" }}
|
|
159
|
-
/>
|
|
160
|
-
</ErrorBoundary>
|
|
161
|
-
);
|
|
162
|
-
};
|
|
163
|
-
|
|
1
|
+
import React, { FunctionComponent, useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { DataGrid, Column, SelectColumn, SortColumn } from "react-data-grid";
|
|
3
|
+
import { format, timeFormat, timeParse } from "@hpcc-js/common";
|
|
4
|
+
import { ErrorBoundary } from "./ErrorBoundary.tsx";
|
|
5
|
+
import { useData } from "./hooks.ts";
|
|
6
|
+
import type { Table } from "./table.ts";
|
|
7
|
+
|
|
8
|
+
import "react-data-grid/lib/styles.css";
|
|
9
|
+
|
|
10
|
+
export type QuerySortItem = { attribute: string, descending: boolean };
|
|
11
|
+
function copyAndSort<T>(items: T[], attribute: string, descending?: boolean): T[] {
|
|
12
|
+
const key = attribute as keyof T;
|
|
13
|
+
return [...items].sort((a: T, b: T) => {
|
|
14
|
+
if (a[key] < b[key]) {
|
|
15
|
+
return descending ? 1 : -1;
|
|
16
|
+
} else if (a[key] > b[key]) {
|
|
17
|
+
return descending ? -1 : 1;
|
|
18
|
+
}
|
|
19
|
+
return 0;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface EmptyRowsRendererProps {
|
|
24
|
+
message: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const EmptyRowsRenderer: FunctionComponent<EmptyRowsRendererProps> = ({
|
|
28
|
+
message
|
|
29
|
+
}) => {
|
|
30
|
+
|
|
31
|
+
return <div style={{ textAlign: "center", gridColumn: "1/-1" }} >
|
|
32
|
+
{message}
|
|
33
|
+
</div>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
interface ColumnEx<TRow, TSummaryRow = unknown> extends Column<TRow, TSummaryRow> {
|
|
37
|
+
renderCell?: (props: any) => any;
|
|
38
|
+
__hpcc_pattern?: ReturnType<typeof timeParse>;
|
|
39
|
+
__hpcc_format?: ReturnType<typeof format> | ReturnType<typeof timeFormat>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ReactTableProps {
|
|
43
|
+
table: Table;
|
|
44
|
+
sort?: QuerySortItem,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const ReactTable: FunctionComponent<ReactTableProps> = ({
|
|
48
|
+
table,
|
|
49
|
+
sort
|
|
50
|
+
}) => {
|
|
51
|
+
const [columns, data] = useData(table);
|
|
52
|
+
const multiSelect = table.multiSelect();
|
|
53
|
+
const columnTypes = table.columnTypes();
|
|
54
|
+
const columnPatterns = table.columnPatterns();
|
|
55
|
+
const columnFormats = table.columnFormats();
|
|
56
|
+
|
|
57
|
+
const [listColumns, setListColumns] = useState<ColumnEx<any[]>[]>([]);
|
|
58
|
+
const [sortColumn, setSortColumn] = useState<SortColumn>();
|
|
59
|
+
const [rows, setRows] = useState<any[]>([]);
|
|
60
|
+
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number>>(new Set());
|
|
61
|
+
|
|
62
|
+
// Columns ---
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
setListColumns([
|
|
65
|
+
...multiSelect ? [SelectColumn as ColumnEx<any[]>] : [],
|
|
66
|
+
...columns.map((column): ColumnEx<any[]> => {
|
|
67
|
+
const type = columnTypes[column] ?? "string";
|
|
68
|
+
let formatter;
|
|
69
|
+
let __hpcc_pattern;
|
|
70
|
+
let __hpcc_format;
|
|
71
|
+
switch (type) {
|
|
72
|
+
case "time":
|
|
73
|
+
__hpcc_pattern = columnPatterns[column] !== undefined ? timeParse(columnPatterns[column]) : undefined;
|
|
74
|
+
__hpcc_format = columnFormats[column] !== undefined ? timeFormat(columnFormats[column]) : undefined;
|
|
75
|
+
break;
|
|
76
|
+
case "number":
|
|
77
|
+
formatter = (props) => {
|
|
78
|
+
return <div style={{ textAlign: "right" }}>{props.row[props.column.key]}</div>;
|
|
79
|
+
};
|
|
80
|
+
// eslint-disable-next-line no-fallthrough
|
|
81
|
+
default:
|
|
82
|
+
__hpcc_format = columnFormats[column] !== undefined ? format(columnFormats[column]) : undefined;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
key: column,
|
|
86
|
+
name: column,
|
|
87
|
+
resizable: true,
|
|
88
|
+
sortable: true,
|
|
89
|
+
minWidth: 80,
|
|
90
|
+
renderCell: formatter,
|
|
91
|
+
__hpcc_pattern,
|
|
92
|
+
__hpcc_format
|
|
93
|
+
};
|
|
94
|
+
})
|
|
95
|
+
]);
|
|
96
|
+
}, [columnFormats, columnPatterns, columnTypes, columns, multiSelect]);
|
|
97
|
+
|
|
98
|
+
const onSortColumnsChange = useCallback((sortColumns: SortColumn[]) => {
|
|
99
|
+
const futureSortColumn = sortColumns.slice(-1)[0];
|
|
100
|
+
const sorted = futureSortColumn !== undefined;
|
|
101
|
+
const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
|
|
102
|
+
setSortColumn(futureSortColumn);
|
|
103
|
+
setRows(copyAndSort(rows, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
|
|
104
|
+
}, [rows]);
|
|
105
|
+
|
|
106
|
+
const rowKeyGetter = useCallback((row: any) => {
|
|
107
|
+
return row.key;
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
const onSelectedRowsChange = useCallback((selectedRows: Set<any>) => {
|
|
111
|
+
setSelectedRows(selectedRows);
|
|
112
|
+
}, []);
|
|
113
|
+
|
|
114
|
+
const onCellClick = useCallback((row, column) => {
|
|
115
|
+
table.onRowClickCallback(row, column.key);
|
|
116
|
+
}, [table]);
|
|
117
|
+
|
|
118
|
+
// Rows ---
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
let items = data.map((row, index) => {
|
|
121
|
+
const retVal = {
|
|
122
|
+
key: index
|
|
123
|
+
};
|
|
124
|
+
listColumns.forEach((column, index) => {
|
|
125
|
+
let val = row[index] as string;
|
|
126
|
+
if (column.__hpcc_pattern && column.__hpcc_format) {
|
|
127
|
+
val = column.__hpcc_format(column.__hpcc_pattern(val));
|
|
128
|
+
} else if (column.__hpcc_pattern) {
|
|
129
|
+
val = column.__hpcc_pattern(val).toString();
|
|
130
|
+
} else if (column.__hpcc_format) {
|
|
131
|
+
val = column.__hpcc_format(val as any);
|
|
132
|
+
}
|
|
133
|
+
retVal[column.key] = val;
|
|
134
|
+
});
|
|
135
|
+
return retVal;
|
|
136
|
+
});
|
|
137
|
+
if (sort?.attribute) {
|
|
138
|
+
items = copyAndSort(items, sort.attribute, sort.descending);
|
|
139
|
+
}
|
|
140
|
+
setRows(items);
|
|
141
|
+
}, [listColumns, data, sort]);
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<ErrorBoundary fallback={<div>Error loading table</div>}>
|
|
145
|
+
<DataGrid
|
|
146
|
+
columns={listColumns}
|
|
147
|
+
headerRowHeight={24}
|
|
148
|
+
rows={rows}
|
|
149
|
+
rowKeyGetter={rowKeyGetter}
|
|
150
|
+
rowHeight={20}
|
|
151
|
+
renderers={{ noRowsFallback: <EmptyRowsRenderer message={table.noDataMessage()} /> as any }}
|
|
152
|
+
className={table.darkMode() ? "rdg-dark" : "rdg-light"}
|
|
153
|
+
sortColumns={sortColumn ? [sortColumn] : []}
|
|
154
|
+
onSortColumnsChange={onSortColumnsChange}
|
|
155
|
+
selectedRows={selectedRows}
|
|
156
|
+
onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
|
|
157
|
+
onCellClick={multiSelect ? undefined : (args, event) => onCellClick(args.row, args.column)}
|
|
158
|
+
style={{ height: "100%" }}
|
|
159
|
+
/>
|
|
160
|
+
</ErrorBoundary>
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
|
package/src/table.css
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
.dgrid2_Table .rdg {
|
|
2
|
-
font-family: monospace, 'Courier New', Courier;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
.dgrid2_Table .rdg-checkbox-label .rdg-checkbox {
|
|
6
|
-
block-size: 16px;
|
|
7
|
-
inline-size: 16px;
|
|
8
|
-
border-width: 2px;
|
|
9
|
-
box-shadow: inset 0 0 0 3px var(--rdg-background-color);
|
|
1
|
+
.dgrid2_Table .rdg {
|
|
2
|
+
font-family: monospace, 'Courier New', Courier;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.dgrid2_Table .rdg-checkbox-label .rdg-checkbox {
|
|
6
|
+
block-size: 16px;
|
|
7
|
+
inline-size: 16px;
|
|
8
|
+
border-width: 2px;
|
|
9
|
+
box-shadow: inset 0 0 0 3px var(--rdg-background-color);
|
|
10
10
|
}
|
package/src/table.md
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
# Table
|
|
2
|
-
|
|
3
|
-
**Class**: `Table`
|
|
4
|
-
|
|
5
|
-
<ClientOnly>
|
|
6
|
-
<hpcc-vitepress preview_border="0px" style="width:100%;height:600px">
|
|
7
|
-
<div id="placeholder" style="height:400px">
|
|
8
|
-
</div>
|
|
9
|
-
<script type="module">
|
|
10
|
-
import { Table } from "@hpcc-js/dgrid2";
|
|
11
|
-
|
|
12
|
-
new Table()
|
|
13
|
-
.target("placeholder")
|
|
14
|
-
.columns(["Category", "Series-1", "Series-2", "Series-3", "Series-4"])
|
|
15
|
-
.data([
|
|
16
|
-
["A", -25, -23, -25, -22],
|
|
17
|
-
["B", -20, -21, -25, -21],
|
|
18
|
-
["C", -18, -20, -25, -19],
|
|
19
|
-
["D", -17, -17, -25, -18],
|
|
20
|
-
["E", -16, -15, -19, -18],
|
|
21
|
-
["F", -15, -14, -16, -16],
|
|
22
|
-
["G", -12, -10, -14, -15],
|
|
23
|
-
["H", -12, -8, -13, -15],
|
|
24
|
-
["I", -11, -6, -12, -12],
|
|
25
|
-
["J", -11, -6, -8, -12],
|
|
26
|
-
["K", -9, 0, -5, -10],
|
|
27
|
-
["L", -5, 1, -5, -9],
|
|
28
|
-
["M", -5, 2, -4, -8],
|
|
29
|
-
["N", -1, 4, -2, -7],
|
|
30
|
-
["O", 3, 7, 0, -5],
|
|
31
|
-
["P", 3, 8, 0, -3],
|
|
32
|
-
["Q", 4, 8, 7, 0],
|
|
33
|
-
["R", 6, 9, 11, 1],
|
|
34
|
-
["S", 9, 11, 11, 5],
|
|
35
|
-
["T", 10, 20, 12, 6],
|
|
36
|
-
["U", 12, 20, 16, 8],
|
|
37
|
-
["V", 12, 21, 18, 14],
|
|
38
|
-
["W", 14, 21, 18, 18],
|
|
39
|
-
["X", 15, 23, 21, 18],
|
|
40
|
-
["Y", 21, 23, 23, 21],
|
|
41
|
-
["Z", 23, 24, 24, 24]
|
|
42
|
-
])
|
|
43
|
-
.render()
|
|
44
|
-
;
|
|
45
|
-
</script>
|
|
46
|
-
</hpcc-vitepress>
|
|
47
|
-
</ClientOnly>
|
|
48
|
-
|
|
49
|
-
::: tip
|
|
50
|
-
See [Getting Started](../README) for details on how to include @hpcc-js/dgrid2 in your application
|
|
51
|
-
:::
|
|
52
|
-
|
|
53
|
-
## `Table`
|
|
54
|
-
|
|
55
|
-
## Events
|
|
56
|
-
|
|
57
|
-
## Credits
|
|
1
|
+
# Table
|
|
2
|
+
|
|
3
|
+
**Class**: `Table`
|
|
4
|
+
|
|
5
|
+
<ClientOnly>
|
|
6
|
+
<hpcc-vitepress preview_border="0px" style="width:100%;height:600px">
|
|
7
|
+
<div id="placeholder" style="height:400px">
|
|
8
|
+
</div>
|
|
9
|
+
<script type="module">
|
|
10
|
+
import { Table } from "@hpcc-js/dgrid2";
|
|
11
|
+
|
|
12
|
+
new Table()
|
|
13
|
+
.target("placeholder")
|
|
14
|
+
.columns(["Category", "Series-1", "Series-2", "Series-3", "Series-4"])
|
|
15
|
+
.data([
|
|
16
|
+
["A", -25, -23, -25, -22],
|
|
17
|
+
["B", -20, -21, -25, -21],
|
|
18
|
+
["C", -18, -20, -25, -19],
|
|
19
|
+
["D", -17, -17, -25, -18],
|
|
20
|
+
["E", -16, -15, -19, -18],
|
|
21
|
+
["F", -15, -14, -16, -16],
|
|
22
|
+
["G", -12, -10, -14, -15],
|
|
23
|
+
["H", -12, -8, -13, -15],
|
|
24
|
+
["I", -11, -6, -12, -12],
|
|
25
|
+
["J", -11, -6, -8, -12],
|
|
26
|
+
["K", -9, 0, -5, -10],
|
|
27
|
+
["L", -5, 1, -5, -9],
|
|
28
|
+
["M", -5, 2, -4, -8],
|
|
29
|
+
["N", -1, 4, -2, -7],
|
|
30
|
+
["O", 3, 7, 0, -5],
|
|
31
|
+
["P", 3, 8, 0, -3],
|
|
32
|
+
["Q", 4, 8, 7, 0],
|
|
33
|
+
["R", 6, 9, 11, 1],
|
|
34
|
+
["S", 9, 11, 11, 5],
|
|
35
|
+
["T", 10, 20, 12, 6],
|
|
36
|
+
["U", 12, 20, 16, 8],
|
|
37
|
+
["V", 12, 21, 18, 14],
|
|
38
|
+
["W", 14, 21, 18, 18],
|
|
39
|
+
["X", 15, 23, 21, 18],
|
|
40
|
+
["Y", 21, 23, 23, 21],
|
|
41
|
+
["Z", 23, 24, 24, 24]
|
|
42
|
+
])
|
|
43
|
+
.render()
|
|
44
|
+
;
|
|
45
|
+
</script>
|
|
46
|
+
</hpcc-vitepress>
|
|
47
|
+
</ClientOnly>
|
|
48
|
+
|
|
49
|
+
::: tip
|
|
50
|
+
See [Getting Started](../README) for details on how to include @hpcc-js/dgrid2 in your application
|
|
51
|
+
:::
|
|
52
|
+
|
|
53
|
+
## `Table`
|
|
54
|
+
|
|
55
|
+
## Events
|
|
56
|
+
|
|
57
|
+
## Credits
|