@hpcc-js/dgrid2 0.0.0 → 2.1.0
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 -0
- package/README.md +5 -5
- package/dist/index.es6.js +115 -115
- package/dist/index.es6.js.map +1 -1
- package/dist/index.js +115 -115
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/package.json +63 -62
- package/src/__package__.ts +3 -3
- package/src/index.ts +2 -2
- package/src/table.css +9 -9
- package/src/table.md +57 -57
- package/src/table.tsx +165 -165
- package/src/test.ts +41 -41
- package/types/__package__.d.ts +3 -3
- package/types/__package__.d.ts.map +1 -1
- package/types/index.d.ts +2 -2
- package/types/table.d.ts +19 -19
- package/types/test.d.ts +5 -5
- package/types-3.4/__package__.d.ts +2 -2
package/package.json
CHANGED
|
@@ -1,62 +1,63 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hpcc-js/dgrid2",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "hpcc-js - DGrid2",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.es6",
|
|
7
|
-
"unpkg": "dist/index.min.js",
|
|
8
|
-
"jsdelivr": "dist/index.min.js",
|
|
9
|
-
"types": "types/index.d.ts",
|
|
10
|
-
"typesVersions": {
|
|
11
|
-
"<3.8": {
|
|
12
|
-
"*": [
|
|
13
|
-
"types-3.4/index.d.ts"
|
|
14
|
-
]
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist/*",
|
|
19
|
-
"types/*",
|
|
20
|
-
"types-3.4/*",
|
|
21
|
-
"src/*"
|
|
22
|
-
],
|
|
23
|
-
"scripts": {
|
|
24
|
-
"clean": "rimraf lib* types dist *.tsbuildinfo",
|
|
25
|
-
"compile-es6": "tsc --module es6 --outDir ./lib-es6",
|
|
26
|
-
"compile-es6-watch": "npm run compile-es6 -- -w",
|
|
27
|
-
"compile-umd": "tsc --module umd --outDir ./lib-umd",
|
|
28
|
-
"compile-umd-watch": "npm run compile-umd -- -w",
|
|
29
|
-
"bundle": "rollup -c",
|
|
30
|
-
"bundle-watch": "npm run bundle -- -w",
|
|
31
|
-
"minimize": "terser dist/index.js -c -m --source-map \"content='dist/index.js.map',url='index.min.js.map'\" -o dist/index.min.js",
|
|
32
|
-
"gen-legacy-types": "downlevel-dts ./types ./types-3.4",
|
|
33
|
-
"build": "npm run compile-es6 && npm run bundle",
|
|
34
|
-
"watch": "npm-run-all compile-es6 -p compile-es6-watch bundle-watch",
|
|
35
|
-
"stamp": "node ../../node_modules/@hpcc-js/bundle/src/stamp.js",
|
|
36
|
-
"lint": "eslint src/**/*.ts",
|
|
37
|
-
"docs": "typedoc --options tdoptions.json .",
|
|
38
|
-
"update": "npx npm-check-updates -u -t minor"
|
|
39
|
-
},
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"@hpcc-js/common": "^2.
|
|
42
|
-
"@hpcc-js/util": "^2.
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"tslib": "2.3.1"
|
|
50
|
-
},
|
|
51
|
-
"repository": {
|
|
52
|
-
"type": "git",
|
|
53
|
-
"url": "git+https://github.com/hpcc-systems/Visualization.git"
|
|
54
|
-
},
|
|
55
|
-
"author": "Gordon Smith <gordonjsmith@gmail.com>",
|
|
56
|
-
"contributors": [],
|
|
57
|
-
"license": "Apache-2.0",
|
|
58
|
-
"bugs": {
|
|
59
|
-
"url": "https://github.com/hpcc-systems/Visualization/issues"
|
|
60
|
-
},
|
|
61
|
-
"homepage": "https://github.com/hpcc-systems/Visualization"
|
|
62
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@hpcc-js/dgrid2",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "hpcc-js - DGrid2",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.es6",
|
|
7
|
+
"unpkg": "dist/index.min.js",
|
|
8
|
+
"jsdelivr": "dist/index.min.js",
|
|
9
|
+
"types": "types/index.d.ts",
|
|
10
|
+
"typesVersions": {
|
|
11
|
+
"<3.8": {
|
|
12
|
+
"*": [
|
|
13
|
+
"types-3.4/index.d.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/*",
|
|
19
|
+
"types/*",
|
|
20
|
+
"types-3.4/*",
|
|
21
|
+
"src/*"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "rimraf lib* types dist *.tsbuildinfo",
|
|
25
|
+
"compile-es6": "tsc --module es6 --outDir ./lib-es6",
|
|
26
|
+
"compile-es6-watch": "npm run compile-es6 -- -w",
|
|
27
|
+
"compile-umd": "tsc --module umd --outDir ./lib-umd",
|
|
28
|
+
"compile-umd-watch": "npm run compile-umd -- -w",
|
|
29
|
+
"bundle": "rollup -c",
|
|
30
|
+
"bundle-watch": "npm run bundle -- -w",
|
|
31
|
+
"minimize": "terser dist/index.js -c -m --source-map \"content='dist/index.js.map',url='index.min.js.map'\" -o dist/index.min.js",
|
|
32
|
+
"gen-legacy-types": "downlevel-dts ./types ./types-3.4",
|
|
33
|
+
"build": "npm run compile-es6 && npm run bundle",
|
|
34
|
+
"watch": "npm-run-all compile-es6 -p compile-es6-watch bundle-watch",
|
|
35
|
+
"stamp": "node ../../node_modules/@hpcc-js/bundle/src/stamp.js",
|
|
36
|
+
"lint": "eslint src/**/*.ts",
|
|
37
|
+
"docs": "typedoc --options tdoptions.json .",
|
|
38
|
+
"update": "npx npm-check-updates -u -t minor"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@hpcc-js/common": "^2.68.0",
|
|
42
|
+
"@hpcc-js/util": "^2.47.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@githubocto/flat-ui": "0.14.0",
|
|
46
|
+
"@hpcc-js/bundle": "^2.11.1",
|
|
47
|
+
"preact": "10.7.1",
|
|
48
|
+
"react-data-grid": "7.0.0-beta.11",
|
|
49
|
+
"tslib": "2.3.1"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/hpcc-systems/Visualization.git"
|
|
54
|
+
},
|
|
55
|
+
"author": "Gordon Smith <gordonjsmith@gmail.com>",
|
|
56
|
+
"contributors": [],
|
|
57
|
+
"license": "Apache-2.0",
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/hpcc-systems/Visualization/issues"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/hpcc-systems/Visualization",
|
|
62
|
+
"gitHead": "33aa52bc641400d8fb3bfa51dcd4c94a9599fc90"
|
|
63
|
+
}
|
package/src/__package__.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const PKG_NAME = "@hpcc-js/dgrid2";
|
|
2
|
-
export const PKG_VERSION = "
|
|
3
|
-
export const BUILD_VERSION = "2.
|
|
1
|
+
export const PKG_NAME = "@hpcc-js/dgrid2";
|
|
2
|
+
export const PKG_VERSION = "2.1.0";
|
|
3
|
+
export const BUILD_VERSION = "2.103.0";
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./__package__";
|
|
2
|
-
export * from "./table";
|
|
1
|
+
export * from "./__package__";
|
|
2
|
+
export * from "./table";
|
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 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 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
|
package/src/table.tsx
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
import { HTMLWidget, publish } from "@hpcc-js/common";
|
|
2
|
-
import * as React from "preact/compat";
|
|
3
|
-
import DataGrid, { Column, SelectColumn, SortColumn } from "react-data-grid";
|
|
4
|
-
|
|
5
|
-
import "../src/table.css";
|
|
6
|
-
|
|
7
|
-
export type QuerySortItem = { attribute: string, descending: boolean };
|
|
8
|
-
function copyAndSort<T>(items: T[], attribute: string, descending?: boolean): T[] {
|
|
9
|
-
const key = attribute as keyof T;
|
|
10
|
-
return [...items].sort((a: T, b: T) => {
|
|
11
|
-
if (a[key] < b[key]) {
|
|
12
|
-
return descending ? 1 : -1;
|
|
13
|
-
} else if (a[key] > b[key]) {
|
|
14
|
-
return descending ? -1 : 1;
|
|
15
|
-
}
|
|
16
|
-
return 0;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
interface ReactTableProps {
|
|
21
|
-
columns: string[];
|
|
22
|
-
data: Array<string | number>[];
|
|
23
|
-
onRowClickCallback: (row: any) => void;
|
|
24
|
-
sort?: QuerySortItem,
|
|
25
|
-
darkMode?: boolean;
|
|
26
|
-
multiSelect?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const ReactTable: React.FunctionComponent<ReactTableProps> = ({
|
|
30
|
-
columns,
|
|
31
|
-
data,
|
|
32
|
-
onRowClickCallback,
|
|
33
|
-
sort,
|
|
34
|
-
darkMode = false,
|
|
35
|
-
multiSelect = false
|
|
36
|
-
}) => {
|
|
37
|
-
const [listColumns, setListColumns] = React.useState<Column<object>[]>([]);
|
|
38
|
-
const [sortColumn, setSortColumn] = React.useState<SortColumn>();
|
|
39
|
-
const [items, setItems] = React.useState<any[]>([]);
|
|
40
|
-
const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(() => new Set());
|
|
41
|
-
|
|
42
|
-
// Columns ---
|
|
43
|
-
React.useEffect(() => {
|
|
44
|
-
setListColumns([
|
|
45
|
-
...multiSelect ? [SelectColumn] : [],
|
|
46
|
-
...columns.map(column => ({
|
|
47
|
-
key: column,
|
|
48
|
-
name: column,
|
|
49
|
-
resizable: true,
|
|
50
|
-
sortable: true,
|
|
51
|
-
minWidth: 80,
|
|
52
|
-
}))
|
|
53
|
-
]);
|
|
54
|
-
}, [columns, multiSelect]);
|
|
55
|
-
|
|
56
|
-
const onSortColumnsChange = React.useCallback((sortColumns: SortColumn[]) => {
|
|
57
|
-
const futureSortColumn = sortColumns.slice(-1)[0];
|
|
58
|
-
const sorted = futureSortColumn !== undefined;
|
|
59
|
-
const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
|
|
60
|
-
setSortColumn(futureSortColumn);
|
|
61
|
-
setItems(copyAndSort(items, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
|
|
62
|
-
}, [items]);
|
|
63
|
-
|
|
64
|
-
const rowKeyGetter = React.useCallback((row: any) => {
|
|
65
|
-
return row.key;
|
|
66
|
-
}, []);
|
|
67
|
-
|
|
68
|
-
const onSelectedRowsChange = React.useCallback((selectedRows: Set<any>) => {
|
|
69
|
-
setSelectedRows(selectedRows);
|
|
70
|
-
onRowClickCallback(items.filter(row => selectedRows.has(rowKeyGetter(row))));
|
|
71
|
-
}, [items, onRowClickCallback, rowKeyGetter]);
|
|
72
|
-
|
|
73
|
-
const onRowClick = React.useCallback((row, column) => {
|
|
74
|
-
onRowClickCallback(items.filter(item => rowKeyGetter(item) === rowKeyGetter(row)));
|
|
75
|
-
}, [items, onRowClickCallback, rowKeyGetter]);
|
|
76
|
-
|
|
77
|
-
// Rows ---
|
|
78
|
-
React.useEffect(() => {
|
|
79
|
-
let items = data.map((row, index) => {
|
|
80
|
-
const retVal = {
|
|
81
|
-
key: index
|
|
82
|
-
};
|
|
83
|
-
columns.forEach((column, index) => {
|
|
84
|
-
retVal[column] = row[index];
|
|
85
|
-
});
|
|
86
|
-
return retVal;
|
|
87
|
-
});
|
|
88
|
-
if (sort?.attribute) {
|
|
89
|
-
items = copyAndSort(items, sort.attribute, sort.descending);
|
|
90
|
-
}
|
|
91
|
-
setItems(items);
|
|
92
|
-
}, [columns, data, sort]);
|
|
93
|
-
|
|
94
|
-
return <DataGrid
|
|
95
|
-
columns={listColumns}
|
|
96
|
-
headerRowHeight={24}
|
|
97
|
-
rows={items}
|
|
98
|
-
rowKeyGetter={rowKeyGetter}
|
|
99
|
-
rowHeight={20}
|
|
100
|
-
className={darkMode ? "rdg-dark" : "rdg-light"}
|
|
101
|
-
sortColumns={sortColumn ? [sortColumn] : []}
|
|
102
|
-
onSortColumnsChange={onSortColumnsChange}
|
|
103
|
-
selectedRows={selectedRows}
|
|
104
|
-
onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
|
|
105
|
-
onRowClick={multiSelect ? undefined : onRowClick}
|
|
106
|
-
aria-describedby={""}
|
|
107
|
-
aria-label={""}
|
|
108
|
-
aria-labelledby={""}
|
|
109
|
-
style={{ height: "100%" }}
|
|
110
|
-
/>;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
export class Table extends HTMLWidget {
|
|
114
|
-
|
|
115
|
-
protected _div;
|
|
116
|
-
|
|
117
|
-
constructor() {
|
|
118
|
-
super();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
@publish(false, "boolean", "Dark Mode")
|
|
122
|
-
darkMode: publish<this, boolean>;
|
|
123
|
-
@publish(false, "boolean", "Multiple Selection")
|
|
124
|
-
multiSelect: publish<this, boolean>;
|
|
125
|
-
|
|
126
|
-
_prevRow;
|
|
127
|
-
_prevColumn;
|
|
128
|
-
private renderTable() {
|
|
129
|
-
return <ReactTable columns={this.columns()} data={this.data()} darkMode={this.darkMode()} onRowClickCallback={(row, column = "") => {
|
|
130
|
-
if (this._prevRow && JSON.stringify(this._prevRow) !== JSON.stringify(row)) {
|
|
131
|
-
this.click(this._prevRow, this._prevColumn ?? "", false);
|
|
132
|
-
}
|
|
133
|
-
if (row) {
|
|
134
|
-
this.click(row, column, true);
|
|
135
|
-
}
|
|
136
|
-
this._prevRow = row;
|
|
137
|
-
this._prevColumn = column;
|
|
138
|
-
}} />;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
enter(domNode, element) {
|
|
142
|
-
super.enter(domNode, element);
|
|
143
|
-
this._div = element
|
|
144
|
-
.append("div")
|
|
145
|
-
;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
update(domNode, element) {
|
|
149
|
-
super.update(domNode, element);
|
|
150
|
-
this._div.style("width", this.width() + "px");
|
|
151
|
-
this._div.style("height", this.height() + "px");
|
|
152
|
-
React.render(this.renderTable(), this._div.node());
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
exit(domNode, element) {
|
|
156
|
-
React.unmountComponentAtNode(this._div.node());
|
|
157
|
-
this._div.remove();
|
|
158
|
-
super.exit(domNode, element);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Events ---
|
|
162
|
-
click(row, col, sel) {
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
Table.prototype._class += " dgrid2_Table";
|
|
1
|
+
import { HTMLWidget, publish } from "@hpcc-js/common";
|
|
2
|
+
import * as React from "preact/compat";
|
|
3
|
+
import DataGrid, { Column, SelectColumn, SortColumn } from "react-data-grid";
|
|
4
|
+
|
|
5
|
+
import "../src/table.css";
|
|
6
|
+
|
|
7
|
+
export type QuerySortItem = { attribute: string, descending: boolean };
|
|
8
|
+
function copyAndSort<T>(items: T[], attribute: string, descending?: boolean): T[] {
|
|
9
|
+
const key = attribute as keyof T;
|
|
10
|
+
return [...items].sort((a: T, b: T) => {
|
|
11
|
+
if (a[key] < b[key]) {
|
|
12
|
+
return descending ? 1 : -1;
|
|
13
|
+
} else if (a[key] > b[key]) {
|
|
14
|
+
return descending ? -1 : 1;
|
|
15
|
+
}
|
|
16
|
+
return 0;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ReactTableProps {
|
|
21
|
+
columns: string[];
|
|
22
|
+
data: Array<string | number>[];
|
|
23
|
+
onRowClickCallback: (row: any) => void;
|
|
24
|
+
sort?: QuerySortItem,
|
|
25
|
+
darkMode?: boolean;
|
|
26
|
+
multiSelect?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ReactTable: React.FunctionComponent<ReactTableProps> = ({
|
|
30
|
+
columns,
|
|
31
|
+
data,
|
|
32
|
+
onRowClickCallback,
|
|
33
|
+
sort,
|
|
34
|
+
darkMode = false,
|
|
35
|
+
multiSelect = false
|
|
36
|
+
}) => {
|
|
37
|
+
const [listColumns, setListColumns] = React.useState<Column<object>[]>([]);
|
|
38
|
+
const [sortColumn, setSortColumn] = React.useState<SortColumn>();
|
|
39
|
+
const [items, setItems] = React.useState<any[]>([]);
|
|
40
|
+
const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(() => new Set());
|
|
41
|
+
|
|
42
|
+
// Columns ---
|
|
43
|
+
React.useEffect(() => {
|
|
44
|
+
setListColumns([
|
|
45
|
+
...multiSelect ? [SelectColumn] : [],
|
|
46
|
+
...columns.map(column => ({
|
|
47
|
+
key: column,
|
|
48
|
+
name: column,
|
|
49
|
+
resizable: true,
|
|
50
|
+
sortable: true,
|
|
51
|
+
minWidth: 80,
|
|
52
|
+
}))
|
|
53
|
+
]);
|
|
54
|
+
}, [columns, multiSelect]);
|
|
55
|
+
|
|
56
|
+
const onSortColumnsChange = React.useCallback((sortColumns: SortColumn[]) => {
|
|
57
|
+
const futureSortColumn = sortColumns.slice(-1)[0];
|
|
58
|
+
const sorted = futureSortColumn !== undefined;
|
|
59
|
+
const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
|
|
60
|
+
setSortColumn(futureSortColumn);
|
|
61
|
+
setItems(copyAndSort(items, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
|
|
62
|
+
}, [items]);
|
|
63
|
+
|
|
64
|
+
const rowKeyGetter = React.useCallback((row: any) => {
|
|
65
|
+
return row.key;
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
const onSelectedRowsChange = React.useCallback((selectedRows: Set<any>) => {
|
|
69
|
+
setSelectedRows(selectedRows);
|
|
70
|
+
onRowClickCallback(items.filter(row => selectedRows.has(rowKeyGetter(row))));
|
|
71
|
+
}, [items, onRowClickCallback, rowKeyGetter]);
|
|
72
|
+
|
|
73
|
+
const onRowClick = React.useCallback((row, column) => {
|
|
74
|
+
onRowClickCallback(items.filter(item => rowKeyGetter(item) === rowKeyGetter(row)));
|
|
75
|
+
}, [items, onRowClickCallback, rowKeyGetter]);
|
|
76
|
+
|
|
77
|
+
// Rows ---
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
let items = data.map((row, index) => {
|
|
80
|
+
const retVal = {
|
|
81
|
+
key: index
|
|
82
|
+
};
|
|
83
|
+
columns.forEach((column, index) => {
|
|
84
|
+
retVal[column] = row[index];
|
|
85
|
+
});
|
|
86
|
+
return retVal;
|
|
87
|
+
});
|
|
88
|
+
if (sort?.attribute) {
|
|
89
|
+
items = copyAndSort(items, sort.attribute, sort.descending);
|
|
90
|
+
}
|
|
91
|
+
setItems(items);
|
|
92
|
+
}, [columns, data, sort]);
|
|
93
|
+
|
|
94
|
+
return <DataGrid
|
|
95
|
+
columns={listColumns}
|
|
96
|
+
headerRowHeight={24}
|
|
97
|
+
rows={items}
|
|
98
|
+
rowKeyGetter={rowKeyGetter}
|
|
99
|
+
rowHeight={20}
|
|
100
|
+
className={darkMode ? "rdg-dark" : "rdg-light"}
|
|
101
|
+
sortColumns={sortColumn ? [sortColumn] : []}
|
|
102
|
+
onSortColumnsChange={onSortColumnsChange}
|
|
103
|
+
selectedRows={selectedRows}
|
|
104
|
+
onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
|
|
105
|
+
onRowClick={multiSelect ? undefined : onRowClick}
|
|
106
|
+
aria-describedby={""}
|
|
107
|
+
aria-label={""}
|
|
108
|
+
aria-labelledby={""}
|
|
109
|
+
style={{ height: "100%" }}
|
|
110
|
+
/>;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export class Table extends HTMLWidget {
|
|
114
|
+
|
|
115
|
+
protected _div;
|
|
116
|
+
|
|
117
|
+
constructor() {
|
|
118
|
+
super();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@publish(false, "boolean", "Dark Mode")
|
|
122
|
+
darkMode: publish<this, boolean>;
|
|
123
|
+
@publish(false, "boolean", "Multiple Selection")
|
|
124
|
+
multiSelect: publish<this, boolean>;
|
|
125
|
+
|
|
126
|
+
_prevRow;
|
|
127
|
+
_prevColumn;
|
|
128
|
+
private renderTable() {
|
|
129
|
+
return <ReactTable columns={this.columns()} data={this.data()} darkMode={this.darkMode()} onRowClickCallback={(row, column = "") => {
|
|
130
|
+
if (this._prevRow && JSON.stringify(this._prevRow) !== JSON.stringify(row)) {
|
|
131
|
+
this.click(this._prevRow, this._prevColumn ?? "", false);
|
|
132
|
+
}
|
|
133
|
+
if (row) {
|
|
134
|
+
this.click(row, column, true);
|
|
135
|
+
}
|
|
136
|
+
this._prevRow = row;
|
|
137
|
+
this._prevColumn = column;
|
|
138
|
+
}} />;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
enter(domNode, element) {
|
|
142
|
+
super.enter(domNode, element);
|
|
143
|
+
this._div = element
|
|
144
|
+
.append("div")
|
|
145
|
+
;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
update(domNode, element) {
|
|
149
|
+
super.update(domNode, element);
|
|
150
|
+
this._div.style("width", this.width() + "px");
|
|
151
|
+
this._div.style("height", this.height() + "px");
|
|
152
|
+
React.render(this.renderTable(), this._div.node());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
exit(domNode, element) {
|
|
156
|
+
React.unmountComponentAtNode(this._div.node());
|
|
157
|
+
this._div.remove();
|
|
158
|
+
super.exit(domNode, element);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Events ---
|
|
162
|
+
click(row, col, sel) {
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
Table.prototype._class += " dgrid2_Table";
|