@teselagen/ui 0.6.6 → 0.7.1

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 (98) hide show
  1. package/DataTable/ColumnFilterMenu.d.ts +2 -1
  2. package/DataTable/Columns.d.ts +51 -0
  3. package/DataTable/DisplayOptions.d.ts +14 -14
  4. package/DataTable/EditabelCell.d.ts +2 -5
  5. package/DataTable/EditableCell.d.ts +7 -0
  6. package/DataTable/FilterAndSortMenu.d.ts +9 -9
  7. package/DataTable/PagingTool.d.ts +25 -2
  8. package/DataTable/RenderCell.d.ts +18 -0
  9. package/DataTable/SearchBar.d.ts +4 -3
  10. package/DataTable/SortableColumns.d.ts +6 -9
  11. package/DataTable/ThComponent.d.ts +9 -0
  12. package/DataTable/index.d.ts +0 -5
  13. package/DataTable/utils/getIdOrCodeOrIndex.d.ts +1 -2
  14. package/DataTable/utils/handleCopyTable.d.ts +1 -0
  15. package/DataTable/utils/index.d.ts +4 -2
  16. package/DataTable/utils/primarySelectedValue.d.ts +1 -0
  17. package/DataTable/utils/queryParams.d.ts +13 -8
  18. package/DataTable/utils/removeCleanRows.d.ts +1 -1
  19. package/DataTable/utils/rowClick.d.ts +24 -3
  20. package/DataTable/utils/useDeepEqualMemo.d.ts +1 -0
  21. package/DataTable/utils/useTableEntities.d.ts +5 -0
  22. package/DataTable/utils/useTableParams.d.ts +49 -0
  23. package/DataTable/utils/withTableParams.d.ts +3 -16
  24. package/DataTable/viewColumn.d.ts +11 -4
  25. package/FormComponents/AbstractField.d.ts +1 -0
  26. package/FormComponents/Uploader.d.ts +34 -1
  27. package/FormComponents/index.d.ts +111 -60
  28. package/MatchHeaders.d.ts +9 -10
  29. package/SimpleStepViz.d.ts +2 -1
  30. package/TgSuggest/index.d.ts +1 -21
  31. package/UploadCsvWizard.d.ts +1 -1
  32. package/index.cjs.js +47861 -49125
  33. package/index.d.ts +6 -3
  34. package/index.es.js +47959 -49223
  35. package/package.json +6 -5
  36. package/src/DataTable/CellDragHandle.js +70 -69
  37. package/src/DataTable/ColumnFilterMenu.js +23 -21
  38. package/src/DataTable/Columns.js +948 -0
  39. package/src/DataTable/Columns.jsx +945 -0
  40. package/src/DataTable/DisplayOptions.js +173 -192
  41. package/src/DataTable/EditabelCell.js +7 -18
  42. package/src/DataTable/EditabelCell.jsx +44 -0
  43. package/src/DataTable/EditableCell.js +44 -0
  44. package/src/DataTable/FilterAndSortMenu.js +215 -234
  45. package/src/DataTable/PagingTool.js +47 -56
  46. package/src/DataTable/RenderCell.js +191 -0
  47. package/src/DataTable/RenderCell.jsx +191 -0
  48. package/src/DataTable/SearchBar.js +12 -5
  49. package/src/DataTable/SortableColumns.js +44 -39
  50. package/src/DataTable/ThComponent.js +44 -0
  51. package/src/DataTable/dataTableEnhancer.js +32 -295
  52. package/src/DataTable/index.js +2945 -3596
  53. package/src/DataTable/utils/getIdOrCodeOrIndex.js +1 -1
  54. package/src/DataTable/utils/handleCopyTable.js +16 -0
  55. package/src/DataTable/utils/index.js +7 -3
  56. package/src/DataTable/utils/primarySelectedValue.js +1 -0
  57. package/src/DataTable/utils/queryParams.js +110 -85
  58. package/src/DataTable/utils/removeCleanRows.js +3 -3
  59. package/src/DataTable/utils/rowClick.js +34 -9
  60. package/src/DataTable/utils/selection.js +1 -1
  61. package/src/DataTable/utils/useDeepEqualMemo.js +10 -0
  62. package/src/DataTable/utils/useTableEntities.js +38 -0
  63. package/src/DataTable/utils/useTableParams.js +362 -0
  64. package/src/DataTable/utils/withTableParams.js +244 -274
  65. package/src/DataTable/validateTableWideErrors.js +1 -1
  66. package/src/DataTable/viewColumn.js +5 -9
  67. package/src/DialogFooter/index.js +3 -3
  68. package/src/FillWindow.js +2 -3
  69. package/src/FormComponents/AbstractField.js +388 -0
  70. package/src/FormComponents/Uploader.js +674 -649
  71. package/src/FormComponents/index.js +505 -654
  72. package/src/FormComponents/tryToMatchSchemas.js +1 -6
  73. package/src/MatchHeaders.js +27 -22
  74. package/src/SimpleStepViz.js +19 -23
  75. package/src/TgSelect/index.js +1 -1
  76. package/src/TgSuggest/index.js +94 -106
  77. package/src/UploadCsvWizard.js +571 -577
  78. package/src/enhancers/withDialog/tg_modalState.js +1 -0
  79. package/src/index.js +10 -4
  80. package/src/showDialogOnDocBody.js +5 -9
  81. package/src/useDialog.js +25 -26
  82. package/src/utils/commandControls.js +2 -2
  83. package/src/utils/handlerHelpers.js +19 -25
  84. package/src/utils/hooks/index.js +1 -0
  85. package/src/utils/hooks/useDeepEqualMemo.js +10 -0
  86. package/src/utils/hooks/useStableReference.js +9 -0
  87. package/src/utils/renderOnDoc.js +8 -5
  88. package/src/utils/tagUtils.js +3 -3
  89. package/src/utils/useTraceUpdate.js +19 -0
  90. package/src/wrapDialog.js +0 -2
  91. package/style.css +251 -251
  92. package/useDialog.d.ts +2 -6
  93. package/utils/hooks/index.d.ts +1 -0
  94. package/utils/hooks/useDeepEqualMemo.d.ts +1 -0
  95. package/utils/hooks/useStableReference.d.ts +1 -0
  96. package/utils/renderOnDoc.d.ts +1 -1
  97. package/utils/tagUtils.d.ts +5 -1
  98. package/utils/useTraceUpdate.d.ts +1 -0
