@hpcc-js/dgrid2 2.1.1 → 2.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hpcc-js/dgrid2",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "hpcc-js - DGrid2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es6",
@@ -32,20 +32,20 @@
32
32
  "gen-legacy-types": "downlevel-dts ./types ./types-3.4",
33
33
  "build": "npm run compile-es6 && npm run bundle",
34
34
  "watch": "npm-run-all compile-es6 -p compile-es6-watch bundle-watch",
35
+ "serve-vite": "vite --config ../../vite.config.js",
35
36
  "stamp": "node ../../node_modules/@hpcc-js/bundle/src/stamp.js",
36
37
  "lint": "eslint src/**/*.ts",
37
38
  "docs": "typedoc --options tdoptions.json .",
38
39
  "update": "npx npm-check-updates -u -t minor"
39
40
  },
40
41
  "dependencies": {
41
- "@hpcc-js/common": "^2.68.1",
42
- "@hpcc-js/util": "^2.47.1"
42
+ "@hpcc-js/common": "^2.71.0",
43
+ "@hpcc-js/util": "^2.48.0"
43
44
  },
44
45
  "devDependencies": {
45
- "@githubocto/flat-ui": "0.14.0",
46
46
  "@hpcc-js/bundle": "^2.11.1",
47
47
  "preact": "10.7.1",
48
- "react-data-grid": "7.0.0-canary.49",
48
+ "react-data-grid": "7.0.0-beta.12",
49
49
  "tslib": "2.3.1"
50
50
  },
51
51
  "repository": {
@@ -59,5 +59,5 @@
59
59
  "url": "https://github.com/hpcc-systems/Visualization/issues"
60
60
  },
61
61
  "homepage": "https://github.com/hpcc-systems/Visualization",
62
- "gitHead": "b7c5d9a853196b53078d665df4368629a1a87903"
62
+ "gitHead": "62cc2d8321326a26647c5ab7e408ece836bfe193"
63
63
  }
@@ -1,3 +1,3 @@
1
1
  export const PKG_NAME = "@hpcc-js/dgrid2";
