@ackplus/react-tanstack-data-table 1.1.11 → 1.1.12

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.
@@ -119,7 +119,7 @@ enableStickyHeaderOrFooter = false, maxHeight = '400px',
119
119
  // Virtualization props
120
120
  enableVirtualization = false, estimateRowHeight = 52,
121
121
  // Toolbar props
122
- enableTableSizeControl = true, enableExport = false, enableReset = true,
122
+ enableTableSizeControl = true, enableExport = false, enableReset = true, enableRefresh = false,
123
123
  // Loading and empty states
124
124
  loading = false, emptyMessage = 'No data available', skeletonRows = 5,
125
125
  // Column filters props
@@ -239,7 +239,7 @@ logging, }, ref) {
239
239
  // Callback hooks (grouped together)
240
240
  // -------------------------------
241
241
  const fetchData = (0, react_1.useCallback)(async (overrides = {}, options) => {
242
- var _a, _b;
242
+ var _a, _b, _c;
243
243
  if (!onFetchData) {
244
244
  if (logger.isLevelEnabled('debug')) {
245
245
  logger.debug('onFetchData not provided, skipping fetch', { overrides, columnFilter, sorting, pagination });
@@ -253,14 +253,16 @@ logging, }, ref) {
253
253
  sorting,
254
254
  ...overrides,
255
255
  };
256
+ console.log('Fetching data', filters);
256
257
  if (logger.isLevelEnabled('info')) {
257
258
  logger.info('Requesting data', { filters });
258
259
  }
259
260
  try {
260
- const result = await debouncedFetch(filters, options === undefined ? 0 : options.delay || 300);
261
+ const delay = (_a = options === null || options === void 0 ? void 0 : options.delay) !== null && _a !== void 0 ? _a : 300; // respects 0
262
+ const result = await debouncedFetch(filters, delay);
261
263
  if (logger.isLevelEnabled('info')) {
262
264
  logger.info('Fetch resolved', {
263
- rows: (_b = (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
265
+ rows: (_c = (_b = result === null || result === void 0 ? void 0 : result.data) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0,
264
266
  total: result === null || result === void 0 ? void 0 : result.total,
265
267
  });
266
268
  }
@@ -286,67 +288,20 @@ logging, }, ref) {
286
288
  debouncedFetch,
287
289
  logger,
288
290
  ]);
289
- const tableStateChange = (0, react_1.useCallback)((overrides = {}) => {
290
- if (!onDataStateChange) {
291
- if (logger.isLevelEnabled('debug')) {
292
- logger.debug('No onDataStateChange handler registered; skipping state update notification', { overrides });
293
- }
294
- return;
295
- }
296
- const currentState = {
297
- globalFilter,
298
- columnFilter,
299
- sorting,
300
- pagination,
301
- columnOrder,
302
- columnPinning,
303
- columnVisibility,
304
- columnSizing,
305
- ...overrides,
306
- };
307
- if (logger.isLevelEnabled('debug')) {
308
- logger.debug('Emitting tableStateChange', currentState);
309
- }
310
- onDataStateChange === null || onDataStateChange === void 0 ? void 0 : onDataStateChange(currentState);
311
- }, [
312
- onDataStateChange,
313
- globalFilter,
314
- columnFilter,
315
- sorting,
316
- pagination,
317
- columnOrder,
318
- columnPinning,
319
- columnVisibility,
320
- columnSizing,
321
- logger,
322
- ]);
323
291
  const handleSelectionStateChange = (0, react_1.useCallback)((updaterOrValue) => {
324
292
  setSelectionState((prevState) => {
325
- const newSelectionState = typeof updaterOrValue === 'function'
326
- ? updaterOrValue(prevState)
327
- : updaterOrValue;
328
- setTimeout(() => {
329
- if (onSelectionChange) {
330
- onSelectionChange(newSelectionState);
331
- }
332
- if (onDataStateChange) {
333
- tableStateChange({ selectionState: newSelectionState });
334
- }
335
- }, 0);
336
- return newSelectionState;
293
+ const next = typeof updaterOrValue === 'function' ? updaterOrValue(prevState) : updaterOrValue;
294
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(next);
295
+ return next;
337
296
  });
338
- }, [onSelectionChange, onDataStateChange, tableStateChange]);
297
+ }, [onSelectionChange]);
339
298
  const handleColumnFilterStateChange = (0, react_1.useCallback)((filterState) => {
340
299
  if (!filterState || typeof filterState !== 'object')
341
300
  return;
342
301
  setColumnFilter(filterState);
343
- if (onColumnFiltersChange) {
344
- setTimeout(() => onColumnFiltersChange(filterState), 0);
345
- }
346
- if (onDataStateChange) {
347
- setTimeout(() => tableStateChange({ columnFilter: filterState }), 0);
348
- }
349
- }, [onColumnFiltersChange, onDataStateChange, tableStateChange]);
302
+ onColumnFiltersChange === null || onColumnFiltersChange === void 0 ? void 0 : onColumnFiltersChange(filterState);
303
+ return filterState;
304
+ }, [onColumnFiltersChange]);
350
305
  const resetPageToFirst = (0, react_1.useCallback)(() => {
351
306
  if (logger.isLevelEnabled('info')) {
352
307
  logger.info('Resetting to first page due to state change', {
@@ -360,40 +315,17 @@ logging, }, ref) {
360
315
  return newPagination;
361
316
  }, [pagination, logger, onPaginationChange]);
362
317
  const handleSortingChange = (0, react_1.useCallback)((updaterOrValue) => {
363
- let newSorting = typeof updaterOrValue === 'function'
364
- ? updaterOrValue(sorting)
365
- : updaterOrValue;
366
- newSorting = newSorting.filter((sort) => sort.id);
367
- setSorting(newSorting);
368
- onSortingChange === null || onSortingChange === void 0 ? void 0 : onSortingChange(newSorting);
369
- if (logger.isLevelEnabled('debug')) {
370
- logger.debug('Sorting change applied', {
371
- sorting: newSorting,
372
- serverMode: isServerMode,
373
- serverSorting: isServerSorting,
374
- });
375
- }
376
- if (isServerMode || isServerSorting) {
377
- const pagination = resetPageToFirst();
378
- if (logger.isLevelEnabled('debug')) {
379
- logger.debug('Sorting change triggered server fetch', { pagination, sorting: newSorting });
318
+ setSorting((prev) => {
319
+ const next = typeof updaterOrValue === 'function' ? updaterOrValue(prev) : updaterOrValue;
320
+ const cleaned = next.filter((s) => s === null || s === void 0 ? void 0 : s.id);
321
+ onSortingChange === null || onSortingChange === void 0 ? void 0 : onSortingChange(cleaned);
322
+ const nextPagination = resetPageToFirst();
323
+ if (isServerMode || isServerSorting) {
324
+ fetchData({ sorting: cleaned, pagination: nextPagination }, { delay: 0 });
380
325
  }
381
- tableStateChange({ sorting: newSorting, pagination });
382
- fetchData({
383
- sorting: newSorting,
384
- pagination,
385
- });
386
- }
387
- else if (onDataStateChange) {
388
- const pagination = resetPageToFirst();
389
- setTimeout(() => {
390
- if (logger.isLevelEnabled('debug')) {
391
- logger.debug('Sorting change notified client state change', { pagination, sorting: newSorting });
392
- }
393
- tableStateChange({ sorting: newSorting, pagination });
394
- }, 0);
395
- }
396
- }, [sorting, onSortingChange, logger, isServerMode, isServerSorting, onDataStateChange, resetPageToFirst, tableStateChange, fetchData]);
326
+ return cleaned;
327
+ });
328
+ }, [onSortingChange, isServerMode, isServerSorting, resetPageToFirst, fetchData]);
397
329
  const handleColumnOrderChange = (0, react_1.useCallback)((updatedColumnOrder) => {
398
330
  const newColumnOrder = typeof updatedColumnOrder === 'function'
399
331
  ? updatedColumnOrder(columnOrder)
@@ -403,131 +335,52 @@ logging, }, ref) {
403
335
  onColumnDragEnd(newColumnOrder);
404
336
  }
405
337
  }, [onColumnDragEnd, columnOrder]);
406
- const handleColumnPinningChange = (0, react_1.useCallback)((updatedColumnPinning) => {
407
- const newColumnPinning = typeof updatedColumnPinning === 'function'
408
- ? updatedColumnPinning(columnPinning)
409
- : updatedColumnPinning;
410
- setColumnPinning(newColumnPinning);
411
- if (onColumnPinningChange) {
412
- onColumnPinningChange(newColumnPinning);
413
- }
414
- }, [onColumnPinningChange, columnPinning]);
338
+ const handleColumnPinningChange = (0, react_1.useCallback)((updater) => {
339
+ setColumnPinning((prev) => {
340
+ const next = typeof updater === "function" ? updater(prev) : updater;
341
+ // keep direct callback here (optional)
342
+ onColumnPinningChange === null || onColumnPinningChange === void 0 ? void 0 : onColumnPinningChange(next);
343
+ return next;
344
+ });
345
+ }, [onColumnPinningChange]);
415
346
  // Column visibility change handler - same pattern as column order
416
347
  const handleColumnVisibilityChange = (0, react_1.useCallback)((updater) => {
417
- const newVisibility = typeof updater === 'function'
418
- ? updater(columnVisibility)
419
- : updater;
420
- setColumnVisibility(newVisibility);
421
- if (onColumnVisibilityChange) {
422
- setTimeout(() => {
423
- onColumnVisibilityChange(newVisibility);
424
- }, 0);
425
- }
426
- if (onDataStateChange) {
427
- setTimeout(() => {
428
- tableStateChange({ columnVisibility: newVisibility });
429
- }, 0);
430
- }
431
- }, [onColumnVisibilityChange, onDataStateChange, tableStateChange, columnVisibility]);
348
+ setColumnVisibility((prev) => {
349
+ const next = typeof updater === 'function' ? updater(prev) : updater;
350
+ onColumnVisibilityChange === null || onColumnVisibilityChange === void 0 ? void 0 : onColumnVisibilityChange(next);
351
+ return next;
352
+ });
353
+ }, [onColumnVisibilityChange]);
432
354
  // Column sizing change handler - same pattern as column order
