@bit.rhplus/ag-grid 0.0.37 → 0.0.39

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 (37) hide show
  1. package/Aggregations.js +26 -3
  2. package/BulkEdit/BulkEditButton.jsx +149 -0
  3. package/BulkEdit/BulkEditPopover.jsx +301 -0
  4. package/BulkEdit/BulkEditSelect.jsx +80 -0
  5. package/BulkEdit/index.js +8 -0
  6. package/BulkEdit/useBulkCellEdit.js +348 -0
  7. package/BulkEdit/utils.js +532 -0
  8. package/Renderers/StateRenderer.jsx +17 -11
  9. package/dist/Aggregations.d.ts +2 -1
  10. package/dist/Aggregations.js +19 -2
  11. package/dist/Aggregations.js.map +1 -1
  12. package/dist/BulkEdit/BulkEditButton.d.ts +27 -0
  13. package/dist/BulkEdit/BulkEditButton.js +82 -0
  14. package/dist/BulkEdit/BulkEditButton.js.map +1 -0
  15. package/dist/BulkEdit/BulkEditPopover.d.ts +27 -0
  16. package/dist/BulkEdit/BulkEditPopover.js +122 -0
  17. package/dist/BulkEdit/BulkEditPopover.js.map +1 -0
  18. package/dist/BulkEdit/BulkEditSelect.d.ts +14 -0
  19. package/dist/BulkEdit/BulkEditSelect.js +31 -0
  20. package/dist/BulkEdit/BulkEditSelect.js.map +1 -0
  21. package/dist/BulkEdit/index.d.ts +5 -0
  22. package/dist/BulkEdit/index.js +7 -0
  23. package/dist/BulkEdit/index.js.map +1 -0
  24. package/dist/BulkEdit/useBulkCellEdit.d.ts +1 -0
  25. package/dist/BulkEdit/useBulkCellEdit.js +277 -0
  26. package/dist/BulkEdit/useBulkCellEdit.js.map +1 -0
  27. package/dist/BulkEdit/utils.d.ts +7 -0
  28. package/dist/BulkEdit/utils.js +472 -0
  29. package/dist/BulkEdit/utils.js.map +1 -0
  30. package/dist/Renderers/StateRenderer.js +16 -11
  31. package/dist/Renderers/StateRenderer.js.map +1 -1
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.js +184 -25
  34. package/dist/index.js.map +1 -1
  35. package/index.jsx +249 -25
  36. package/package.json +5 -4
  37. /package/dist/{preview-1760103803977.js → preview-1760429814166.js} +0 -0
