@gpa-gemstone/react-table 1.2.30 → 1.2.32

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.
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ import { IColumnProps } from './Column';
3
+ interface IAdjustableColProps<T> extends IColumnProps<T> {
4
+ /**
5
+ * The minimum Width used for this Collumn - default is 100px
6
+ */
7
+ MinWidth?: number;
8
+ }
9
+ export default function AdjustableCol<T>(props: React.PropsWithChildren<IAdjustableColProps<T>>): JSX.Element;
10
+ interface IHeaderWrapperProps<T> extends IAdjustableColProps<T> {
11
+ setWidth: (w: number, m: number) => void;
12
+ width?: number;
13
+ onSort: (key: string, event: any, field?: keyof T) => void;
14
+ sorted: boolean;
15
+ asc: boolean;
16
+ startAdjustment: (e: any) => void;
17
+ fixedLayout: boolean;
18
+ }
19
+ export declare function AdjColumnHeaderWrapper<T>(props: React.PropsWithChildren<IHeaderWrapperProps<T>>): JSX.Element;
20
+ export {};
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ // ******************************************************************************************************
3
+ // AdjustableColumn.tsx - Gbtc
4
+ //
5
+ // Copyright © 2023, Grid Protection Alliance. All Rights Reserved.
6
+ //
7
+ // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
8
+ // the NOTICE file distributed with this work for additional information regarding copyright ownership.
9
+ // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
10
+ // file except in compliance with the License. You may obtain a copy of the License at:
11
+ //
12
+ // http://opensource.org/licenses/MIT
13
+ //
14
+ // Unless agreed to in writing, the subject software distributed under the License is distributed on an
15
+ // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
16
+ // License for the specific language governing permissions and limitations.
17
+ //
18
+ // Code Modification History:
19
+ // ----------------------------------------------------------------------------------------------------
20
+ // 11/19/2023 - C. Lackner
21
+ // Generated original version of source code.
22
+ //
23
+ // ******************************************************************************************************
24
+ var __assign = (this && this.__assign) || function () {
25
+ __assign = Object.assign || function(t) {
26
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
27
+ s = arguments[i];
28
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
29
+ t[p] = s[p];
30
+ }
31
+ return t;
32
+ };
33
+ return __assign.apply(this, arguments);
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.AdjColumnHeaderWrapper = void 0;
37
+ var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
38
+ var helper_functions_1 = require("@gpa-gemstone/helper-functions");
39
+ var React = require("react");
40
+ function AdjustableCol(props) {
41
+ return React.createElement(React.Fragment, null, props.children);
42
+ }
43
+ exports.default = AdjustableCol;
44
+ function AdjColumnHeaderWrapper(props) {
45
+ var _a, _b;
46
+ var thref = React.useRef(null);
47
+ var _c = React.useState(false), showBorder = _c[0], setShowBorder = _c[1];
48
+ var onHover = React.useCallback(function () { setShowBorder(true); }, []);
49
+ var onLeave = React.useCallback(function () { setShowBorder(false); }, []);
50
+ var style = (props.RowStyle !== undefined) ? __assign({}, props.HeaderStyle) : {};
51
+ if (props.width !== undefined && props.fixedLayout) {
52
+ style.width = props.width;
53
+ style.minWidth = props.width;
54
+ style.maxWidth = props.width;
55
+ }
56
+ if (style.cursor === undefined && ((_a = props.AllowSort) !== null && _a !== void 0 ? _a : true)) {
57
+ style.cursor = 'pointer';
58
+ }
59
+ if (style.position === undefined) {
60
+ style.position = 'relative';
61
+ }
62
+ if (style.borderTop === undefined) {
63
+ style.borderTop = 'none';
64
+ }
65
+ React.useLayoutEffect(function () {
66
+ var _a, _b;
67
+ if (thref.current == null || props.width !== undefined)
68
+ return;
69
+ var w = (_a = (0, helper_functions_1.GetNodeSize)(thref.current)) === null || _a === void 0 ? void 0 : _a.width;
70
+ if (w === undefined)
71
+ return;
72
+ props.setWidth(w, (_b = props.MinWidth) !== null && _b !== void 0 ? _b : 100);
73
+ });
74
+ var onClick = React.useCallback(function (e) {
75
+ props.onSort(props.Key, e, props.Field);
76
+ e.stopPropagation();
77
+ console.log('onCLick th');
78
+ }, [props.onSort]);
79
+ var onClickBorder = React.useCallback(function (e) {
80
+ props.startAdjustment(e);
81
+ console.log('onCLick border div');
82
+ }, [props.startAdjustment]);
83
+ return React.createElement("th", { ref: thref, style: style, onClick: onClick },
84
+ React.createElement("div", { style: {
85
+ width: 5,
86
+ height: '100%',
87
+ position: 'absolute',
88
+ top: 0,
89
+ left: 0,
90
+ opacity: showBorder ? 1 : 0,
91
+ background: '#e9ecef',
92
+ cursor: 'col-resize'
93
+ }, onMouseEnter: onHover, onMouseLeave: onLeave, onMouseDown: onClickBorder }),
94
+ props.sorted && props.asc ? React.createElement("div", { style: { position: 'absolute', width: 25 } }, gpa_symbols_1.SVGIcons.ArrowDropDown) : null,
95
+ props.sorted && !props.asc ? React.createElement("div", { style: { position: 'absolute', width: 25 } }, gpa_symbols_1.SVGIcons.ArrowDropUp) : null,
96
+ React.createElement("div", { style: {
97
+ marginLeft: (props.sorted ? 25 : 0),
98
+ } }, (_b = props.children) !== null && _b !== void 0 ? _b : props.Key));
99
+ }
100
+ exports.AdjColumnHeaderWrapper = AdjColumnHeaderWrapper;
@@ -0,0 +1,102 @@
1
+ import * as React from 'react';
2
+ interface TableProps<T> {
3
+ /**
4
+ * List of T objects used to generate rows
5
+ */
6
+ Data: T[];
7
+ /**
8
+ * Callback when the user clicks on a data entry
9
+ * @param data contains the data including the columnKey
10
+ * @param event the onClick Event to allow propagation as needed
11
+ * @returns
12
+ */
13
+ OnClick?: (data: {
14
+ colKey: string;
15
+ colField?: keyof T;
16
+ row: T;
17
+ data: T[keyof T] | null;
18
+ index: number;
19
+ }, event: any) => void;
20
+ /**
21
+ * Key of the collumn to sort by
22
+ */
23
+ SortKey: string;
24
+ /**
25
+ * Boolen to indicate whether the sort is ascending or descending
26
+ */
27
+ Ascending: boolean;
28
+ /**
29
+ * Callback when the data should be sorted
30
+ * @param data the information of the collumn including the Key of the collumn
31
+ * @param event The onCLick event to allow Propagation as needed
32
+ */
33
+ OnSort(data: {
34
+ colKey: string;
35
+ colField?: keyof T;
36
+ ascending: boolean;
37
+ }, event: any): void;
38
+ /**
39
+ * Class of the table component
40
+ */
41
+ TableClass?: string;
42
+ /**
43
+ * style of the table component
44
+ */
45
+ TableStyle?: React.CSSProperties;
46
+ /**
47
+ * style of the thead component
48
+ */
49
+ TheadStyle?: React.CSSProperties;
50
+ /**
51
+ * Class of the thead component
52
+ */
53
+ TheadClass?: string;
54
+ /**
55
+ * style of the tbody component
56
+ */
57
+ TbodyStyle?: React.CSSProperties;
58
+ /**
59
+ * Class of the tbody component
60
+ */
61
+ TbodyClass?: string;
62
+ /**
63
+ * determines if a row should be styled as selected
64
+ * @param data the item to be checked
65
+ * @returns true if the row should be styled as selected
66
+ */
67
+ Selected?: (data: T) => boolean;
68
+ /**
69
+ *
70
+ * @param data he information of the row including the item of the row
71
+ * @param e the event triggering this
72
+ * @returns
73
+ */
74
+ OnDragStart?: (data: {
75
+ colKey: string;
76
+ colField?: keyof T;
77
+ row: T;
78
+ data: T[keyof T] | null;
79
+ index: number;
80
+ }, e: any) => void;
81
+ /**
82
+ * The default style for the tr element
83
+ */
84
+ RowStyle?: React.CSSProperties;
85
+ /**
86
+ * a Function that retrieves a unique key used for React key properties
87
+ * @param data the item to be turned into a key
88
+ * @returns a unique Key
89
+ */
90
+ KeySelector: (data: T) => string | number;
91
+ /**
92
+ * Optional Element to display in the last row of the Table
93
+ * use this for displaying warnings when the Table content gets cut off
94
+ */
95
+ LastRow?: string | React.ReactNode;
96
+ /**
97
+ * Optional Element to display on upper Right corner
98
+ */
99
+ LastColumn?: string | React.ReactNode;
100
+ }
101
+ export default function AdjustableTable<T>(props: React.PropsWithChildren<TableProps<T>>): JSX.Element;
102
+ export {};
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ // ******************************************************************************************************
3
+ // Adjustable.tsx - Gbtc
4
+ //
5
+ // Copyright © 2023, Grid Protection Alliance. All Rights Reserved.
6
+ //
7
+ // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
8
+ // the NOTICE file distributed with this work for additional information regarding copyright ownership.
9
+ // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
10
+ // file except in compliance with the License. You may obtain a copy of the License at:
11
+ //
12
+ // http://opensource.org/licenses/MIT
13
+ //
14
+ // Unless agreed to in writing, the subject software distributed under the License is distributed on an
15
+ // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
16
+ // License for the specific language governing permissions and limitations.
17
+ //
18
+ // Code Modification History:
19
+ // ----------------------------------------------------------------------------------------------------
20
+ // 11/18/2023 - C. Lackner
21
+ // Generated original version of source code.
22
+ //
23
+ // ******************************************************************************************************
24
+ var __assign = (this && this.__assign) || function () {
25
+ __assign = Object.assign || function(t) {
26
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
27
+ s = arguments[i];
28
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
29
+ t[p] = s[p];
30
+ }
31
+ return t;
32
+ };
33
+ return __assign.apply(this, arguments);
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ var React = require("react");
37
+ var _ = require("lodash");
38
+ var Column_1 = require("./Column");
39
+ var AdjustableColumn_1 = require("./AdjustableColumn");
40
+ function AdjustableTable(props) {
41
+ var _a, _b, _c, _d;
42
+ var _e = React.useState(false), fixLayout = _e[0], setFixLayout = _e[1];
43
+ var _f = React.useState(new Map()), fixedWidths = _f[0], setFixedWidths = _f[1];
44
+ var _g = React.useState(new Map()), adjustedWidths = _g[0], setAdjustedWidths = _g[1];
45
+ var _h = React.useState((_b = (_a = React.Children.map(props.children, function (element) {
46
+ if (!React.isValidElement(element))
47
+ return undefined;
48
+ if (element.type === Column_1.default)
49
+ return element.props.Key;
50
+ return undefined;
51
+ })) === null || _a === void 0 ? void 0 : _a.filter(function (s) { return s !== undefined; })) !== null && _b !== void 0 ? _b : []), fixedkeys = _h[0], setFixedKeys = _h[1];
52
+ var _j = React.useState((_d = (_c = React.Children.map(props.children, function (element) {
53
+ if (!React.isValidElement(element))
54
+ return undefined;
55
+ if (element.type === AdjustableColumn_1.default)
56
+ return element.props.Key;
57
+ return undefined;
58
+ })) === null || _c === void 0 ? void 0 : _c.filter(function (d) { return d !== undefined; })) !== null && _d !== void 0 ? _d : []), adjKeys = _j[0], setAdjKeys = _j[1];
59
+ React.useEffect(function () {
60
+ var _a, _b;
61
+ var keysFixed = (_b = (_a = React.Children.map(props.children, function (element) {
62
+ if (!React.isValidElement(element))
63
+ return undefined;
64
+ if (element.type === Column_1.default)
65
+ return element.props.Key;
66
+ return undefined;
67
+ })) === null || _a === void 0 ? void 0 : _a.filter(function (k) { return k !== undefined; })) !== null && _b !== void 0 ? _b : [];
68
+ var keysAdj = React.Children.map(props.children, function (element) {
69
+ if (!React.isValidElement(element))
70
+ return undefined;
71
+ if (element.type === AdjustableColumn_1.default)
72
+ return element.props.Key;
73
+ return undefined;
74
+ });
75
+ setFixedKeys(function (k) {
76
+ var _a;
77
+ var u = (_a = keysFixed === null || keysFixed === void 0 ? void 0 : keysFixed.filter(function (d) { return d !== undefined; })) !== null && _a !== void 0 ? _a : [];
78
+ if (_.isEqual(k, u))
79
+ return k;
80
+ return u;
81
+ });
82
+ setAdjKeys(function (k) {
83
+ var _a;
84
+ var u = (_a = keysAdj === null || keysAdj === void 0 ? void 0 : keysAdj.filter(function (d) { return d !== undefined; })) !== null && _a !== void 0 ? _a : [];
85
+ if (_.isEqual(k, u))
86
+ return k;
87
+ return u;
88
+ });
89
+ }, [props.children]);
90
+ React.useEffect(function () {
91
+ setFixLayout(false);
92
+ setFixedWidths(new Map());
93
+ setAdjustedWidths(new Map());
94
+ }, [fixedkeys.length, adjKeys.length]);
95
+ React.useEffect(function () {
96
+ var fixed = fixedkeys.length == fixedWidths.size;
97
+ var adj = adjKeys.length == adjustedWidths.size;
98
+ setFixLayout(fixed && adj);
99
+ }, [fixedkeys, adjKeys, fixedWidths, adjustedWidths]);
100
+ var handleSort = React.useCallback(function (data, event) {
101
+ if (data.colKey !== null)
102
+ props.OnSort(data, event);
103
+ }, [props.OnSort]);
104
+ var getWidth = React.useCallback(function (key, type) {
105
+ var _a;
106
+ if (type === 'fixed' && fixedWidths.has(key))
107
+ return fixedWidths.get(key);
108
+ if (type === 'adjustable' && adjustedWidths.has(key))
109
+ return (_a = adjustedWidths.get(key)) === null || _a === void 0 ? void 0 : _a.width;
110
+ return undefined;
111
+ }, [fixedWidths, adjustedWidths]);
112
+ var getMinWidth = React.useCallback(function (key) {
113
+ var _a, _b;
114
+ if (adjustedWidths.has(key))
115
+ return (_b = (_a = adjustedWidths.get(key)) === null || _a === void 0 ? void 0 : _a.minWidth) !== null && _b !== void 0 ? _b : 100;
116
+ return 100;
117
+ }, [fixedWidths, adjustedWidths]);
118
+ var getPrevKey = React.useCallback(function (key) {
119
+ var i = adjKeys.findIndex(function (a) { return a === key; });
120
+ if (i < 1)
121
+ return undefined;
122
+ return adjKeys[i - 1];
123
+ }, [adjKeys]);
124
+ var setWidth = React.useCallback(function (key, width, type, minWidth) {
125
+ var _a, _b;
126
+ if (type === 'fixed')
127
+ setFixedWidths(function (d) {
128
+ var u = new Map(d);
129
+ u.set(key, width);
130
+ return u;
131
+ });
132
+ if (type === 'adjustable') {
133
+ if (adjustedWidths.has(key))
134
+ minWidth = (_b = (_a = adjustedWidths.get(key)) === null || _a === void 0 ? void 0 : _a.minWidth) !== null && _b !== void 0 ? _b : 100;
135
+ setAdjustedWidths(function (d) {
136
+ var u = new Map(d);
137
+ u.set(key, { width: width, minWidth: minWidth !== null && minWidth !== void 0 ? minWidth : 100 });
138
+ return u;
139
+ });
140
+ }
141
+ }, []);
142
+ return (React.createElement("table", { className: props.TableClass !== undefined ? props.TableClass : '', style: props.TableStyle },
143
+ React.createElement(Header, { Class: props.TheadClass, Style: props.TheadStyle, SortKey: props.SortKey, Ascending: props.Ascending, OnSort: handleSort, SetWidth: setWidth, GetWidth: getWidth, GetMinWidth: getMinWidth, GetLeftKey: getPrevKey, LastColumn: props.LastColumn, FixedLayout: fixLayout }, props.children),
144
+ React.createElement(Rows, { DragStart: props.OnDragStart, Data: props.Data, RowStyle: props.RowStyle, BodyStyle: props.TbodyStyle, BodyClass: props.TbodyClass, OnClick: props.OnClick, Selected: props.Selected, KeySelector: props.KeySelector, AdjWidth: adjustedWidths, FixedWidth: fixedWidths, FixedLayout: fixLayout }, props.children),
145
+ props.LastRow !== undefined ? React.createElement("tr", { style: (props.RowStyle !== undefined) ? __assign({}, props.RowStyle) : {}, key: -1 }, props.LastRow) : null));
146
+ }
147
+ exports.default = AdjustableTable;
148
+ function Rows(props) {
149
+ if (props.Data.length === 0)
150
+ return null;
151
+ var getWidth = React.useCallback(function (key, type) {
152
+ var _a;
153
+ if (type === 'fixed' && props.FixedWidth.has(key))
154
+ return props.FixedWidth.get(key);
155
+ if (type === 'adjustable' && props.AdjWidth.has(key))
156
+ return (_a = props.AdjWidth.get(key)) === null || _a === void 0 ? void 0 : _a.width;
157
+ return undefined;
158
+ }, [props.FixedWidth, props.AdjWidth]);
159
+ return (React.createElement("tbody", { style: props.BodyStyle, className: props.BodyClass }, props.Data.map(function (d, i) {
160
+ var style = (props.RowStyle !== undefined) ? __assign({}, props.RowStyle) : {};
161
+ if (style.cursor === undefined && (props.OnClick !== undefined || props.DragStart !== undefined))
162
+ style.cursor = 'pointer';
163
+ if (props.Selected !== undefined && props.Selected(d))
164
+ style.backgroundColor = 'yellow';
165
+ var key = props.KeySelector(d);
166
+ return React.createElement("tr", { key: key, style: { display: (props.FixedLayout ? 'block' : undefined) } },
167
+ React.Children.map(props.children, function (element) {
168
+ if (!React.isValidElement(element))
169
+ return null;
170
+ if (element.type === AdjustableColumn_1.default)
171
+ return React.createElement(Column_1.ColumnDataWrapper, __assign({}, element.props, { width: getWidth(element.props.Key, 'adjustable'), item: d, index: i, key: element.key, fixedLayout: props.FixedLayout }));
172
+ return null;
173
+ }),
174
+ React.Children.map(props.children, function (element) {
175
+ if (!React.isValidElement(element))
176
+ return null;
177
+ if (element.type === Column_1.default)
178
+ return React.createElement(Column_1.ColumnDataWrapper, __assign({}, element.props, { width: getWidth(element.props.Key, 'fixed'), item: d, index: i, key: element.key, fixedLayout: props.FixedLayout }));
179
+ return null;
180
+ }));
181
+ })));
182
+ }
183
+ function Header(props) {
184
+ var trRef = React.useRef(null);
185
+ var _a = React.useState(undefined), adjKeys = _a[0], setAdjKeys = _a[1];
186
+ var _b = React.useState(0), mouseDown = _b[0], setMouseDown = _b[1];
187
+ var _c = React.useState(0), deltaX = _c[0], setDeltaX = _c[1];
188
+ var finishAdjustment = React.useCallback(function (e) {
189
+ var _a, _b;
190
+ var d = deltaX;
191
+ if (adjKeys === undefined)
192
+ return;
193
+ var w = limitMovement(adjKeys[0], adjKeys[1], d);
194
+ var leftWidth = (_a = props.GetWidth(adjKeys[0], 'adjustable')) !== null && _a !== void 0 ? _a : 100;
195
+ var rightWidth = (_b = props.GetWidth(adjKeys[1], 'adjustable')) !== null && _b !== void 0 ? _b : 100;
196
+ props.SetWidth(adjKeys[0], (leftWidth - w), 'adjustable');
197
+ props.SetWidth(adjKeys[1], (rightWidth + w), 'adjustable');
198
+ setMouseDown(0);
199
+ setAdjKeys(undefined);
200
+ setDeltaX(0);
201
+ }, [adjKeys]);
202
+ var onMove = React.useCallback(function (e) {
203
+ if (adjKeys === undefined)
204
+ return;
205
+ var w = limitMovement(adjKeys[0], adjKeys[1], mouseDown - e.screenX);
206
+ setDeltaX(w);
207
+ }, [mouseDown, adjKeys]);
208
+ function limitMovement(leftKey, rightKey, delta) {
209
+ var w = delta;
210
+ var leftWidth = props.GetWidth(leftKey, 'adjustable');
211
+ var rightWidth = props.GetWidth(rightKey, 'adjustable');
212
+ var leftMin = props.GetMinWidth(leftKey);
213
+ var rightMin = props.GetMinWidth(rightKey);
214
+ if ((leftWidth !== null && leftWidth !== void 0 ? leftWidth : 100) - w < leftMin) {
215
+ w = (leftWidth !== null && leftWidth !== void 0 ? leftWidth : 100) - leftMin;
216
+ }
217
+ if ((rightWidth !== null && rightWidth !== void 0 ? rightWidth : 100) + w < rightMin) {
218
+ w = (rightWidth !== null && rightWidth !== void 0 ? rightWidth : 100) - rightMin;
219
+ }
220
+ return w;
221
+ }
222
+ var adjustWidth = function (w, key) {
223
+ if (w === undefined)
224
+ return w;
225
+ if (adjKeys === undefined)
226
+ return w;
227
+ if (key === adjKeys[0])
228
+ return w - deltaX;
229
+ if (key === adjKeys[1])
230
+ return w + deltaX;
231
+ };
232
+ return (React.createElement("thead", { className: props.Class, style: props.Style, onMouseMove: function (e) { onMove(e.nativeEvent); e.stopPropagation(); }, onMouseUp: function (e) { finishAdjustment(e.nativeEvent); e.stopPropagation(); }, onMouseLeave: function (e) { finishAdjustment(e.nativeEvent); e.stopPropagation(); } },
233
+ React.createElement("tr", { ref: trRef },
234
+ React.Children.map(props.children, function (element) {
235
+ var _a;
236
+ if (!React.isValidElement(element))
237
+ return null;
238
+ if (element.type === AdjustableColumn_1.default)
239
+ return React.createElement(AdjustableColumn_1.AdjColumnHeaderWrapper, __assign({}, element.props, { setWidth: function (w, min) { return props.SetWidth(element.props.Key, w, 'adjustable', min); }, onSort: function (key, e, fld) { return props.OnSort({ colKey: key, colField: fld, ascending: props.Ascending }, e); }, sorted: props.SortKey === element.props.Key && ((_a = element.props.AllowSort) !== null && _a !== void 0 ? _a : true), asc: props.Ascending, width: adjustWidth(props.GetWidth(element.props.Key, 'adjustable'), element.props.Key), startAdjustment: function (e) {
240
+ if (props.GetLeftKey(element.props.Key) !== undefined)
241
+ setAdjKeys([props.GetLeftKey(element.props.Key), element.props.Key]);
242
+ setMouseDown(e.screenX);
243
+ }, fixedLayout: props.FixedLayout }));
244
+ return null;
245
+ }),
246
+ React.Children.map(props.children, function (element) {
247
+ var _a;
248
+ if (!React.isValidElement(element))
249
+ return null;
250
+ if (element.type === Column_1.default)
251
+ return React.createElement(Column_1.ColumnHeaderWrapper, __assign({}, element.props, { setWidth: function (w) { return props.SetWidth(element.props.Key, w, 'fixed'); }, onSort: function (key, e, fld) { return props.OnSort({ colKey: key, colField: fld, ascending: props.Ascending }, e); }, sorted: props.SortKey === element.props.Key && ((_a = element.props.AllowSort) !== null && _a !== void 0 ? _a : true), asc: props.Ascending, width: props.GetWidth(element.props.Key, 'fixed'), fixedLayout: props.FixedLayout }));
252
+ return null;
253
+ }),
254
+ React.createElement("th", { style: { width: 17, padding: 0 } }, props.LastColumn))));
255
+ }
@@ -0,0 +1,66 @@
1
+ import * as React from 'react';
2
+ export interface IColumnProps<T> {
3
+ /**
4
+ * a unique Key for this Collumn
5
+ */
6
+ Key: string;
7
+ /**
8
+ * Flag indicating whether sorting by this Collumn is allowed
9
+ */
10
+ AllowSort?: boolean;
11
+ /**
12
+ * Optional - the Field to be used
13
+ */
14
+ Field?: keyof T;
15
+ /**
16
+ * The Default style for the td element
17
+ */
18
+ RowStyle?: React.CSSProperties;
19
+ /**
20
+ * The Default style for the th element
21
+ */
22
+ HeaderStyle?: React.CSSProperties;
23
+ /**
24
+ * Determines the Content to be displaye
25
+ * @param d the data to be turned into content
26
+ * @returns the content displayed
27
+ */
28
+ Content?: (d: {
29
+ item: T;
30
+ key: string;
31
+ field: keyof T | undefined;
32
+ index: number;
33
+ }) => React.ReactNode;
34
+ }
35
+ export default function Column<T>(props: React.PropsWithChildren<IColumnProps<T>>): JSX.Element;
36
+ interface IHeaderWrapperProps<T> extends IColumnProps<T> {
37
+ setWidth: (w: number) => void;
38
+ width?: number;
39
+ onSort: (key: string, event: any, field?: keyof T) => void;
40
+ sorted: boolean;
41
+ asc: boolean;
42
+ fixedLayout: boolean;
43
+ }
44
+ export declare function ColumnHeaderWrapper<T>(props: React.PropsWithChildren<IHeaderWrapperProps<T>>): JSX.Element;
45
+ interface IDataWrapperProps<T> extends IColumnProps<T> {
46
+ width?: number;
47
+ item: T;
48
+ index: number;
49
+ dragStart: (data: {
50
+ colKey: string;
51
+ colField?: keyof T;
52
+ row: T;
53
+ data: T[keyof T] | null;
54
+ index: number;
55
+ }, e: any) => void;
56
+ onClick?: (data: {
57
+ colKey: string;
58
+ colField?: keyof T;
59
+ row: T;
60
+ data: T[keyof T] | null;
61
+ index: number;
62
+ }, e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
63
+ fixedLayout: boolean;
64
+ }
65
+ export declare function ColumnDataWrapper<T>(props: IDataWrapperProps<T>): JSX.Element;
66
+ export {};
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ // ******************************************************************************************************
3
+ // Column.tsx - Gbtc
4
+ //
5
+ // Copyright © 2018, Grid Protection Alliance. All Rights Reserved.
6
+ //
7
+ // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
8
+ // the NOTICE file distributed with this work for additional information regarding copyright ownership.
9
+ // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
10
+ // file except in compliance with the License. You may obtain a copy of the License at:
11
+ //
12
+ // http://opensource.org/licenses/MIT
13
+ //
14
+ // Unless agreed to in writing, the subject software distributed under the License is distributed on an
15
+ // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
16
+ // License for the specific language governing permissions and limitations.
17
+ //
18
+ // Code Modification History:
19
+ // ----------------------------------------------------------------------------------------------------
20
+ // 08/02/2018 - Billy Ernest
21
+ // Generated original version of source code.
22
+ //
23
+ // ******************************************************************************************************
24
+ var __assign = (this && this.__assign) || function () {
25
+ __assign = Object.assign || function(t) {
26
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
27
+ s = arguments[i];
28
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
29
+ t[p] = s[p];
30
+ }
31
+ return t;
32
+ };
33
+ return __assign.apply(this, arguments);
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ColumnDataWrapper = exports.ColumnHeaderWrapper = void 0;
37
+ var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
38
+ var helper_functions_1 = require("@gpa-gemstone/helper-functions");
39
+ var React = require("react");
40
+ function Column(props) {
41
+ return React.createElement(React.Fragment, null, props.children);
42
+ }
43
+ exports.default = Column;
44
+ function ColumnHeaderWrapper(props) {
45
+ var _a, _b;
46
+ var thref = React.useRef(null);
47
+ var style = (props.RowStyle !== undefined) ? __assign({}, props.HeaderStyle) : {};
48
+ if (props.width !== undefined && props.fixedLayout) {
49
+ style.width = props.width;
50
+ style.minWidth = props.width;
51
+ style.maxWidth = props.width;
52
+ style.overflowX = 'hidden';
53
+ style.display = 'inline-block';
54
+ }
55
+ if (style.cursor === undefined && ((_a = props.AllowSort) !== null && _a !== void 0 ? _a : true)) {
56
+ style.cursor = 'pointer';
57
+ }
58
+ if (style.position === undefined) {
59
+ style.position = 'relative';
60
+ }
61
+ if (style.borderTop === undefined) {
62
+ style.borderTop = 'none';
63
+ }
64
+ React.useLayoutEffect(function () {
65
+ var _a;
66
+ if (thref.current == null || props.width !== undefined)
67
+ return;
68
+ var w = (_a = (0, helper_functions_1.GetNodeSize)(thref.current)) === null || _a === void 0 ? void 0 : _a.width;
69
+ if (w === undefined)
70
+ return;
71
+ props.setWidth(w);
72
+ });
73
+ var onClick = React.useCallback(function (e) {
74
+ props.onSort(props.Key, e, props.Field);
75
+ }, [props.onSort]);
76
+ return React.createElement("th", { ref: thref, style: style, onClick: onClick },
77
+ props.sorted && props.asc ? React.createElement("div", { style: { position: 'absolute', width: 25 } }, gpa_symbols_1.SVGIcons.ArrowDropDown) : null,
78
+ props.sorted && !props.asc ? React.createElement("div", { style: { position: 'absolute', width: 25 } }, gpa_symbols_1.SVGIcons.ArrowDropUp) : null,
79
+ React.createElement("div", { style: {
80
+ marginLeft: (props.sorted ? 25 : 0),
81
+ } }, (_b = props.children) !== null && _b !== void 0 ? _b : props.Key));
82
+ }
83
+ exports.ColumnHeaderWrapper = ColumnHeaderWrapper;
84
+ function ColumnDataWrapper(props) {
85
+ var css = (props.RowStyle !== undefined) ? __assign({}, props.RowStyle) : {};
86
+ if (props.width !== undefined && props.fixedLayout) {
87
+ css.width = props.width;
88
+ css.minWidth = props.width;
89
+ css.maxWidth = props.width;
90
+ css.overflowX = 'hidden';
91
+ css.display = 'inline-block';
92
+ }
93
+ if (props.dragStart !== undefined)
94
+ css.cursor = "grab";
95
+ var getFieldValue = function () { return props.Field !== undefined ? props.item[props.Field] : null; };
96
+ var getFieldContent = function () { return props.Content !== undefined ? props.Content({
97
+ item: props.item,
98
+ key: props.Key,
99
+ field: props.Field,
100
+ index: props.index
101
+ }) : getFieldValue(); };
102
+ var onClick = React.useCallback(function (e) {
103
+ if (props.onClick !== undefined)
104
+ props.onClick({
105
+ colKey: props.Key,
106
+ colField: props.Field,
107
+ row: props.item,
108
+ data: getFieldValue(),
109
+ index: props.index
110
+ }, e);
111
+ }, [props.Key, props.Field, props.item, props.index, props.onClick]);
112
+ return (React.createElement("td", { style: css, onClick: onClick, draggable: props.dragStart !== undefined, onDragStart: function (e) {
113
+ props.dragStart({
114
+ colKey: props.Key,
115
+ colField: props.Field,
116
+ row: props.item,
117
+ data: getFieldValue(),
118
+ index: props.index
119
+ }, e);
120
+ } }, getFieldContent()));
121
+ }
122
+ exports.ColumnDataWrapper = ColumnDataWrapper;
@@ -25,7 +25,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.DynamicTable = void 0;
26
26
  var React = require("react");
27
27
  var Table_1 = require("./Table");
28
- var lodash_1 = require("lodash");
29
28
  var empty = new Map();
30
29
  function DynamicTable(props) {
31
30
  if (props.data.length <= 0)
@@ -37,8 +36,8 @@ function DynamicTable(props) {
37
36
  cols.push({ key: key, label: key, field: key });
38
37
  }
39
38
  return (React.createElement("table", { className: props.tableClass !== undefined ? props.tableClass : '', style: props.tableStyle },
40
- React.createElement(Table_1.Header, { Class: props.theadClass, Style: props.theadStyle, Cols: cols, SortKey: props.sortKey, Ascending: props.ascending, Click: function (d) { return handleSort(d); }, SetResizeColIndex: function () { lodash_1.noop; }, AllowResize: false, Width: empty, MeasureWidth: function () { lodash_1.noop; } }),
41
- React.createElement(Table_1.Rows, { Data: props.data, Cols: cols, RowStyle: props.rowStyle, BodyStyle: props.tbodyStyle, BodyClass: props.tbodyClass, Click: function (data, e) { return props.onClick(data, e); }, Selected: props.selected, KeySelector: props.keySelector, Width: empty, MeasureWidth: function () { lodash_1.noop; }, SetTBodyWidth: function () { lodash_1.noop; } })));
39
+ React.createElement(Table_1.Header, { Class: props.theadClass, Style: props.theadStyle, Cols: cols, SortKey: props.sortKey, Ascending: props.ascending, Click: function (d) { return handleSort(d); } }),
40
+ React.createElement(Table_1.Rows, { Data: props.data, Cols: cols, RowStyle: props.rowStyle, BodyStyle: props.tbodyStyle, BodyClass: props.tbodyClass, Click: function (data, e) { return props.onClick(data, e); }, Selected: props.selected, KeySelector: props.keySelector })));
42
41
  function handleSort(data) {
43
42
  if (data.colKey !== null)
44
43
  props.onSort(data);
package/lib/Table.d.ts CHANGED
@@ -6,7 +6,6 @@ export interface Column<T> {
6
6
  headerStyle?: React.CSSProperties;
7
7
  rowStyle?: React.CSSProperties;
8
8
  content?(item: T, key: string, field: keyof T | undefined, style: React.CSSProperties, index: number): React.ReactNode;
9
- allowResize?: boolean;
10
9
  }
11
10
  export interface TableProps<T> {
12
11
  /**
@@ -53,30 +52,11 @@ export interface TableProps<T> {
53
52
  }, e: any) => void);
54
53
  rowStyle?: React.CSSProperties;
55
54
  keySelector?: (data: T) => string;
56
- /**
57
- * Flag indicating if the collumns are resizable
58
- * Default is <code>false</code>
59
- */
60
- allowResize?: boolean;
61
55
  /**
62
56
  * Optional Element to display in the last row of the Table
63
57
  * use this for displaying warnings when the Table content gets cut off
64
58
  */
65
59
  lastRow?: string | React.ReactNode;
66
- /**
67
- * Minimum width of a collumn when allowResize is true
68
- */
69
- MinColWidth?: number;
70
- /**
71
- * Function that updates the width of the Table - primary use is for collumn counting in Cnfigurable Table
72
- * @param w - the width of the table in px
73
- * @returns - void
74
- */
75
- UpdateWidth?: (w: number) => void;
76
- }
77
- interface IWidth {
78
- header: number | undefined;
79
- row: number | undefined;
80
60
  }
81
61
  export default function Table<T>(props: TableProps<T>): JSX.Element;
82
62
  interface IRowProps<T> {
@@ -101,9 +81,6 @@ interface IRowProps<T> {
101
81
  }, e: any) => void);