@@ -1,4 +1,4 @@
1
- export default (record, rowIndex) => {
1
+ export const getIdOrCodeOrIndex = (record, rowIndex) => {
2
2
  if (record.id || record.id === 0) {
3
3
  return record.id;
4
4
  } else if (record.code) {
@@ -0,0 +1,16 @@
1
+ import { getAllRows } from "./getAllRows";
2
+ import { handleCopyRows } from "./handleCopyRows";
3
+
4
+ export const handleCopyTable = (e, opts) => {
5
+ try {
6
+ const allRowEls = getAllRows(e);
7
+ if (!allRowEls) return;
8
+ handleCopyRows(allRowEls, {
9
+ ...opts,
10
+ onFinishMsg: "Table Copied"
11
+ });
12
+ } catch (error) {
13
+ console.error(`error:`, error);
14
+ window.toastr.error("Error copying rows.");
15
+ }
16
+ };
@@ -2,7 +2,6 @@ import { isEntityClean } from "./isEntityClean";
2
2
  import { getSelectedRowsFromEntities } from "./selection";
3
3
  import { removeCleanRows } from "./removeCleanRows";
4
4
  import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
5
- import computePresets from "./computePresets";
6
5
  import { getRecordsFromIdMap } from "./withSelectedEntities";
7
6
  import { formatPasteData } from "./formatPasteData";
8
7
  import { getFieldPathToField } from "./getFieldPathToField";
@@ -23,9 +22,11 @@ import { handleCopyHelper } from "./handleCopyHelper";
23
22
  import { handleCopyRows } from "./handleCopyRows";
24
23
  import { handleCopyColumn } from "./handleCopyColumn";
25
24
  import { isBottomRightCornerOfRectangle } from "./isBottomRightCornerOfRectangle";
25
+ import { handleCopyTable } from "./handleCopyTable";
26
+ import { PRIMARY_SELECTED_VAL } from "./primarySelectedValue";
27
+ import { useTableEntities } from "./useTableEntities";
26
28
 
27
29
  export {
28
- computePresets,
29
30
  defaultParsePaste,
30
31
  formatPasteData,
31
32
  getAllRows,
@@ -44,8 +45,11 @@ export {
44
45
  handleCopyColumn,
45
46
  handleCopyHelper,
46
47
  handleCopyRows,
48
+ handleCopyTable,
47
49
  isBottomRightCornerOfRectangle,
48
50
  isEntityClean,
51
+ PRIMARY_SELECTED_VAL,
49
52
  removeCleanRows,
50
- stripNumberAtEnd
53
+ stripNumberAtEnd,
54
+ useTableEntities
51
55
  };
@@ -0,0 +1 @@
1
+ export const PRIMARY_SELECTED_VAL = "main_cell";
@@ -39,7 +39,7 @@ export function getMergedOpts(topLevel = {}, instanceLevel = {}) {
39
39
  //filters look like this:
40
40
  // {
41
41
  // selectedFilter: 'textContains', //camel case
42
- // filterOn: ccDisplayName, //camel case display name
42
+ // filterOn: ccDisplayName, //camel case display name if available and string, otherwise path
43
43
  // filterValue: 'thomas',
44
44
  // }
45
45
  ],
@@ -63,9 +63,27 @@ function safeParse(val) {
63
63
  return val;
64
64
  }
65
65
  }
66
+
67
+ /**
68
+ *
69
+ * @param {object} field
70
+ * @returns the camelCase display name of the field, to be used for filters, sorting, etc
71
+ */
72
+ export function getCCDisplayName(field) {
73
+ return camelCase(
74
+ typeof field.displayName === "string" ? field.displayName : field.path
75
+ );
76
+ }
77
+
78
+ /**
79
+ * Takes a schema and returns an object with the fields mapped by their camelCased display name.
80
+ * If the displayName is not set or is a jsx element, the path is used instead.
81
+ * The same conversion must be done when using the result of this method
82
+ */
66
83
  function getFieldsMappedByCCDisplayName(schema) {
67
84
  return schema.fields.reduce((acc, field) => {
68
- acc[camelCase(field.displayName || field.path)] = field;
85
+ const ccDisplayName = getCCDisplayName(field);
86
+ acc[ccDisplayName] = field;
69
87
  return acc;
70
88
  }, {});
71
89
  }
@@ -75,14 +93,14 @@ function orderEntitiesLocal(orderArray, entities, schema, ownProps) {
75
93
  const orderFuncs = [];
76
94
  const ascOrDescArray = [];
77
95
  orderArray.forEach(order => {
78
- const ccDisplayName = order.replace(/^-/gi, "");
96
+ const ccDisplayName = order.replace(/^-/gi, ""); // "-updatedAt" => "updatedAt"
79
97
  const ccFields = getFieldsMappedByCCDisplayName(schema);
80
98
  const field = ccFields[ccDisplayName];
81
99
  if (!field) {
82
100
  throw new Error(
83
101
  "Ruh roh, there should have been a column to sort on for " +
84
102
  order +
85
- "but none was found in " +
103
+ " but none was found in " +
86
104
  schema.fields
87
105
  );
88
106
  }
@@ -234,14 +252,14 @@ function getFiltersFromSearchTerm(searchTerm, schema) {
234
252
  isSearchTermFilter: true
235
253
  };
236
254
  schema.fields.forEach(field => {
237
- const { type, displayName, path, searchDisabled } = field;
255
+ const { type, searchDisabled } = field;
238
256
  if (searchDisabled || field.filterDisabled || type === "color") return;
239
- const nameToUse = camelCase(displayName || path);
257
+ const ccDisplayName = getCCDisplayName(field);
240
258
  const filterValue = cleanFilterValue(searchTerm, type);
241
259
  if (type === "string" || type === "lookup") {
242
260
  searchTermFilters.push({
243
261
  ...sharedFields,
244
- filterOn: nameToUse,
262
+ filterOn: ccDisplayName,
245
263
  filterValue: searchTerm,
246
264
  selectedFilter: "contains"
247
265
  });
@@ -256,14 +274,14 @@ function getFiltersFromSearchTerm(searchTerm, schema) {
256
274
  if ("true".replace(regex, "") !== "true") {
257
275
  searchTermFilters.push({
258
276
  ...sharedFields,
259
- filterOn: nameToUse,
277
+ filterOn: ccDisplayName,
260
278
  filterValue: true,
261
279
  selectedFilter: "true"
262
280
  });
263
281
  } else if ("false".replace(regex, "") !== "false") {
264
282
  searchTermFilters.push({
265
283
  ...sharedFields,
266
- filterOn: nameToUse,
284
+ filterOn: ccDisplayName,
267
285
  filterValue: false,
268
286
  selectedFilter: "false"
269
287
  });
@@ -278,7 +296,7 @@ function getFiltersFromSearchTerm(searchTerm, schema) {
278
296
  }
279
297
  searchTermFilters.push({
280
298
  ...sharedFields,
281
- filterOn: nameToUse,
299
+ filterOn: ccDisplayName,
282
300
  filterValue: filterValue,
283
301
  selectedFilter: "equalTo"
284
302
  });
@@ -347,7 +365,13 @@ function getSubFilter(
347
365
  if (!fieldVal?.toString) return false;
348
366
  return (
349
367
  arrayFilterValue
350
- .map(val => val && val.toLowerCase())
368
+ .map(val => {
369
+ if (val) {
370
+ if (val.toString) return val.toString().toLowerCase();
371
+ return val.toLowerCase();
372
+ }
373
+ return undefined;
374
+ })
351
375
  .indexOf(fieldVal.toString().toLowerCase()) > -1
352
376
  );
353
377
  };
@@ -359,7 +383,13 @@ function getSubFilter(
359
383
  if (!fieldVal?.toString) return false;
360
384
  return (
361
385
  arrayFilterValue
362
- .map(val => val && val.toLowerCase())
386
+ .map(val => {
387
+ if (val) {
388
+ if (val.toString) return val.toString().toLowerCase();
389
+ return val.toLowerCase();
390
+ }
391
+ return undefined;
392
+ })
363
393
  .indexOf(fieldVal.toString().toLowerCase()) === -1
364
394
  );
365
395
  };
@@ -573,49 +603,45 @@ function buildRef(qb, reference, searchField, expression) {
573
603
 
574
604
  export function makeDataTableHandlers({
575
605
  setNewParams,
576
- updateSearch,
577
606
  defaults,
578
607
  onlyOneFilter
579
608
  }) {
580
609
  //all of these actions have currentParams bound to them as their last arg in withTableParams
581
- function setSearchTerm(searchTerm, currentParams) {
582
- const newParams = {
583
- ...currentParams,
610
+ const setSearchTerm = searchTerm => {
611
+ setNewParams(prev => ({
612
+ ...(prev ?? {}),
584
613
  page: undefined, //set page undefined to return the table to page 1
585
614
  searchTerm: searchTerm === defaults.searchTerm ? undefined : searchTerm
586
- };
587
- setNewParams(newParams);
588
- updateSearch(searchTerm);
615
+ }));
589
616
  onlyOneFilter && clearFilters();
590
- }
591
- function addFilters(newFilters, currentParams) {
617
+ };
618
+
619
+ const addFilters = newFilters => {
592
620
  if (!newFilters) return;
593
- const filters = uniqBy(
594
- [...newFilters, ...(onlyOneFilter ? [] : currentParams.filters || [])],
595
- "filterOn"
596
- );
621
+ setNewParams(prev => {
622
+ const filters = uniqBy(
623
+ [...newFilters, ...(onlyOneFilter ? [] : prev?.filters || [])],
624
+ "filterOn"
625
+ );
626
+ return {
627
+ ...(prev ?? {}),
628
+ page: undefined, //set page undefined to return the table to page 1
629
+ filters
630
+ };
631
+ });
632
+ };
597
633
 
598
- const newParams = {
599
- ...currentParams,
600
- page: undefined, //set page undefined to return the table to page 1
601
- filters
602
- };
603
- setNewParams(newParams);
604
- onlyOneFilter && updateSearch();
605
- }
606
- function removeSingleFilter(filterOn, currentParams) {
607
- const filters = currentParams.filters
608
- ? currentParams.filters.filter(filter => {
609
- return filter.filterOn !== filterOn;
610
- })
611
- : undefined;
612
- const newParams = {
613
- ...currentParams,
614
- filters
615
- };
616
- setNewParams(newParams);
617
- }
618
- function clearFilters(additionalFilterKeys = []) {
634
+ const removeSingleFilter = filterOn =>
635
+ setNewParams(prev => {
636
+ const filters = prev?.filters
637
+ ? prev.filters.filter(filter => {
638
+ return filter.filterOn !== filterOn;
639
+ })
640
+ : undefined;
641
+ return { ...(prev ?? {}), filters };
642
+ });
643
+
644
+ const clearFilters = (additionalFilterKeys = []) => {
619
645
  const toClear = {
620
646
  filters: undefined,
621
647
  searchTerm: undefined,
@@ -625,49 +651,49 @@ export function makeDataTableHandlers({
625
651
  toClear[key] = undefined;
626
652
  });
627
653
  setNewParams(toClear);
628
- updateSearch();
629
- }
630
- function setPageSize(pageSize, currentParams) {
631
- const newParams = {
632
- ...currentParams,
654
+ };
655
+
656
+ const setPageSize = pageSize =>
657
+ setNewParams(prev => ({
658
+ ...(prev ?? {}),
633
659
  pageSize: pageSize === defaults.pageSize ? undefined : pageSize,
634
660
  page: undefined //set page undefined to return the table to page 1
635
- };
636
- setNewParams(newParams);
637
- }
638
- function setOrder(order, isRemove, shiftHeld, currentParams) {
639
- let newOrder = [];
640
- if (shiftHeld) {
641
- //first remove the old order
642
- newOrder = [...(currentParams.order || [])].filter(value => {
643
- const shouldRemove =
644
- value.replace(/^-/, "") === order.replace(/^-/, "");
645
- return !shouldRemove;
646
- });
647
- //then, if we are adding, pop the order onto the array
648
- if (!isRemove) {
649
- newOrder.push(order);
650
- }
651
- } else {
652
- if (isRemove) {
653
- newOrder = [];
661
+ }));
662
+
663
+ const setOrder = (order, isRemove, shiftHeld) =>
664
+ setNewParams(prev => {
665
+ let newOrder = [];
666
+ if (shiftHeld) {
667
+ //first remove the old order
668
+ newOrder = [...(prev?.order || [])].filter(value => {
669
+ const shouldRemove =
670
+ value.replace(/^-/, "") === order.replace(/^-/, "");
671
+ return !shouldRemove;
672
+ });
673
+ //then, if we are adding, pop the order onto the array
674
+ if (!isRemove) {
675
+ newOrder.push(order);
676
+ }
654
677
  } else {
655
- newOrder = [order];
678
+ if (isRemove) {
679
+ newOrder = [];
680
+ } else {
681
+ newOrder = [order];
682
+ }
656
683
  }
657
- }
658
- const newParams = {
659
- ...currentParams,
660
- order: newOrder
661
- };
662
- setNewParams(newParams);
663
- }
664
- function setPage(page, currentParams) {
665
- const newParams = {
666
- ...currentParams,
684
+ return {
685
+ ...(prev ?? {}),
686
+ order: newOrder
687
+ };
688
+ });
689
+
690
+ const setPage = page => {
691
+ setNewParams(prev => ({
692
+ ...(prev ?? {}),
667
693
  page: page === defaults.page ? undefined : page
668
- };
669
- setNewParams(newParams);
670
- }
694
+ }));
695
+ };
696
+
671
697
  return {
672
698
  setSearchTerm,
673
699
  addFilters,
@@ -859,7 +885,6 @@ export function getQueryParams({
859
885
  }));
860
886
  });
861
887
  };
862
-
863
888
  const orFiltersObject = getQueries(orFilters, qb, ccFields);
864
889
  let allOrFilters = flattenFilters(orFiltersObject);
865
890
 
@@ -1,9 +1,9 @@
1
1
  import { isEntityClean } from "./isEntityClean";
2
2
  import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
3
3
 
4
- export const removeCleanRows = (reduxFormEntities, reduxFormCellValidation) => {
4
+ export const removeCleanRows = (entities, cellValidation) => {
5
5
  const toFilterOut = {};
6
- const entsToUse = (reduxFormEntities || []).filter(e => {
6
+ const entsToUse = (entities || []).filter(e => {
7
7
  if (!(e._isClean || isEntityClean(e))) return true;
8
8
  else {
9
9
  toFilterOut[getIdOrCodeOrIndex(e)] = true;
@@ -12,7 +12,7 @@ export const removeCleanRows = (reduxFormEntities, reduxFormCellValidation) => {
12
12
  });
13
13
 
14
14
  const validationToUse = {};
15
- Object.entries(reduxFormCellValidation || {}).forEach(([k, v]) => {
15
+ Object.entries(cellValidation || {}).forEach(([k, v]) => {
16
16
  const [rowId] = k.split(":");
17
17
  if (!toFilterOut[rowId]) {
18
18
  validationToUse[k] = v;
@@ -1,17 +1,27 @@
1
1
  import { isEmpty, forEach, range } from "lodash-es";
2
2
  import { getSelectedRowsFromEntities } from "./selection";
3
- import getIdOrCodeOrIndex from "./getIdOrCodeOrIndex";
3
+ import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
4
4
  import { getRecordsFromIdMap } from "./withSelectedEntities";
5
5
 
6
- export default function rowClick(e, rowInfo, entities, props) {
7
- const {
6
+ export default function rowClick(
7
+ e,
8
+ rowInfo,
9
+ entities,
10
+ {
8
11
  reduxFormSelectedEntityIdMap,
9
12
  isSingleSelect,
10
13
  noSelect,
11
14
  onRowClick,
12
15
  isEntityDisabled,
13
- withCheckboxes
14
- } = props;
16
+ withCheckboxes,
17
+ onDeselect,
18
+ onSingleRowSelect,
19
+ onMultiRowSelect,
20
+ noDeselectAll,
21
+ onRowSelect,
22
+ change
23
+ }
24
+ ) {
15
25
  const entity = rowInfo.original;
16
26
  onRowClick(e, entity, rowInfo);
17
27
  if (noSelect || isEntityDisabled(entity)) return;
@@ -101,7 +111,19 @@ export default function rowClick(e, rowInfo, entities, props) {
101
111
  }
102
112
  }
103
113
 
104
- finalizeSelection({ idMap: newIdMap, entities, props });
114
+ finalizeSelection({
115
+ idMap: newIdMap,
116
+ entities,
117
+ props: {
118
+ onDeselect,
119
+ onSingleRowSelect,
120
+ onMultiRowSelect,
121
+ noDeselectAll,
122
+ onRowSelect,
123
+ noSelect,
124
+ change
125
+ }
126
+ });
105
127
  }
106
128
 
107
129
  export function changeSelectedEntities({ idMap, entities = [], change }) {
@@ -124,8 +146,10 @@ export function changeSelectedEntities({ idMap, entities = [], change }) {
124
146
  change("reduxFormSelectedEntityIdMap", newIdMap);
125
147
  }
126
148
 
127
- export function finalizeSelection({ idMap, entities, props }) {
128
- const {
149
+ export function finalizeSelection({
150
+ idMap,
151
+ entities,
152
+ props: {
129
153
  onDeselect,
130
154
  onSingleRowSelect,
131
155
  onMultiRowSelect,
@@ -133,7 +157,8 @@ export function finalizeSelection({ idMap, entities, props }) {
133
157
  onRowSelect,
134
158
  noSelect,
135
159
  change
136
- } = props;
160
+ }
161
+ }) {
137
162
  if (noSelect) return;
138
163
  if (
139
164
  noDeselectAll &&
@@ -1,4 +1,4 @@
1
- import getIdOrCodeOrIndex from "./getIdOrCodeOrIndex";
1
+ import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
2
2
 
3
3
  export const getSelectedRowsFromEntities = (entities, idMap) => {
4
4
  if (!idMap) return [];
@@ -0,0 +1,10 @@
1
+ import { isEqual } from "lodash-es";
2
+ import { useRef } from "react";
3
+
4
+ export const useDeepEqualMemo = value => {
5
+ const ref = useRef();
6
+ if (!isEqual(value, ref.current)) {
7
+ ref.current = value;
8
+ }
9
+ return ref.current;
10
+ };
@@ -0,0 +1,38 @@
1
+ import { useCallback } from "react";
2
+ import { useDispatch, useSelector } from "react-redux";
3
+ import { change, initialize } from "redux-form";
4
+
5
+ export const useTableEntities = tableFormName => {
6
+ const dispatch = useDispatch();
7
+ const selectTableEntities = useCallback(
8
+ (entities = []) => {
9
+ initialize(tableFormName, {}, true, {
10
+ keepDirty: true,
11
+ updateUnregisteredFields: true,
12
+ keepValues: true
13
+ });
14
+ const selectedEntityIdMap = {};
15
+ entities.forEach(entity => {
16
+ selectedEntityIdMap[entity.id] = {
17
+ entity,
18
+ time: Date.now()
19
+ };
20
+ });
21
+ dispatch(
22
+ change(
23
+ tableFormName,
24
+ "reduxFormSelectedEntityIdMap",
25
+ selectedEntityIdMap
26
+ )
27
+ );
28
+ },
29
+ [dispatch, tableFormName]
30
+ );
31
+
32
+ const { allOrderedEntities, selectedEntities } = useSelector(state => ({
33
+ allOrderedEntities: state.form?.[tableFormName]?.values?.allOrderedEntities,
34
+ selectedEntities:
35
+ state.form?.[tableFormName]?.values?.reduxFormSelectedEntityIdMap
36
+ }));
37
+ return { selectTableEntities, allOrderedEntities, selectedEntities };
38
+ };