@@ -0,0 +1,348 @@
1
+ /* eslint-disable */
2
+ import { useState, useCallback, useEffect, useRef } from 'react';
3
+ import {
4
+ validateRangeEditable,
5
+ getLastCellPosition,
6
+ applyBulkChanges,
7
+ applyBulkChangesWithApi,
8
+ } from './utils';
9
+ import useData from '@bit.rhplus/data';
10
+
11
+ /**
12
+ * Hook pro bulk cell edit funkcionalitu v AG Grid
13
+ * @param {Object} gridRef - Reference na AG Grid
14
+ * @param {Object} options - Konfigurace bulk edit
15
+ * @returns {Object} - State a handlery pro bulk edit
16
+ */
17
+ export const useBulkCellEdit = (gridRef, options = {}) => {
18
+ const {
19
+ enabled = false,
20
+ minCells = 2,
21
+ allowMultiColumn = false,
22
+ buttonPosition = 'bottom-right',
23
+ buttonOffset = { x: 5, y: 5 },
24
+ accessToken = null,
25
+ onBulkEditStart,
26
+ onBulkEditComplete,
27
+ } = options;
28
+
29
+ const { fetchDataUIAsync } = useData();
30
+
31
+ const [floatingButton, setFloatingButton] = useState({
32
+ visible: false,
33
+ position: null,
34
+ range: null,
35
+ column: null,
36
+ cellCount: 0,
37
+ });
38
+
39
+ const [editPopover, setEditPopover] = useState({
40
+ visible: false,
41
+ value: '',
42
+ loading: false,
43
+ error: null,
44
+ });
45
+
46
+ const scrollListenerRef = useRef(null);
47
+ const debounceTimeoutRef = useRef(null);
48
+ const pendingEventRef = useRef(null);
49
+
50
+ /**
51
+ * Handler pro změnu range selection s debounce optimalizací
52
+ */
53
+ const handleRangeChange = useCallback(
54
+ (event) => {
55
+ // Lightweight pre-checks - okamžitě bez debounce
56
+ if (!enabled) {
57
+ setFloatingButton({ visible: false });
58
+ return;
59
+ }
60
+
61
+ const ranges = event.api.getCellRanges();
62
+ if (!ranges || ranges.length === 0) {
63
+ setFloatingButton({ visible: false });
64
+ return;
65
+ }
66
+
67
+ // Uložit event pro debounced zpracování
68
+ pendingEventRef.current = event;
69
+
70
+ // Debounce logika - čekat až přestane označovat (150ms)
71
+ if (debounceTimeoutRef.current) {
72
+ clearTimeout(debounceTimeoutRef.current);
73
+ }
74
+
75
+ debounceTimeoutRef.current = setTimeout(() => {
76
+ if (pendingEventRef.current) {
77
+ executeRangeValidation(pendingEventRef.current);
78
+ }
79
+ }, 150);
80
+ },
81
+ [enabled]
82
+ );
83
+
84
+ /**
85
+ * Těžké validace a výpočty v debounced callbacku
86
+ */
87
+ const executeRangeValidation = useCallback(
88
+ (event) => {
89
+ const ranges = event.api.getCellRanges();
90
+ if (!ranges || ranges.length === 0) {
91
+ setFloatingButton({ visible: false });
92
+ return;
93
+ }
94
+
95
+ const range = ranges[0];
96
+
97
+ // Kontrola single column
98
+ if (!allowMultiColumn && range.columns.length !== 1) {
99
+ setFloatingButton({ visible: false });
100
+ return;
101
+ }
102
+
103
+ // Výpočet počtu buněk
104
+ const cellCount =
105
+ Math.abs(range.endRow.rowIndex - range.startRow.rowIndex) + 1;
106
+
107
+ // Kontrola min počtu buněk
108
+ if (cellCount < minCells) {
109
+ setFloatingButton({ visible: false });
110
+ return;
111
+ }
112
+
113
+ // Těžká validace - editable sloupce
114
+ if (!validateRangeEditable(range, event.api)) {
115
+ setFloatingButton({ visible: false });
116
+ return;
117
+ }
118
+
119
+ // Těžký výpočet - pozice buňky
120
+ const cellPosition = getLastCellPosition(range, event.api);
121
+ if (!cellPosition) {
122
+ setFloatingButton({ visible: false });
123
+ return;
124
+ }
125
+
126
+ let buttonX, buttonY;
127
+
128
+ switch (buttonPosition) {
129
+ case 'bottom-right':
130
+ buttonX = cellPosition.right + buttonOffset.x;
131
+ buttonY = cellPosition.bottom + buttonOffset.y;
132
+ break;
133
+ case 'bottom-left':
134
+ buttonX = cellPosition.left - buttonOffset.x;
135
+ buttonY = cellPosition.bottom + buttonOffset.y;
136
+ break;
137
+ case 'top-right':
138
+ buttonX = cellPosition.right + buttonOffset.x;
139
+ buttonY = cellPosition.top - buttonOffset.y;
140
+ break;
141
+ case 'top-left':
142
+ buttonX = cellPosition.left - buttonOffset.x;
143
+ buttonY = cellPosition.top - buttonOffset.y;
144
+ break;
145
+ default:
146
+ buttonX = cellPosition.right + buttonOffset.x;
147
+ buttonY = cellPosition.bottom + buttonOffset.y;
148
+ }
149
+
150
+ setFloatingButton({
151
+ visible: true,
152
+ position: { x: buttonX, y: buttonY },
153
+ range,
154
+ column: range.columns[0],
155
+ cellCount,
156
+ });
157
+ },
158
+ [minCells, allowMultiColumn, buttonPosition, buttonOffset]
159
+ );
160
+
161
+ /**
162
+ * Handler pro kliknutí na bulk edit button - otevře popover
163
+ */
164
+ const handleOpenPopover = useCallback(() => {
165
+ if (onBulkEditStart) {
166
+ onBulkEditStart({
167
+ range: floatingButton.range,
168
+ column: floatingButton.column,
169
+ cellCount: floatingButton.cellCount,
170
+ });
171
+ }
172
+
173
+ setEditPopover({
174
+ visible: true,
175
+ value: '',
176
+ loading: false,
177
+ error: null,
178
+ });
179
+ }, [floatingButton, onBulkEditStart]);
180
+
181
+ /**
182
+ * Handler pro submit bulk edit změn
183
+ */
184
+ const handleSubmitEdit = useCallback(
185
+ async (newValue) => {
186
+ if (!gridRef.current || !floatingButton.range) {
187
+ return;
188
+ }
189
+
190
+ setEditPopover((prev) => ({ ...prev, loading: true, error: null }));
191
+
192
+ try {
193
+ const colDef = floatingButton.column?.getColDef();
194
+ const bulkEditApi = colDef?.bulkEditApi;
195
+
196
+ let result;
197
+
198
+ // Pokud je definováno bulkEditApi, použij API volání
199
+ if (bulkEditApi?.endpoint) {
200
+ result = await applyBulkChangesWithApi(
201
+ floatingButton.range,
202
+ newValue,
203
+ gridRef.current.api,
204
+ bulkEditApi,
205
+ accessToken,
206
+ fetchDataUIAsync,
207
+ (result) => {
208
+ if (onBulkEditComplete) {
209
+ onBulkEditComplete({
210
+ ...result,
211
+ range: floatingButton.range,
212
+ column: floatingButton.column,
213
+ newValue,
214
+ });
215
+ }
216
+ }
217
+ );
218
+ } else {
219
+ // Pokud není API, použij lokální změnu
220
+ result = applyBulkChanges(
221
+ floatingButton.range,
222
+ newValue,
223
+ gridRef.current.api,
224
+ (result) => {
225
+ if (onBulkEditComplete) {
226
+ onBulkEditComplete({
227
+ ...result,
228
+ range: floatingButton.range,
229
+ column: floatingButton.column,
230
+ newValue,
231
+ });
232
+ }
233
+ }
234
+ );
235
+ }
236
+
237
+ if (result.success) {
238
+ // Úspěch - zavřít popover a skrýt button
239
+ setEditPopover({
240
+ visible: false,
241
+ value: '',
242
+ loading: false,
243
+ error: null,
244
+ });
245
+ setFloatingButton({ visible: false });
246
+ } else {
247
+ // Chyba - zobrazit error
248
+ setEditPopover((prev) => ({
249
+ ...prev,
250
+ loading: false,
251
+ error: result.error || result.errors?.[0] || 'Chyba při aplikaci změn',
252
+ }));
253
+ }
254
+ } catch (error) {
255
+ setEditPopover((prev) => ({
256
+ ...prev,
257
+ loading: false,
258
+ error: error?.message || 'Neznámá chyba',
259
+ }));
260
+ }
261
+ },
262
+ [gridRef, floatingButton, onBulkEditComplete, accessToken, fetchDataUIAsync]
263
+ );
264
+
265
+ /**
266
+ * Handler pro zrušení bulk edit
267
+ */
268
+ const handleCancelEdit = useCallback(() => {
269
+ setEditPopover({
270
+ visible: false,
271
+ value: '',
272
+ loading: false,
273
+ error: null,
274
+ });
275
+ setFloatingButton({ visible: false });
276
+ }, []);
277
+
278
+ /**
279
+ * Handler pro změnu hodnoty v popover inputu
280
+ */
281
+ const handleValueChange = useCallback((value) => {
282
+ setEditPopover((prev) => ({ ...prev, value, error: null }));
283
+ }, []);
284
+
285
+ /**
286
+ * Scroll listener - skrýt button při scrollování
287
+ */
288
+ useEffect(() => {
289
+ if (!enabled || !gridRef.current) return;
290
+
291
+ const gridElement = gridRef.current.api?.gridBodyCtrl?.eBodyViewport;
292
+ if (!gridElement) return;
293
+
294
+ const handleScroll = () => {
295
+ if (floatingButton.visible) {
296
+ setFloatingButton({ visible: false });
297
+ }
298
+ };
299
+
300
+ scrollListenerRef.current = handleScroll;
301
+ gridElement.addEventListener('scroll', handleScroll);
302
+
303
+ return () => {
304
+ if (gridElement && scrollListenerRef.current) {
305
+ gridElement.removeEventListener('scroll', scrollListenerRef.current);
306
+ }
307
+ };
308
+ }, [enabled, gridRef, floatingButton.visible]);
309
+
310
+ /**
311
+ * Window resize listener - přepočítat pozici
312
+ */
313
+ useEffect(() => {
314
+ if (!enabled || !floatingButton.visible) return;
315
+
316
+ const handleResize = () => {
317
+ // Při resize okna skryjeme button (user musí znovu označit)
318
+ setFloatingButton({ visible: false });
319
+ };
320
+
321
+ window.addEventListener('resize', handleResize);
322
+
323
+ return () => {
324
+ window.removeEventListener('resize', handleResize);
325
+ };
326
+ }, [enabled, floatingButton.visible]);
327
+
328
+ /**
329
+ * Cleanup timeout on unmount
330
+ */
331
+ useEffect(() => {
332
+ return () => {
333
+ if (debounceTimeoutRef.current) {
334
+ clearTimeout(debounceTimeoutRef.current);
335
+ }
336
+ };
337
+ }, []);
338
+
339
+ return {
340
+ floatingButton,
341
+ editPopover,
342
+ handleRangeChange,
343
+ handleOpenPopover,
344
+ handleSubmitEdit,
345
+ handleCancelEdit,
346
+ handleValueChange,
347
+ };
348
+ };