2
- export const PKG_VERSION = "2.1.1";
3
- export const BUILD_VERSION = "2.103.1";
2
+ export const PKG_VERSION = "2.3.0";
3
+ export const BUILD_VERSION = "2.104.0";
package/src/hooks.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Widget } from "@hpcc-js/common";
2
+ import * as React from "react";
3
+
4
+ export function useData(widget: Widget): [string[], Array<string | number>[]] {
5
+ // eslint-disable-next-line react-hooks/exhaustive-deps
6
+ const columns: string[] = React.useMemo(() => widget.columns(), [widget, widget.dataChecksum()]);
7
+ // eslint-disable-next-line react-hooks/exhaustive-deps
8
+ const data: Array<string | number>[] = React.useMemo(() => widget.data(), [widget, widget.dataChecksum()]);
9
+
10
+ return [columns, data];
11
+ }
12
+
@@ -0,0 +1,161 @@
1
+ import * as React from "react";
2
+ import DataGrid, { Column, SelectColumn, SortColumn } from "react-data-grid";
3
+ import { format, timeFormat, timeParse } from "@hpcc-js/common";
4
+ import { useData } from "./hooks";
5
+ import type { Table } from "./table";
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 EmptyRowsRendererProps {
21
+ message: string
22
+ }
23
+
24
+ const EmptyRowsRenderer: React.FunctionComponent<EmptyRowsRendererProps> = ({
25
+ message
26
+ }) => {
27
+
28
+ return <div style={{ textAlign: "center", gridColumn: "1/-1" }} >
29
+ {message}
30
+ <span>
31
+ --- * --- * ---
32
+ </span>
33
+ </div>;
34
+ };
35
+
36
+ interface ColumnEx<TRow, TSummaryRow = unknown> extends Column<TRow, TSummaryRow> {
37
+ __hpcc_pattern?: ReturnType<typeof timeParse>;
38
+ __hpcc_format?: ReturnType<typeof format> | ReturnType<typeof timeFormat>;
39
+ }
40
+
41
+ export interface ReactTableProps {
42
+ table: Table;
43
+ sort?: QuerySortItem,
44
+ }
45
+
46
+ export const ReactTable: React.FunctionComponent<ReactTableProps> = ({
47
+ table,
48
+ sort
49
+ }) => {
50
+ const [columns, data] = useData(table);
51
+ const multiSelect = table.multiSelect();
52
+ const columnTypes = table.columnTypes();
53
+ const columnPatterns = table.columnPatterns();
54
+ const columnFormats = table.columnFormats();
55
+
56
+ const [listColumns, setListColumns] = React.useState<ColumnEx<any[]>[]>([]);
57
+ const [sortColumn, setSortColumn] = React.useState<SortColumn>();
58
+ const [rows, setRows] = React.useState<any[]>([]);
59
+ const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(new Set());
60
+
61
+ // Columns ---
62
+ React.useEffect(() => {
63
+ setListColumns([
64
+ ...multiSelect ? [SelectColumn] : [],
65
+ ...columns.map((column): ColumnEx<any[]> => {
66
+ const type = columnTypes[column] ?? "string";
67
+ let formatter;
68
+ let __hpcc_pattern;
69
+ let __hpcc_format;
70
+ switch (type) {
71
+ case "time":
72
+ __hpcc_pattern = columnPatterns[column] !== undefined ? timeParse(columnPatterns[column]) : undefined;
73
+ __hpcc_format = columnFormats[column] !== undefined ? timeFormat(columnFormats[column]) : undefined;
74
+ break;
75
+ case "number":
76
+ formatter = (props) => {
77
+ return <div style={{ textAlign: "right" }}>{props.row[props.column.key]}</div>;
78
+ };
79
+ // eslint-disable-next-line no-fallthrough
80
+ default:
81
+ __hpcc_format = columnFormats[column] !== undefined ? format(columnFormats[column]) : undefined;
82
+ }
83
+ return {
84
+ key: column,
85
+ name: column,
86
+ resizable: true,
87
+ sortable: true,
88
+ minWidth: 80,
89
+ formatter,
90
+ __hpcc_pattern,
91
+ __hpcc_format
92
+ };
93
+ })
94
+ ]);
95
+ }, [columnFormats, columnPatterns, columnTypes, columns, multiSelect]);
96
+
97
+ const onSortColumnsChange = React.useCallback((sortColumns: SortColumn[]) => {
98
+ const futureSortColumn = sortColumns.slice(-1)[0];
99
+ const sorted = futureSortColumn !== undefined;
100
+ const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
101
+ setSortColumn(futureSortColumn);
102
+ setRows(copyAndSort(rows, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
103
+ }, [rows]);
104
+
105
+ const rowKeyGetter = React.useCallback((row: any) => {
106
+ return row.key;
107
+ }, []);
108
+
109
+ const onSelectedRowsChange = React.useCallback((selectedRows: Set<any>) => {
110
+ setSelectedRows(selectedRows);
111
+ }, []);
112
+
113
+ const onRowClick = React.useCallback((row, column) => {
114
+ table.onRowClickCallback(row, column.key);
115
+ }, [table]);
116
+
117
+ // Rows ---
118
+ React.useEffect(() => {
119
+ let items = data.map((row, index) => {
120
+ const retVal = {
121
+ key: index
122
+ };
123
+ listColumns.forEach((column, index) => {
124
+ let val = row[index] as string;
125
+ if (column.__hpcc_pattern && column.__hpcc_format) {
126
+ val = column.__hpcc_format(column.__hpcc_pattern(val));
127
+ } else if (column.__hpcc_pattern) {
128
+ val = column.__hpcc_pattern(val).toString();
129
+ } else if (column.__hpcc_format) {
130
+ val = column.__hpcc_format(val as any);
131
+ }
132
+ retVal[column.key] = val;
133
+ });
134
+ return retVal;
135
+ });
136
+ if (sort?.attribute) {
137
+ items = copyAndSort(items, sort.attribute, sort.descending);
138
+ }
139
+ setRows(items);
140
+ }, [listColumns, data, sort]);
141
+
142
+ return <DataGrid
143
+ columns={listColumns}
144
+ headerRowHeight={24}
145
+ rows={rows}
146
+ rowKeyGetter={rowKeyGetter}
147
+ rowHeight={20}
148
+ components={{ noRowsFallback: <EmptyRowsRenderer message={table.noDataMessage()} /> }}
149
+ className={table.darkMode() ? "rdg-dark" : "rdg-light"}
150
+ sortColumns={sortColumn ? [sortColumn] : []}
151
+ onSortColumnsChange={onSortColumnsChange}
152
+ selectedRows={selectedRows}
153
+ onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
154
+ onRowClick={multiSelect ? undefined : onRowClick}
155
+ aria-describedby={""}
156
+ aria-label={""}
157
+ aria-labelledby={""}
158
+ style={{ height: "100%" }}
159
+ />;
160
+ };
161
+
package/src/table.ts ADDED
@@ -0,0 +1,92 @@
1
+ import * as React from "react";
2
+ import { HTMLWidget, publish } from "@hpcc-js/common";
3
+ import { render, unmountComponentAtNode } from "react-dom";
4
+ import { ReactTable } from "./reactTable";
5
+
6
+ import "../src/table.css";
7
+
8
+ export type ColumnType = "boolean" | "number" | "string" | "time";
9
+
10
+ export class Table extends HTMLWidget {
11
+
12
+ protected _div;
13
+
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ @publish("...empty...", "string", "No Data Message")
19
+ noDataMessage: publish<this, string>;
20
+ @publish(false, "boolean", "Dark Mode")
21
+ darkMode: publish<this, boolean>;
22
+ @publish(false, "boolean", "Multiple Selection")
23
+ multiSelect: publish<this, boolean>;
24
+ @publish({}, "object", "Column Types (\"boolean\" | \"number\" | \"string\" | \"time\"")
25
+ columnTypes: publish<this, { [column: string]: ColumnType }>;
26
+ @publish({}, "object", "Column Patterns")
27
+ columnPatterns: publish<this, { [column: string]: string }>;
28
+ @publish({}, "object", "Column Formats")
29
+ columnFormats: publish<this, { [column: string]: string }>;
30
+
31
+ columnType(column: string): ColumnType;
32
+ columnType(column: string, type: ColumnType): this;
33
+ columnType(column: string, type?: ColumnType): ColumnType | this {
34
+ if (arguments.length === 1) return this.columnTypes()[column];
35
+ this.columnTypes({ ...this.columnTypes(), [column]: type });
36
+ return this;
37
+ }
38
+
39
+ columnPattern(column: string): string;
40
+ columnPattern(column: string, pattern: string): this;
41
+ columnPattern(column: string, pattern?: string): string | this {
42
+ if (arguments.length === 1) return this.columnPatterns()[column];
43
+ this.columnPatterns({ ...this.columnPatterns(), [column]: pattern });
44
+ return this;
45
+ }
46
+
47
+ columnFormat(column: string): string;
48
+ columnFormat(column: string, format: string): this;
49
+ columnFormat(column: string, format?: string): string | this {
50
+ if (arguments.length === 1) return this.columnFormats()[column];
51
+ this.columnFormats({ ...this.columnFormats(), [column]: format });
52
+ return this;
53
+ }
54
+
55
+ private _prevRow;
56
+ private _prevColumn;
57
+ onRowClickCallback(row, column) {
58
+ if (this._prevRow && JSON.stringify(this._prevRow) !== JSON.stringify(row)) {
59
+ this.click(this._prevRow, this._prevColumn ?? "", false);
60
+ }
61
+ if (row) {
62
+ this.click(row, column, true);
63
+ }
64
+ this._prevRow = row;
65
+ this._prevColumn = column;
66
+ }
67
+
68
+ enter(domNode, element) {
69
+ super.enter(domNode, element);
70
+ this._div = element
71
+ .append("div")
72
+ ;
73
+ }
74
+
75
+ update(domNode, element) {
76
+ super.update(domNode, element);
77
+ this._div.style("width", this.width() + "px");
78
+ this._div.style("height", this.height() + "px");
79
+ render(React.createElement(ReactTable, { table: this }), this._div.node());
80
+ }
81
+
82
+ exit(domNode, element) {
83
+ unmountComponentAtNode(this._div.node());
84
+ this._div.remove();
85
+ super.exit(domNode, element);
86
+ }
87
+
88
+ // Events ---
89
+ click(row, col, sel) {
90
+ }
91
+ }
92
+ Table.prototype._class += " dgrid2_Table";
@@ -1,4 +1,4 @@
1
1
  export declare const PKG_NAME = "@hpcc-js/dgrid2";
2
- export declare const PKG_VERSION = "2.1.1";
3
- export declare const BUILD_VERSION = "2.103.1";
2
+ export declare const PKG_VERSION = "2.3.0";
3
+ export declare const BUILD_VERSION = "2.104.0";
4
4
  //# sourceMappingURL=__package__.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { Widget } from "@hpcc-js/common";
2
+ export declare function useData(widget: Widget): [string[], Array<string | number>[]];
3
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGzC,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC,CAO5E"}
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import type { Table } from "./table";
3
+ export declare type QuerySortItem = {
4
+ attribute: string;
5
+ descending: boolean;
6
+ };
7
+ export interface ReactTableProps {
8
+ table: Table;
9
+ sort?: QuerySortItem;
10
+ }
11
+ export declare const ReactTable: React.FunctionComponent<ReactTableProps>;
12
+ //# sourceMappingURL=reactTable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactTable.d.ts","sourceRoot":"","sources":["../src/reactTable.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAI/B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,oBAAY,aAAa,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,OAAO,CAAA;CAAE,CAAC;AAkCvE,MAAM,WAAW,eAAe;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb,IAAI,CAAC,EAAE,aAAa,CAAC;CACxB;AAED,eAAO,MAAM,UAAU,EAAE,KAAK,CAAC,iBAAiB,CAAC,eAAe,CAkH/D,CAAC"}
package/types/table.d.ts CHANGED
@@ -1,17 +1,30 @@
1
1
  import { HTMLWidget, publish } from "@hpcc-js/common";
2
2
  import "../src/table.css";
3
- export declare type QuerySortItem = {
4
- attribute: string;
5
- descending: boolean;
6
- };
3
+ export declare type ColumnType = "boolean" | "number" | "string" | "time";
7
4
  export declare class Table extends HTMLWidget {
8
5
  protected _div: any;
9
6
  constructor();
7
+ noDataMessage: publish<this, string>;
10
8
  darkMode: publish<this, boolean>;
11
9
  multiSelect: publish<this, boolean>;
12
- _prevRow: any;
13
- _prevColumn: any;
14
- private renderTable;
10
+ columnTypes: publish<this, {
11
+ [column: string]: ColumnType;
12
+ }>;
13
+ columnPatterns: publish<this, {
14
+ [column: string]: string;
15
+ }>;
16
+ columnFormats: publish<this, {
17
+ [column: string]: string;
18
+ }>;
19
+ columnType(column: string): ColumnType;
20
+ columnType(column: string, type: ColumnType): this;
21
+ columnPattern(column: string): string;
22
+ columnPattern(column: string, pattern: string): this;
23
+ columnFormat(column: string): string;
24
+ columnFormat(column: string, format: string): this;
25
+ private _prevRow;
26
+ private _prevColumn;
27
+ onRowClickCallback(row: any, column: any): void;
15
28
  enter(domNode: any, element: any): void;
16
29
  update(domNode: any, element: any): void;
17
30
  exit(domNode: any, element: any): void;
@@ -1 +1 @@
1
- {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAKtD,OAAO,kBAAkB,CAAC;AAE1B,oBAAY,aAAa,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,OAAO,CAAA;CAAE,CAAC;AA0GvE,qBAAa,KAAM,SAAQ,UAAU;IAEjC,SAAS,CAAC,IAAI,MAAC;;IAOf,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAEjC,WAAW,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAEpC,QAAQ,MAAC;IACT,WAAW,MAAC;IACZ,OAAO,CAAC,WAAW;IAanB,KAAK,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOtB,MAAM,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOvB,IAAI,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOrB,KAAK,CAAC,GAAG,KAAA,EAAE,GAAG,KAAA,EAAE,GAAG,KAAA;CAEtB"}
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAItD,OAAO,kBAAkB,CAAC;AAE1B,oBAAY,UAAU,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAAC;AAElE,qBAAa,KAAM,SAAQ,UAAU;IAEjC,SAAS,CAAC,IAAI,MAAC;;IAOf,aAAa,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAErC,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAEjC,WAAW,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAEpC,WAAW,EAAE,OAAO,CAAC,IAAI,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAA;KAAE,CAAC,CAAC;IAE7D,cAAc,EAAE,OAAO,CAAC,IAAI,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC,CAAC;IAE5D,aAAa,EAAE,OAAO,CAAC,IAAI,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC,CAAC;IAE3D,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IACtC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI;IAOlD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IACrC,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAOpD,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IACpC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAOlD,OAAO,CAAC,QAAQ,CAAC;IACjB,OAAO,CAAC,WAAW,CAAC;IACpB,kBAAkB,CAAC,GAAG,KAAA,EAAE,MAAM,KAAA;IAW9B,KAAK,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOtB,MAAM,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOvB,IAAI,CAAC,OAAO,KAAA,EAAE,OAAO,KAAA;IAOrB,KAAK,CAAC,GAAG,KAAA,EAAE,GAAG,KAAA,EAAE,GAAG,KAAA;CAEtB"}
@@ -1,4 +1,4 @@
1
1
  export declare const PKG_NAME = "@hpcc-js/dgrid2";
2
- export declare const PKG_VERSION = "2.1.1";
3
- export declare const BUILD_VERSION = "2.103.1";
2
+ export declare const PKG_VERSION = "2.3.0";
3
+ export declare const BUILD_VERSION = "2.104.0";
4
4
  //# sourceMappingURL=__package__.d.ts.map
@@ -0,0 +1,6 @@
1
+ import { Widget } from "@hpcc-js/common";
2
+ export declare function useData(widget: Widget): [
3
+ string[],
4
+ Array<string | number>[]
5
+ ];
6
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import { Table } from "./table";
3
+ export declare type QuerySortItem = {
4
+ attribute: string;
5
+ descending: boolean;
6
+ };
7
+ export interface ReactTableProps {
8
+ table: Table;
9
+ sort?: QuerySortItem;
10
+ }
11
+ export declare const ReactTable: React.FunctionComponent<ReactTableProps>;
12
+ //# sourceMappingURL=reactTable.d.ts.map
@@ -1,17 +1,30 @@
1
1
  import { HTMLWidget, publish } from "@hpcc-js/common";
2
2
  import "../src/table.css";
3
- export declare type QuerySortItem = {
4
- attribute: string;
5
- descending: boolean;
6
- };
3
+ export declare type ColumnType = "boolean" | "number" | "string" | "time";
7
4
  export declare class Table extends HTMLWidget {
8
5
  protected _div: any;
9
6
  constructor();
7
+ noDataMessage: publish<this, string>;
10
8
  darkMode: publish<this, boolean>;
11
9
  multiSelect: publish<this, boolean>;
12
- _prevRow: any;
13
- _prevColumn: any;
14
- private renderTable;
10
+ columnTypes: publish<this, {
11
+ [column: string]: ColumnType;
12
+ }>;
13
+ columnPatterns: publish<this, {
14
+ [column: string]: string;
15
+ }>;
16
+ columnFormats: publish<this, {
17
+ [column: string]: string;
18
+ }>;
19
+ columnType(column: string): ColumnType;
20
+ columnType(column: string, type: ColumnType): this;
21
+ columnPattern(column: string): string;
22
+ columnPattern(column: string, pattern: string): this;
23
+ columnFormat(column: string): string;
24
+ columnFormat(column: string, format: string): this;
25
+ private _prevRow;
26
+ private _prevColumn;
27
+ onRowClickCallback(row: any, column: any): void;
15
28
  enter(domNode: any, element: any): void;
16
29
  update(domNode: any, element: any): void;
17
30
  exit(domNode: any, element: any): void;
package/src/table.tsx DELETED
@@ -1,166 +0,0 @@
1
- import { HTMLWidget, publish } from "@hpcc-js/common";
2
- import * as React from "react";
3
- import { render, unmountComponentAtNode } from "react-dom";
4
- import DataGrid, { Column, SelectColumn, SortColumn } from "react-data-grid";
5
-
6
- import "../src/table.css";
7
-
8
- export type QuerySortItem = { attribute: string, descending: boolean };
9
- function copyAndSort<T>(items: T[], attribute: string, descending?: boolean): T[] {
10
- const key = attribute as keyof T;
11
- return [...items].sort((a: T, b: T) => {
12
- if (a[key] < b[key]) {
13
- return descending ? 1 : -1;
14
- } else if (a[key] > b[key]) {
15
- return descending ? -1 : 1;
16
- }
17
- return 0;
18
- });
19
- }
20
-
21
- interface ReactTableProps {
22
- columns: string[];
23
- data: Array<string | number>[];
24
- onRowClickCallback: (row: any) => void;
25
- sort?: QuerySortItem,
26
- darkMode?: boolean;
27
- multiSelect?: boolean;
28
- }
29
-
30
- const ReactTable: React.FunctionComponent<ReactTableProps> = ({
31
- columns,
32
- data,
33
- onRowClickCallback,
34
- sort,
35
- darkMode = false,
36
- multiSelect = false
37
- }) => {
38
- const [listColumns, setListColumns] = React.useState<Column<object>[]>([]);
39
- const [sortColumn, setSortColumn] = React.useState<SortColumn>();
40
- const [items, setItems] = React.useState<any[]>([]);
41
- const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(() => new Set());
42
-
43
- // Columns ---
44
- React.useEffect(() => {
45
- setListColumns([
46
- ...multiSelect ? [SelectColumn] : [],
47
- ...columns.map(column => ({
48
- key: column,
49
- name: column,
50
- resizable: true,
51
- sortable: true,
52
- minWidth: 80,
53
- }))
54
- ]);
55
- }, [columns, multiSelect]);
56
-
57
- const onSortColumnsChange = React.useCallback((sortColumns: SortColumn[]) => {
58
- const futureSortColumn = sortColumns.slice(-1)[0];
59
- const sorted = futureSortColumn !== undefined;
60
- const isSortedDescending: boolean = futureSortColumn?.direction === "DESC";
61
- setSortColumn(futureSortColumn);
62
- setItems(copyAndSort(items, sorted ? futureSortColumn.columnKey : "key", sorted ? isSortedDescending : false));
63
- }, [items]);
64
-
65
- const rowKeyGetter = React.useCallback((row: any) => {
66
- return row.key;
67
- }, []);
68
-
69
- const onSelectedRowsChange = React.useCallback((selectedRows: Set<any>) => {
70
- setSelectedRows(selectedRows);
71
- onRowClickCallback(items.filter(row => selectedRows.has(rowKeyGetter(row))));
72
- }, [items, onRowClickCallback, rowKeyGetter]);
73
-
74
- const onRowClick = React.useCallback((row, column) => {
75
- onRowClickCallback(items.filter(item => rowKeyGetter(item) === rowKeyGetter(row)));
76
- }, [items, onRowClickCallback, rowKeyGetter]);
77
-
78
- // Rows ---
79
- React.useEffect(() => {
80
- let items = data.map((row, index) => {
81
- const retVal = {
82
- key: index
83
- };
84
- columns.forEach((column, index) => {
85
- retVal[column] = row[index];
86
- });
87
- return retVal;
88
- });
89
- if (sort?.attribute) {
90
- items = copyAndSort(items, sort.attribute, sort.descending);
91
- }
92
- setItems(items);
93
- }, [columns, data, sort]);
94
-
95
- return <DataGrid
96
- columns={listColumns}
97
- headerRowHeight={24}
98
- rows={items}
99
- rowKeyGetter={rowKeyGetter}
100
- rowHeight={20}
101
- className={darkMode ? "rdg-dark" : "rdg-light"}
102
- sortColumns={sortColumn ? [sortColumn] : []}
103
- onSortColumnsChange={onSortColumnsChange}
104
- selectedRows={selectedRows}
105
- onSelectedRowsChange={multiSelect ? onSelectedRowsChange : undefined}
106
- onRowClick={multiSelect ? undefined : onRowClick}
107
- aria-describedby={""}
108
- aria-label={""}
109
- aria-labelledby={""}
110
- style={{ height: "100%" }}
111
- />;
112
- };
113
-
114
- export class Table extends HTMLWidget {
115
-
116
- protected _div;
117
-
118
- constructor() {
119
- super();
120
- }
121
-
122
- @publish(false, "boolean", "Dark Mode")
123
- darkMode: publish<this, boolean>;
124
- @publish(false, "boolean", "Multiple Selection")
125
- multiSelect: publish<this, boolean>;
126
-
127
- _prevRow;
128
- _prevColumn;
129
- private renderTable() {
130
- return <ReactTable columns={this.columns()} data={this.data()} darkMode={this.darkMode()} onRowClickCallback={(row, column = "") => {
131
- if (this._prevRow && JSON.stringify(this._prevRow) !== JSON.stringify(row)) {
132
- this.click(this._prevRow, this._prevColumn ?? "", false);
133
- }
134
- if (row) {
135
- this.click(row, column, true);
136
- }
137
- this._prevRow = row;
138
- this._prevColumn = column;
139
- }} />;
140
- }
141
-
142
- enter(domNode, element) {
143
- super.enter(domNode, element);
144
- this._div = element
145
- .append("div")
146
- ;
147
- }
148
-
149
- update(domNode, element) {
150
- super.update(domNode, element);
151
- this._div.style("width", this.width() + "px");
152
- this._div.style("height", this.height() + "px");
153
- render(this.renderTable(), this._div.node());
154
- }
155
-
156
- exit(domNode, element) {
157
- unmountComponentAtNode(this._div.node());
158
- this._div.remove();
159
- super.exit(domNode, element);
160
- }
161
-
162
- // Events ---
163
- click(row, col, sel) {
164
- }
165
- }
166
- Table.prototype._class += " dgrid2_Table";
package/src/test.ts DELETED
@@ -1,41 +0,0 @@
1
- import { Table } from "./table";
2
-
3
- export { Test1 as Test };
4
-
5
- export class Test1 extends Table {
6
-
7
- constructor() {
8
- super();
9
- this
10
- .columns(["Category", "Series-1", "Series-2", "Series-3", "Series-4"])
11
- .data([
12
- ["A", -25, -23, -25, -22],
13
- ["B", -20, -21, -25, -21],
14
- ["C", -18, -20, -25, -19],
15
- ["D", -17, -17, -25, -18],
16
- ["E", -16, -15, -19, -18],
17
- ["F", -15, -14, -16, -16],
18
- ["G", -12, -10, -14, -15],
19
- ["H", -12, -8, -13, -15],
20
- ["I", -11, -6, -12, -12],
21
- ["J", -11, -6, -8, -12],
22
- ["K", -9, 0, -5, -10],
23
- ["L", -5, 1, -5, -9],
24
- ["M", -5, 2, -4, -8],
25
- ["N", -1, 4, -2, -7],
26
- ["O", 3, 7, 0, -5],
27
- ["P", 3, 8, 0, -3],
28
- ["Q", 4, 8, 7, 0],
29
- ["R", 6, 9, 11, 1],
30
- ["S", 9, 11, 11, 5],
31
- ["T", 10, 20, 12, 6],
32
- ["U", 12, 20, 16, 8],
33
- ["V", 12, 21, 18, 14],
34
- ["W", 14, 21, 18, 18],
35
- ["X", 15, 23, 21, 18],
36
- ["Y", 21, 23, 23, 21],
37
- ["Z", 23, 24, 24, 24]
38
- ])
39
- ;
40
- }
41
- }