@xcelsior/ui-spreadsheets 1.0.2 → 1.0.3

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.
package/dist/index.mjs CHANGED
@@ -2138,6 +2138,7 @@ import { Pagination } from "@xcelsior/design-system";
2138
2138
 
2139
2139
  // src/hooks/useSpreadsheetFiltering.ts
2140
2140
  import { useCallback as useCallback3, useMemo as useMemo2, useState as useState6 } from "react";
2141
+ import { FilterChain, LazyArray } from "@xcelsior/utils";
2141
2142
  function useSpreadsheetFiltering({
2142
2143
  data,
2143
2144
  columns,
@@ -2284,19 +2285,10 @@ function useSpreadsheetFiltering({
2284
2285
  },
2285
2286
  []
2286
2287
  );
2287
- const filteredData = useMemo2(() => {
2288
- if (!data || !Array.isArray(data)) return [];
2289
- if (serverSide) {
2290
- return data;
2291
- }
2292
- if (!columns || !Array.isArray(columns)) return data;
2293
- let result = [...data];
2294
- for (const [columnId, filter] of Object.entries(filters)) {
2295
- if (!filter) continue;
2296
- const column = columns.find((c) => c.id === columnId);
2297
- if (!column) continue;
2298
- result = result.filter((row) => {
2299
- const value = column.getValue ? column.getValue(row) : row[columnId];
2288
+ const buildFilterPredicate = useCallback3(
2289
+ (column, filter) => {
2290
+ return (row) => {
2291
+ const value = column.getValue ? column.getValue(row) : row[column.id];
2300
2292
  if (filter.includeBlanks && isBlankValue(value)) {
2301
2293
  return true;
2302
2294
  }
@@ -2323,32 +2315,48 @@ function useSpreadsheetFiltering({
2323
2315
  if (filter.max !== void 0 && numValue > Number(filter.max)) return false;
2324
2316
  }
2325
2317
  return true;
2326
- });
2327
- }
2328
- if (sortConfig) {
2329
- const column = columns.find((c) => c.id === sortConfig.columnId);
2330
- result.sort((a, b) => {
2331
- const aValue = column?.getValue ? column.getValue(a) : a[sortConfig.columnId];
2332
- const bValue = column?.getValue ? column.getValue(b) : b[sortConfig.columnId];
2318
+ };
2319
+ },
2320
+ [applyTextCondition, applyNumberCondition, applyDateCondition]
2321
+ );
2322
+ const buildSortComparator = useCallback3(
2323
+ (column, direction) => {
2324
+ return (a, b) => {
2325
+ const aValue = column?.getValue ? column.getValue(a) : a[sortConfig?.columnId];
2326
+ const bValue = column?.getValue ? column.getValue(b) : b[sortConfig?.columnId];
2333
2327
  if (aValue === null || aValue === void 0) return 1;
2334
2328
  if (bValue === null || bValue === void 0) return -1;
2335
2329
  if (typeof aValue === "string" && typeof bValue === "string") {
2336
- return sortConfig.direction === "asc" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
2330
+ return direction === "asc" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
2337
2331
  }
2338
- return sortConfig.direction === "asc" ? aValue < bValue ? -1 : aValue > bValue ? 1 : 0 : aValue > bValue ? -1 : aValue < bValue ? 1 : 0;
2339
- });
2332
+ return direction === "asc" ? aValue < bValue ? -1 : aValue > bValue ? 1 : 0 : aValue > bValue ? -1 : aValue < bValue ? 1 : 0;
2333
+ };
2334
+ },
2335
+ [sortConfig?.columnId]
2336
+ );
2337
+ const filteredData = useMemo2(() => {
2338
+ if (!data || !Array.isArray(data)) return LazyArray.empty();
2339
+ if (serverSide) {
2340
+ return LazyArray.from(data);
2340
2341
  }
2341
- return result;
2342
- }, [
2343
- data,
2344
- filters,
2345
- sortConfig,
2346
- columns,
2347
- serverSide,
2348
- applyDateCondition,
2349
- applyNumberCondition,
2350
- applyTextCondition
2351
- ]);
2342
+ if (!columns || !Array.isArray(columns)) return LazyArray.from(data);
2343
+ let lazyResult = LazyArray.from(data);
2344
+ const filterChain = new FilterChain();
2345
+ for (const [columnId, filter] of Object.entries(filters)) {
2346
+ if (!filter) continue;
2347
+ const column = columns.find((c) => c.id === columnId);
2348
+ if (!column) continue;
2349
+ filterChain.add(buildFilterPredicate(column, filter));
2350
+ }
2351
+ if (!filterChain.isEmpty) {
2352
+ lazyResult = filterChain.applyTo(lazyResult);
2353
+ }
2354
+ if (sortConfig) {
2355
+ const column = columns.find((c) => c.id === sortConfig.columnId);
2356
+ lazyResult = lazyResult.sort(buildSortComparator(column, sortConfig.direction));
2357
+ }
2358
+ return lazyResult;
2359
+ }, [data, filters, sortConfig, columns, serverSide, buildFilterPredicate, buildSortComparator]);
2352
2360
  const handleFilterChange = useCallback3(
2353
2361
  (columnId, filter) => {
2354
2362
  const newFilters = { ...filters };