433
355
  const handleColumnSizingChange = (0, react_1.useCallback)((updater) => {
434
- const newSizing = typeof updater === 'function'
435
- ? updater(columnSizing)
436
- : updater;
437
- setColumnSizing(newSizing);
438
- if (onColumnSizingChange) {
439
- setTimeout(() => {
440
- onColumnSizingChange(newSizing);
441
- }, 0);
442
- }
443
- if (onDataStateChange) {
444
- setTimeout(() => {
445
- tableStateChange({ columnSizing: newSizing });
446
- }, 0);
447
- }
448
- }, [onColumnSizingChange, onDataStateChange, tableStateChange, columnSizing]);
356
+ setColumnSizing((prev) => {
357
+ const next = typeof updater === 'function' ? updater(prev) : updater;
358
+ onColumnSizingChange === null || onColumnSizingChange === void 0 ? void 0 : onColumnSizingChange(next);
359
+ return next;
360
+ });
361
+ }, [onColumnSizingChange]);
449
362
  const handlePaginationChange = (0, react_1.useCallback)((updater) => {
450
- const newPagination = typeof updater === 'function' ? updater(pagination) : updater;
451
- if (logger.isLevelEnabled('debug')) {
452
- logger.debug('Pagination change requested', {
453
- previous: pagination,
454
- next: newPagination,
455
- serverSide: isServerMode || isServerPagination,
456
- });
457
- }
458
- // Update pagination state
459
- setPagination(newPagination);
460
- onPaginationChange === null || onPaginationChange === void 0 ? void 0 : onPaginationChange(newPagination);
461
- if (logger.isLevelEnabled('debug')) {
462
- logger.debug('Pagination state updated', newPagination);
463
- }
464
- // Notify state change and fetch data if needed
465
- if (isServerMode || isServerPagination) {
466
- setTimeout(() => {
467
- if (logger.isLevelEnabled('debug')) {
468
- logger.debug('Notifying server-side pagination change', newPagination);
469
- }
470
- tableStateChange({ pagination: newPagination });
471
- fetchData({ pagination: newPagination });
472
- }, 0);
473
- }
474
- else if (onDataStateChange) {
475
- setTimeout(() => {
476
- if (logger.isLevelEnabled('debug')) {
477
- logger.debug('Notifying client-side pagination change', newPagination);
478
- }
479
- tableStateChange({ pagination: newPagination });
480
- }, 0);
481
- }
482
- }, [
483
- pagination,
484
- isServerMode,
485
- isServerPagination,
486
- onDataStateChange,
487
- fetchData,
488
- tableStateChange,
489
- logger,
490
- onPaginationChange,
491
- ]);
363
+ setPagination((prev) => {
364
+ const next = typeof updater === 'function' ? updater(prev) : updater;
365
+ onPaginationChange === null || onPaginationChange === void 0 ? void 0 : onPaginationChange(next);
366
+ if (isServerMode || isServerPagination) {
367
+ fetchData({ pagination: next }, { delay: 0 });
368
+ }
369
+ return next;
370
+ });
371
+ }, [isServerMode, isServerPagination, fetchData, onPaginationChange]);
492
372
  const handleGlobalFilterChange = (0, react_1.useCallback)((updaterOrValue) => {
493
- const newFilter = typeof updaterOrValue === 'function'
494
- ? updaterOrValue(globalFilter)
495
- : updaterOrValue;
496
- setGlobalFilter(newFilter);
497
- if (logger.isLevelEnabled('debug')) {
498
- logger.debug('Global filter change applied', {
499
- value: newFilter,
500
- serverMode: isServerMode,
501
- serverFiltering: isServerFiltering,
502
- });
503
- }
504
- if (isServerMode || isServerFiltering) {
505
- const pagination = resetPageToFirst();
506
- setTimeout(() => {
507
- if (logger.isLevelEnabled('debug')) {
508
- logger.debug('Global filter change triggering server fetch', {
509
- pagination,
510
- value: newFilter,
511
- });
512
- }
513
- tableStateChange({ globalFilter: newFilter, pagination });
514
- fetchData({ globalFilter: newFilter, pagination });
515
- }, 0);
516
- }
517
- else if (onDataStateChange) {
518
- const pagination = resetPageToFirst();
519
- setTimeout(() => {
520
- if (logger.isLevelEnabled('debug')) {
521
- logger.debug('Global filter change notifying client listeners', {
522
- pagination,
523
- value: newFilter,
524
- });
525
- }
526
- tableStateChange({ globalFilter: newFilter, pagination });
527
- }, 0);
528
- }
529
- onGlobalFilterChange === null || onGlobalFilterChange === void 0 ? void 0 : onGlobalFilterChange(newFilter);
530
- }, [globalFilter, logger, isServerMode, isServerFiltering, onDataStateChange, onGlobalFilterChange, resetPageToFirst, tableStateChange, fetchData]);
373
+ setGlobalFilter((prev) => {
374
+ const next = typeof updaterOrValue === 'function' ? updaterOrValue(prev) : updaterOrValue;
375
+ onGlobalFilterChange === null || onGlobalFilterChange === void 0 ? void 0 : onGlobalFilterChange(next);
376
+ if (isServerMode || isServerFiltering) {
377
+ const nextPagination = { pageIndex: 0, pageSize: pagination.pageSize };
378
+ setPagination(nextPagination);
379
+ fetchData({ globalFilter: next, pagination: nextPagination }, { delay: 0 });
380
+ }
381
+ return next;
382
+ });
383
+ }, [isServerMode, isServerFiltering, onGlobalFilterChange, fetchData, pagination.pageSize]);
531
384
  const onColumnFilterChangeHandler = (0, react_1.useCallback)((updater) => {
532
385
  const currentState = columnFilter;
533
386
  const newState = typeof updater === 'function'
@@ -544,22 +397,13 @@ logging, }, ref) {
544
397
  const onColumnFilterApplyHandler = (0, react_1.useCallback)((appliedState) => {
545
398
  const pagination = resetPageToFirst();
546
399
  if (isServerFiltering) {
547
- tableStateChange({
548
- columnFilter: appliedState,
549
- pagination,
550
- });
551
400
  fetchData({
552
401
  columnFilter: appliedState,
553
402
  pagination,
554
403
  });
555
404
  }
556
- else if (onDataStateChange) {
557
- setTimeout(() => tableStateChange({ columnFilter: appliedState, pagination }), 0);
558
- }
559
- setTimeout(() => {
560
- onColumnFiltersChange === null || onColumnFiltersChange === void 0 ? void 0 : onColumnFiltersChange(appliedState);
561
- }, 0);
562
- }, [resetPageToFirst, isServerFiltering, onDataStateChange, tableStateChange, fetchData, onColumnFiltersChange]);
405
+ onColumnFiltersChange === null || onColumnFiltersChange === void 0 ? void 0 : onColumnFiltersChange(appliedState);
406
+ }, [resetPageToFirst, isServerFiltering, fetchData, onColumnFiltersChange]);
563
407
  // -------------------------------
564
408
  // Table creation (after callbacks/memo)
565
409
  // -------------------------------
@@ -710,502 +554,526 @@ logging, }, ref) {
710
554
  setColumnOrder(initialOrder);
711
555
  }
712
556
  }, [enableColumnDragging, enhancedColumns, columnOrder.length]);
