@m4l/components 9.4.1 → 9.4.2-JT11122025.beta.2

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 (108) hide show
  1. package/components/DataGrid/DataGrid.js +7 -1
  2. package/components/DataGrid/constants.d.ts +13 -0
  3. package/components/DataGrid/constants.js +11 -0
  4. package/components/DataGrid/contexts/DataGridContext/index.js +60 -275
  5. package/components/DataGrid/contexts/DataGridContext/types.d.ts +24 -23
  6. package/components/DataGrid/helpers/getAllViewModes/getAllViewModes.d.ts +19 -0
  7. package/components/DataGrid/helpers/getAllViewModes/getAllViewModes.js +6 -0
  8. package/components/DataGrid/helpers/getAllViewModes/getAllViewModes.test.d.ts +1 -0
  9. package/components/DataGrid/helpers/getAllViewModes/index.d.ts +1 -0
  10. package/components/DataGrid/helpers/getAllViewModes/index.js +1 -0
  11. package/components/DataGrid/helpers/getColumnsWidth/getColumnsWidth.d.ts +47 -0
  12. package/components/DataGrid/helpers/getColumnsWidth/getColumnsWidth.js +32 -0
  13. package/components/DataGrid/helpers/getColumnsWidth/getColumnsWidth.test.d.ts +1 -0
  14. package/components/DataGrid/helpers/getColumnsWidth/index.d.ts +1 -0
  15. package/components/DataGrid/helpers/getColumnsWidth/index.js +1 -0
  16. package/components/DataGrid/helpers/getIndexRowHeightVariant/getIndexRowHeightVariant.d.ts +24 -0
  17. package/components/DataGrid/helpers/getIndexRowHeightVariant/getIndexRowHeightVariant.js +12 -0
  18. package/components/DataGrid/helpers/getIndexRowHeightVariant/getIndexRowHeightVariant.test.d.ts +1 -0
  19. package/components/DataGrid/helpers/getIndexRowHeightVariant/index.d.ts +1 -0
  20. package/components/DataGrid/helpers/getIndexRowHeightVariant/index.js +1 -0
  21. package/components/DataGrid/helpers/getInitialColumnsConfig/getInitialColumnsConfig.d.ts +48 -0
  22. package/components/DataGrid/helpers/getInitialColumnsConfig/getInitialColumnsConfig.js +35 -0
  23. package/components/DataGrid/helpers/getInitialColumnsConfig/getInitialColumnsConfig.test.d.ts +1 -0
  24. package/components/DataGrid/helpers/getInitialColumnsConfig/index.d.ts +1 -0
  25. package/components/DataGrid/helpers/getInitialColumnsConfig/index.js +1 -0
  26. package/components/DataGrid/helpers/getViewMode/getViewMode.d.ts +41 -0
  27. package/components/DataGrid/helpers/getViewMode/getViewMode.js +19 -0
  28. package/components/DataGrid/helpers/getViewMode/getViewMode.test.d.ts +1 -0
  29. package/components/DataGrid/helpers/getViewMode/index.d.ts +1 -0
  30. package/components/DataGrid/helpers/getViewMode/index.js +1 -0
  31. package/components/DataGrid/helpers/getViewSpecificConfig/getViewSpecificConfig.d.ts +28 -0
  32. package/components/DataGrid/helpers/getViewSpecificConfig/getViewSpecificConfig.js +22 -0
  33. package/components/DataGrid/helpers/getViewSpecificConfig/getViewSpecificConfig.test.d.ts +1 -0
  34. package/components/DataGrid/helpers/getViewSpecificConfig/index.d.ts +1 -0
  35. package/components/DataGrid/helpers/getViewSpecificConfig/index.js +1 -0
  36. package/components/DataGrid/helpers/getViewSuffix/getViewSuffix.d.ts +24 -0
  37. package/components/DataGrid/helpers/getViewSuffix/getViewSuffix.js +9 -0
  38. package/components/DataGrid/helpers/getViewSuffix/getViewSuffix.test.d.ts +1 -0
  39. package/components/DataGrid/helpers/getViewSuffix/index.d.ts +1 -0
  40. package/components/DataGrid/helpers/getViewSuffix/index.js +1 -0
  41. package/components/DataGrid/helpers/index.d.ts +13 -0
  42. package/components/DataGrid/helpers/index.js +1 -0
  43. package/components/DataGrid/helpers/loadViewConfig/index.d.ts +1 -0
  44. package/components/DataGrid/helpers/loadViewConfig/index.js +1 -0
  45. package/components/DataGrid/helpers/loadViewConfig/loadViewConfig.d.ts +23 -0
  46. package/components/DataGrid/helpers/loadViewConfig/loadViewConfig.js +12 -0
  47. package/components/DataGrid/helpers/loadViewConfig/loadViewConfig.test.d.ts +1 -0
  48. package/components/DataGrid/helpers/saveColumnsWidth/index.d.ts +1 -0
  49. package/components/DataGrid/helpers/saveColumnsWidth/index.js +1 -0
  50. package/components/DataGrid/helpers/saveColumnsWidth/saveColumnsWidth.d.ts +52 -0
  51. package/components/DataGrid/helpers/saveColumnsWidth/saveColumnsWidth.js +18 -0
  52. package/components/DataGrid/helpers/saveColumnsWidth/saveColumnsWidth.test.d.ts +1 -0
  53. package/components/DataGrid/helpers/saveViewConfig/index.d.ts +1 -0
  54. package/components/DataGrid/helpers/saveViewConfig/index.js +1 -0
  55. package/components/DataGrid/helpers/saveViewConfig/saveViewConfig.d.ts +35 -0
  56. package/components/DataGrid/helpers/saveViewConfig/saveViewConfig.js +15 -0
  57. package/components/DataGrid/helpers/saveViewConfig/saveViewConfig.test.d.ts +1 -0
  58. package/components/DataGrid/helpers/saveViewMode/index.d.ts +1 -0
  59. package/components/DataGrid/helpers/saveViewMode/index.js +1 -0
  60. package/components/DataGrid/helpers/saveViewMode/saveViewMode.d.ts +33 -0
  61. package/components/DataGrid/helpers/saveViewMode/saveViewMode.js +13 -0
  62. package/components/DataGrid/helpers/saveViewMode/saveViewMode.test.d.ts +1 -0
  63. package/components/DataGrid/hooks/index.d.ts +6 -0
  64. package/components/DataGrid/hooks/index.js +1 -0
  65. package/components/DataGrid/hooks/useChangeColumnsConfig/index.d.ts +1 -0
  66. package/components/DataGrid/hooks/useChangeColumnsConfig/index.js +1 -0
  67. package/components/DataGrid/hooks/useChangeColumnsConfig/useChangeColumnsConfig.d.ts +9 -0
  68. package/components/DataGrid/hooks/useChangeColumnsConfig/useChangeColumnsConfig.js +27 -0
  69. package/components/DataGrid/hooks/useChangeColumnsConfig/useChangeColumnsConfig.test.d.ts +1 -0
  70. package/components/DataGrid/hooks/useChangeColumnsOrder/index.d.ts +1 -0
  71. package/components/DataGrid/hooks/useChangeColumnsOrder/index.js +1 -0
  72. package/components/DataGrid/hooks/useChangeColumnsOrder/useChangeColumnsOrder.d.ts +10 -0
  73. package/components/DataGrid/hooks/useChangeColumnsOrder/useChangeColumnsOrder.js +34 -0
  74. package/components/DataGrid/hooks/useChangeColumnsOrder/useChangeColumnsOrder.test.d.ts +1 -0
  75. package/components/DataGrid/hooks/useColumnsWidth/index.d.ts +1 -0
  76. package/components/DataGrid/hooks/useColumnsWidth/index.js +1 -0
  77. package/components/DataGrid/hooks/useColumnsWidth/useColumnsWidth.d.ts +11 -0
  78. package/components/DataGrid/hooks/useColumnsWidth/useColumnsWidth.js +29 -0
  79. package/components/DataGrid/hooks/useColumnsWidth/useColumnsWidth.test.d.ts +1 -0
  80. package/components/DataGrid/hooks/useRowHeight/index.d.ts +1 -0
  81. package/components/DataGrid/hooks/useRowHeight/index.js +1 -0
  82. package/components/DataGrid/hooks/useRowHeight/useRowHeight.d.ts +28 -0
  83. package/components/DataGrid/hooks/useRowHeight/useRowHeight.js +51 -0
  84. package/components/DataGrid/hooks/useRowHeight/useRowHeight.test.d.ts +1 -0
  85. package/components/DataGrid/hooks/useViewConfig/index.d.ts +1 -0
  86. package/components/DataGrid/hooks/useViewConfig/index.js +1 -0
  87. package/components/DataGrid/hooks/useViewConfig/useViewConfig.d.ts +9 -0
  88. package/components/DataGrid/hooks/useViewConfig/useViewConfig.js +30 -0
  89. package/components/DataGrid/hooks/useViewConfig/useViewConfig.test.d.ts +1 -0
  90. package/components/DataGrid/hooks/useViewMode/index.d.ts +1 -0
  91. package/components/DataGrid/hooks/useViewMode/index.js +1 -0
  92. package/components/DataGrid/hooks/useViewMode/useViewMode.d.ts +31 -0
  93. package/components/DataGrid/hooks/useViewMode/useViewMode.js +44 -0
  94. package/components/DataGrid/hooks/useViewMode/useViewMode.test.d.ts +1 -0
  95. package/components/DataGrid/index.d.ts +5 -4
  96. package/components/DataGrid/subcomponents/Cards/helpers/calculateCardHeight.d.ts +5 -4
  97. package/components/DataGrid/subcomponents/Cards/helpers/calculateCardHeight.js +4 -1
  98. package/components/DataGrid/types.d.ts +30 -6
  99. package/components/DataGrid/types.helpers.d.ts +55 -0
  100. package/components/DataGrid/types.helpers.js +1 -0
  101. package/components/DataGrid/types.hooks.d.ts +81 -0
  102. package/hooks/useDataGridPersistence/constants.d.ts +2 -0
  103. package/hooks/useDataGridPersistence/constants.js +6 -2
  104. package/hooks/useDataGridPersistence/helpers.d.ts +5 -1
  105. package/hooks/useDataGridPersistence/helpers.js +8 -1
  106. package/hooks/useDataGridPersistence/useDataGridPersistence.d.ts +11 -3
  107. package/hooks/useDataGridPersistence/useDataGridPersistence.js +43 -20
  108. package/package.json +1 -1
