@gpa-gemstone/react-table 1.2.57 → 1.2.58

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.
Files changed (47) hide show
  1. package/lib/AdjustableTable/Column.d.ts +6 -45
  2. package/lib/AdjustableTable/Column.js +32 -53
  3. package/lib/AdjustableTable/Table.d.ts +2 -116
  4. package/lib/AdjustableTable/Table.js +330 -348
  5. package/lib/AdjustableTable/Types.d.ts +148 -0
  6. package/lib/AdjustableTable/Types.js +24 -0
  7. package/lib/ConfigurableTable/ConfigurableColumn.d.ts +11 -0
  8. package/lib/ConfigurableTable/ConfigurableColumn.js +31 -0
  9. package/lib/ConfigurableTable/ConfigurableTable.d.ts +25 -0
  10. package/lib/ConfigurableTable/ConfigurableTable.js +189 -0
  11. package/lib/FilterableTable/BooleanFilter.d.ts +26 -0
  12. package/lib/FilterableTable/BooleanFilter.js +68 -0
  13. package/lib/FilterableTable/DateTimeFilters.d.ts +10 -0
  14. package/lib/FilterableTable/DateTimeFilters.js +321 -0
  15. package/lib/FilterableTable/EnumFilter.d.ts +37 -0
  16. package/lib/FilterableTable/EnumFilter.js +89 -0
  17. package/lib/FilterableTable/FilterableColumn.d.ts +20 -0
  18. package/lib/FilterableTable/FilterableColumn.js +34 -0
  19. package/lib/FilterableTable/FilterableTable.d.ts +12 -0
  20. package/lib/FilterableTable/FilterableTable.js +95 -0
  21. package/lib/FilterableTable/NumberFilter.d.ts +19 -0
  22. package/lib/FilterableTable/NumberFilter.js +160 -0
  23. package/lib/FilterableTable/TextFilter.d.ts +14 -0
  24. package/lib/FilterableTable/TextFilter.js +78 -0
  25. package/lib/Filters/BooleanFilter.d.ts +26 -0
  26. package/lib/Filters/BooleanFilter.js +68 -0
  27. package/lib/Filters/DateTimeFilters.d.ts +10 -0
  28. package/lib/Filters/DateTimeFilters.js +321 -0
  29. package/lib/Filters/EnumFilter.d.ts +37 -0
  30. package/lib/Filters/EnumFilter.js +89 -0
  31. package/lib/Filters/NumberFilter.d.ts +19 -0
  32. package/lib/Filters/NumberFilter.js +160 -0
  33. package/lib/Filters/TextFilter.d.ts +14 -0
  34. package/lib/Filters/TextFilter.js +78 -0
  35. package/lib/Table/Column.d.ts +19 -0
  36. package/lib/Table/Column.js +73 -0
  37. package/lib/Table/FilterableColumn.d.ts +20 -0
  38. package/lib/Table/FilterableColumn.js +74 -0
  39. package/lib/Table/Table.d.ts +3 -0
  40. package/lib/Table/Table.js +498 -0
  41. package/lib/Table/Types.d.ts +185 -0
  42. package/lib/Table/Types.js +24 -0
  43. package/lib/index.d.ts +7 -14
  44. package/lib/index.js +13 -19
  45. package/package.json +56 -52
  46. package/lib/AdjustableTable/AdjustableColumn.d.ts +0 -18
  47. package/lib/AdjustableTable/AdjustableColumn.js +0 -187