102
82
  Selected?: ((data: T) => boolean);
103
83
  KeySelector?: (data: T) => string;
104
- Width: Map<string, IWidth>;
105
- MeasureWidth: (key: string, width: number) => void;
106
- SetTBodyWidth: (w: number) => void;
107
84
  }
108
85
  export declare function Rows<T>(props: IRowProps<T>): JSX.Element | null;
109
86
  interface IHeaderProps<T> {
@@ -112,16 +89,11 @@ interface IHeaderProps<T> {
112
89
  Cols: Column<T>[];
113
90
  SortKey: string;
114
91
  Ascending: boolean;
115
- ResizeColIndex?: number;
116
- SetResizeColIndex: (index: number, e: MouseEvent) => void;
117
92
  Click: (data: {
118
93
  colKey: string;
119
94
  colField?: keyof T;
120
95
  ascending: boolean;
121
96
  }, event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
122
- AllowResize: boolean;
123
- Width: Map<string, IWidth>;
124
- MeasureWidth: (key: string, width: number) => void;
125
97
  }
126
98
  export declare function Header<T>(props: IHeaderProps<T>): JSX.Element;
127
99
  export {};
package/lib/Table.js CHANGED
@@ -35,225 +35,24 @@ var __assign = (this && this.__assign) || function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Header = exports.Rows = void 0;
37
37
  var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
38
- var helper_functions_1 = require("@gpa-gemstone/helper-functions");
39
38
  var React = require("react");
40
- var _ = require("lodash");
41
39
  function Table(props) {
42
- var _a, _b;
43
- var measuredWidth = React.useRef(new Map());
44
- var incWidthChange = React.useRef(0);
45
- var minimumColWidth = (_a = props.MinColWidth) !== null && _a !== void 0 ? _a : 100;
46
- var _c = React.useState(null), resizingCol = _c[0], setResizingCol = _c[1];
47
- var allowResize = (_b = props.allowResize) !== null && _b !== void 0 ? _b : false;
48
- var _d = React.useState(new Map()), fixedWidths = _d[0], setFixedWidths = _d[1];
49
- var _e = React.useState(0), positionX = _e[0], setPositionX = _e[1];
50
- var _f = React.useState(0), totalWidth = _f[0], setTotalWidth = _f[1];
51
- var nRow = React.useMemo(function () { return props.cols.length; }, [props.cols]);
52
- React.useEffect(function () { if (props.UpdateWidth !== undefined)
53
- props.UpdateWidth(totalWidth); }, [totalWidth]);
54
- (0, helper_functions_1.useEffectWithPrevious)(function (r) {
55
- if (r === 0 || r === undefined)
56
- return;
57
- var updatedWidth = _.cloneDeep(fixedWidths);
58
- var measuredPrevTotal = Array.from(measuredWidth.current.keys()).reduce(function (s, k) {
59
- var _a, _b;
60
- return s + Math.max(((_b = (_a = measuredWidth.current.get(k)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0), minimumColWidth);
61
- }, 0);
62
- var diff = measuredPrevTotal - totalWidth;
63
- props.cols.forEach(function (c) {
64
- if (!measuredWidth.current.has(c.key))
65
- updatedWidth.set(c.key, { row: minimumColWidth, header: minimumColWidth });
66
- });
67
- var keys = props.cols
68
- .filter(function (c) {
69
- var _a, _b, _c, _d, _e;
70
- return ((_a = c.allowResize) !== null && _a !== void 0 ? _a : true) && updatedWidth.has(c.key) &&
71
- ((_c = (_b = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c.key)) === null || _b === void 0 ? void 0 : _b.header) !== null && _c !== void 0 ? _c : 0) > minimumColWidth && ((_e = (_d = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c.key)) === null || _d === void 0 ? void 0 : _d.row) !== null && _e !== void 0 ? _e : 0) > minimumColWidth;
72
- }).map(function (c) { return c.key; });
73
- var _loop_1 = function () {
74
- var deltaCol = diff / keys.length;
75
- keys.forEach(function (k) {
76
- var _a, _b, _c, _d;
77
- var dCol = determineMaxMovement(updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(k), deltaCol);
78
- updatedWidth.get(k).header = ((_b = (_a = updatedWidth.get(k)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0) - dCol;
79
- updatedWidth.get(k).row = ((_d = (_c = updatedWidth.get(k)) === null || _c === void 0 ? void 0 : _c.row) !== null && _d !== void 0 ? _d : 0) - dCol;
80
- diff = diff - dCol;
81
- });
82
- keys = keys.filter(function (c) { var _a, _b, _c, _d; return ((_b = (_a = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0) > minimumColWidth && ((_d = (_c = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c)) === null || _c === void 0 ? void 0 : _c.row) !== null && _d !== void 0 ? _d : 0) > minimumColWidth; });
83
- };
84
- while (keys.length > 0 && Math.abs(diff) > 1) {
85
- _loop_1();
86
- }
87
- setFixedWidths(updatedWidth);
88
- }, nRow);
89
- (0, helper_functions_1.useEffectWithPrevious)(function (prevWidth) {
90
- if (totalWidth === 0 || prevWidth === 0 || prevWidth === undefined)
91
- return;
92
- if (props.allowResize === undefined || !props.allowResize)
93
- return;
94
- var change = prevWidth - totalWidth;
95
- if (Math.abs(change + incWidthChange.current) < 10) {
96
- incWidthChange.current = incWidthChange.current + change;
97
- return;
98
- }
99
- incWidthChange.current = 0;
100
- var measuredPrevTotal = Array.from(measuredWidth.current.keys()).reduce(function (s, k) {
101
- var _a, _b;
102
- return s + ((_b = (_a = measuredWidth.current.get(k)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0);
103
- }, 0);
104
- var updatedWidth = _.cloneDeep(fixedWidths);
105
- var diff = measuredPrevTotal - totalWidth;
106
- //return;
107
- var keys = props.cols
108
- .filter(function (c) {
109
- var _a, _b, _c, _d, _e;
110
- return ((_a = c.allowResize) !== null && _a !== void 0 ? _a : true) && updatedWidth.has(c.key) &&
111
- ((_c = (_b = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c.key)) === null || _b === void 0 ? void 0 : _b.header) !== null && _c !== void 0 ? _c : 0) > minimumColWidth && ((_e = (_d = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c.key)) === null || _d === void 0 ? void 0 : _d.row) !== null && _e !== void 0 ? _e : 0) > minimumColWidth;
112
- }).map(function (c) { return c.key; });
113
- var _loop_2 = function () {
114
- var deltaCol = diff / keys.length;
115
- keys.forEach(function (k) {
116
- var _a, _b, _c, _d;
117
- var dCol = determineMaxMovement(updatedWidth.get(k), deltaCol);
118
- updatedWidth.get(k).header = ((_b = (_a = updatedWidth.get(k)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0) - dCol;
119
- updatedWidth.get(k).row = ((_d = (_c = updatedWidth.get(k)) === null || _c === void 0 ? void 0 : _c.row) !== null && _d !== void 0 ? _d : 0) - dCol;
120
- diff = diff - dCol;
121
- });
122
- keys = keys.filter(function (c) { var _a, _b, _c, _d; return ((_b = (_a = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0) > minimumColWidth && ((_d = (_c = updatedWidth === null || updatedWidth === void 0 ? void 0 : updatedWidth.get(c)) === null || _c === void 0 ? void 0 : _c.row) !== null && _d !== void 0 ? _d : 0) > minimumColWidth; });
123
- };
124
- while (keys.length > 0 && Math.abs(diff) > 1) {
125
- _loop_2();
126
- }
127
- setFixedWidths(updatedWidth);
128
- }, totalWidth);
129
- var handleSort = React.useCallback(function (data, event) {
40
+ return (React.createElement("table", { className: props.tableClass !== undefined ? props.tableClass : '', style: props.tableStyle },
41
+ React.createElement(Header, { Class: props.theadClass, Style: props.theadStyle, Cols: props.cols, SortKey: props.sortKey, Ascending: props.ascending, Click: function (d, e) { return handleSort(d, e); } }),
42
+ React.createElement(Rows, { DragStart: props.onDragStart, Data: props.data, Cols: props.cols, RowStyle: props.rowStyle, BodyStyle: props.tbodyStyle, BodyClass: props.tbodyClass, Click: function (data, e) { return (props.onClick === undefined ? null : props.onClick(data, e)); }, Selected: props.selected, KeySelector: props.keySelector }),
43
+ props.lastRow !== undefined ? React.createElement("tr", { style: (props.rowStyle !== undefined) ? __assign({}, props.rowStyle) : {}, key: -1 }, props.lastRow) : null));
44
+ function handleSort(data, event) {
130
45
  if (data.colKey !== null)
131
46
  props.onSort(data, event);
132
- }, [props.onSort]);
133
- var handleResizeSelect = React.useCallback(function (i, e) {
134
- var _a;
135
- if ((_a = props.allowResize) !== null && _a !== void 0 ? _a : false) {
136
- setResizingCol(i);
137
- setPositionX(e.clientX);
138
- }
139
- }, [props.allowResize]);
140
- var handleMouseUp = React.useCallback(function (i) {
141
- if (resizingCol === null)
142
- return;
143
- setResizingCol(null);
144
- }, [resizingCol]);
145
- var handleMouseMove = React.useCallback(function (e) {
146
- var _a, _b, _c, _d, _e, _f, _g, _h;
147
- if (resizingCol === null)
148
- return;
149
- var dW = positionX - e.clientX;
150
- if (dW == 0)
151
- return;
152
- // try to reduce index on left by dW
153
- var wLeft = 0, whLeft = 0;
154
- var wRight = 0, whRight = 0;
155
- if (!measuredWidth.current.has(props.cols[resizingCol - 1].key) ||
156
- !measuredWidth.current.has(props.cols[resizingCol].key))
157
- return;
158
- whLeft = (_b = (_a = measuredWidth.current.get(props.cols[resizingCol - 1].key)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0;
159
- whRight = (_d = (_c = measuredWidth.current.get(props.cols[resizingCol].key)) === null || _c === void 0 ? void 0 : _c.header) !== null && _d !== void 0 ? _d : 0;
160
- wLeft = (_f = (_e = measuredWidth.current.get(props.cols[resizingCol - 1].key)) === null || _e === void 0 ? void 0 : _e.row) !== null && _f !== void 0 ? _f : 0;
161
- wRight = (_h = (_g = measuredWidth.current.get(props.cols[resizingCol].key)) === null || _g === void 0 ? void 0 : _g.row) !== null && _h !== void 0 ? _h : 0;
162
- if (dW > 0)
163
- dW = determineMaxMovement(measuredWidth.current.get(props.cols[resizingCol - 1].key), dW);
164
- else
165
- dW = -determineMaxMovement(measuredWidth.current.get(props.cols[resizingCol].key), -dW);
166
- if (dW === 0)
167
- return;
168
- setPositionX(e.clientX);
169
- setFixedWidths(function (d) {
170
- var _a, _b, _c, _d, _e, _f, _g, _h;
171
- var u = new Map(d);
172
- if (!u.has(props.cols[resizingCol - 1].key))
173
- u.set(props.cols[resizingCol - 1].key, { header: undefined, row: undefined });
174
- else {
175
- whLeft = (_b = (_a = u.get(props.cols[resizingCol - 1].key)) === null || _a === void 0 ? void 0 : _a.header) !== null && _b !== void 0 ? _b : 0;
176
- wLeft = (_d = (_c = u.get(props.cols[resizingCol - 1].key)) === null || _c === void 0 ? void 0 : _c.row) !== null && _d !== void 0 ? _d : 0;
177
- }
178
- if (!u.has(props.cols[resizingCol].key))
179
- u.set(props.cols[resizingCol].key, { header: undefined, row: undefined });
180
- else {
181
- whRight = (_f = (_e = u.get(props.cols[resizingCol].key)) === null || _e === void 0 ? void 0 : _e.header) !== null && _f !== void 0 ? _f : 0;
182
- wRight = (_h = (_g = u.get(props.cols[resizingCol].key)) === null || _g === void 0 ? void 0 : _g.row) !== null && _h !== void 0 ? _h : 0;
183
- }
184
- u.get(props.cols[resizingCol - 1].key).header = whLeft - dW;
185
- u.get(props.cols[resizingCol].key).header = whRight + dW;
186
- u.get(props.cols[resizingCol - 1].key).row = wLeft - dW;
187
- u.get(props.cols[resizingCol].key).row = wRight + dW;
188
- return u;
189
- });
190
- }, [resizingCol, positionX]);
191
- var determineMaxMovement = function (w, dW) {
192
- var _a, _b, _c;
193
- if (dW < 0)
194
- return dW;
195
- if (w === undefined)
196
- return 0;
197
- if (((_a = w.header) !== null && _a !== void 0 ? _a : 0) <= minimumColWidth || ((_b = w.row) !== null && _b !== void 0 ? _b : 0) <= minimumColWidth)
198
- return 0;
199
- if (w.row === undefined || w.row === 0)
200
- return Math.min(dW, (((_c = w.header) !== null && _c !== void 0 ? _c : 0) - minimumColWidth));
201
- if (w.header === undefined || w.header === 0)
202
- return Math.min(dW, (w.row - minimumColWidth));
203
- var ratio = w.header / w.row;
204
- if (ratio > 1)
205
- return Math.min(dW, (w.row - minimumColWidth));
206
- return Math.min(dW, (w.header - minimumColWidth));
207
- };
208
- var measureRowWidth = React.useCallback(function (key, width) {
209
- var _a, _b, _c;
210
- if (measuredWidth.current.has(key))
211
- measuredWidth.current.get(key).row = width;
212
- else
213
- measuredWidth.current.set(key, { header: undefined, row: width });
214
- var canResize = (_b = (_a = props.cols.find(function (c) { return c.key === key; })) === null || _a === void 0 ? void 0 : _a.allowResize) !== null && _b !== void 0 ? _b : true;
215
- if ((!fixedWidths.has(key) || (((_c = fixedWidths.get(key)) === null || _c === void 0 ? void 0 : _c.row) === undefined)))
216
- setFixedWidths(function (d) {
217
- var _a;
218
- var u = new Map(d);
219
- u.set(key, { header: (_a = u.get(key)) === null || _a === void 0 ? void 0 : _a.header, row: width });
220
- return u;
221
- });
222
- }, [props.cols, fixedWidths]);
223
- var measureHeaderWidth = React.useCallback(function (key, width) {
224
- if (measuredWidth.current.has(key))
225
- measuredWidth.current.get(key).header = width;
226
- else
227
- measuredWidth.current.set(key, { header: width, row: undefined });
228
- if ((!fixedWidths.has(key) || fixedWidths.get(key).header === undefined))
229
- setFixedWidths(function (d) {
230
- var _a;
231
- var u = new Map(d);
232
- u.set(key, { row: (_a = u.get(key)) === null || _a === void 0 ? void 0 : _a.row, header: width });
233
- return u;
234
- });
235
- }, [props.cols, fixedWidths]);
236
- return (React.createElement("table", { className: props.tableClass !== undefined ? props.tableClass : '', style: props.tableStyle, onMouseLeave: handleMouseUp, onMouseMove: handleMouseMove, onMouseUp: handleMouseUp },
237
- React.createElement(Header, { Class: props.theadClass, Style: props.theadStyle, Cols: props.cols, SortKey: props.sortKey, Ascending: props.ascending, Click: function (d, e) { return handleSort(d, e); }, SetResizeColIndex: handleResizeSelect, AllowResize: allowResize, MeasureWidth: measureHeaderWidth, Width: fixedWidths }),
238
- React.createElement(Rows, { MeasureWidth: measureRowWidth, Width: fixedWidths, DragStart: props.onDragStart, Data: props.data, Cols: props.cols, RowStyle: props.rowStyle, BodyStyle: props.tbodyStyle, BodyClass: props.tbodyClass, Click: function (data, e) { return (props.onClick === undefined ? null : props.onClick(data, e)); }, Selected: props.selected, KeySelector: props.keySelector, SetTBodyWidth: setTotalWidth }),
239
- props.lastRow !== undefined ? React.createElement("tr", { style: (props.rowStyle !== undefined) ? __assign({}, props.rowStyle) : {}, key: -1 }, props.lastRow) : null));
47
+ }
240
48
  }
241
49
  exports.default = Table;
242
50
  function Rows(props) {
243
- var tbody = React.useRef(null);
244
- React.useLayoutEffect(function () {
245
- var _a, _b;
246
- if (tbody.current == null)
247
- return;
248
- var w = (_b = (_a = (0, helper_functions_1.GetNodeSize)(tbody.current)) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
249
- props.SetTBodyWidth(w);
250
- });
251
51
  if (props.Data.length === 0)
252
52
  return null;
253
53
  var rows = props.Data.map(function (item, rowIndex) {
254
54
  var cells = props.Cols.map(function (colData) {
255
- var _a;
256
- return React.createElement(Cell, { key: colData.key, Style: colData.rowStyle, DataKey: colData.key, DataField: colData.field, Object: item, RowIndex: rowIndex, Content: colData.content, Click: function (data, e) { return props.Click(data, e); }, DragStart: props.DragStart, MeasureWidth: function (w) { return props.MeasureWidth(colData.key, w); }, Width: (_a = props.Width.get(colData.key)) === null || _a === void 0 ? void 0 : _a.row });
55
+ return React.createElement(Cell, { key: colData.key, Style: colData.rowStyle, DataKey: colData.key, DataField: colData.field, Object: item, RowIndex: rowIndex, Content: colData.content, Click: function (data, e) { return props.Click(data, e); }, DragStart: props.DragStart });
257
56
  });
258
57
  var style = (props.RowStyle !== undefined) ? __assign({}, props.RowStyle) : {};
259
58
  if (style.cursor === undefined)
@@ -267,95 +66,34 @@ function Rows(props) {
267
66
  }
268
67
  return (React.createElement("tr", { style: style, key: ToKey(rowIndex, item) }, cells));
269
68
  });
270
- return (React.createElement("tbody", { ref: tbody, style: props.BodyStyle, className: props.BodyClass }, rows));
69
+ return (React.createElement("tbody", { style: props.BodyStyle, className: props.BodyClass }, rows));
271
70
  }
272
71
  exports.Rows = Rows;
273
72
  function Cell(props) {
274
- var tdRef = React.useRef(null);
275
- var css = React.useMemo(function () { return (props.Style !== undefined) ? __assign(__assign({}, props.Style), { width: (props.Width !== undefined) ? props.Width : props.Style.width }) : {
276
- width: (props.Width !== undefined) ? props.Width : undefined
277
- }; }, [props.Width, props.Style]);
73
+ var css = (props.Style !== undefined) ? __assign({}, props.Style) : {};
278
74
  if (props.DragStart !== undefined)
279
75
  css.cursor = "grab";
280
76
  var getFieldValue = function () { return props.DataField !== undefined ? props.Object[props.DataField] : null; };
281
77
  var getFieldContent = function () { return props.Content !== undefined ? props.Content(props.Object, props.DataKey, props.DataField, css, props.RowIndex) : getFieldValue(); };
282
- React.useEffect(function () {
283
- var _a, _b;
284
- if (tdRef.current == null)
285
- return;
286
- var w = (_b = (_a = (0, helper_functions_1.GetNodeSize)(tdRef.current)) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
287
- props.MeasureWidth(w - 1);
288
- });
289
- return (React.createElement("td", { ref: tdRef, style: css, onClick: function (e) { return props.Click({ colKey: props.DataKey, colField: props.DataField, row: props.Object, data: getFieldValue(), index: props.RowIndex }, e); }, draggable: props.DragStart !== undefined, onDragStart: function (e) { if (props.DragStart !== undefined)
78
+ return (React.createElement("td", { style: css, onClick: function (e) { return props.Click({ colKey: props.DataKey, colField: props.DataField, row: props.Object, data: getFieldValue(), index: props.RowIndex }, e); }, draggable: props.DragStart !== undefined, onDragStart: function (e) { if (props.DragStart !== undefined)
290
79
  props.DragStart({ colKey: props.DataKey, colField: props.DataField, row: props.Object, data: getFieldValue(), index: props.RowIndex }, e); } }, getFieldContent()));
291
80
  }
292
81
  function Header(props) {
293
- var _a = React.useState(-1), lastResize = _a[0], setLastResize = _a[1];
294
- var _b = React.useState(-1), firstResize = _b[0], setFirstResize = _b[1];
295
- React.useEffect(function () {
296
- var last = (0, helper_functions_1.findLastIndex)(props.Cols, function (c) { return c.allowResize === undefined || c.allowResize; });
297
- var first = props.Cols.findIndex(function (c) { return c.allowResize === undefined || c.allowResize; });
298
- setFirstResize(first);
299
- setLastResize(last);
300
- }, [props.Cols]);
301
- var allowResize = React.useCallback(function (col, i) {
302
- if (i == 0 || !props.AllowResize)
303
- return false;
304
- if (firstResize > -1 && i <= firstResize)
305
- return false;
306
- if (lastResize > -1 && i > lastResize)
307
- return false;
308
- return true;
309
- }, [firstResize, lastResize]);
310
82
  return (React.createElement("thead", { className: props.Class, style: props.Style },
311
- React.createElement("tr", null, props.Cols.map(function (col, i) {
312
- var _a;
313
- return React.createElement(HeaderCell, { key: col.key, HeaderStyle: col.headerStyle, DataKey: col.key, Click: function (e) { return props.Click({ colKey: col.key, colField: col.field, ascending: props.Ascending }, e); }, Label: col.label, SortKey: props.SortKey, Ascending: props.Ascending, PullBorder: allowResize(col, i) ? function (e) { props.SetResizeColIndex(i, e); } : undefined, MeasureWidth: function (width) { return props.MeasureWidth(col.key, width); }, Width: (_a = props.Width.get(col.key)) === null || _a === void 0 ? void 0 : _a.header });
314
- }))));
83
+ React.createElement("tr", null, props.Cols.map(function (col) { return React.createElement(HeaderCell, { key: col.key, HeaderStyle: col.headerStyle, DataKey: col.key, Click: function (e) { return props.Click({ colKey: col.key, colField: col.field, ascending: props.Ascending }, e); }, Label: col.label, SortKey: props.SortKey, Ascending: props.Ascending }); }))));
315
84
  }
316
85
  exports.Header = Header;
317
86
  function HeaderCell(props) {
318
- var thRef = React.useRef(null);
319
- var _a = React.useState(false), showBorder = _a[0], setShowBorder = _a[1];
320
- var style = React.useMemo(function () { return (props.HeaderStyle !== undefined) ? __assign(__assign({}, props.HeaderStyle), { width: (props.Width !== undefined) ? props.Width : props.HeaderStyle.width }) : {
321
- width: (props.Width !== undefined) ? props.Width : undefined
322
- }; }, [props.Width, props.HeaderStyle]);
87
+ var style = (props.HeaderStyle !== undefined) ? props.HeaderStyle : {};
323
88
  if (style.cursor === undefined && props.DataKey !== null) {
324
89
  style.cursor = 'pointer';
325
90
  }
326
91
  if (style.position === undefined) {
327
92
  style.position = 'relative';
328
93
  }
329
- React.useEffect(function () {
330
- var _a;
331
- if (thRef.current == null)
332
- return;
333
- var w = (_a = (0, helper_functions_1.GetNodeSize)(thRef.current)) === null || _a === void 0 ? void 0 : _a.width;
334
- if (w === undefined)
335
- return;
336
- props.MeasureWidth(w - 1);
337
- });
338
- var onHover = React.useCallback(function () { setShowBorder(true); }, []);
339
- var onLeave = React.useCallback(function () { setShowBorder(false); }, []);
340
- var onClick = React.useCallback(function (e) { if (showBorder) {
341
- props.PullBorder(e.nativeEvent);
342
- e.stopPropagation();
343
- } }, [showBorder]);
344
- return (React.createElement("th", { ref: thRef, style: style, onClick: function (e) { return props.Click(e); } },
345
- props.PullBorder != undefined ? React.createElement("div", { style: {
346
- width: 5,
347
- height: '100%',
348
- position: 'absolute',
349
- top: 0,
350
- left: 0,
351
- opacity: showBorder ? 1 : 0,
352
- background: '#e9ecef',
353
- cursor: 'col-resize'
354
- }, onMouseEnter: onHover, onMouseLeave: onLeave, onMouseDown: onClick }) : null,
94
+ return (React.createElement("th", { style: style, onClick: function (e) { return props.Click(e); } },
355
95
  React.createElement(RenderAngleIcon, { SortKey: props.SortKey, Key: props.DataKey, Ascending: props.Ascending }),
356
- React.createElement("div", { style: {
357
- marginLeft: (props.SortKey === props.DataKey ? 25 : 0),
358
- } }, props.Label)));
96
+ React.createElement("div", { style: { marginLeft: (props.SortKey === props.DataKey ? 25 : 0) } }, props.Label)));
359
97
  }
360
98
  function RenderAngleIcon(props) {
361
99
  var AngleIcon = function (a) { return a.ascending ? gpa_symbols_1.SVGIcons.ArrowDropUp : gpa_symbols_1.SVGIcons.ArrowDropDown; };
package/lib/index.d.ts CHANGED
@@ -3,5 +3,13 @@ import { SelectTable, ISelectTableProps } from './SelectTable';
3
3
  import { SearchableTable } from './SearchableTable';
4
4
  import { DynamicTableProps, DynamicTable } from './DynamicTable';
5
5
  import Paging from './Paging';
6
- export { TableProps, SelectTable, ISelectTableProps, SearchableTable, DynamicTable, DynamicTableProps, Rows, Column, Paging };
6
+ import AdjustableTable from './AdjustableTable/AdjustableTable';
7
+ import UpdatedColumn from './AdjustableTable/Column';
8
+ import AdjustableCol from './AdjustableTable/AdjustableColumn';
9
+ declare const ReactTable: {
10
+ Table: typeof AdjustableTable;
11
+ Column: typeof UpdatedColumn;
12
+ AdjustableCol: typeof AdjustableCol;
13
+ };
14
+ export { ReactTable, TableProps, SelectTable, ISelectTableProps, SearchableTable, DynamicTable, DynamicTableProps, Rows, Column, Paging };
7
15
  export default Table;
package/lib/index.js CHANGED
@@ -24,7 +24,7 @@
24
24
  //
25
25
  // ******************************************************************************************************
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.Paging = exports.Rows = exports.DynamicTable = exports.SearchableTable = exports.SelectTable = void 0;
27
+ exports.Paging = exports.Rows = exports.DynamicTable = exports.SearchableTable = exports.SelectTable = exports.ReactTable = void 0;
28
28
  var Table_1 = require("./Table");
29
29
  Object.defineProperty(exports, "Rows", { enumerable: true, get: function () { return Table_1.Rows; } });
30
30
  var SelectTable_1 = require("./SelectTable");
@@ -35,4 +35,13 @@ var DynamicTable_1 = require("./DynamicTable");
35
35
  Object.defineProperty(exports, "DynamicTable", { enumerable: true, get: function () { return DynamicTable_1.DynamicTable; } });
36
36
  var Paging_1 = require("./Paging");
37
37
  exports.Paging = Paging_1.default;
38
+ var AdjustableTable_1 = require("./AdjustableTable/AdjustableTable");
39
+ var Column_1 = require("./AdjustableTable/Column");
40
+ var AdjustableColumn_1 = require("./AdjustableTable/AdjustableColumn");
41
+ var ReactTable = {
42
+ Table: AdjustableTable_1.default,
43
+ Column: Column_1.default,
44
+ AdjustableCol: AdjustableColumn_1.default,
45
+ };
46
+ exports.ReactTable = ReactTable;
38
47
  exports.default = Table_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gpa-gemstone/react-table",
3
- "version": "1.2.30",
3
+ "version": "1.2.32",
4
4
  "description": "Table for GPA web applications",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@gpa-gemstone/gpa-symbols": "0.0.28",
46
- "@gpa-gemstone/helper-functions": "0.0.26",
46
+ "@gpa-gemstone/helper-functions": "0.0.27",
47
47
  "@types/lodash": "^4.14.171",
48
48
  "@types/react": "^17.0.14",
49
49
  "lodash": "^4.17.21",