@@ -34,8 +34,12 @@ function DataGrid(props) {
34
34
  dataTestId = "",
35
35
  customHeader: CustomHeader,
36
36
  visibleCustomHeader = true,
37
+ defaultConfig,
38
+ onChangeConfig,
37
39
  defaultUserColumns,
40
+ // deprecated
38
41
  onChangeUserColumns,
42
+ // deprecated
39
43
  size,
40
44
  externalSortSettings,
41
45
  externalFilterSettings,
@@ -91,8 +95,10 @@ function DataGrid(props) {
91
95
  checkedRowsMultiple,
92
96
  rowKeyGetter,
93
97
  rows,
94
- onChangeUserColumns,
98
+ defaultConfig,
99
+ onChangeConfig,
95
100
  defaultUserColumns,
101
+ onChangeUserColumns,
96
102
  size: currentSize,
97
103
  externalSortSettings,
98
104
  externalFilterSettings,
@@ -52,3 +52,16 @@ export declare const DATAGRID_ROW_HEIGHTS_CONFORTABLE: number[];
52
52
  * Densidad confortable
53
53
  */
54
54
  export declare const DATAGRID_DENSITIES_CONFORTABLE: DataGridDensityConfig;
55
+ /**
56
+ * Constantes para el cálculo de altura de cards
57
+ */
58
+ export declare const CARD_HEIGHT: {
59
+ /** Altura del CardHeader (checkbox + iconos de acción) */
60
+ HEADER: number;
61
+ /** Altura por columna (label + value) */
62
+ COLUMN: number;
63
+ /** Padding vertical total (arriba + abajo) */
64
+ PADDING_VERTICAL: number;
65
+ /** Altura mínima base */
66
+ BASE_MIN: number;
67
+ };
@@ -28,7 +28,18 @@ var DATAGRID_SEMANTIC_WIDTHS = /* @__PURE__ */ ((DATAGRID_SEMANTIC_WIDTHS2) => {
28
28
  })(DATAGRID_SEMANTIC_WIDTHS || {});
29
29
  const DATAGRID_ROW_HEADER_HEIGHTS = [32, 40, 48];
30
30
  const DATAGRID_ROW_HEIGHTS = [32, 40, 48];
31
+ const CARD_HEIGHT = {
32
+ /** Altura del CardHeader (checkbox + iconos de acción) */
33
+ HEADER: 42,
34
+ /** Altura por columna (label + value) */
35
+ COLUMN: 32,
36
+ /** Padding vertical total (arriba + abajo) */
37
+ PADDING_VERTICAL: 40,
38
+ /** Altura mínima base */
39
+ BASE_MIN: 120
40
+ };
31
41
  export {
42
+ CARD_HEIGHT as C,
32
43
  DATAGRID_PREFIX_NAME as D,
33
44
  TEST_SKELETON as T,
34
45
  DATAGRID_ROW_HEIGHTS as a,
@@ -1,152 +1,33 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
- import { useState, useCallback, useEffect, useMemo, createContext } from "react";
3
- import { c as castMapColumnsWidthToRecord } from "../../helpers/castMapColumnsWidthToRecord.js";
4
- import { useFirstRender, useIsMobile } from "@m4l/graphics";
2
+ import { useState, useEffect, useMemo, createContext } from "react";
3
+ import { useFirstRender } from "@m4l/graphics";
5
4
  import { g as getComponentClasses } from "../../../../utils/getComponentSlotRoot.js";
6
5
  import { a as DATAGRID_ROW_HEIGHTS, b as DATAGRID_ROW_HEADER_HEIGHTS, D as DATAGRID_PREFIX_NAME } from "../../constants.js";
7
6
  import { C as ControlNavigateSlots, a as ColumnsConfigSlots, T as TextEditorSlots, b as TableSlots, R as RowsCountSlots, A as ActionsSlots, D as DataGridSlots } from "../../slots/DataGridEnum.js";
8
7
  import { deepEqual } from "fast-equals";
8
+ import { u as useViewMode } from "../../hooks/useViewMode/useViewMode.js";
9
+ import { u as useRowHeight } from "../../hooks/useRowHeight/useRowHeight.js";
10
+ import { u as useColumnsWidth } from "../../hooks/useColumnsWidth/useColumnsWidth.js";
11
+ import { u as useViewConfig } from "../../hooks/useViewConfig/useViewConfig.js";
12
+ import { u as useChangeColumnsConfig } from "../../hooks/useChangeColumnsConfig/useChangeColumnsConfig.js";
13
+ import { u as useChangeColumnsOrder } from "../../hooks/useChangeColumnsOrder/useChangeColumnsOrder.js";
14
+ import { g as getColumnsWidth } from "../../helpers/getColumnsWidth/getColumnsWidth.js";
15
+ import { g as getInitialColumnsConfig } from "../../helpers/getInitialColumnsConfig/getInitialColumnsConfig.js";
16
+ import { g as getAllViewModes } from "../../helpers/getAllViewModes/getAllViewModes.js";
17
+ import { g as getViewSpecificConfig } from "../../helpers/getViewSpecificConfig/getViewSpecificConfig.js";
18
+ import { g as getViewMode } from "../../helpers/getViewMode/getViewMode.js";
9
19
  const DataGridContext = createContext(null);
10
- function getColumnsWidth(id, columns, defaultUserColumns) {
11
- const newMap = /* @__PURE__ */ new Map();
12
- let storeColumnsWidth;
13
- try {
14
- const item = window.localStorage.getItem(`${id}_columns_width`);
15
- storeColumnsWidth = item !== null ? new Map(JSON.parse(item)) : /* @__PURE__ */ new Map();
16
- } catch (_e) {
17
- storeColumnsWidth = /* @__PURE__ */ new Map();
18
- }
19
- for (let i = 0; i < columns.length; i++) {
20
- let width;
21
- if (defaultUserColumns) {
22
- try {
23
- width = defaultUserColumns.columnsWidths[columns[i].key];
24
- } catch (_e) {
25
- }
26
- }
27
- if (width === void 0) {
28
- const storeWidth = storeColumnsWidth.get(columns[i].key);
29
- if (typeof storeWidth === "number") {
30
- width = storeWidth;
31
- }
32
- }
33
- if (width !== void 0) {
34
- newMap.set(columns[i].key, width);
35
- }
36
- }
37
- return newMap;
38
- }
39
- function saveColumnsWidth(id, storeColumnsWidth, onChangeUserColumns) {
40
- if (onChangeUserColumns) {
41
- const columnsWidthObject = castMapColumnsWidthToRecord(storeColumnsWidth);
42
- onChangeUserColumns({
43
- reason: "columnsWidths",
44
- userConfig: columnsWidthObject
45
- });
46
- return;
47
- }
48
- localStorage.setItem(
49
- `${id}_columns_width`,
50
- JSON.stringify([...storeColumnsWidth])
51
- );
52
- }
53
- const getAllViewModes = () => {
54
- return ["table", "cards"];
55
- };
56
- const getViewSpecificConfig = (viewMode) => {
57
- const configs = {
58
- table: {
59
- defaults: { frozen: false },
60
- originalDefaults: { originalFrozen: false },
61
- dynamicOriginalProperty: (item) => ({
62
- originalFrozen: item?.frozen ?? false
63
- })
64
- },
65
- cards: {
66
- defaults: { showTitle: true },
67
- originalDefaults: { originalShowTitle: true },
68
- dynamicOriginalProperty: (item) => ({
69
- originalShowTitle: item?.showTitle ?? true
70
- })
71
- }
72
- };
73
- return configs[viewMode] || configs.table;
74
- };
75
- const getViewSuffix = (viewMode) => {
76
- if (viewMode === "table") {
77
- return "";
78
- }
79
- return `_${viewMode}`;
80
- };
81
- function getInitialColumnsConfig(id, columns, viewMode, defaultUserConfig, viewSpecificDefaults, originalViewSpecificDefaults) {
82
- let columnsConfig;
83
- if (defaultUserConfig) {
84
- columnsConfig = defaultUserConfig;
85
- } else {
86
- columnsConfig = loadViewConfig(
87
- id,
88
- getViewSuffix(viewMode)
89
- ) || [];
90
- }
91
- return columns.map((column, index) => {
92
- const existingConfig = columnsConfig.find((c) => c.key === column.key);
93
- const columnConfigIndex = existingConfig ? columnsConfig.indexOf(existingConfig) : -1;
94
- const baseConfig = {
95
- key: column.key,
96
- name: typeof column.name === "string" ? column.name : String(column.name),
97
- hidden: column.hidden === void 0 ? false : column.hidden,
98
- index: columnConfigIndex > -1 ? columnConfigIndex : index,
99
- visible: existingConfig?.visible !== void 0 ? existingConfig.visible : column.visible === void 0 ? true : column.visible,
100
- originalIndex: index,
101
- originalVisible: column.visible === void 0 ? true : column.visible
102
- };
103
- return {
104
- ...baseConfig,
105
- ...viewSpecificDefaults,
106
- ...originalViewSpecificDefaults,
107
- ...existingConfig || {}
108
- };
109
- }).sort((a, b) => a.index - b.index);
110
- }
111
- function saveViewConfig(id, viewSuffix, config, onChangeCallback) {
112
- if (onChangeCallback) {
113
- const reason = viewSuffix === "" ? "columnsConfig" : `columnsConfig${viewSuffix.replace("_", "")}`;
114
- onChangeCallback({
115
- reason,
116
- userConfig: config
117
- });
118
- return;
119
- }
120
- const storageKey = viewSuffix === "" ? `${id}_columns_config` : `${id}_columns_config${viewSuffix}`;
121
- localStorage.setItem(storageKey, JSON.stringify(config));
122
- }
123
- function loadViewConfig(id, viewSuffix) {
124
- try {
125
- const storageKey = viewSuffix === "" ? `${id}_columns_config` : `${id}_columns_config${viewSuffix}`;
126
- const stored = localStorage.getItem(storageKey);
127
- return stored ? JSON.parse(stored) : null;
128
- } catch (_e) {
129
- return null;
130
- }
131
- }
132
- function saveViewMode(id, viewMode) {
133
- localStorage.setItem(`${id}_view_mode`, viewMode);
134
- }
135
- function getIndexRowHeightVariant(variant) {
136
- if (variant === "compact") {
137
- return 0;
138
- }
139
- if (variant === "standard") {
140
- return 1;
141
- }
142
- return 2;
143
- }
144
20
  function DataGridProvider(props) {
145
21
  const {
146
22
  id,
147
23
  children,
148
24
  columns,
25
+ defaultConfig,
26
+ onChangeConfig,
149
27
  defaultUserColumns,
28
+ // deprecated
29
+ onChangeUserColumns,
30
+ // deprecated
150
31
  rows,
151
32
  rowsCount,
152
33
  rowActionsGetter,
@@ -157,7 +38,6 @@ function DataGridProvider(props) {
157
38
  onCheckedRowsChange,
158
39
  checkedRowsMultiple,
159
40
  rowKeyGetter,
160
- onChangeUserColumns,
161
41
  externalSortSettings: sortSettings,
162
42
  externalFilterSettings: filterSettings,
163
43
  size,
@@ -166,8 +46,37 @@ function DataGridProvider(props) {
166
46
  defaultViewMode,
167
47
  cardsViewConfig
168
48
  } = props;
49
+ const resolvedDefaultConfig = defaultConfig ?? defaultUserColumns;
50
+ const resolvedOnChangeConfig = onChangeConfig ?? onChangeUserColumns;
169
51
  const isFirstRender = useFirstRender([columns, id]);
170
- const isMobile = useIsMobile();
52
+ const { viewModeState, setViewModeState, handleViewModeChange } = useViewMode(
53
+ {
54
+ id,
55
+ viewMode,
56
+ defaultViewMode,
57
+ onViewModeChange,
58
+ onChangeConfig: resolvedOnChangeConfig
59
+ }
60
+ );
61
+ const { currentRowHeightVariant, setRowHeightVariant } = useRowHeight({
62
+ rowHeights,
63
+ rowHeaderHeights,
64
+ initialRowHeightVariant
65
+ });
66
+ const { columnsWidths, setColumnsWidths, onChangeColumnWidth } = useColumnsWidth({
67
+ id,
68
+ onChangeConfig: resolvedOnChangeConfig
69
+ });
70
+ const { getConfigColumns, updateConfigColumns } = useViewConfig();
71
+ const { onChangeColumnsConfig } = useChangeColumnsConfig({
72
+ id,
73
+ onChangeConfig: resolvedOnChangeConfig,
74
+ updateConfigColumns
75
+ });
76
+ const { onChangeColumnsOrder } = useChangeColumnsOrder({
77
+ getConfigColumns,
78
+ onChangeColumnsConfig
79
+ });
171
80
  const classes = getComponentClasses(DATAGRID_PREFIX_NAME, {
172
81
  ...DataGridSlots,
173
82
  ...ActionsSlots,
@@ -177,80 +86,8 @@ function DataGridProvider(props) {
177
86
  ...ColumnsConfigSlots,
178
87
  ...ControlNavigateSlots
179
88
  });
180
- const [columnsWidths, setColumnsWidths] = useState(/* @__PURE__ */ new Map());
181
- const [viewConfig, setViewConfig] = useState(() => ({
182
- table: [],
183
- cards: []
184
- }));
185
- const getConfigColumns = useCallback(
186
- (viewModeParam) => {
187
- return viewConfig[viewModeParam];
188
- },
189
- [viewConfig]
190
- );
191
- const updateConfigColumns = useCallback(
192
- (viewModeParam, config) => {
193
- setViewConfig((prev) => ({
194
- ...prev,
195
- [viewModeParam]: config
196
- }));
197
- },
198
- []
199
- );
200
89
  const [rowsFilterCount, setRowFilterCount] = useState(rowsCount);
201
90
  const [sortColumns, setSortColumns] = useState([]);
202
- const [viewModeState, setViewModeState] = useState(() => {
203
- if (viewMode) {
204
- return viewMode;
205
- }
206
- try {
207
- const savedViewMode = localStorage.getItem(`${id}_view_mode`);
208
- if (savedViewMode === "cards" || savedViewMode === "table") {
209
- return savedViewMode;
210
- }
211
- } catch (_e) {
212
- }
213
- if (defaultViewMode) {
214
- return defaultViewMode;
215
- }
216
- return isMobile ? "cards" : "table";
217
- });
218
- const handleViewModeChange = useCallback(
219
- (newViewMode) => {
220
- setViewModeState(newViewMode);
221
- saveViewMode(id, newViewMode);
222
- onViewModeChange?.(newViewMode);
223
- },
224
- [onViewModeChange, id]
225
- );
226
- useEffect(() => {
227
- if (viewMode !== void 0) {
228
- setViewModeState(viewMode);
229
- }
230
- }, [viewMode]);
231
- const [currentRowHeightVariant, setCurrentRowHeightVariant] = useState(() => {
232
- if (typeof rowHeights !== typeof rowHeaderHeights) {
233
- throw new Error("Must be same type rowheights and rowHeaderTypes");
234
- }
235
- if (typeof rowHeights === "number" && typeof rowHeaderHeights === "number") {
236
- return {
237
- rowHVariant: "compact",
238
- rowHeight: rowHeights,
239
- rowHeaderHeight: rowHeaderHeights
240
- };
241
- }
242
- if (typeof rowHeights !== "number" && rowHeights.length !== 3) {
243
- throw new Error("Must be 3 heights");
244
- }
245
- if (typeof rowHeaderHeights !== "number" && rowHeaderHeights.length !== 3) {
246
- throw new Error("Must be 3 HeaderHeights");
247
- }
248
- return {
249
- rowHVariant: initialRowHeightVariant,
250
- rowHeight: typeof rowHeights !== "number" ? rowHeights[getIndexRowHeightVariant(initialRowHeightVariant)] : rowHeights,
251
- rowHeaderHeight: typeof rowHeaderHeights !== "number" ? rowHeaderHeights[getIndexRowHeightVariant(initialRowHeightVariant)] : rowHeaderHeights
252
- };
253
- });
254
91
  useEffect(() => {
255
92
  const keys = new Set(columns.map((c) => c.key));
256
93
  if (sortSettings?.sortsColumns) {
@@ -316,24 +153,26 @@ function DataGridProvider(props) {
316
153
  }
317
154
  }, [columns, sortSettings, filterSettings, cardsViewConfig]);
318
155
  const finalRowHeights = useMemo(() => rowHeights, [rowHeights]);
319
- let timerSaveColumns;
320
156
  useEffect(() => {
321
157
  if (isFirstRender) {
322
- setColumnsWidths(getColumnsWidth(id, columns, defaultUserColumns));
158
+ setColumnsWidths(getColumnsWidth(id, columns, resolvedDefaultConfig));
159
+ setViewModeState(
160
+ getViewMode(id, resolvedDefaultConfig, defaultViewMode) ?? "table"
161
+ );
323
162
  updateConfigColumns(
324
163
  "table",
325
164
  getInitialColumnsConfig(
326
165
  id,
327
166
  columns,
328
167
  "table",
329
- defaultUserColumns?.columnsConfig,
168
+ resolvedDefaultConfig?.columnsConfig,
330
169
  { frozen: false },
331
170
  { originalFrozen: false }
332
171
  )
333
172
  );
334
173
  const finalCardsConfig = (() => {
335
- if (defaultUserColumns?.columnsConfigCards) {
336
- return defaultUserColumns.columnsConfigCards;
174
+ if (resolvedDefaultConfig?.columnsConfigCards && resolvedDefaultConfig.columnsConfigCards.length > 0) {
175
+ return resolvedDefaultConfig.columnsConfigCards;
337
176
  }
338
177
  if (cardsViewConfig?.columnsConfig) {
339
178
  return cardsViewConfig.columnsConfig.map((colConfig, index) => {
@@ -362,7 +201,7 @@ function DataGridProvider(props) {
362
201
  )
363
202
  );
364
203
  }
365
- }, [defaultUserColumns]);
204
+ }, [resolvedDefaultConfig]);
366
205
  useEffect(() => {
367
206
  if (!isFirstRender) {
368
207
  const updatedConfigs = {
@@ -451,65 +290,9 @@ function DataGridProvider(props) {
451
290
  });
452
291
  }
453
292
  }, [columns, id]);
454
- const onChangeColumnsConfig = useCallback(
455
- (viewModeParam, newConfig) => {
456
- saveViewConfig(
457
- id,
458
- getViewSuffix(viewModeParam),
459
- newConfig,
460
- onChangeUserColumns
461
- );
462
- updateConfigColumns(viewModeParam, newConfig);
463
- },
464
- [id, onChangeUserColumns, updateConfigColumns]
465
- );
466
- const onChangeColumnsOrder = useCallback(
467
- (viewModeParam, sourceKey, targetKey) => {
468
- if (sourceKey === targetKey) {
469
- return;
470
- }
471
- const currentConfig = getConfigColumns(viewModeParam);
472
- const sourceColumnIndex = currentConfig.findIndex(
473
- (c) => c.key === sourceKey
474
- );
475
- const targetColumnIndex = currentConfig.findIndex(
476
- (c) => c.key === targetKey
477
- );
478
- const reorderedConfig = [...currentConfig];
479
- reorderedConfig.splice(
480
- targetColumnIndex,
481
- 0,
482
- reorderedConfig.splice(sourceColumnIndex, 1)[0]
483
- );
484
- onChangeColumnsConfig(viewModeParam, reorderedConfig);
485
- },
486
- [getConfigColumns, onChangeColumnsConfig]
487
- );
488
- const onChangeColumnWidth = (columnKey, width) => {
489
- columnsWidths.set(columnKey, Math.round(width));
490
- if (timerSaveColumns) {
491
- clearTimeout(timerSaveColumns);
492
- }
493
- timerSaveColumns = setTimeout(() => {
494
- saveColumnsWidth(id, columnsWidths, onChangeUserColumns);
495
- }, 500);
496
- };
497
293
  const setRowFilterCountInternal = (newRowsCount) => {
498
294
  setRowFilterCount(newRowsCount);
499
295
  };
500
- const setCurrentRowHeightInternal = useCallback(
501
- (newVariant) => {
502
- if (typeof rowHeights === "number" || typeof rowHeaderHeights === "number") {
503
- throw new Error("Only use setCurrentRowHeight with vector rowHeights");
504
- }
505
- setCurrentRowHeightVariant({
506
- rowHVariant: newVariant,
507
- rowHeight: rowHeights[getIndexRowHeightVariant(newVariant)],
508
- rowHeaderHeight: rowHeaderHeights[getIndexRowHeightVariant(newVariant)]
509
- });
510
- },
511
- [setCurrentRowHeightVariant, rowHeights, rowHeaderHeights]
512
- );
513
296
  return /* @__PURE__ */ jsx(
514
297
  DataGridContext.Provider,
515
298
  {
@@ -529,11 +312,13 @@ function DataGridProvider(props) {
529
312
  onChangeColumnWidth,
530
313
  setRowsCount: setRowFilterCountInternal,
531
314
  rowActionsGetter,
532
- setRowHeightVariant: setCurrentRowHeightInternal,
315
+ setRowHeightVariant,
533
316
  onCheckedRowsChange,
534
317
  checkedRowsMultiple,
535
318
  rowKeyGetter,
536
- onChangeUserColumns,
319
+ onChangeConfig: resolvedOnChangeConfig,
320
+ onChangeUserColumns: resolvedOnChangeConfig,
321
+ // deprecated alias
537
322
  sortColumns,
538
323
  setSortColumns,
539
324
  externalSortSettings: sortSettings,
@@ -1,23 +1,14 @@
1
1
  import { ReactNode } from 'react';
2
- import { RowKey, RowHeightVariants, GridProps } from '../../types';
2
+ import { RowKey, RowHeightVariants, GridProps, OnChangeConfig } from '../../types';
3
3
  import { Maybe } from '@m4l/core';
4
4
  import { ActionsSlots, ColumnsConfigSlots, ControlNavigateSlots, DataGridSlots, RowsCountSlots, TableSlots, TextEditorSlots } from '../../slots/DataGridEnum';
5
5
  import { SortColumn } from 'react-data-grid';
6
- /**
7
- * Configuración base genérica que puede extenderse con propiedades específicas de cualquier vista
8
- * USO INTERNO: El sistema usa esto para manejar genéricamente las configuraciones
9
- */
10
- export type BaseViewConfig<TViewSpecific = {}> = TViewSpecific & {
11
- key: string;
12
- visible: boolean;
13
- index: number;
14
- name?: string;
15
- };
6
+ import { IGridConfig, BaseViewConfig } from '../../types.helpers';
16
7
  /**
17
8
  * Configuración extendida genérica con propiedades originales
18
9
  * USO INTERNO: El contexto usa esto para el estado interno con propiedades de tracking
19
10
  */
20
- export type IViewConfig<TViewSpecific = {}, TOriginalViewSpecific = {}> = BaseViewConfig<TViewSpecific> & {
11
+ export type IViewConfig<TViewSpecific = object, TOriginalViewSpecific = object> = BaseViewConfig<TViewSpecific> & {
21
12
  hidden: boolean;
22
13
  originalIndex: number;
23
14
  originalVisible: boolean;
@@ -62,15 +53,6 @@ export interface IConfigColumnCards extends Omit<IConfigColumn, 'frozen' | 'orig
62
53
  originalShowTitle: boolean;
63
54
  }
64
55
  export type ViewMode = 'table' | 'cards';
65
- /**
66
- * Configuración base del Grid (API pública original)
67
- * USO: Tipo base para defaultUserColumns - mantiene retrocompatibilidad
68
- * IMPORTANTE: No modificar para mantener retrocompatibilidad con código existente
69
- */
70
- export interface IGridConfig {
71
- columnsConfig: BaseConfigColumn[];
72
- columnsWidths: Record<RowKey, number>;
73
- }
74
56
  /**
75
57
  * Configuración extendida del Grid para soportar múltiples vistas
76
58
  * USO: Para nuevas implementaciones que soporten cards y futuras vistas
@@ -78,21 +60,40 @@ export interface IGridConfig {
78
60
  */
79
61
  export interface IGridConfigExtended extends IGridConfig {
80
62
  columnsConfigCards?: BaseConfigColumnCards[];
63
+ viewMode?: ViewMode;
81
64
  }
82
- export interface DataGridProviderProps<TRow, TSummaryRow, TKey extends RowKey = RowKey> extends Pick<GridProps<TRow, TSummaryRow, TKey>, 'rows' | 'columns' | 'rowActionsGetter' | 'rowHeaderHeights' | 'rowHeights' | 'initialRowHeightVariant' | 'checkedRows' | 'onCheckedRowsChange' | 'checkedRowsMultiple' | 'rowKeyGetter' | 'onChangeUserColumns' | 'defaultUserColumns' | 'externalSortSettings' | 'externalFilterSettings' | 'defaultViewMode' | 'cardsViewConfig'> {
65
+ export interface DataGridProviderProps<TRow, TSummaryRow, TKey extends RowKey = RowKey> extends Pick<GridProps<TRow, TSummaryRow, TKey>, 'rows' | 'columns' | 'rowActionsGetter' | 'rowHeaderHeights' | 'rowHeights' | 'initialRowHeightVariant' | 'checkedRows' | 'onCheckedRowsChange' | 'checkedRowsMultiple' | 'rowKeyGetter' | 'externalSortSettings' | 'externalFilterSettings' | 'defaultViewMode' | 'cardsViewConfig'> {
83
66
  id: number | string;
84
67
  children: ReactNode;
85
68
  rowsCount: number;
86
69
  size: GridProps<any, any, any>['size'];
87
70
  viewMode?: ViewMode;
88
71
  onViewModeChange?: (viewMode: ViewMode) => void;
72
+ /** Configuración inicial del DataGrid */
73
+ defaultConfig?: IGridConfigExtended;
74
+ /** Callback para persistir cambios de configuración */
75
+ onChangeConfig?: OnChangeConfig;
76
+ /**
77
+ * @deprecated Usar `defaultConfig` en su lugar.
78
+ */
79
+ defaultUserColumns?: IGridConfigExtended;
80
+ /**
81
+ * @deprecated Usar `onChangeConfig` en su lugar.
82
+ */
83
+ onChangeUserColumns?: OnChangeConfig;
89
84
  }
90
85
  export type RowHeightState = {
91
86
  rowHVariant: RowHeightVariants;
92
87
  rowHeight: number;
93
88
  rowHeaderHeight: number;
94
89
  };
95
- export interface DataGridContextProps<TRow, TSummaryRow, TKey extends RowKey = RowKey> extends Pick<DataGridProviderProps<TRow, TSummaryRow, TKey>, 'rows' | 'checkedRows' | 'onCheckedRowsChange' | 'checkedRowsMultiple' | 'rowHeights' | 'rowsCount' | 'rowActionsGetter' | 'rowKeyGetter' | 'onChangeUserColumns' | 'externalSortSettings' | 'externalFilterSettings' | 'viewMode' | 'onViewModeChange' | 'defaultViewMode' | 'cardsViewConfig'> {
90
+ export interface DataGridContextProps<TRow, TSummaryRow, TKey extends RowKey = RowKey> extends Pick<DataGridProviderProps<TRow, TSummaryRow, TKey>, 'rows' | 'checkedRows' | 'onCheckedRowsChange' | 'checkedRowsMultiple' | 'rowHeights' | 'rowsCount' | 'rowActionsGetter' | 'rowKeyGetter' | 'externalSortSettings' | 'externalFilterSettings' | 'viewMode' | 'onViewModeChange' | 'defaultViewMode' | 'cardsViewConfig'> {
91
+ /** Callback para persistir cambios de configuración */
92
+ onChangeConfig?: OnChangeConfig;
93
+ /**
94
+ * @deprecated Usar `onChangeConfig` en su lugar.
95
+ */
96
+ onChangeUserColumns?: OnChangeConfig;
96
97
  getConfigColumns: (viewMode: ViewMode) => IViewConfig[];
97
98
  onChangeColumnsConfig: (viewMode: ViewMode, config: IViewConfig[]) => void;
98
99
  onChangeColumnsOrder: (viewMode: ViewMode, sourceKey: string, targetKey: string) => void;
@@ -0,0 +1,19 @@
1
+ import { ViewMode } from '../../contexts/DataGridContext/types';
2
+ /**
3
+ * Obtiene todos los modos de vista disponibles en el DataGrid.
4
+ *
5
+ * ## Modos disponibles
6
+ *
7
+ * - `table`: Vista de tabla tradicional con filas y columnas
8
+ * - `cards`: Vista de tarjetas para visualización compacta
9
+ *
10
+ * ## Uso
11
+ *
12
+ * Esta función centraliza los modos de vista disponibles,
13
+ * facilitando la extensión futura si se agregan nuevos modos.
14
+ * @returns Array con los modos de vista disponibles
15
+ * @example
16
+ * const modes = getAllViewModes();
17
+ * // ['table', 'cards']
18
+ */
19
+ export declare const getAllViewModes: () => ViewMode[];
@@ -0,0 +1,6 @@
1
+ const getAllViewModes = () => {
2
+ return ["table", "cards"];
3
+ };
4
+ export {
5
+ getAllViewModes as g
6
+ };
@@ -0,0 +1 @@
1
+ export { getAllViewModes } from './getAllViewModes';
@@ -0,0 +1,47 @@
1
+ import { Column } from 'react-data-grid';
2
+ import { Maybe } from '@m4l/core';
3
+ import { RowKey } from '../../types';
4
+ import { IGridConfig } from '../../types.helpers';
5
+ /**
6
+ * Obtiene la configuración de anchos de las columnas del DataGrid.
7
+ *
8
+ * ## Flujo de persistencia
9
+ *
10
+ * Esta función obtiene los anchos de columnas siguiendo una jerarquía de precedencia:
11
+ *
12
+ * 1. **defaultConfig** (Cookies de red) - Prioridad máxima
13
+ * Configuración que viene de las cookies de red cuando el componente se monta.
14
+ * En producción, esta es la fuente principal de datos persistidos.
15
+ *
16
+ * 2. **localStorage** - Fallback local
17
+ * Solo se usa cuando NO hay configuración de cookies de red.
18
+ * Útil para desarrollo o cuando no hay integración con cookies de red.
19
+ *
20
+ * Si no se encuentra configuración en ninguna fuente, la columna no se incluye
21
+ * en el Map resultante y usará el ancho por defecto definido en la columna.
22
+ *
23
+ * ## Relación con saveColumnsWidth
24
+ *
25
+ * - Cuando el usuario modifica anchos, `saveColumnsWidth` envía los cambios
26
+ * mediante el callback `onChangeConfig`
27
+ * - En la siguiente carga, esos datos vuelven como `defaultConfig`
28
+ * - localStorage es solo un fallback cuando no hay callback configurado
29
+ * @template TRow - Tipo de datos de cada fila del DataGrid
30
+ * @template TSummaryRow - Tipo de datos de la fila de resumen del DataGrid
31
+ * @param id - Identificador único del DataGrid. Se usa como prefijo para la key en localStorage (fallback)
32
+ * @param columns - Array de columnas del DataGrid con su configuración
33
+ * @param defaultConfig - Configuración del usuario obtenida de cookies de red.
34
+ * Esta es la fuente principal en producción.
35
+ * @returns Map donde la key es el identificador de la columna (column.key) y el value
36
+ * es el ancho configurado (número en píxeles o string con unidad CSS)
37
+ * @example
38
+ * // Sin configuración de cookies de red (usa localStorage como fallback)
39
+ * const columnsWidths = getColumnsWidth('my-grid', columns, null);
40
+ * @example
41
+ * // Con configuración de cookies de red (caso típico en producción)
42
+ * const columnsWidths = getColumnsWidth('my-grid', columns, {
43
+ * columnsWidths: { name: 200, email: 300 }
44
+ * });
45
+ * @see saveColumnsWidth - Función complementaria para persistir la configuración
46
+ */
47
+ export declare function getColumnsWidth<TRow, TSummaryRow>(id: RowKey, columns: readonly Column<TRow, TSummaryRow>[], defaultConfig: Maybe<IGridConfig>): Map<string, Maybe<string | number>>;
@@ -0,0 +1,32 @@
1
+ function getColumnsWidth(id, columns, defaultConfig) {
2
+ const newMap = /* @__PURE__ */ new Map();
3
+ let storeColumnsWidth;
4
+ try {
5
+ const item = window.localStorage.getItem(`${id}_columns_width`);
6
+ storeColumnsWidth = item !== null ? new Map(JSON.parse(item)) : /* @__PURE__ */ new Map();
7
+ } catch (_e) {
8
+ storeColumnsWidth = /* @__PURE__ */ new Map();
9
+ }
10
+ for (let i = 0; i < columns.length; i++) {
11
+ let width;
12
+ if (defaultConfig) {
13
+ try {
14
+ width = defaultConfig.columnsWidths[columns[i].key];
15
+ } catch (_e) {
16
+ }
17
+ }
18
+ if (width === void 0) {
19
+ const storeWidth = storeColumnsWidth.get(columns[i].key);
20
+ if (typeof storeWidth === "number") {
21
+ width = storeWidth;
22
+ }
23
+ }
24
+ if (width !== void 0) {
25
+ newMap.set(columns[i].key, width);
26
+ }
27
+ }
28
+ return newMap;
29
+ }
30
+ export {
31
+ getColumnsWidth as g
32
+ };