@@ -0,0 +1,498 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Table = Table;
4
+ // ******************************************************************************************************
5
+ // Table.tsx - Gbtc
6
+ //
7
+ // Copyright © 2023, Grid Protection Alliance. All Rights Reserved.
8
+ //
9
+ // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
10
+ // the NOTICE file distributed with this work for additional information regarding copyright ownership.
11
+ // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
12
+ // file except in compliance with the License. You may obtain a copy of the License at:
13
+ //
14
+ // http://opensource.org/licenses/MIT
15
+ //
16
+ // Unless agreed to in writing, the subject software distributed under the License is distributed on an
17
+ // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
18
+ // License for the specific language governing permissions and limitations.
19
+ //
20
+ // Code Modification History:
21
+ // ----------------------------------------------------------------------------------------------------
22
+ // 11/18/2023 - C. Lackner
23
+ // Generated original version of source code.
24
+ // 05/31/2024 - C. Lackner
25
+ // Refactored to fix sizing issues.
26
+ // 12/04/2024 - G. Santos
27
+ // Refactored to fix performance issues.
28
+ //
29
+ // ******************************************************************************************************
30
+ const React = require("react");
31
+ const _ = require("lodash");
32
+ const Column_1 = require("./Column");
33
+ const helper_functions_1 = require("@gpa-gemstone/helper-functions");
34
+ const FilterableColumn_1 = require("./FilterableColumn");
35
+ const defaultTableStyle = {
36
+ padding: 0,
37
+ flex: 1,
38
+ tableLayout: 'fixed',
39
+ overflow: 'hidden',
40
+ display: 'flex',
41
+ flexDirection: 'column',
42
+ marginBottom: 0,
43
+ width: '100%'
44
+ };
45
+ const defaultHeadStyle = {
46
+ fontSize: 'auto',
47
+ tableLayout: 'fixed',
48
+ display: 'table',
49
+ width: '100%'
50
+ };
51
+ const defaultBodyStyle = {
52
+ flex: 1,
53
+ display: 'block',
54
+ overflow: 'auto'
55
+ };
56
+ const defaultRowStyle = {
57
+ display: 'table',
58
+ tableLayout: 'fixed',
59
+ width: '100%'
60
+ };
61
+ const defaultDataHeadStyle = {
62
+ display: 'inline-block',
63
+ position: 'relative',
64
+ borderTop: 'none',
65
+ width: 'auto'
66
+ };
67
+ const defaultDataCellStyle = {
68
+ overflowX: 'hidden',
69
+ display: 'inline-block',
70
+ width: 'auto'
71
+ };
72
+ const IsColumnProps = (props) => ((props === null || props === void 0 ? void 0 : props['Key']) != null);
73
+ const IsColumnAdjustable = (props) => {
74
+ const propValue = props === null || props === void 0 ? void 0 : props['Adjustable'];
75
+ if (propValue === false || propValue === true)
76
+ return propValue;
77
+ return false;
78
+ };
79
+ function Table(props) {
80
+ const bodyRef = React.useRef(null);
81
+ const colWidthsRef = React.useRef(new Map());
82
+ const oldWidthRef = React.useRef(0);
83
+ const [currentTableWidth, setCurrentTableWidth] = React.useState(0);
84
+ const [scrolled, setScrolled] = React.useState(false);
85
+ const [trigger, setTrigger] = React.useState(0);
86
+ // Style consts
87
+ const tableStyle = React.useMemo(() => (Object.assign(Object.assign({}, defaultTableStyle), props.TableStyle)), [props.TableStyle]);
88
+ const headStyle = React.useMemo(() => (Object.assign(Object.assign({}, defaultHeadStyle), props.TheadStyle)), [props.TheadStyle]);
89
+ const bodyStyle = React.useMemo(() => (Object.assign(Object.assign({}, defaultBodyStyle), props.TbodyStyle)), [props.TbodyStyle]);
90
+ const rowStyle = React.useMemo(() => (Object.assign(Object.assign({}, defaultRowStyle), props.RowStyle)), [props.RowStyle]);
91
+ // Send warning if styles are overridden
92
+ React.useEffect(() => {
93
+ if (props.TableStyle !== undefined)
94
+ console.warn('TableStyle properties may be overridden if needed. consider using the defaults');
95
+ if (props.TheadStyle !== undefined)
96
+ console.warn('TheadStyle properties may be overridden if needed. consider using the defaults');
97
+ if (props.TbodyStyle !== undefined)
98
+ console.warn('TBodyStyle properties may be overridden if needed. consider using the defaults');
99
+ if (props.RowStyle !== undefined)
100
+ console.warn('RowStyle properties may be overridden if needed. consider using the defaults');
101
+ }, []);
102
+ // Measure widths and hide columns
103
+ React.useLayoutEffect(() => {
104
+ if (currentTableWidth <= 0)
105
+ return;
106
+ // Helper functions for the calculations
107
+ const getWidthfromProps = (p, type) => {
108
+ var _a, _b;
109
+ // This priotizes rowstyling for width over header, since it was decided that they need to be the same
110
+ if (((_a = p === null || p === void 0 ? void 0 : p.RowStyle) === null || _a === void 0 ? void 0 : _a[type]) !== undefined)
111
+ return p.RowStyle[type];
112
+ if (((_b = p === null || p === void 0 ? void 0 : p.HeaderStyle) === null || _b === void 0 ? void 0 : _b[type]) !== undefined)
113
+ return p.HeaderStyle[type];
114
+ return undefined;
115
+ };
116
+ // Construct base map
117
+ const newMap = new Map();
118
+ React.Children.forEach(props.children, (element) => {
119
+ if (React.isValidElement(element) && IsColumnProps(element.props)) {
120
+ if (newMap.get(element.props.Key) != null)
121
+ console.error("Multiple of the same key detected in table, this will cause issues.");
122
+ newMap.set(element.props.Key, { minWidth: 100, maxWidth: 1000, width: 100 });
123
+ }
124
+ });
125
+ // If width is the same and keys are identical, we can skip the operation
126
+ if (currentTableWidth === oldWidthRef.current &&
127
+ (newMap.size === colWidthsRef.current.size &&
128
+ ![...newMap.keys()].some(key => !colWidthsRef.current.has(key))))
129
+ return;
130
+ // Find and set widths for map
131
+ ['minWidth', 'width', 'maxWidth'].forEach(type => {
132
+ const widthsContainer = document.createElement("div");
133
+ widthsContainer.style.height = '0px';
134
+ widthsContainer.style.width = `${currentTableWidth}px`;
135
+ // Append columns as divs for measurement
136
+ const autoKeys = [];
137
+ const measureKeys = [];
138
+ React.Children.forEach(props.children, (element) => {
139
+ if (React.isValidElement(element) && IsColumnProps(element.props)) {
140
+ let widthValue = getWidthfromProps(element.props, type);
141
+ if (type === 'width' && widthValue == null)
142
+ widthValue = 'auto';
143
+ if (widthValue != null) {
144
+ if (widthValue === 'auto')
145
+ autoKeys.push(element.props.Key);
146
+ else {
147
+ const widthElement = document.createElement("div");
148
+ widthElement.id = element.props.Key + "_measurement";
149
+ widthElement.style.height = '0px';
150
+ if ((widthValue === null || widthValue === void 0 ? void 0 : widthValue.length) != null)
151
+ widthElement.style.width = widthValue;
152
+ else
153
+ widthElement.style.width = `${widthValue}px`;
154
+ widthsContainer.appendChild(widthElement);
155
+ measureKeys.push(element.props.Key);
156
+ }
157
+ }
158
+ }
159
+ });
160
+ document.body.appendChild(widthsContainer);
161
+ // Handle Measurements
162
+ let autoSpace = currentTableWidth;
163
+ measureKeys.forEach(key => {
164
+ const element = document.getElementById(key + "_measurement");
165
+ if (element != null) {
166
+ const widthObj = newMap.get(key);
167
+ if (widthObj != null) {
168
+ autoSpace -= element.clientWidth;
169
+ widthObj[type] = element.clientWidth;
170
+ }
171
+ else
172
+ console.error("Could not find width object for Key: " + key);
173
+ }
174
+ else
175
+ console.error("Could not find measurement div with Key: " + key);
176
+ });
177
+ document.body.removeChild(widthsContainer);
178
+ // Handle Autos (width type only)
179
+ if (type === 'width' && autoKeys.length > 0) {
180
+ const spacePerElement = Math.floor(autoSpace / autoKeys.length);
181
+ autoKeys.forEach(key => {
182
+ const widthObj = newMap.get(key);
183
+ if (widthObj != null)
184
+ widthObj[type] = spacePerElement;
185
+ else
186
+ console.error("Could not find width object for Key: " + key);
187
+ });
188
+ }
189
+ let remainingSpace = currentTableWidth;
190
+ [...newMap.keys()].forEach(key => {
191
+ const widthObj = newMap.get(key);
192
+ if (widthObj != null) {
193
+ if (widthObj.minWidth <= remainingSpace) {
194
+ // This follows behavior consistent with MDN documentation on how these width types should behave
195
+ if (widthObj.minWidth > widthObj.width)
196
+ widthObj.width = widthObj.minWidth;
197
+ if (widthObj.minWidth > widthObj.maxWidth)
198
+ widthObj.maxWidth = widthObj.minWidth;
199
+ if (widthObj.width > widthObj.maxWidth)
200
+ widthObj.width = widthObj.maxWidth;
201
+ // Constrain Width to remainingSpace
202
+ if (widthObj.width > remainingSpace)
203
+ widthObj.width = remainingSpace;
204
+ remainingSpace -= widthObj.width;
205
+ }
206
+ else {
207
+ widthObj.minWidth = 0;
208
+ widthObj.width = 0;
209
+ widthObj.maxWidth = 0;
210
+ }
211
+ }
212
+ else
213
+ console.error("Could not find width object for Key: " + key);
214
+ });
215
+ });
216
+ colWidthsRef.current = newMap;
217
+ oldWidthRef.current = currentTableWidth;
218
+ setTrigger(c => c + 1);
219
+ }, [props.children, currentTableWidth]);
220
+ const setTableWidth = React.useCallback(_.debounce(() => {
221
+ var _a, _b, _c, _d;
222
+ if (bodyRef.current == null)
223
+ return;
224
+ // Note: certain body classes may break this check if they set overflow to scroll
225
+ let newScroll = false;
226
+ if (((_a = props.TbodyStyle) === null || _a === void 0 ? void 0 : _a.overflowY) === 'scroll' || ((_b = props.TbodyStyle) === null || _b === void 0 ? void 0 : _b.overflow) === 'scroll')
227
+ newScroll = true;
228
+ else
229
+ newScroll = bodyRef.current.clientHeight < bodyRef.current.scrollHeight;
230
+ setScrolled(newScroll);
231
+ setCurrentTableWidth(((_d = (_c = bodyRef.current) === null || _c === void 0 ? void 0 : _c.clientWidth) !== null && _d !== void 0 ? _d : 17) - (newScroll ? 0 : 17));
232
+ }, 100), []);
233
+ React.useEffect(() => {
234
+ let resizeObserver;
235
+ const intervalHandle = setInterval(() => {
236
+ if ((bodyRef === null || bodyRef === void 0 ? void 0 : bodyRef.current) == null)
237
+ return;
238
+ resizeObserver = new ResizeObserver(() => {
239
+ setTableWidth();
240
+ });
241
+ resizeObserver.observe(bodyRef.current);
242
+ clearInterval(intervalHandle);
243
+ }, 10);
244
+ return () => {
245
+ clearInterval(intervalHandle);
246
+ if (resizeObserver != null && resizeObserver.disconnect != null)
247
+ resizeObserver.disconnect();
248
+ };
249
+ }, []);
250
+ const handleSort = React.useCallback((data, event) => {
251
+ if (data.colKey !== null)
252
+ props.OnSort(data, event);
253
+ }, [props.OnSort]);
254
+ return (React.createElement("table", { className: props.TableClass !== undefined ? props.TableClass : 'table table-hover', style: tableStyle },
255
+ React.createElement(Header, { Class: props.TheadClass, Style: headStyle, SortKey: props.SortKey, Ascending: props.Ascending, LastColumn: props.LastColumn, OnSort: handleSort, ColWidths: colWidthsRef, Trigger: trigger, TriggerRerender: () => setTrigger(c => c + 1) }, props.children),
256
+ React.createElement(Rows, { DragStart: props.OnDragStart, Data: props.Data, RowStyle: rowStyle, BodyStyle: bodyStyle, BodyClass: props.TbodyClass, OnClick: props.OnClick, Selected: props.Selected, KeySelector: props.KeySelector, BodyRef: bodyRef, BodyScrolled: scrolled, ColWidths: colWidthsRef, Trigger: trigger }, props.children),
257
+ props.LastRow !== undefined ? (React.createElement("tfoot", { style: props.TfootStyle, className: props.TfootClass },
258
+ React.createElement("tr", { style: props.RowStyle !== undefined ? Object.assign({}, props.RowStyle) : {} }, props.LastRow))) : null));
259
+ }
260
+ function Rows(props) {
261
+ const bodyStyle = React.useMemo(() => (Object.assign(Object.assign({}, props.BodyStyle), { paddingRight: (props.BodyScrolled ? 0 : 17), display: "block" })), [props.BodyStyle, props.BodyScrolled]);
262
+ const onClick = React.useCallback((e, item, index) => {
263
+ if (props.OnClick !== undefined)
264
+ props.OnClick({
265
+ colKey: undefined,
266
+ colField: undefined,
267
+ row: item,
268
+ data: null,
269
+ index: index,
270
+ }, e);
271
+ }, [props.OnClick]);
272
+ return (React.createElement("tbody", { style: bodyStyle, className: props.BodyClass, ref: props.BodyRef }, props.Data.map((d, i) => {
273
+ const style = props.RowStyle !== undefined ? Object.assign({}, props.RowStyle) : {};
274
+ if (style.cursor === undefined && (props.OnClick !== undefined || props.DragStart !== undefined))
275
+ style.cursor = 'pointer';
276
+ if (props.Selected !== undefined && props.Selected(d, i))
277
+ style.backgroundColor = 'var(--warning)';
278
+ const key = props.KeySelector(d, i);
279
+ return (React.createElement("tr", { key: key, style: style, onClick: (e) => onClick(e, d, i) }, React.Children.map(props.children, (element) => {
280
+ var _a, _b, _c, _d, _e, _f;
281
+ if (!React.isValidElement(element))
282
+ return null;
283
+ if (!IsColumnProps(element.props))
284
+ return null;
285
+ const colWidth = props.ColWidths.current.get(element.props.Key);
286
+ if (colWidth == null || colWidth.width === 0)
287
+ return null;
288
+ let cursor = undefined;
289
+ if (((_b = (_a = element.props) === null || _a === void 0 ? void 0 : _a.RowStyle) === null || _b === void 0 ? void 0 : _b.cursor) != null)
290
+ cursor = element.props.RowStyle.cursor;
291
+ else if ((props === null || props === void 0 ? void 0 : props.OnClick) != null) {
292
+ cursor = 'pointer';
293
+ }
294
+ else if ((props === null || props === void 0 ? void 0 : props.DragStart) != null)
295
+ cursor = 'grab';
296
+ const style = Object.assign(Object.assign(Object.assign({}, defaultDataCellStyle), ((_c = element.props) === null || _c === void 0 ? void 0 : _c.RowStyle)), { width: colWidth.width, cursor: cursor });
297
+ return (React.createElement(Column_1.ColumnDataWrapper, { key: element.key, onClick: (e) => {
298
+ var _a, _b;
299
+ if (props.OnClick == null)
300
+ return;
301
+ return props.OnClick({
302
+ colKey: element.props.Key,
303
+ colField: (_a = element.props) === null || _a === void 0 ? void 0 : _a.Field,
304
+ row: d,
305
+ data: d[(_b = element.props) === null || _b === void 0 ? void 0 : _b.Field],
306
+ index: i,
307
+ }, e);
308
+ }, dragStart: (e) => {
309
+ var _a, _b;
310
+ if (props.DragStart == null)
311
+ return;
312
+ return props.DragStart({
313
+ colKey: element.props.Key,
314
+ colField: (_a = element.props) === null || _a === void 0 ? void 0 : _a.Field,
315
+ row: d,
316
+ data: d[(_b = element.props) === null || _b === void 0 ? void 0 : _b.Field],
317
+ index: i,
318
+ }, e);
319
+ }, style: style }, ((_d = element.props) === null || _d === void 0 ? void 0 : _d.Content) != null
320
+ ? element.props.Content({
321
+ item: d,
322
+ key: element.props.Key,
323
+ field: (_e = element.props) === null || _e === void 0 ? void 0 : _e.Field,
324
+ style: style,
325
+ index: i,
326
+ })
327
+ : ((_f = element.props) === null || _f === void 0 ? void 0 : _f.Field) != null
328
+ ? d[element.props.Field]
329
+ : null));
330
+ })));
331
+ })));
332
+ }
333
+ function Header(props) {
334
+ var _a;
335
+ const headStyle = React.useMemo(() => (Object.assign(Object.assign({}, defaultHeadStyle), props.Style)), [props.Style]);
336
+ // Consts for adjustable columns
337
+ const [mouseDown, setMouseDown] = React.useState(0);
338
+ const [currentKeys, setCurrentKeys] = React.useState(undefined);
339
+ const [deltaW, setDeltaW] = React.useState(0);
340
+ const [tentativeLimits, setTentativeLimits] = React.useState({ min: -Infinity, max: Infinity });
341
+ // Consts for filterable columns
342
+ const [filters, setFilters] = React.useState(((_a = props.Filters) !== null && _a !== void 0 ? _a : []));
343
+ const [guid] = React.useState((0, helper_functions_1.CreateGuid)());
344
+ const getLeftKey = React.useCallback((key, colWidthsRef) => {
345
+ var _a;
346
+ // Filtering down to shown adjustables only
347
+ const keys = React.Children.map((_a = props.children) !== null && _a !== void 0 ? _a : [], (element) => {
348
+ var _a;
349
+ if (!React.isValidElement(element))
350
+ return null;
351
+ const keyWidth = (_a = colWidthsRef.current.get(key)) === null || _a === void 0 ? void 0 : _a.width;
352
+ if (keyWidth == null || keyWidth <= 0)
353
+ return null;
354
+ if (IsColumnProps(element.props) && IsColumnAdjustable(element.props))
355
+ return element.props.Key;
356
+ return null;
357
+ }).filter((item) => item !== null);
358
+ const index = keys.indexOf(key);
359
+ if (index <= 0)
360
+ return undefined;
361
+ return keys[index - 1];
362
+ }, [props.children]);
363
+ const calculateDeltaLimits = React.useCallback((mapKeys, colWidthsRef) => {
364
+ if (mapKeys === undefined)
365
+ return ({ min: -Infinity, max: Infinity });
366
+ const widthObjLeft = colWidthsRef.current.get(mapKeys[0]);
367
+ const widthObjRight = colWidthsRef.current.get(mapKeys[1]);
368
+ if (widthObjLeft == null || widthObjRight == null)
369
+ return ({ min: -Infinity, max: Infinity });
370
+ const limitByShrinkLeft = widthObjLeft.width - widthObjLeft.minWidth;
371
+ const limitByGrowthLeft = widthObjLeft.maxWidth - widthObjLeft.width;
372
+ const limitByShrinkRight = widthObjRight.width - widthObjRight.minWidth;
373
+ const limitByGrowthRight = widthObjRight.maxWidth - widthObjRight.width;
374
+ // Recall that a left movement is a negative deltaW
375
+ const minDeltaW = -(limitByShrinkLeft < limitByGrowthRight ? limitByShrinkLeft : limitByGrowthRight);
376
+ const maxDeltaW = limitByShrinkRight < limitByGrowthLeft ? limitByShrinkRight : limitByGrowthLeft;
377
+ return ({ min: minDeltaW, max: maxDeltaW });
378
+ }, []);
379
+ const getDeltaSign = React.useCallback((index) => {
380
+ // Recall that a left movement is a negative deltaW
381
+ if (index === 0)
382
+ return 1;
383
+ else if (index === 1)
384
+ return -1;
385
+ else
386
+ return 0;
387
+ }, []);
388
+ const finishAdjustment = React.useCallback((adjustment, adjustKeys, colWidthsRef) => {
389
+ const deltaLimits = calculateDeltaLimits(adjustKeys, colWidthsRef);
390
+ let delta;
391
+ if (adjustment > deltaLimits.max)
392
+ delta = deltaLimits.max;
393
+ else if (adjustment < deltaLimits.min)
394
+ delta = deltaLimits.min;
395
+ else
396
+ delta = adjustment;
397
+ if (Math.abs(delta) > 5) {
398
+ const leftWidthObj = colWidthsRef.current.get(adjustKeys[0]);
399
+ const rightWidthObj = colWidthsRef.current.get(adjustKeys[1]);
400
+ if (leftWidthObj == null || rightWidthObj == null) {
401
+ console.error(`Unable to finalize adjustment on keys ${adjustKeys[0]}, ${adjustKeys[1]}`);
402
+ }
403
+ else {
404
+ leftWidthObj.width += (getDeltaSign(0) * delta);
405
+ rightWidthObj.width += (getDeltaSign(1) * delta);
406
+ }
407
+ }
408
+ setMouseDown(0);
409
+ setTentativeLimits({ min: -Infinity, max: Infinity });
410
+ setCurrentKeys(undefined);
411
+ setDeltaW(0);
412
+ }, [calculateDeltaLimits, getDeltaSign]);
413
+ const onMove = React.useCallback((e) => {
414
+ if (currentKeys === undefined)
415
+ return;
416
+ const w = e.screenX - mouseDown;
417
+ setDeltaW(w);
418
+ }, [mouseDown, currentKeys]);
419
+ const updateFilters = React.useCallback((flts, fld) => {
420
+ setFilters((fls) => {
421
+ const newFilters = fls.filter(item => item.FieldName !== fld).concat(flts);
422
+ if (props.SetFilters == null)
423
+ console.error("Attempted to set filters from column when no set filter arguement to table was provided. Data has not been filtered!");
424
+ else
425
+ props.SetFilters(newFilters);
426
+ return newFilters;
427
+ });
428
+ }, [props.SetFilters]);
429
+ React.useEffect(() => { var _a; return setFilters((_a = props.Filters) !== null && _a !== void 0 ? _a : []); }, [props.Filters]);
430
+ return (React.createElement("thead", { className: props.Class, style: headStyle, onMouseMove: (e) => {
431
+ onMove(e.nativeEvent);
432
+ e.stopPropagation();
433
+ }, onMouseUp: (e) => {
434
+ e.stopPropagation();
435
+ if (currentKeys == null)
436
+ return;
437
+ finishAdjustment(deltaW, currentKeys, props.ColWidths);
438
+ props.TriggerRerender();
439
+ }, onMouseLeave: (e) => {
440
+ e.stopPropagation();
441
+ if (currentKeys == null)
442
+ return;
443
+ finishAdjustment(deltaW, currentKeys, props.ColWidths);
444
+ props.TriggerRerender();
445
+ } },
446
+ React.createElement("tr", { style: { width: '100%', display: 'table' } },
447
+ React.Children.map(props.children, (element) => {
448
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
449
+ if (!React.isValidElement(element))
450
+ return null;
451
+ if (!IsColumnProps(element.props))
452
+ return null;
453
+ const colWidth = props.ColWidths.current.get(element.props.Key);
454
+ if (colWidth == null || colWidth.width === 0)
455
+ return null;
456
+ // Handling temporary width changes due to being in mid-adjustments
457
+ let currentWidth = colWidth.width;
458
+ const keyIndex = (_a = currentKeys === null || currentKeys === void 0 ? void 0 : currentKeys.indexOf(element.props.Key)) !== null && _a !== void 0 ? _a : -1;
459
+ if (keyIndex > -1) {
460
+ let delta;
461
+ if (deltaW > tentativeLimits.max)
462
+ delta = tentativeLimits.max;
463
+ else if (deltaW < tentativeLimits.min)
464
+ delta = tentativeLimits.min;
465
+ else
466
+ delta = deltaW;
467
+ currentWidth += (getDeltaSign(keyIndex) * delta);
468
+ }
469
+ let cursor = undefined;
470
+ if (((_c = (_b = element.props) === null || _b === void 0 ? void 0 : _b.HeaderStyle) === null || _c === void 0 ? void 0 : _c.cursor) != null)
471
+ cursor = element.props.HeaderStyle.cursor;
472
+ else if (((_e = (_d = element.props) === null || _d === void 0 ? void 0 : _d.AllowSort) !== null && _e !== void 0 ? _e : true))
473
+ cursor = 'pointer';
474
+ const style = Object.assign(Object.assign(Object.assign({}, defaultDataHeadStyle), (_f = element.props) === null || _f === void 0 ? void 0 : _f.HeaderStyle), { width: currentWidth, cursor: cursor });
475
+ let startAdjustment;
476
+ if (IsColumnAdjustable(element.props))
477
+ startAdjustment = (e) => {
478
+ const leftKey = getLeftKey(element.props.Key, props.ColWidths);
479
+ if (leftKey != null) {
480
+ const newCurrentKeys = [leftKey, element.props.Key];
481
+ setCurrentKeys(newCurrentKeys);
482
+ setMouseDown(e.screenX);
483
+ setTentativeLimits(calculateDeltaLimits(newCurrentKeys, props.ColWidths));
484
+ setDeltaW(0);
485
+ }
486
+ };
487
+ return (React.createElement(Column_1.ColumnHeaderWrapper, { onSort: (e) => {
488
+ var _a;
489
+ return props.OnSort({ colKey: element.props.Key, colField: (_a = element.props) === null || _a === void 0 ? void 0 : _a.Field, ascending: props.Ascending }, e);
490
+ }, sorted: props.SortKey === element.props.Key && ((_h = (_g = element.props) === null || _g === void 0 ? void 0 : _g.AllowSort) !== null && _h !== void 0 ? _h : true), asc: props.Ascending, colKey: element.props.Key, key: element.props.Key, allowSort: (_j = element.props) === null || _j === void 0 ? void 0 : _j.AllowSort, startAdjustment: startAdjustment, style: style },
491
+ ' ',
492
+ (element.type === FilterableColumn_1.default) ?
493
+ React.createElement(FilterableColumn_1.FilterableColumnHeader, { Label: (_k = element.props) === null || _k === void 0 ? void 0 : _k.children, Filter: filters.filter(f => { var _a, _b; return f.FieldName === ((_b = (_a = element.props) === null || _a === void 0 ? void 0 : _a.Field) === null || _b === void 0 ? void 0 : _b.toString()); }), SetFilter: (f) => { var _a; return updateFilters(f, (_a = element.props) === null || _a === void 0 ? void 0 : _a.Field); }, Field: (_l = element.props) === null || _l === void 0 ? void 0 : _l.Field, Type: (_m = element.props) === null || _m === void 0 ? void 0 : _m.Type, Options: (_o = element.props) === null || _o === void 0 ? void 0 : _o.Enum, ExpandedLabel: (_p = element.props) === null || _p === void 0 ? void 0 : _p.ExpandedLabel, Guid: guid, Unit: (_q = element.props) === null || _q === void 0 ? void 0 : _q.Unit }) :
494
+ ((_r = element.props.children) !== null && _r !== void 0 ? _r : element.props.Key),
495
+ ' '));
496
+ }),
497
+ React.createElement("th", { style: { width: 17, padding: 0, maxWidth: 17 } }, props.LastColumn))));
498
+ }
@@ -0,0 +1,185 @@
1
+ import { Search } from "@gpa-gemstone/react-interactive";
2
+ import { IUnit } from "../Filters/NumberFilter";
3
+ export interface ITable<T> {
4
+ /**
5
+ * List of T objects used to generate rows
6
+ */
7
+ Data: T[];
8
+ /**
9
+ * Callback when the user clicks on a data entry
10
+ * @param data contains the data including the columnKey
11
+ * @param event the onClick Event to allow propagation as needed
12
+ * @returns
13
+ */
14
+ OnClick?: (data: {
15
+ colKey?: string;
16
+ colField?: keyof T;
17
+ row: T;
18
+ data: T[keyof T] | null;
19
+ index: number;
20
+ }, event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
21
+ /**
22
+ * Key of the collumn to sort by
23
+ */
24
+ SortKey: string;
25
+ /**
26
+ * Boolen to indicate whether the sort is ascending or descending
27
+ */
28
+ Ascending: boolean;
29
+ /**
30
+ * Callback when the data should be sorted
31
+ * @param data the information of the collumn including the Key of the collumn
32
+ * @param event The onCLick event to allow Propagation as needed
33
+ */
34
+ OnSort(data: {
35
+ colKey: string;
36
+ colField?: keyof T;
37
+ ascending: boolean;
38
+ }, event: React.MouseEvent<HTMLElement, MouseEvent>): void;
39
+ /**
40
+ * Class of the table component
41
+ */
42
+ TableClass?: string;
43
+ /**
44
+ * style of the table component
45
+ */
46
+ TableStyle?: React.CSSProperties;
47
+ /**
48
+ * style of the thead component
49
+ */
50
+ TheadStyle?: React.CSSProperties;
51
+ /**
52
+ * Class of the thead component
53
+ */
54
+ TheadClass?: string;
55
+ /**
56
+ * style of the tbody component
57
+ * Note: Display style overwritten to "block"
58
+ */
59
+ TbodyStyle?: React.CSSProperties;
60
+ /**
61
+ * Class of the tbody component
62
+ */
63
+ TbodyClass?: string;
64
+ /**
65
+ * style of the tfoot component
66
+ */
67
+ TfootStyle?: React.CSSProperties;
68
+ /**
69
+ * Class of the tfoot component
70
+ */
71
+ TfootClass?: string;
72
+ /**
73
+ * determines if a row should be styled as selected
74
+ * @param data the item to be checked
75
+ * @returns true if the row should be styled as selected
76
+ */
77
+ Selected?: (data: T, index: number) => boolean;
78
+ /**
79
+ *
80
+ * @param data the information of the row including the item of the row
81
+ * @param e the event triggering this
82
+ * @returns
83
+ */
84
+ OnDragStart?: (data: {
85
+ colKey?: string;
86
+ colField?: keyof T;
87
+ row: T;
88
+ data: T[keyof T] | null;
89
+ index: number;
90
+ }, e: React.DragEvent<Element>) => void;
91
+ /**
92
+ * The default style for the tr element
93
+ */
94
+ RowStyle?: React.CSSProperties;
95
+ /**
96
+ * a Function that retrieves a unique key used for React key properties
97
+ * @param data the item to be turned into a key
98
+ * @returns a unique Key
99
+ */
100
+ KeySelector: (data: T, index?: number) => string | number;
101
+ /**
102
+ * Optional Element to display in the last row of the Table
103
+ * use this for displaying warnings when the Table content gets cut off.
104
+ * Data appears in the tfoot element
105
+ */
106
+ LastRow?: string | React.ReactNode;
107
+ /**
108
+ * Optional Element to display on upper Right corner
109
+ */
110
+ LastColumn?: string | React.ReactNode;
111
+ /**
112
+ * Optional Callback that gets called when there is not enough space to display columns
113
+ * @param disabled takes in string of disabled keys
114
+ */
115
+ ReduceWidthCallback?: (disabled: string[]) => void;
116
+ /**
117
+ * Callback to set filters for data. Required if using filterable columns.
118
+ * @param filters sets the set of filters
119
+ */
120
+ SetFilters?: (filters: Search.IFilter<T>[]) => void;
121
+ /**
122
+ * Filters currently filtering the data, required if using filterable columns.
123
+ */
124
+ Filters?: Search.IFilter<T>[];
125
+ }
126
+ export interface IColumn<T> {
127
+ /**
128
+ * a unique Key for this Collumn
129
+ */
130
+ Key: string;
131
+ /**
132
+ * Flag indicating whether sorting by this Collumn is allowed
133
+ */
134
+ AllowSort?: boolean;
135
+ /**
136
+ * Optional - the Field to be used
137
+ */
138
+ Field?: keyof T;
139
+ /**
140
+ * The Default style for the th element
141
+ */
142
+ HeaderStyle?: React.CSSProperties;
143
+ /**
144
+ * The Default style for the td element
145
+ */
146
+ RowStyle?: React.CSSProperties;
147
+ /**
148
+ * Determines the Content to be displayed
149
+ * @param d the data to be turned into content
150
+ * @returns the content displayed
151
+ */
152
+ Content?: (d: {
153
+ item: T;
154
+ key: string;
155
+ field: keyof T | undefined;
156
+ index: number;
157
+ style?: React.CSSProperties;
158
+ }) => React.ReactNode;
159
+ /**
160
+ * Determines if a column's width is adjustable, undefined is read as false.
161
+ */
162
+ Adjustable?: boolean;
163
+ }
164
+ export interface IOptions {
165
+ Value: string | number;
166
+ Label: string;
167
+ }
168
+ export interface IFilterableCollumn<T> extends IColumn<T> {
169
+ /**
170
+ * Type of field, determines filter entry UI of header.
171
+ */
172
+ Type?: Search.FieldType;
173
+ /**
174
+ * Enumeration for field, required for 'enum' type fields (has no effect otherwise)
175
+ */
176
+ Enum?: IOptions[];
177
+ /**
178
+ * Definition for a different label on hover of header.
179
+ */
180
+ ExpandedLabel?: string;
181
+ /**
182
+ * Definition for units on a number filter (has no effect if field type is not 'number')
183
+ */
184
+ Unit?: IUnit[];
185
+ }