@alaarab/ogrid-react 2.1.5 → 2.1.6

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 (2) hide show
  1. package/dist/esm/index.js +28 -18
  2. package/package.json +2 -2
package/dist/esm/index.js CHANGED
@@ -1312,6 +1312,13 @@ function validateRowIds(items, getRowId) {
1312
1312
  }
1313
1313
  var DEFAULT_DEBOUNCE_MS = 300;
1314
1314
  var PEOPLE_SEARCH_DEBOUNCE_MS = DEFAULT_DEBOUNCE_MS;
1315
+ function useLatestRef(value) {
1316
+ const ref = useRef(value);
1317
+ ref.current = value;
1318
+ return ref;
1319
+ }
1320
+
1321
+ // src/hooks/useFilterOptions.ts
1315
1322
  function fieldsEqual(a, b) {
1316
1323
  if (a.length !== b.length) return false;
1317
1324
  for (let i = 0; i < a.length; i++) {
@@ -1319,19 +1326,23 @@ function fieldsEqual(a, b) {
1319
1326
  }
1320
1327
  return true;
1321
1328
  }
1329
+ var EMPTY_FILTER_OPTIONS = {};
1330
+ var EMPTY_LOADING = {};
1322
1331
  function useFilterOptions(dataSource, fields) {
1323
1332
  const fieldsRef = useRef(fields);
1324
1333
  if (!fieldsEqual(fieldsRef.current, fields)) {
1325
1334
  fieldsRef.current = fields;
1326
1335
  }
1327
1336
  const stableFields = fieldsRef.current;
1328
- const [filterOptions, setFilterOptions] = useState({});
1329
- const [loadingOptions, setLoadingOptions] = useState({});
1337
+ const dataSourceRef = useLatestRef(dataSource);
1338
+ const [filterOptions, setFilterOptions] = useState(EMPTY_FILTER_OPTIONS);
1339
+ const [loadingOptions, setLoadingOptions] = useState(EMPTY_LOADING);
1330
1340
  const load = useCallback(async () => {
1331
- const fetcher = "fetchFilterOptions" in dataSource && typeof dataSource.fetchFilterOptions === "function" ? dataSource.fetchFilterOptions.bind(dataSource) : void 0;
1341
+ const ds = dataSourceRef.current;
1342
+ const fetcher = "fetchFilterOptions" in ds && typeof ds.fetchFilterOptions === "function" ? ds.fetchFilterOptions.bind(ds) : void 0;
1332
1343
  if (!fetcher) {
1333
- setFilterOptions({});
1334
- setLoadingOptions({});
1344
+ setFilterOptions(EMPTY_FILTER_OPTIONS);
1345
+ setLoadingOptions(EMPTY_LOADING);
1335
1346
  return;
1336
1347
  }
1337
1348
  const loading = {};
@@ -1350,8 +1361,8 @@ function useFilterOptions(dataSource, fields) {
1350
1361
  })
1351
1362
  );
1352
1363
  setFilterOptions(results);
1353
- setLoadingOptions({});
1354
- }, [dataSource, stableFields]);
1364
+ setLoadingOptions(EMPTY_LOADING);
1365
+ }, [stableFields]);
1355
1366
  useEffect(() => {
1356
1367
  load().catch(() => {
1357
1368
  });
@@ -1535,14 +1546,17 @@ function useOGridDataFetching(params) {
1535
1546
  const [serverLoading, setServerLoading] = useState(true);
1536
1547
  const fetchIdRef = useRef(0);
1537
1548
  const [refreshCounter, setRefreshCounter] = useState(0);
1549
+ const dataSourceRef = useLatestRef(dataSource);
1550
+ const onErrorRef = useLatestRef(onError);
1538
1551
  useEffect(() => {
1539
- if (!isServerSide || !dataSource) {
1552
+ if (!isServerSide || !dataSourceRef.current) {
1540
1553
  if (!isServerSide) setServerLoading(false);
1541
1554
  return;
1542
1555
  }
1556
+ const ds = dataSourceRef.current;
1543
1557
  const id = ++fetchIdRef.current;
1544
1558
  setServerLoading(true);
1545
- dataSource.fetchPage({
1559
+ ds.fetchPage({
1546
1560
  page,
1547
1561
  pageSize,
1548
1562
  sort: { field: sort.field, direction: sort.direction },
@@ -1553,22 +1567,23 @@ function useOGridDataFetching(params) {
1553
1567
  setServerTotalCount(res.totalCount);
1554
1568
  }).catch((err) => {
1555
1569
  if (id !== fetchIdRef.current) return;
1556
- onError?.(err);
1570
+ onErrorRef.current?.(err);
1557
1571
  setServerItems([]);
1558
1572
  setServerTotalCount(0);
1559
1573
  }).finally(() => {
1560
1574
  if (id === fetchIdRef.current) setServerLoading(false);
1561
1575
  });
1562
- }, [isServerSide, dataSource, page, pageSize, sort.field, sort.direction, stableFilters, onError, refreshCounter]);
1576
+ }, [isServerSide, page, pageSize, sort.field, sort.direction, stableFilters, refreshCounter]);
1563
1577
  const displayItems = isClientSide && clientItemsAndTotal ? clientItemsAndTotal.items : serverItems;
1564
1578
  const displayTotalCount = isClientSide && clientItemsAndTotal ? clientItemsAndTotal.totalCount : serverTotalCount;
1579
+ const onFirstDataRenderedRef = useLatestRef(onFirstDataRendered);
1565
1580
  const firstDataRenderedRef = useRef(false);
1566
1581
  useEffect(() => {
1567
1582
  if (!firstDataRenderedRef.current && displayItems.length > 0) {
1568
1583
  firstDataRenderedRef.current = true;
1569
- onFirstDataRendered?.();
1584
+ onFirstDataRenderedRef.current?.();
1570
1585
  }
1571
- }, [displayItems.length, onFirstDataRendered]);
1586
+ }, [displayItems.length]);
1572
1587
  return {
1573
1588
  displayItems,
1574
1589
  displayTotalCount,
@@ -1576,11 +1591,6 @@ function useOGridDataFetching(params) {
1576
1591
  refreshData: () => setRefreshCounter((prev) => prev + 1)
1577
1592
  };
1578
1593
  }
1579
- function useLatestRef(value) {
1580
- const ref = useRef(value);
1581
- ref.current = value;
1582
- return ref;
1583
- }
1584
1594
  var DEFAULT_PANELS = ["columns", "filters"];
1585
1595
  function useSideBarState(params) {
1586
1596
  const { config } = params;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alaarab/ogrid-react",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "OGrid React – React hooks, headless components, and utilities for OGrid data grids.",
5
5
  "main": "dist/esm/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -39,7 +39,7 @@
39
39
  "node": ">=18"
40
40
  },
41
41
  "dependencies": {
42
- "@alaarab/ogrid-core": "2.1.5",
42
+ "@alaarab/ogrid-core": "2.1.6",
43
43
  "@tanstack/react-virtual": "^3.0.0"
44
44
  },
45
45
  "peerDependencies": {