713
- const dataTableApi = (0, react_1.useMemo)(() => ({
714
- table: {
715
- getTable: () => table,
716
- },
717
- // Column Management
718
- columnVisibility: {
719
- showColumn: (columnId) => {
720
- var _a;
721
- (_a = table.getColumn(columnId)) === null || _a === void 0 ? void 0 : _a.toggleVisibility(true);
722
- },
723
- hideColumn: (columnId) => {
724
- var _a;
725
- (_a = table.getColumn(columnId)) === null || _a === void 0 ? void 0 : _a.toggleVisibility(false);
726
- },
727
- toggleColumn: (columnId) => {
728
- var _a;
729
- (_a = table.getColumn(columnId)) === null || _a === void 0 ? void 0 : _a.toggleVisibility();
730
- },
731
- showAllColumns: () => {
732
- table.toggleAllColumnsVisible(true);
733
- },
734
- hideAllColumns: () => {
735
- table.toggleAllColumnsVisible(false);
736
- },
737
- resetColumnVisibility: () => {
738
- const initialVisibility = initialStateConfig.columnVisibility || {};
739
- table.setColumnVisibility(initialVisibility);
740
- // Manually trigger handler to ensure callbacks are called
741
- handleColumnVisibilityChange(initialVisibility);
742
- },
743
- },
744
- // Column Ordering
745
- columnOrdering: {
746
- setColumnOrder: (columnOrder) => {
747
- table.setColumnOrder(columnOrder);
748
- },
749
- moveColumn: (columnId, toIndex) => {
750
- const currentOrder = table.getState().columnOrder || [];
751
- const currentIndex = currentOrder.indexOf(columnId);
752
- if (currentIndex === -1)
753
- return;
754
- const newOrder = [...currentOrder];
755
- newOrder.splice(currentIndex, 1);
756
- newOrder.splice(toIndex, 0, columnId);
757
- table.setColumnOrder(newOrder);
758
- },
759
- resetColumnOrder: () => {
760
- const initialOrder = enhancedColumns.map((col, index) => {
761
- if (col.id)
762
- return col.id;
763
- const anyCol = col;
764
- if (anyCol.accessorKey && typeof anyCol.accessorKey === 'string') {
765
- return anyCol.accessorKey;
557
+ const lastSentRef = (0, react_1.useRef)("");
558
+ const emitTableState = (0, react_1.useCallback)(() => {
559
+ if (!onDataStateChange)
560
+ return;
561
+ const live = table.getState();
562
+ // only keep what you persist/store
563
+ const payload = {
564
+ sorting: live.sorting,
565
+ pagination: live.pagination,
566
+ globalFilter: live.globalFilter,
567
+ columnFilter: live.columnFilter,
568
+ columnVisibility: live.columnVisibility,
569
+ columnSizing: live.columnSizing,
570
+ columnOrder: live.columnOrder,
571
+ columnPinning: live.columnPinning,
572
+ };
573
+ const key = JSON.stringify(payload);
574
+ if (key === lastSentRef.current)
575
+ return;
576
+ lastSentRef.current = key;
577
+ onDataStateChange(payload);
578
+ }, [onDataStateChange, table]);
579
+ (0, react_1.useEffect)(() => {
580
+ emitTableState();
581
+ }, [
582
+ emitTableState,
583
+ sorting,
584
+ pagination,
585
+ globalFilter,
586
+ columnFilter,
587
+ columnVisibility,
588
+ columnSizing,
589
+ columnOrder,
590
+ columnPinning,
591
+ ]);
592
+ const getResetState = (0, react_1.useCallback)(() => {
593
+ var _a;
594
+ const resetSorting = initialStateConfig.sorting || [];
595
+ const resetGlobalFilter = (_a = initialStateConfig.globalFilter) !== null && _a !== void 0 ? _a : '';
596
+ const resetColumnFilter = initialStateConfig.columnFilter || { filters: [], logic: 'AND', pendingFilters: [], pendingLogic: 'AND' };
597
+ const resetPagination = enablePagination
598
+ ? (initialStateConfig.pagination || { pageIndex: 0, pageSize: 10 })
599
+ : undefined;
600
+ return {
601
+ sorting: resetSorting,
602
+ globalFilter: resetGlobalFilter,
603
+ columnFilter: resetColumnFilter,
604
+ ...(resetPagination ? { pagination: resetPagination } : {}),
605
+ };
606
+ }, [initialStateConfig, enablePagination]);
607
+ const resetAllAndReload = (0, react_1.useCallback)(() => {
608
+ var _a;
609
+ const resetState = getResetState();
610
+ setSorting(resetState.sorting || []);
611
+ setGlobalFilter((_a = resetState.globalFilter) !== null && _a !== void 0 ? _a : '');
612
+ setColumnFilter(resetState.columnFilter);
613
+ if (resetState.pagination)
614
+ setPagination(resetState.pagination);
615
+ setSelectionState(initialSelectionState);
616
+ setExpanded({});
617
+ // layout state
618
+ setColumnVisibility(initialStateConfig.columnVisibility || {});
619
+ setColumnSizing(initialStateConfig.columnSizing || {});
620
+ setColumnOrder(initialStateConfig.columnOrder || []);
621
+ setColumnPinning(initialStateConfig.columnPinning || { left: [], right: [] });
622
+ if (onFetchData)
623
+ fetchData(resetState, { delay: 0 });
624
+ }, [getResetState, initialSelectionState, initialStateConfig, onFetchData, fetchData]);
625
+ const dataTableApi = (0, react_1.useMemo)(() => {
626
+ // helpers (avoid repeating boilerplate)
627
+ const buildInitialOrder = () => enhancedColumns.map((col, index) => {
628
+ if (col.id)
629
+ return col.id;
630
+ const anyCol = col;
631
+ if (anyCol.accessorKey && typeof anyCol.accessorKey === "string")
632
+ return anyCol.accessorKey;
633
+ return `column_${index}`;
634
+ });
635
+ const applyColumnOrder = (next) => {
636
+ // handleColumnOrderChange supports both Updater<ColumnOrderState> and array in your impl
637
+ handleColumnOrderChange(next);
638
+ };
639
+ const applyPinning = (next) => {
640
+ handleColumnPinningChange(next);
641
+ };
642
+ const applyVisibility = (next) => {
643
+ handleColumnVisibilityChange(next);
644
+ };
645
+ const applySizing = (next) => {
646
+ handleColumnSizingChange(next);
647
+ };
648
+ const applyPagination = (next) => {
649
+ handlePaginationChange(next);
650
+ };
651
+ const applySorting = (next) => {
652
+ handleSortingChange(next);
653
+ };
654
+ const applyGlobalFilter = (next) => {
655
+ handleGlobalFilterChange(next);
656
+ };
657
+ return {
658
+ table: {
659
+ getTable: () => table,
660
+ },
661
+ // -------------------------------
662
+ // Column Management
663
+ // -------------------------------
664
+ columnVisibility: {
665
+ showColumn: (columnId) => {
666
+ applyVisibility({ ...table.getState().columnVisibility, [columnId]: true });
667
+ },
668
+ hideColumn: (columnId) => {
669
+ applyVisibility({ ...table.getState().columnVisibility, [columnId]: false });
670
+ },
671
+ toggleColumn: (columnId) => {
672
+ var _a, _b;
673
+ const curr = (_b = (_a = table.getState().columnVisibility) === null || _a === void 0 ? void 0 : _a[columnId]) !== null && _b !== void 0 ? _b : true;
674
+ applyVisibility({ ...table.getState().columnVisibility, [columnId]: !curr });
675
+ },
676
+ showAllColumns: () => {
677
+ // set all known columns true
678
+ const all = {};
679
+ table.getAllLeafColumns().forEach((c) => (all[c.id] = true));
680
+ applyVisibility(all);
681
+ },
682
+ hideAllColumns: () => {
683
+ const all = {};
684
+ table.getAllLeafColumns().forEach((c) => (all[c.id] = false));
685
+ applyVisibility(all);
686
+ },
687
+ resetColumnVisibility: () => {
688
+ const initialVisibility = initialStateConfig.columnVisibility || {};
689
+ applyVisibility(initialVisibility);
690
+ },
691
+ },
692
+ // -------------------------------
693
+ // Column Ordering
694
+ // -------------------------------
695
+ columnOrdering: {
696
+ setColumnOrder: (nextOrder) => {
697
+ applyColumnOrder(nextOrder);
698
+ },
699
+ moveColumn: (columnId, toIndex) => {
700
+ var _a;
701
+ const currentOrder = (((_a = table.getState().columnOrder) === null || _a === void 0 ? void 0 : _a.length) ? table.getState().columnOrder : buildInitialOrder()) || [];
702
+ const fromIndex = currentOrder.indexOf(columnId);
703
+ if (fromIndex === -1)
704
+ return;
705
+ const next = [...currentOrder];
706
+ next.splice(fromIndex, 1);
707
+ next.splice(toIndex, 0, columnId);
708
+ applyColumnOrder(next);
709
+ },
710
+ resetColumnOrder: () => {
711
+ applyColumnOrder(buildInitialOrder());
712
+ },
713
+ },
714
+ // -------------------------------
715
+ // Column Pinning
716
+ // -------------------------------
717
+ columnPinning: {
718
+ pinColumnLeft: (columnId) => {
719
+ const current = table.getState().columnPinning || { left: [], right: [] };
720
+ const next = {
721
+ left: [...(current.left || []).filter((id) => id !== columnId), columnId],
722
+ right: (current.right || []).filter((id) => id !== columnId),
723
+ };
724
+ applyPinning(next);
725
+ },
726
+ pinColumnRight: (columnId) => {
727
+ const current = table.getState().columnPinning || { left: [], right: [] };
728
+ const next = {
729
+ left: (current.left || []).filter((id) => id !== columnId),
730
+ // keep your "prepend" behavior
731
+ right: [columnId, ...(current.right || []).filter((id) => id !== columnId)],
732
+ };
733
+ applyPinning(next);
734
+ },
735
+ unpinColumn: (columnId) => {
736
+ const current = table.getState().columnPinning || { left: [], right: [] };
737
+ const next = {
738
+ left: (current.left || []).filter((id) => id !== columnId),
739
+ right: (current.right || []).filter((id) => id !== columnId),
740
+ };
741
+ applyPinning(next);
742
+ },
743
+ setPinning: (pinning) => {
744
+ applyPinning(pinning);
745
+ },
746
+ resetColumnPinning: () => {
747
+ const initialPinning = initialStateConfig.columnPinning || { left: [], right: [] };
748
+ applyPinning(initialPinning);
749
+ },
750
+ },
751
+ // -------------------------------
752
+ // Column Resizing
753
+ // -------------------------------
754
+ columnResizing: {
755
+ resizeColumn: (columnId, width) => {
756
+ const currentSizing = table.getState().columnSizing || {};
757
+ applySizing({ ...currentSizing, [columnId]: width });
758
+ },
759
+ autoSizeColumn: (columnId) => {
760
+ // safe to call tanstack helper; it will feed into onColumnSizingChange if wired,
761
+ // but since you're controlled, we still prefer to update through handler:
762
+ const col = table.getColumn(columnId);
763
+ if (!col)
764
+ return;
765
+ col.resetSize();
766
+ // after resetSize, read state and emit via handler so controlled stays synced
767
+ applySizing({ ...(table.getState().columnSizing || {}) });
768
+ },
769
+ autoSizeAllColumns: () => {
770
+ const initialSizing = initialStateConfig.columnSizing || {};
771
+ applySizing(initialSizing);
772
+ },
773
+ resetColumnSizing: () => {
774
+ const initialSizing = initialStateConfig.columnSizing || {};
775
+ applySizing(initialSizing);
776
+ },
777
+ },
778
+ // -------------------------------
779
+ // Filtering
780
+ // -------------------------------
781
+ filtering: {
782
+ setGlobalFilter: (filter) => {
783
+ applyGlobalFilter(filter);
784
+ },
785
+ clearGlobalFilter: () => {
786
+ applyGlobalFilter("");
787
+ },
788
+ setColumnFilters: (filters) => {
789
+ handleColumnFilterStateChange(filters);
790
+ },
791
+ addColumnFilter: (columnId, operator, value) => {
792
+ const newFilter = {
793
+ id: `filter_${Date.now()}`,
794
+ columnId,
795
+ operator,
796
+ value,
797
+ };
798
+ const current = table.getState().columnFilter;
799
+ const currentFilters = (current === null || current === void 0 ? void 0 : current.filters) || [];
800
+ const nextFilters = [...currentFilters, newFilter];
801
+ handleColumnFilterStateChange({
802
+ filters: nextFilters,
803
+ logic: current === null || current === void 0 ? void 0 : current.logic,
804
+ pendingFilters: (current === null || current === void 0 ? void 0 : current.pendingFilters) || [],
805
+ pendingLogic: (current === null || current === void 0 ? void 0 : current.pendingLogic) || "AND",
806
+ });
807
+ if (logger.isLevelEnabled("debug")) {
808
+ logger.debug(`Adding column filter ${columnId} ${operator} ${value}`, nextFilters);
766
809
  }
767
- return `column_${index}`;
768
- });
769
- table.setColumnOrder(initialOrder);
770
- // Manually trigger handler to ensure callbacks are called
771
- handleColumnOrderChange(initialOrder);
772
- },
773
- },
774
- // Column Pinning
775
- columnPinning: {
776
- pinColumnLeft: (columnId) => {
777
- const currentPinning = table.getState().columnPinning;
778
- const newPinning = { ...currentPinning };
779
- // Remove from right if exists
780
- newPinning.right = (newPinning.right || []).filter(id => id !== columnId);
781
- // Add to left if not exists
782
- newPinning.left = [...(newPinning.left || []).filter(id => id !== columnId), columnId];
783
- table.setColumnPinning(newPinning);
784
- },
785
- pinColumnRight: (columnId) => {
786
- const currentPinning = table.getState().columnPinning;
787
- const newPinning = { ...currentPinning };
788
- // Remove from left if exists
789
- newPinning.left = (newPinning.left || []).filter(id => id !== columnId);
790
- // Add to right if not exists - prepend to beginning (appears rightmost to leftmost)
791
- // First column pinned appears rightmost, second appears to its left, etc.
792
- newPinning.right = [columnId, ...(newPinning.right || []).filter(id => id !== columnId)];
793
- table.setColumnPinning(newPinning);
794
- },
795
- unpinColumn: (columnId) => {
796
- const currentPinning = table.getState().columnPinning;
797
- const newPinning = {
798
- left: (currentPinning.left || []).filter(id => id !== columnId),
799
- right: (currentPinning.right || []).filter(id => id !== columnId),
800
- };
801
- table.setColumnPinning(newPinning);
802
- },
803
- setPinning: (pinning) => {
804
- table.setColumnPinning(pinning);
805
- },
806
- resetColumnPinning: () => {
807
- const initialPinning = initialStateConfig.columnPinning || { left: [], right: [] };
808
- table.setColumnPinning(initialPinning);
809
- // Manually trigger handler to ensure callbacks are called
810
- handleColumnPinningChange(initialPinning);
811
- },
812
- },
813
- // Column Resizing
814
- columnResizing: {
815
- resizeColumn: (columnId, width) => {
816
- // Use table's setColumnSizing method
817
- const currentSizing = table.getState().columnSizing;
818
- table.setColumnSizing({
819
- ...currentSizing,
820
- [columnId]: width,
821
- });
822
- },
823
- autoSizeColumn: (columnId) => {
824
- var _a;
825
- // TanStack doesn't have built-in auto-size, so reset to default
826
- (_a = table.getColumn(columnId)) === null || _a === void 0 ? void 0 : _a.resetSize();
827
- },
828
- autoSizeAllColumns: () => {
829
- const initialSizing = initialStateConfig.columnSizing || {};
830
- table.setColumnSizing(initialSizing);
831
- // Manually trigger handler to ensure callbacks are called
832
- handleColumnSizingChange(initialSizing);
833
- },
834
- resetColumnSizing: () => {
835
- const initialSizing = initialStateConfig.columnSizing || {};
836
- table.setColumnSizing(initialSizing);
837
- // Manually trigger handler to ensure callbacks are called
838
- handleColumnSizingChange(initialSizing);
839
- },
840
- },
841
- // Filtering
842
- filtering: {
843
- setGlobalFilter: (filter) => {
844
- table.setGlobalFilter(filter);
845
- },
846
- clearGlobalFilter: () => {
847
- table.setGlobalFilter('');
848
- },
849
- setColumnFilters: (filters) => {
850
- handleColumnFilterStateChange(filters);
851
- },
852
- addColumnFilter: (columnId, operator, value) => {
853
- const newFilter = {
854
- id: `filter_${Date.now()}`,
855
- columnId,
856
- operator,
857
- value,
858
- };
859
- const columnFilter = table.getState().columnFilter;
860
- const currentFilters = columnFilter.filters || [];
861
- const newFilters = [...currentFilters, newFilter];
862
- handleColumnFilterStateChange({
863
- filters: newFilters,
864
- logic: columnFilter.logic,
865
- pendingFilters: columnFilter.pendingFilters || [],
866
- pendingLogic: columnFilter.pendingLogic || 'AND',
867
- });
868
- if (logger.isLevelEnabled('debug')) {
869
- logger.debug(`Adding column filter ${columnId} ${operator} ${value}`, newFilters);
870
- }
871
- },
872
- removeColumnFilter: (filterId) => {
873
- const columnFilter = table.getState().columnFilter;
874
- const currentFilters = columnFilter.filters || [];
875
- const newFilters = currentFilters.filter((f) => f.id !== filterId);
876
- handleColumnFilterStateChange({
877
- filters: newFilters,
878
- logic: columnFilter.logic,
879
- pendingFilters: columnFilter.pendingFilters || [],
880
- pendingLogic: columnFilter.pendingLogic || 'AND',
881
- });
882
- if (logger.isLevelEnabled('debug')) {
883
- logger.debug(`Removing column filter ${filterId}`, newFilters);
884
- }
885
- },
886
- clearAllFilters: () => {
887
- table.setGlobalFilter('');
888
- handleColumnFilterStateChange({
889
- filters: [],
890
- logic: 'AND',
891
- pendingFilters: [],
892
- pendingLogic: 'AND',
893
- });
894
- },
895
- resetFilters: () => {
896
- handleColumnFilterStateChange({
897
- filters: [],
898
- logic: 'AND',
899
- pendingFilters: [],
900
- pendingLogic: 'AND',
901
- });
902
- if (logger.isLevelEnabled('debug')) {
903
- logger.debug('Resetting filters');
904
- }
905
- },
906
- },
907
- // Sorting
908
- sorting: {
909
- setSorting: (sortingState) => {
910
- table.setSorting(sortingState);
911
- if (logger.isLevelEnabled('debug')) {
912
- logger.debug(`Setting sorting`, sortingState);
913
- }
914
- },
915
- sortColumn: (columnId, direction) => {
916
- const column = table.getColumn(columnId);
917
- if (!column)
918
- return;
919
- if (direction === false) {
920
- column.clearSorting();
921
- }
922
- else {
923
- column.toggleSorting(direction === 'desc');
924
- }
925
- },
926
- clearSorting: () => {
927
- table.setSorting([]);
928
- // Manually trigger handler to ensure callbacks are called
929
- handleSortingChange([]);
930
- },
931
- resetSorting: () => {
932
- const initialSorting = initialStateConfig.sorting || [];
933
- table.setSorting(initialSorting);
934
- // Manually trigger handler to ensure callbacks are called
935
- handleSortingChange(initialSorting);
936
- },
937
- },
938
- // Pagination
939
- pagination: {
940
- goToPage: (pageIndex) => {
941
- table.setPageIndex(pageIndex);
942
- if (logger.isLevelEnabled('debug')) {
943
- logger.debug(`Going to page ${pageIndex}`);
944
- }
945
- },
946
- nextPage: () => {
947
- table.nextPage();
948
- if (logger.isLevelEnabled('debug')) {
949
- logger.debug('Next page');
950
- }
951
- },
952
- previousPage: () => {
953
- table.previousPage();
954
- if (logger.isLevelEnabled('debug')) {
955
- logger.debug('Previous page');
956
- }
957
- },
958
- setPageSize: (pageSize) => {
959
- table.setPageSize(pageSize);
960
- if (logger.isLevelEnabled('debug')) {
961
- logger.debug(`Setting page size to ${pageSize}`);
962
- }
963
- },
964
- goToFirstPage: () => {
965
- table.setPageIndex(0);
966
- if (logger.isLevelEnabled('debug')) {
967
- logger.debug('Going to first page');
968
- }
969
- },
970
- goToLastPage: () => {
971
- const pageCount = table.getPageCount();
972
- if (pageCount > 0) {
973
- table.setPageIndex(pageCount - 1);
974
- if (logger.isLevelEnabled('debug')) {
975
- logger.debug(`Going to last page ${pageCount - 1}`);
810
+ },
811
+ removeColumnFilter: (filterId) => {
812
+ const current = table.getState().columnFilter;
813
+ const currentFilters = (current === null || current === void 0 ? void 0 : current.filters) || [];
814
+ const nextFilters = currentFilters.filter((f) => f.id !== filterId);
815
+ handleColumnFilterStateChange({
816
+ filters: nextFilters,
817
+ logic: current === null || current === void 0 ? void 0 : current.logic,
818
+ pendingFilters: (current === null || current === void 0 ? void 0 : current.pendingFilters) || [],
819
+ pendingLogic: (current === null || current === void 0 ? void 0 : current.pendingLogic) || "AND",
820
+ });
821
+ if (logger.isLevelEnabled("debug")) {
822
+ logger.debug(`Removing column filter ${filterId}`, nextFilters);
976
823
  }
977
- }
978
- },
979
- resetPagination: () => {
980
- const initialPagination = initialStateConfig.pagination || { pageIndex: 0, pageSize: 10 };
981
- table.setPagination(initialPagination);
982
- // Manually trigger handler to ensure callbacks are called
983
- handlePaginationChange(initialPagination);
984
- },
985
- },
986
- // Access via table methods: table.selectRow(), table.getIsRowSelected(), etc.
987
- selection: {
988
- selectRow: (rowId) => { var _a; return (_a = table.selectRow) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
989
- deselectRow: (rowId) => { var _a; return (_a = table.deselectRow) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
990
- toggleRowSelection: (rowId) => { var _a; return (_a = table.toggleRowSelected) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
991
- selectAll: () => { var _a; return (_a = table.selectAll) === null || _a === void 0 ? void 0 : _a.call(table); },
992
- deselectAll: () => { var _a; return (_a = table.deselectAll) === null || _a === void 0 ? void 0 : _a.call(table); },
993
- toggleSelectAll: () => { var _a; return (_a = table.toggleAllRowsSelected) === null || _a === void 0 ? void 0 : _a.call(table); },
994
- getSelectionState: () => { var _a; return ((_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table)) || { ids: [], type: 'include' }; },
995
- getSelectedRows: () => table.getSelectedRows(),
996
- getSelectedCount: () => table.getSelectedCount(),
997
- isRowSelected: (rowId) => table.getIsRowSelected(rowId) || false,
998
- },
999
- // Data Management
1000
- data: {
1001
- refresh: (resetPagination = false) => {
1002
- var _a, _b, _c;
1003
- const filters = table.getState();
1004
- const pagination = {
1005
- pageIndex: resetPagination ? 0 : ((_a = initialStateConfig.pagination) === null || _a === void 0 ? void 0 : _a.pageIndex) || 0,
1006
- pageSize: ((_b = filters.pagination) === null || _b === void 0 ? void 0 : _b.pageSize) || ((_c = initialStateConfig.pagination) === null || _c === void 0 ? void 0 : _c.pageSize) || 10,
1007
- };
1008
- const allState = table.getState();
1009
- setPagination(pagination);
1010
- onDataStateChange === null || onDataStateChange === void 0 ? void 0 : onDataStateChange({ ...allState, pagination });
1011
- fetchData === null || fetchData === void 0 ? void 0 : fetchData({ pagination });
1012
- if (logger.isLevelEnabled('debug')) {
1013
- logger.debug('Refreshing data using Ref', { pagination, allState });
1014
- }
1015
- },
1016
- reload: () => {
1017
- const allState = table.getState();
1018
- onDataStateChange === null || onDataStateChange === void 0 ? void 0 : onDataStateChange(allState);
1019
- fetchData === null || fetchData === void 0 ? void 0 : fetchData({});
1020
- if (logger.isLevelEnabled('debug')) {
1021
- logger.info('Reloading data', allState);
1022
- }
1023
- },
1024
- // Data CRUD operations
1025
- getAllData: () => {
1026
- var _a;
1027
- return ((_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map(row => row.original)) || [];
1028
- },
1029
- getRowData: (rowId) => {
1030
- var _a, _b;
1031
- return (_b = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.find(row => String(row.original[idKey]) === rowId)) === null || _b === void 0 ? void 0 : _b.original;
1032
- },
1033
- getRowByIndex: (index) => {
1034
- var _a, _b;
1035
- return (_b = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b.original;
1036
- },
1037
- updateRow: (rowId, updates) => {
1038
- var _a;
1039
- const newData = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map(row => String(row.original[idKey]) === rowId
1040
- ? {
1041
- ...row.original,
1042
- ...updates,
824
+ },
825
+ clearAllFilters: () => {
826
+ applyGlobalFilter("");
827
+ handleColumnFilterStateChange({
828
+ filters: [],
829
+ logic: "AND",
830
+ pendingFilters: [],
831
+ pendingLogic: "AND",
832
+ });
833
+ },
834
+ resetFilters: () => {
835
+ handleColumnFilterStateChange({
836
+ filters: [],
837
+ logic: "AND",
838
+ pendingFilters: [],
839
+ pendingLogic: "AND",
840
+ });
841
+ if (logger.isLevelEnabled("debug")) {
842
+ logger.debug("Resetting filters");
1043
843
  }
1044
- : row.original);
1045
- setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData || []);
1046
- if (logger.isLevelEnabled('debug')) {
1047
- logger.debug(`Updating row ${rowId}`, updates);
1048
- }
1049
- },
1050
- updateRowByIndex: (index, updates) => {
1051
- var _a;
1052
- const newData = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map(row => row.original);
1053
- if (newData === null || newData === void 0 ? void 0 : newData[index]) {
1054
- newData[index] = {
1055
- ...newData[index],
1056
- ...updates,
1057
- };
1058
- setServerData(newData);
1059
- if (logger.isLevelEnabled('debug')) {
1060
- logger.debug(`Updating row by index ${index}`, updates);
844
+ },
845
+ },
846
+ // -------------------------------
847
+ // Sorting
848
+ // -------------------------------
849
+ sorting: {
850
+ setSorting: (sortingState) => {
851
+ applySorting(sortingState);
852
+ if (logger.isLevelEnabled("debug"))
853
+ logger.debug("Setting sorting", sortingState);
854
+ },
855
+ // NOTE: toggleSorting is okay, but can become "one behind" in controlled server mode.
856
+ // So we implement deterministic sorting through handler.
857
+ sortColumn: (columnId, direction) => {
858
+ const current = table.getState().sorting || [];
859
+ const filtered = current.filter((s) => s.id !== columnId);
860
+ if (direction === false) {
861
+ applySorting(filtered);
862
+ return;
1061
863
  }
1062
- }
1063
- },
1064
- insertRow: (newRow, index) => {
1065
- var _a;
1066
- const newData = ((_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map(row => row.original)) || [];
1067
- if (index !== undefined) {
1068
- newData.splice(index, 0, newRow);
1069
- }
1070
- else {
1071
- newData.push(newRow);
1072
- }
1073
- setServerData(newData || []);
1074
- if (logger.isLevelEnabled('debug')) {
1075
- logger.debug(`Inserting row`, newRow);
1076
- }
1077
- },
1078
- deleteRow: (rowId) => {
1079
- var _a;
1080
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.filter(row => String(row.original[idKey]) !== rowId);
1081
- setServerData === null || setServerData === void 0 ? void 0 : setServerData((newData === null || newData === void 0 ? void 0 : newData.map(row => row.original)) || []);
1082
- if (logger.isLevelEnabled('debug')) {
1083
- logger.debug(`Deleting row ${rowId}`);
1084
- }
1085
- },
1086
- deleteRowByIndex: (index) => {
1087
- var _a;
1088
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.map(row => row.original);
1089
- newData.splice(index, 1);
1090
- setServerData(newData);
1091
- if (logger.isLevelEnabled('debug')) {
1092
- logger.debug(`Deleting row by index ${index}`);
1093
- }
1094
- },
1095
- deleteSelectedRows: () => {
1096
- var _a, _b, _c;
1097
- const selectedRows = ((_a = table.getSelectedRows) === null || _a === void 0 ? void 0 : _a.call(table)) || [];
1098
- if (selectedRows.length === 0)
1099
- return;
1100
- const selectedIds = new Set(selectedRows.map(row => String(row.original[idKey])));
1101
- const newData = (_b = (table.getRowModel().rows || [])) === null || _b === void 0 ? void 0 : _b.filter(row => !selectedIds.has(String(row.original[idKey])));
1102
- setServerData((newData === null || newData === void 0 ? void 0 : newData.map(row => row.original)) || []);
1103
- (_c = table.deselectAll) === null || _c === void 0 ? void 0 : _c.call(table);
1104
- if (logger.isLevelEnabled('debug')) {
1105
- logger.debug('Deleting selected rows');
1106
- }
1107
- },
1108
- replaceAllData: (newData) => {
1109
- setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1110
- },
1111
- // Bulk operations
1112
- updateMultipleRows: (updates) => {
1113
- var _a;
1114
- const updateMap = new Map(updates.map(u => [u.rowId, u.data]));
1115
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.map(row => {
1116
- const rowId = String(row.original[idKey]);
1117
- const updateData = updateMap.get(rowId);
1118
- return updateData ? {
1119
- ...row.original,
1120
- ...updateData,
1121
- } : row.original;
1122
- });
1123
- setServerData(newData || []);
1124
- },
1125
- insertMultipleRows: (newRows, startIndex) => {
1126
- var _a;
1127
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.map(row => row.original);
1128
- if (startIndex !== undefined) {
1129
- newData.splice(startIndex, 0, ...newRows);
1130
- }
1131
- else {
1132
- newData.push(...newRows);
1133
- }
1134
- setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1135
- },
1136
- deleteMultipleRows: (rowIds) => {
1137
- var _a, _b;
1138
- const idsToDelete = new Set(rowIds);
1139
- const newData = (_b = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.filter(row => !idsToDelete.has(String(row.original[idKey])))) === null || _b === void 0 ? void 0 : _b.map(row => row.original);
1140
- setServerData(newData);
1141
- },
1142
- // Field-specific updates
1143
- updateField: (rowId, fieldName, value) => {
1144
- var _a;
1145
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.map(row => String(row.original[idKey]) === rowId
1146
- ? {
1147
- ...row.original,
1148
- [fieldName]: value,
864
+ applySorting([{ id: columnId, desc: direction === "desc" }, ...filtered]);
865
+ },
866
+ clearSorting: () => {
867
+ applySorting([]);
868
+ },
869
+ resetSorting: () => {
870
+ const initialSorting = initialStateConfig.sorting || [];
871
+ applySorting(initialSorting);
872
+ },
873
+ },
874
+ // -------------------------------
875
+ // Pagination
876
+ // -------------------------------
877
+ pagination: {
878
+ goToPage: (pageIndex) => {
879
+ applyPagination((prev) => ({ ...prev, pageIndex }));
880
+ if (logger.isLevelEnabled("debug"))
881
+ logger.debug(`Going to page ${pageIndex}`);
882
+ },
883
+ nextPage: () => {
884
+ applyPagination((prev) => { var _a; return ({ ...prev, pageIndex: ((_a = prev === null || prev === void 0 ? void 0 : prev.pageIndex) !== null && _a !== void 0 ? _a : 0) + 1 }); });
885
+ if (logger.isLevelEnabled("debug"))
886
+ logger.debug("Next page");
887
+ },
888
+ previousPage: () => {
889
+ applyPagination((prev) => { var _a; return ({ ...prev, pageIndex: Math.max(0, ((_a = prev === null || prev === void 0 ? void 0 : prev.pageIndex) !== null && _a !== void 0 ? _a : 0) - 1) }); });
890
+ if (logger.isLevelEnabled("debug"))
891
+ logger.debug("Previous page");
892
+ },
893
+ setPageSize: (pageSize) => {
894
+ // usually want pageIndex reset
895
+ applyPagination(() => ({ pageIndex: 0, pageSize }));
896
+ if (logger.isLevelEnabled("debug"))
897
+ logger.debug(`Setting page size to ${pageSize}`);
898
+ },
899
+ goToFirstPage: () => {
900
+ applyPagination((prev) => ({ ...prev, pageIndex: 0 }));
901
+ if (logger.isLevelEnabled("debug"))
902
+ logger.debug("Going to first page");
903
+ },
904
+ goToLastPage: () => {
905
+ var _a, _b;
906
+ // pageCount can be derived; keep safe fallback
907
+ const pageCount = (_b = (_a = table.getPageCount) === null || _a === void 0 ? void 0 : _a.call(table)) !== null && _b !== void 0 ? _b : 0;
908
+ if (pageCount > 0) {
909
+ applyPagination((prev) => ({ ...prev, pageIndex: pageCount - 1 }));
910
+ if (logger.isLevelEnabled("debug"))
911
+ logger.debug(`Going to last page ${pageCount - 1}`);
1149
912
  }
1150
- : row.original);
1151
- setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1152
- },
1153
- updateFieldByIndex: (index, fieldName, value) => {
1154
- var _a;
1155
- const newData = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.map(row => row.original);
1156
- if (newData[index]) {
1157
- newData[index] = {
1158
- ...newData[index],
1159
- [fieldName]: value,
913
+ },
914
+ resetPagination: () => {
915
+ const initialPagination = initialStateConfig.pagination || { pageIndex: 0, pageSize: 10 };
916
+ applyPagination(initialPagination);
917
+ },
918
+ },
919
+ // -------------------------------
920
+ // Selection
921
+ // -------------------------------
922
+ selection: {
923
+ selectRow: (rowId) => { var _a; return (_a = table.selectRow) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
924
+ deselectRow: (rowId) => { var _a; return (_a = table.deselectRow) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
925
+ toggleRowSelection: (rowId) => { var _a; return (_a = table.toggleRowSelected) === null || _a === void 0 ? void 0 : _a.call(table, rowId); },
926
+ selectAll: () => { var _a; return (_a = table.selectAll) === null || _a === void 0 ? void 0 : _a.call(table); },
927
+ deselectAll: () => { var _a; return (_a = table.deselectAll) === null || _a === void 0 ? void 0 : _a.call(table); },
928
+ toggleSelectAll: () => { var _a; return (_a = table.toggleAllRowsSelected) === null || _a === void 0 ? void 0 : _a.call(table); },
929
+ getSelectionState: () => { var _a; return ((_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table)) || { ids: [], type: "include" }; },
930
+ getSelectedRows: () => table.getSelectedRows(),
931
+ getSelectedCount: () => table.getSelectedCount(),
932
+ isRowSelected: (rowId) => table.getIsRowSelected(rowId) || false,
933
+ },
934
+ // -------------------------------
935
+ // Data Management (kept same, but ensure state changes go through handlers)
936
+ // -------------------------------
937
+ data: {
938
+ refresh: (resetPagination = false) => {
939
+ var _a, _b, _c, _d;
940
+ const allState = table.getState();
941
+ const current = allState.pagination;
942
+ const nextPagination = {
943
+ pageIndex: resetPagination ? 0 : (_a = current === null || current === void 0 ? void 0 : current.pageIndex) !== null && _a !== void 0 ? _a : 0,
944
+ pageSize: (_d = (_b = current === null || current === void 0 ? void 0 : current.pageSize) !== null && _b !== void 0 ? _b : (_c = initialStateConfig.pagination) === null || _c === void 0 ? void 0 : _c.pageSize) !== null && _d !== void 0 ? _d : 10,
1160
945
  };
946
+ // must go through handler so server fetch triggers
947
+ applyPagination(nextPagination);
948
+ // emit persisted state (your emitTableState effect will also do it)
949
+ onDataStateChange === null || onDataStateChange === void 0 ? void 0 : onDataStateChange({ ...allState, pagination: nextPagination });
950
+ fetchData === null || fetchData === void 0 ? void 0 : fetchData({ pagination: nextPagination });
951
+ if (logger.isLevelEnabled("debug")) {
952
+ logger.debug("Refreshing data", { nextPagination, allState });
953
+ }
954
+ },
955
+ reload: () => {
956
+ const allState = table.getState();
957
+ onDataStateChange === null || onDataStateChange === void 0 ? void 0 : onDataStateChange(allState);
958
+ fetchData === null || fetchData === void 0 ? void 0 : fetchData();
959
+ if (logger.isLevelEnabled("debug")) {
960
+ logger.info("Reloading data", allState);
961
+ }
962
+ },
963
+ resetAll: () => resetAllAndReload({ resetLayout: true }),
964
+ getAllData: () => { var _a; return ((_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map((row) => row.original)) || []; },
965
+ getRowData: (rowId) => { var _a, _b; return (_b = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.find((row) => String(row.original[idKey]) === rowId)) === null || _b === void 0 ? void 0 : _b.original; },
966
+ getRowByIndex: (index) => { var _a, _b; return (_b = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b.original; },
967
+ updateRow: (rowId, updates) => {
968
+ var _a;
969
+ const newData = (_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map((row) => String(row.original[idKey]) === rowId ? { ...row.original, ...updates } : row.original);
970
+ setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData || []);
971
+ if (logger.isLevelEnabled("debug"))
972
+ logger.debug(`Updating row ${rowId}`, updates);
973
+ },
974
+ updateRowByIndex: (index, updates) => {
975
+ var _a;
976
+ const newData = ((_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map((row) => row.original)) || [];
977
+ if (newData[index]) {
978
+ newData[index] = { ...newData[index], ...updates };
979
+ setServerData(newData);
980
+ if (logger.isLevelEnabled("debug"))
981
+ logger.debug(`Updating row by index ${index}`, updates);
982
+ }
983
+ },
984
+ insertRow: (newRow, index) => {
985
+ var _a;
986
+ const newData = ((_a = table.getRowModel().rows) === null || _a === void 0 ? void 0 : _a.map((row) => row.original)) || [];
987
+ if (index !== undefined)
988
+ newData.splice(index, 0, newRow);
989
+ else
990
+ newData.push(newRow);
991
+ setServerData(newData || []);
992
+ if (logger.isLevelEnabled("debug"))
993
+ logger.debug("Inserting row", newRow);
994
+ },
995
+ deleteRow: (rowId) => {
996
+ const newData = (table.getRowModel().rows || []).filter((row) => String(row.original[idKey]) !== rowId);
997
+ setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData.map((r) => r.original) || []);
998
+ if (logger.isLevelEnabled("debug"))
999
+ logger.debug(`Deleting row ${rowId}`);
1000
+ },
1001
+ deleteRowByIndex: (index) => {
1002
+ const newData = (table.getRowModel().rows || []).map((row) => row.original);
1003
+ newData.splice(index, 1);
1004
+ setServerData(newData);
1005
+ if (logger.isLevelEnabled("debug"))
1006
+ logger.debug(`Deleting row by index ${index}`);
1007
+ },
1008
+ deleteSelectedRows: () => {
1009
+ var _a, _b;
1010
+ const selectedRows = ((_a = table.getSelectedRows) === null || _a === void 0 ? void 0 : _a.call(table)) || [];
1011
+ if (selectedRows.length === 0)
1012
+ return;
1013
+ const selectedIds = new Set(selectedRows.map((row) => String(row.original[idKey])));
1014
+ const newData = (table.getRowModel().rows || [])
1015
+ .filter((row) => !selectedIds.has(String(row.original[idKey])))
1016
+ .map((row) => row.original);
1017
+ setServerData(newData);
1018
+ (_b = table.deselectAll) === null || _b === void 0 ? void 0 : _b.call(table);
1019
+ if (logger.isLevelEnabled("debug"))
1020
+ logger.debug("Deleting selected rows");
1021
+ },
1022
+ replaceAllData: (newData) => setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData),
1023
+ updateMultipleRows: (updates) => {
1024
+ const updateMap = new Map(updates.map((u) => [u.rowId, u.data]));
1025
+ const newData = (table.getRowModel().rows || []).map((row) => {
1026
+ const rowId = String(row.original[idKey]);
1027
+ const updateData = updateMap.get(rowId);
1028
+ return updateData ? { ...row.original, ...updateData } : row.original;
1029
+ });
1030
+ setServerData(newData || []);
1031
+ },
1032
+ insertMultipleRows: (newRows, startIndex) => {
1033
+ const newData = (table.getRowModel().rows || []).map((row) => row.original);
1034
+ if (startIndex !== undefined)
1035
+ newData.splice(startIndex, 0, ...newRows);
1036
+ else
1037
+ newData.push(...newRows);
1161
1038
  setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1162
- }
1163
- },
1164
- // Data queries
1165
- findRows: (predicate) => {
1166
- var _a, _b;
1167
- return (_b = (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.filter(row => predicate(row.original))) === null || _b === void 0 ? void 0 : _b.map(row => row.original);
1168
- },
1169
- findRowIndex: (predicate) => {
1170
- var _a;
1171
- return (_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.findIndex(row => predicate(row.original));
1172
- },
1173
- getDataCount: () => {
1174
- var _a;
1175
- return ((_a = (table.getRowModel().rows || [])) === null || _a === void 0 ? void 0 : _a.length) || 0;
1176
- },
1177
- getFilteredDataCount: () => {
1178
- return table.getFilteredRowModel().rows.length;
1179
- },
1180
- },
1181
- // Layout Management
1182
- layout: {
1183
- resetLayout: () => {
1184
- table.resetColumnSizing();
1185
- table.resetColumnVisibility();
1186
- table.resetSorting();
1187
- table.resetGlobalFilter();
1188
- },
1189
- resetAll: () => {
1190
- // Reset everything to initial state
1191
- table.resetColumnSizing();
1192
- table.resetColumnVisibility();
1193
- table.resetSorting();
1194
- table.resetGlobalFilter();
1195
- table.resetColumnOrder();
1196
- table.resetExpanded();
1197
- handleSelectionStateChange(initialSelectionState);
1198
- table.resetColumnPinning();
1199
- handleColumnFilterStateChange(initialStateConfig.columnFilter || { filters: [], logic: 'AND', pendingFilters: [], pendingLogic: 'AND' });
1200
- if (enablePagination) {
1201
- table.setPagination(initialStateConfig.pagination || { pageIndex: 0, pageSize: 10 });
1202
- }
1203
- if (enableColumnPinning) {
1204
- table.setColumnPinning(initialStateConfig.columnPinning || { left: [], right: [] });
1205
- }
1206
- },
1207
- saveLayout: () => {
1208
- return {
1039
+ },
1040
+ deleteMultipleRows: (rowIds) => {
1041
+ const idsToDelete = new Set(rowIds);
1042
+ const newData = (table.getRowModel().rows || [])
1043
+ .filter((row) => !idsToDelete.has(String(row.original[idKey])))
1044
+ .map((row) => row.original);
1045
+ setServerData(newData);
1046
+ },
1047
+ updateField: (rowId, fieldName, value) => {
1048
+ const newData = (table.getRowModel().rows || []).map((row) => String(row.original[idKey]) === rowId ? { ...row.original, [fieldName]: value } : row.original);
1049
+ setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1050
+ },
1051
+ updateFieldByIndex: (index, fieldName, value) => {
1052
+ const newData = (table.getRowModel().rows || []).map((row) => row.original);
1053
+ if (newData[index]) {
1054
+ newData[index] = { ...newData[index], [fieldName]: value };
1055
+ setServerData === null || setServerData === void 0 ? void 0 : setServerData(newData);
1056
+ }
1057
+ },
1058
+ findRows: (predicate) => (table.getRowModel().rows || []).filter((row) => predicate(row.original)).map((row) => row.original),
1059
+ findRowIndex: (predicate) => (table.getRowModel().rows || []).findIndex((row) => predicate(row.original)),
1060
+ getDataCount: () => (table.getRowModel().rows || []).length || 0,
1061
+ getFilteredDataCount: () => table.getFilteredRowModel().rows.length,
1062
+ },
1063
+ // -------------------------------
1064
+ // Layout Management
1065
+ // -------------------------------
1066
+ layout: {
1067
+ resetLayout: () => {
1068
+ var _a;
1069
+ // go through handlers so controlled state updates + emit works
1070
+ applySizing(initialStateConfig.columnSizing || {});
1071
+ applyVisibility(initialStateConfig.columnVisibility || {});
1072
+ applySorting(initialStateConfig.sorting || []);
1073
+ applyGlobalFilter((_a = initialStateConfig.globalFilter) !== null && _a !== void 0 ? _a : "");
1074
+ },
1075
+ resetAll: () => resetAllAndReload({ resetLayout: true }),
1076
+ saveLayout: () => ({
1209
1077
  columnVisibility: table.getState().columnVisibility,
1210
1078
  columnSizing: table.getState().columnSizing,
1211
1079
  columnOrder: table.getState().columnOrder,
@@ -1214,132 +1082,156 @@ logging, }, ref) {
1214
1082
  pagination: table.getState().pagination,
1215
1083
  globalFilter: table.getState().globalFilter,
1216
1084
  columnFilter: table.getState().columnFilter,
1217
- };
1218
- },
1219
- restoreLayout: (layout) => {
1220
- if (layout.columnVisibility) {
1221
- table.setColumnVisibility(layout.columnVisibility);
1222
- }
1223
- if (layout.columnSizing) {
1224
- table.setColumnSizing(layout.columnSizing);
1225
- }
1226
- if (layout.columnOrder) {
1227
- table.setColumnOrder(layout.columnOrder);
1228
- }
1229
- if (layout.columnPinning) {
1230
- table.setColumnPinning(layout.columnPinning);
1231
- }
1232
- if (layout.sorting) {
1233
- table.setSorting(layout.sorting);
1234
- }
1235
- if (layout.pagination && enablePagination) {
1236
- table.setPagination(layout.pagination);
1237
- }
1238
- if (layout.globalFilter !== undefined) {
1239
- table.setGlobalFilter(layout.globalFilter);
1240
- }
1241
- if (layout.columnFilter) {
1242
- handleColumnFilterStateChange(layout.columnFilter);
1243
- }
1244
- },
1245
- },
1246
- // Table State
1247
- state: {
1248
- getTableState: () => {
1249
- return table.getState();
1250
- },
1251
- getCurrentFilters: () => {
1252
- return table.getState().columnFilter;
1253
- },
1254
- getCurrentSorting: () => {
1255
- return table.getState().sorting;
1256
- },
1257
- getCurrentPagination: () => {
1258
- return table.getState().pagination;
1259
- },
1260
- // Backward compatibility: expose the raw selection array expected by older consumers
1261
- getCurrentSelection: () => {
1262
- var _a;
1263
- return (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table);
1264
- },
1265
- },
1266
- // Simplified Export
1267
- export: {
1268
- exportCSV: async (options = {}) => {
1269
- var _a;
1270
- const { filename = exportFilename, } = options;
1271
- try {
1272
- // Create abort controller for this export
1273
- const controller = new AbortController();
1274
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1275
- if (dataMode === 'server' && onServerExport) {
1276
- // Server export with selection data
1277
- const currentFilters = {
1278
- globalFilter: table.getState().globalFilter,
1279
- columnFilter: table.getState().columnFilter,
1280
- sorting: table.getState().sorting,
1281
- pagination: table.getState().pagination,
1282
- };
1283
- if (logger.isLevelEnabled('debug')) {
1284
- logger.debug('Server export CSV', { currentFilters });
1085
+ }),
1086
+ restoreLayout: (layout) => {
1087
+ if (layout.columnVisibility)
1088
+ applyVisibility(layout.columnVisibility);
1089
+ if (layout.columnSizing)
1090
+ applySizing(layout.columnSizing);
1091
+ if (layout.columnOrder)
1092
+ applyColumnOrder(layout.columnOrder);
1093
+ if (layout.columnPinning)
1094
+ applyPinning(layout.columnPinning);
1095
+ if (layout.sorting)
1096
+ applySorting(layout.sorting);
1097
+ if (layout.pagination && enablePagination)
1098
+ applyPagination(layout.pagination);
1099
+ if (layout.globalFilter !== undefined)
1100
+ applyGlobalFilter(layout.globalFilter);
1101
+ if (layout.columnFilter)
1102
+ handleColumnFilterStateChange(layout.columnFilter);
1103
+ },
1104
+ },
1105
+ // -------------------------------
1106
+ // Table State
1107
+ // -------------------------------
1108
+ state: {
1109
+ getTableState: () => table.getState(),
1110
+ getCurrentFilters: () => table.getState().columnFilter,
1111
+ getCurrentSorting: () => table.getState().sorting,
1112
+ getCurrentPagination: () => table.getState().pagination,
1113
+ getCurrentSelection: () => { var _a; return (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table); },
1114
+ },
1115
+ // -------------------------------
1116
+ // Export (unchanged mostly)
1117
+ // -------------------------------
1118
+ export: {
1119
+ exportCSV: async (options = {}) => {
1120
+ var _a;
1121
+ const { filename = exportFilename } = options;
1122
+ try {
1123
+ const controller = new AbortController();
1124
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1125
+ if (dataMode === "server" && onServerExport) {
1126
+ const currentFilters = {
1127
+ globalFilter: table.getState().globalFilter,
1128
+ columnFilter: table.getState().columnFilter,
1129
+ sorting: table.getState().sorting,
1130
+ pagination: table.getState().pagination,
1131
+ };
1132
+ if (logger.isLevelEnabled("debug"))
1133
+ logger.debug("Server export CSV", { currentFilters });
1134
+ await (0, utils_1.exportServerData)(table, {
1135
+ format: "csv",
1136
+ filename,
1137
+ fetchData: (filters, selection) => onServerExport(filters, selection),
1138
+ currentFilters,
1139
+ selection: (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table),
1140
+ onProgress: onExportProgress,
1141
+ onComplete: onExportComplete,
1142
+ onError: onExportError,
1143
+ });
1144
+ }
1145
+ else {
1146
+ await (0, utils_1.exportClientData)(table, {
1147
+ format: "csv",
1148
+ filename,
1149
+ onProgress: onExportProgress,
1150
+ onComplete: onExportComplete,
1151
+ onError: onExportError,
1152
+ });
1153
+ if (logger.isLevelEnabled("debug"))
1154
+ logger.debug("Client export CSV", filename);
1285
1155
  }
1286
- await (0, utils_1.exportServerData)(table, {
1287
- format: 'csv',
1288
- filename,
1289
- fetchData: (filters, selection) => onServerExport(filters, selection),
1290
- currentFilters,
1291
- selection: (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table),
1292
- onProgress: onExportProgress,
1293
- onComplete: onExportComplete,
1294
- onError: onExportError,
1295
- });
1296
1156
  }
1297
- else {
1298
- // Client export - auto-detect selected rows if not specified
1299
- await (0, utils_1.exportClientData)(table, {
1300
- format: 'csv',
1301
- filename,
1302
- onProgress: onExportProgress,
1303
- onComplete: onExportComplete,
1304
- onError: onExportError,
1305
- });
1306
- if (logger.isLevelEnabled('debug')) {
1307
- logger.debug('Client export CSV', filename);
1157
+ catch (error) {
1158
+ onExportError === null || onExportError === void 0 ? void 0 : onExportError({ message: error.message || "Export failed", code: "EXPORT_ERROR" });
1159
+ }
1160
+ finally {
1161
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1162
+ }
1163
+ },
1164
+ exportExcel: async (options = {}) => {
1165
+ var _a;
1166
+ const { filename = exportFilename } = options;
1167
+ try {
1168
+ const controller = new AbortController();
1169
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1170
+ if (dataMode === "server" && onServerExport) {
1171
+ const currentFilters = {
1172
+ globalFilter: table.getState().globalFilter,
1173
+ columnFilter: table.getState().columnFilter,
1174
+ sorting: table.getState().sorting,
1175
+ pagination: table.getState().pagination,
1176
+ };
1177
+ if (logger.isLevelEnabled("debug"))
1178
+ logger.debug("Server export Excel", { currentFilters });
1179
+ await (0, utils_1.exportServerData)(table, {
1180
+ format: "excel",
1181
+ filename,
1182
+ fetchData: (filters, selection) => onServerExport(filters, selection),
1183
+ currentFilters,
1184
+ selection: (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table),
1185
+ onProgress: onExportProgress,
1186
+ onComplete: onExportComplete,
1187
+ onError: onExportError,
1188
+ });
1189
+ }
1190
+ else {
1191
+ await (0, utils_1.exportClientData)(table, {
1192
+ format: "excel",
1193
+ filename,
1194
+ onProgress: onExportProgress,
1195
+ onComplete: onExportComplete,
1196
+ onError: onExportError,
1197
+ });
1198
+ if (logger.isLevelEnabled("debug"))
1199
+ logger.debug("Client export Excel", filename);
1308
1200
  }
1309
1201
  }
1310
- }
1311
- catch (error) {
1312
- onExportError === null || onExportError === void 0 ? void 0 : onExportError({
1313
- message: error.message || 'Export failed',
1314
- code: 'EXPORT_ERROR',
1315
- });
1316
- }
1317
- finally {
1318
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1319
- }
1320
- },
1321
- exportExcel: async (options = {}) => {
1322
- var _a;
1323
- const { filename = exportFilename } = options;
1324
- try {
1325
- // Create abort controller for this export
1326
- const controller = new AbortController();
1327
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1328
- if (dataMode === 'server' && onServerExport) {
1329
- // Server export with selection data
1202
+ catch (error) {
1203
+ onExportError === null || onExportError === void 0 ? void 0 : onExportError({ message: error.message || "Export failed", code: "EXPORT_ERROR" });
1204
+ if (logger.isLevelEnabled("debug"))
1205
+ logger.debug("Server export Excel failed", error);
1206
+ }
1207
+ finally {
1208
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1209
+ }
1210
+ },
1211
+ exportServerData: async (options) => {
1212
+ var _a;
1213
+ const { format, filename = exportFilename, fetchData: fetchFn = onServerExport } = options;
1214
+ if (!fetchFn) {
1215
+ onExportError === null || onExportError === void 0 ? void 0 : onExportError({ message: "No server export function provided", code: "NO_SERVER_EXPORT" });
1216
+ if (logger.isLevelEnabled("debug"))
1217
+ logger.debug("Server export data failed", "No server export function provided");
1218
+ return;
1219
+ }
1220
+ try {
1221
+ const controller = new AbortController();
1222
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1330
1223
  const currentFilters = {
1331
1224
  globalFilter: table.getState().globalFilter,
1332
1225
  columnFilter: table.getState().columnFilter,
1333
1226
  sorting: table.getState().sorting,
1334
1227
  pagination: table.getState().pagination,
1335
1228
  };
1336
- if (logger.isLevelEnabled('debug')) {
1337
- logger.debug('Server export Excel', { currentFilters });
1338
- }
1229
+ if (logger.isLevelEnabled("debug"))
1230
+ logger.debug("Server export data", { currentFilters });
1339
1231
  await (0, utils_1.exportServerData)(table, {
1340
- format: 'excel',
1232
+ format,
1341
1233
  filename,
1342
- fetchData: (filters, selection) => onServerExport(filters, selection),
1234
+ fetchData: (filters, selection) => fetchFn(filters, selection),
1343
1235
  currentFilters,
1344
1236
  selection: (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table),
1345
1237
  onProgress: onExportProgress,
@@ -1347,116 +1239,52 @@ logging, }, ref) {
1347
1239
  onError: onExportError,
1348
1240
  });
1349
1241
  }
1350
- else {
1351
- // Client export - auto-detect selected rows if not specified
1352
- await (0, utils_1.exportClientData)(table, {
1353
- format: 'excel',
1354
- filename,
1355
- onProgress: onExportProgress,
1356
- onComplete: onExportComplete,
1357
- onError: onExportError,
1358
- });
1359
- if (logger.isLevelEnabled('debug')) {
1360
- logger.debug('Client export Excel', filename);
1361
- }
1242
+ catch (error) {
1243
+ onExportError === null || onExportError === void 0 ? void 0 : onExportError({ message: error.message || "Export failed", code: "EXPORT_ERROR" });
1244
+ if (logger.isLevelEnabled("debug"))
1245
+ logger.debug("Server export data failed", error);
1362
1246
  }
1363
- }
1364
- catch (error) {
1365
- onExportError === null || onExportError === void 0 ? void 0 : onExportError({
1366
- message: error.message || 'Export failed',
1367
- code: 'EXPORT_ERROR',
1368
- });
1369
- if (logger.isLevelEnabled('debug')) {
1370
- logger.debug('Server export Excel failed', error);
1247
+ finally {
1248
+ setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1371
1249
  }
1372
- }
1373
- finally {
1250
+ },
1251
+ isExporting: () => isExporting || false,
1252
+ cancelExport: () => {
1253
+ exportController === null || exportController === void 0 ? void 0 : exportController.abort();
1374
1254
  setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1375
- }
1255
+ if (logger.isLevelEnabled("debug"))
1256
+ logger.debug("Export cancelled");
1257
+ },
1376
1258
  },
1377
- exportServerData: async (options) => {
1378
- var _a;
1379
- const { format, filename = exportFilename, fetchData = onServerExport, } = options;
1380
- if (!fetchData) {
1381
- onExportError === null || onExportError === void 0 ? void 0 : onExportError({
1382
- message: 'No server export function provided',
1383
- code: 'NO_SERVER_EXPORT',
1384
- });
1385
- if (logger.isLevelEnabled('debug')) {
1386
- logger.debug('Server export data failed', 'No server export function provided');
1387
- }
1388
- return;
1389
- }
1390
- try {
1391
- // Create abort controller for this export
1392
- const controller = new AbortController();
1393
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(controller);
1394
- const currentFilters = {
1395
- globalFilter: table.getState().globalFilter,
1396
- columnFilter: table.getState().columnFilter,
1397
- sorting: table.getState().sorting,
1398
- pagination: table.getState().pagination,
1399
- };
1400
- if (logger.isLevelEnabled('debug')) {
1401
- logger.debug('Server export data', { currentFilters });
1402
- }
1403
- await (0, utils_1.exportServerData)(table, {
1404
- format,
1405
- filename,
1406
- fetchData: (filters, selection) => fetchData(filters, selection),
1407
- currentFilters,
1408
- selection: (_a = table.getSelectionState) === null || _a === void 0 ? void 0 : _a.call(table),
1409
- onProgress: onExportProgress,
1410
- onComplete: onExportComplete,
1411
- onError: onExportError,
1412
- });
1413
- }
1414
- catch (error) {
1415
- onExportError === null || onExportError === void 0 ? void 0 : onExportError({
1416
- message: error.message || 'Export failed',
1417
- code: 'EXPORT_ERROR',
1418
- });
1419
- if (logger.isLevelEnabled('debug')) {
1420
- logger.debug('Server export data failed', error);
1421
- }
1422
- }
1423
- finally {
1424
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1425
- }
1426
- },
1427
- // Export state
1428
- isExporting: () => isExporting || false,
1429
- cancelExport: () => {
1430
- exportController === null || exportController === void 0 ? void 0 : exportController.abort();
1431
- setExportController === null || setExportController === void 0 ? void 0 : setExportController(null);
1432
- if (logger.isLevelEnabled('debug')) {
1433
- logger.debug('Export cancelled');
1434
- }
1435
- },
1436
- },
1259
+ };
1437
1260
  // eslint-disable-next-line react-hooks/exhaustive-deps
1438
- }), [
1261
+ }, [
1439
1262
  table,
1440
1263
  enhancedColumns,
1264
+ handleColumnOrderChange,
1265
+ handleColumnPinningChange,
1266
+ handleColumnVisibilityChange,
1267
+ handleColumnSizingChange,
1268
+ handlePaginationChange,
1269
+ handleSortingChange,
1270
+ handleGlobalFilterChange,
1441
1271
  handleColumnFilterStateChange,
1272
+ initialStateConfig,
1273
+ enablePagination,
1442
1274
  idKey,
1443
1275
  onDataStateChange,
1444
1276
  fetchData,
1445
- enableColumnPinning,
1446
- enablePagination,
1447
- // Export dependencies
1277
+ // export
1448
1278
  exportFilename,
1449
1279
  onExportProgress,
1450
1280
  onExportComplete,
1451
1281
  onExportError,
1452
1282
  onServerExport,
1453
1283
  exportController,
1454
- setExportController,
1455
1284
  isExporting,
1456
1285
  dataMode,
1457
- selectMode,
1458
- onSelectionChange,
1459
- // Note: custom selection removed from dependency array
1286
+ logger,
1287
+ resetAllAndReload,
1460
1288
  ]);
1461
1289
  internalApiRef.current = dataTableApi;
1462
1290
  (0, react_1.useImperativeHandle)(ref, () => dataTableApi, [dataTableApi]);
@@ -1539,7 +1367,12 @@ logging, }, ref) {
1539
1367
  // -------------------------------
1540
1368
  return ((0, jsx_runtime_1.jsx)(data_table_context_1.DataTableProvider, { table: table, apiRef: internalApiRef, dataMode: dataMode, tableSize: tableSize, onTableSizeChange: (size) => {
1541
1369
  setTableSize(size);
1542
- }, columnFilter: columnFilter, onChangeColumnFilter: handleColumnFilterStateChange, slots: slots, slotProps: slotProps, isExporting: isExporting, exportController: exportController, onCancelExport: handleCancelExport, exportFilename: exportFilename, onExportProgress: onExportProgress, onExportComplete: onExportComplete, onExportError: onExportError, onServerExport: onServerExport, children: (0, jsx_runtime_1.jsxs)(RootComponent, { ...rootSlotProps, children: [(enableGlobalFilter || extraFilter) ? ((0, jsx_runtime_1.jsx)(ToolbarComponent, { extraFilter: extraFilter, enableGlobalFilter: enableGlobalFilter, enableColumnVisibility: enableColumnVisibility, enableColumnFilter: enableColumnFilter, enableExport: enableExport, enableReset: enableReset, enableTableSizeControl: enableTableSizeControl, enableColumnPinning: enableColumnPinning, ...toolbarSlotProps })) : null, enableBulkActions && enableRowSelection && isSomeRowsSelected ? ((0, jsx_runtime_1.jsx)(BulkActionsComponent, { selectionState: selectionState, selectedRowCount: selectedRowCount, bulkActions: bulkActions, sx: {
1370
+ }, columnFilter: columnFilter, onChangeColumnFilter: handleColumnFilterStateChange, slots: slots, slotProps: slotProps, isExporting: isExporting, exportController: exportController, onCancelExport: handleCancelExport, exportFilename: exportFilename, onExportProgress: onExportProgress, onExportComplete: onExportComplete, onExportError: onExportError, onServerExport: onServerExport, children: (0, jsx_runtime_1.jsxs)(RootComponent, { ...rootSlotProps, children: [(enableGlobalFilter || extraFilter) ? ((0, jsx_runtime_1.jsx)(ToolbarComponent, { extraFilter: extraFilter, enableGlobalFilter: enableGlobalFilter, enableColumnVisibility: enableColumnVisibility, enableColumnFilter: enableColumnFilter, enableExport: enableExport, enableReset: enableReset, enableTableSizeControl: enableTableSizeControl, enableColumnPinning: enableColumnPinning, enableRefresh: enableRefresh, ...toolbarSlotProps, refreshButtonProps: {
1371
+ loading: tableLoading, // disable while fetching
1372
+ showSpinnerWhileLoading: false,
1373
+ onRefresh: () => { var _a, _b, _c; return (_c = (_b = (_a = internalApiRef.current) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.refresh) === null || _c === void 0 ? void 0 : _c.call(_b, true); },
1374
+ ...toolbarSlotProps.refreshButtonProps,
1375
+ } })) : null, enableBulkActions && enableRowSelection && isSomeRowsSelected ? ((0, jsx_runtime_1.jsx)(BulkActionsComponent, { selectionState: selectionState, selectedRowCount: selectedRowCount, bulkActions: bulkActions, sx: {
1543
1376
  position: 'relative',
1544
1377
  zIndex: 2,
1545
1378
  ...bulkActionsSlotProps.sx,