@cerberus-design/data-grid 1.3.0 → 1.4.0

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 (40) hide show
  1. package/dist/components/data-grid.client.cjs +4 -4
  2. package/dist/components/data-grid.client.js +6 -6
  3. package/dist/components/grid.client.cjs +0 -1
  4. package/dist/components/grid.client.js +0 -1
  5. package/dist/components/pagination.client.cjs +1 -1
  6. package/dist/components/pagination.client.js +1 -1
  7. package/dist/components/viewport.client.cjs +2 -3
  8. package/dist/components/viewport.client.js +2 -3
  9. package/dist/context.client.cjs +3 -3
  10. package/dist/context.client.d.cts +3 -4
  11. package/dist/context.client.d.ts +3 -4
  12. package/dist/context.client.js +3 -3
  13. package/dist/panda.buildinfo.json +3 -1
  14. package/dist/store.cjs +33 -281
  15. package/dist/store.js +35 -283
  16. package/dist/stores/data.cjs +65 -0
  17. package/dist/stores/data.d.cts +10 -0
  18. package/dist/stores/data.d.ts +10 -0
  19. package/dist/stores/data.js +65 -0
  20. package/dist/stores/filter.cjs +51 -0
  21. package/dist/stores/filter.d.cts +13 -0
  22. package/dist/stores/filter.d.ts +13 -0
  23. package/dist/stores/filter.js +51 -0
  24. package/dist/stores/index.cjs +11 -0
  25. package/dist/stores/index.d.cts +5 -0
  26. package/dist/stores/index.d.ts +5 -0
  27. package/dist/stores/index.js +6 -0
  28. package/dist/stores/layout.cjs +97 -0
  29. package/dist/stores/layout.d.cts +21 -0
  30. package/dist/stores/layout.d.ts +21 -0
  31. package/dist/stores/layout.js +97 -0
  32. package/dist/stores/pagination.cjs +35 -0
  33. package/dist/stores/pagination.d.cts +18 -0
  34. package/dist/stores/pagination.d.ts +18 -0
  35. package/dist/stores/pagination.js +35 -0
  36. package/dist/stores/sort.cjs +70 -0
  37. package/dist/stores/sort.d.cts +16 -0
  38. package/dist/stores/sort.d.ts +16 -0
  39. package/dist/stores/sort.js +70 -0
  40. package/package.json +18 -18
package/dist/store.js CHANGED
@@ -1,301 +1,53 @@
1
1
  "use client";
2
- import { DEFAULT_THEME, OPERATORS } from "./const.js";
3
- import { applyFilterOperator, determineInitialCount, determinePageIndex, determinePageRange, determinePageSize, determineRowHeight } from "./utils.js";
4
- import { batch, createComputed, createSignal } from "@cerberus-design/signals";
2
+ import { determineInitialCount } from "./utils.js";
3
+ import { createDataStore } from "./stores/data.js";
4
+ import { createPaginationStore } from "./stores/pagination.js";
5
+ import { createFilterStore } from "./stores/filter.js";
6
+ import { createSortStore } from "./stores/sort.js";
7
+ import { createLayoutStore } from "./stores/layout.js";
8
+ import { createComputed } from "@cerberus-design/signals";
5
9
  //#region src/store.ts
6
10
  /**
7
11
  * Internal signal-based Store engine driving the state. We expose this in
8
12
  * the public Context API.
9
13
  */
10
14
  function createGridStore(options) {
11
- const [containerWidth, setContainerWidth] = createSignal(0);
12
- const [rows, setRows] = createSignal(options.data);
13
- const [rowSize] = createSignal(determineRowHeight(options.rowSize));
14
- const [pending, setPending] = createSignal(options.pending ?? false);
15
- const [hasSkeleton] = createSignal(options.overlays?.pending === "skeleton");
16
- const [showColFilter, setShowColFilter] = createSignal(false);
17
- const [globalFilter, setGlobalFilter] = createSignal({
18
- operator: OPERATORS.contains,
19
- value: null
15
+ const dataStore = createDataStore(options);
16
+ const paginationStore = createPaginationStore(options);
17
+ const filterStore = createFilterStore(dataStore);
18
+ const sortStore = createSortStore({
19
+ columns: dataStore.columns,
20
+ filteredRows: filterStore.filteredRows
20
21
  });
21
- const [colFilters, setColFilters] = createSignal({
22
- active: [],
23
- filters: {},
24
- editing: null
25
- });
26
- const [sorting, setSorting] = createSignal([]);
27
- const [pageIndex, setPageIndex] = createSignal(determinePageIndex(options.initialState?.pagination));
28
- const [pageSize, setPageSize] = createSignal(determinePageSize(options.initialState?.pagination));
29
- const [pageRange] = createSignal(determinePageRange(options.initialState?.pagination));
30
- const [isServerPaginated] = createSignal(Boolean(determineInitialCount(options.initialState?.pagination)));
31
- const [columns] = createSignal(options.columns.map((col) => {
32
- const pinnable = Boolean(col.features?.pinning);
33
- const filterable = Boolean(col.features?.filter);
34
- const sortable = Boolean(col.features?.sort);
35
- const hasFeatures = pinnable || filterable || sortable;
36
- const minWForFeatures = 100;
37
- let finalWidth = col.width ?? 150;
38
- if (hasFeatures && col.width && col.width < minWForFeatures) finalWidth = minWForFeatures;
39
- const [isVisible] = createSignal(true);
40
- const [isFlex, setFlex] = createSignal(col.width === void 0);
41
- const [pinned, setPinned] = createSignal(col.features?.pinning?.defaultPosition ?? false);
42
- const [width, setColWidth] = createSignal(finalWidth);
43
- return {
44
- id: col.id,
45
- isFlex,
46
- isVisible,
47
- original: col,
48
- pinned,
49
- width,
50
- getValue: col.accessor,
51
- pinnable,
52
- filterable,
53
- sortable,
54
- setFlex,
55
- setPinned,
56
- setColWidth
57
- };
58
- }));
59
- const currentPageRange = createComputed(() => {
60
- const dataIdx = pageIndex() - 1;
61
- return {
62
- start: dataIdx === 0 ? 0 : dataIdx * pageSize() - 1,
63
- end: pageIndex() * pageSize()
64
- };
65
- });
66
- const filteredRows = createComputed(() => {
67
- let result = [...rows()];
68
- const gFilter = globalFilter();
69
- const cFilters = colFilters();
70
- if (cFilters.active.length > 0) result = result.filter((row) => {
71
- return cFilters.active.every((filterId) => {
72
- const col = columns().find((c) => c.id === filterId);
73
- const filter = cFilters.filters[filterId];
74
- if (!col || !col.filterable) return true;
75
- const customFn = typeof col.original.features?.filter === "function" ? col.original.features.filter : void 0;
76
- if (customFn) return customFn(row, col.id, filter.value);
77
- return applyFilterOperator(col.getValue(row), filter.value, filter.operator);
78
- });
79
- });
80
- if (gFilter.value) result = result.filter((row) => {
81
- return columns().some((col) => {
82
- if (!col.filterable) return false;
83
- return applyFilterOperator(col.getValue(row), gFilter.value, gFilter.operator);
84
- });
85
- });
86
- return result;
87
- });
88
- const sortedRows = createComputed(() => {
89
- const currentRows = [...filteredRows()];
90
- const sortState = sorting();
91
- if (sortState.length === 0) return currentRows;
92
- return currentRows.sort((a, b) => {
93
- for (const sort of sortState) {
94
- const col = columns().find((c) => c.id === sort.id);
95
- if (!col) continue;
96
- const valA = col.getValue(a);
97
- const valB = col.getValue(b);
98
- if (valA === valB) continue;
99
- let comparison = 0;
100
- const customComparator = typeof col.original.features?.sort === "object" ? col.original.features.sort.comparator : void 0;
101
- if (customComparator) comparison = customComparator(valA, valB);
102
- else comparison = valA > valB ? 1 : -1;
103
- return sort.desc ? -comparison : comparison;
104
- }
105
- return 0;
106
- });
22
+ const layoutStore = createLayoutStore({
23
+ ...options,
24
+ orderedColumns: dataStore.orderedColumns,
25
+ columns: dataStore.columns,
26
+ rowSize: dataStore.rowSize
107
27
  });
108
28
  const rowCount = createComputed(() => {
109
- return determineInitialCount(options?.initialState?.pagination) ?? sortedRows().length;
29
+ return determineInitialCount(options?.initialState?.pagination) ?? filterStore.filteredRows().length;
110
30
  });
111
- const pageCount = createComputed(() => Math.ceil(rowCount() / pageSize()));
112
- const orderedColumns = createComputed(() => {
113
- const left = [];
114
- const center = [];
115
- const right = [];
116
- columns().forEach((col) => {
117
- const pin = col.pinned();
118
- if (pin === "left") left.push(col);
119
- else if (pin === "right") right.push(col);
120
- else center.push(col);
121
- });
122
- return [
123
- ...left,
124
- ...center,
125
- ...right
126
- ];
31
+ const pageCount = createComputed(() => Math.ceil(rowCount() / paginationStore.pageSize()));
32
+ const visibleRows = createComputed(() => {
33
+ const size = paginationStore.pageSize();
34
+ const rows = sortStore.sortedRows();
35
+ if (paginationStore.isServerPaginated()) return rows;
36
+ if (size) {
37
+ const ctx = paginationStore.currentPageRange();
38
+ return rows.slice(ctx.start, ctx.end);
39
+ }
40
+ return rows;
127
41
  });
128
42
  return {
129
- columns,
130
- rows,
43
+ ...dataStore,
44
+ ...paginationStore,
45
+ ...filterStore,
46
+ ...sortStore,
47
+ ...layoutStore,
131
48
  rowCount,
132
- rowSize,
133
- visibleRows: createComputed(() => {
134
- if (pageSize() && pageCount() > 1) {
135
- const currentRange = currentPageRange();
136
- return sortedRows().slice(currentRange.start, currentRange.end);
137
- }
138
- return sortedRows();
139
- }),
140
- showColFilter,
141
- globalFilter,
142
- colFilters,
143
- sorting,
144
- pending,
145
- hasSkeleton,
146
49
  pageCount,
147
- pageIndex,
148
- pageSize,
149
- pageRange,
150
- currentPageRange,
151
- isServerPaginated,
152
- rootCssVars: createComputed(() => {
153
- const vars = {};
154
- const visibleCols = [];
155
- const cols = columns();
156
- const cWidth = containerWidth();
157
- let fixedSpace = 0;
158
- let flexCount = 0;
159
- for (let i = 0; i < cols.length; i++) {
160
- const col = cols[i];
161
- if (!col.isVisible()) continue;
162
- visibleCols.push(col);
163
- if (col.isFlex()) flexCount++;
164
- else fixedSpace += col.width();
165
- const order = orderedColumns().findIndex((orderedCol) => orderedCol.id === col.id);
166
- vars[`--col-${col.id}-order`] = `${order}`;
167
- }
168
- const remainingSpace = Math.max(0, cWidth - fixedSpace);
169
- const flexWidth = flexCount > 0 ? remainingSpace / flexCount : 0;
170
- let leftOffset = 0;
171
- let totalW = 0;
172
- const computedWidths = new Float64Array(visibleCols.length);
173
- for (let i = 0; i < visibleCols.length; i++) {
174
- const col = visibleCols[i];
175
- let finalWidth = col.width();
176
- if (col.isFlex()) finalWidth = Math.max(col.original.minWidth ?? 150, flexWidth);
177
- computedWidths[i] = finalWidth;
178
- totalW += finalWidth;
179
- vars[`--col-${col.id}-width`] = `${finalWidth}px`;
180
- if (col.pinned() === "left") {
181
- vars[`--col-${col.id}-left`] = `${leftOffset}px`;
182
- leftOffset += finalWidth;
183
- }
184
- }
185
- let rightOffset = 0;
186
- for (let i = visibleCols.length - 1; i >= 0; i--) {
187
- const col = visibleCols[i];
188
- if (col.pinned() === "right") {
189
- vars[`--col-${col.id}-right`] = `${rightOffset}px`;
190
- rightOffset += computedWidths[i];
191
- }
192
- }
193
- vars["--total-grid-width"] = `${totalW}px`;
194
- vars["--row-height"] = `${rowSize()}px`;
195
- const theme = {
196
- ...DEFAULT_THEME,
197
- ...options.theme
198
- };
199
- vars["--border"] = theme.border;
200
- vars["--border-color"] = theme.borderColor;
201
- vars["--rounded"] = theme.rounded;
202
- vars["--row-bg-color"] = theme.rowBgColor;
203
- vars["--row-even-bg-color"] = theme.rowEvenBgColor;
204
- vars["--row-hover-bg-color"] = theme.rowHoverBgColor;
205
- vars["--head-cell-bg-color"] = theme.headCellBgColor;
206
- vars["--head-cell-border-bottom-color"] = theme.headCellBorderBottomColor;
207
- vars["--grid-cell-border-color"] = theme.gridCellBorderColor;
208
- vars["--grid-cell-pinned-border-color"] = theme.gridCellPinnedBorderColor;
209
- return vars;
210
- }),
211
- totalWidth: createComputed(() => columns().reduce((acc, c) => acc + c.width(), 0)),
212
- updateData: (newData) => {
213
- setRows(newData);
214
- },
215
- updatePending: (newState) => {
216
- setPending(newState);
217
- },
218
- setSort: (colId, direction, multi = false) => {
219
- if (direction === null) {
220
- setSorting(sorting().filter((s) => s.id !== colId));
221
- return;
222
- }
223
- const current = sorting();
224
- const newSort = {
225
- id: colId,
226
- desc: direction === "desc"
227
- };
228
- if (multi) {
229
- const existingIndex = current.findIndex((s) => s.id === colId);
230
- if (existingIndex >= 0) {
231
- const next = [...current];
232
- next[existingIndex] = newSort;
233
- setSorting(next);
234
- } else setSorting([...current, newSort]);
235
- } else setSorting([newSort]);
236
- },
237
- togglePinned: (colId, state) => {
238
- const col = columns().find((c) => c.id === colId);
239
- if (col) col.setPinned(state ?? false);
240
- },
241
- toggleSort: (colId, multi) => {
242
- const current = sorting();
243
- const exists = current.findIndex((s) => s.id === colId) !== -1;
244
- const updatedSort = current.map((s) => {
245
- if (s.id === colId) return {
246
- ...s,
247
- desc: !s.desc
248
- };
249
- return s;
250
- });
251
- if (exists) setSorting(multi ? [...current, ...updatedSort] : [...updatedSort]);
252
- else {
253
- const newSort = {
254
- id: colId,
255
- desc: true
256
- };
257
- setSorting(multi ? [...current, newSort] : [newSort]);
258
- }
259
- },
260
- setPage: (details) => {
261
- setPageIndex(details.page);
262
- options.onPageChange?.(details);
263
- },
264
- setPageSize: (size) => {
265
- if (isServerPaginated()) setPageIndex(1);
266
- setPageSize(size);
267
- },
268
- setGlobalFilter: (val) => {
269
- batch(() => {
270
- setGlobalFilter((prev) => ({
271
- ...prev,
272
- ...val
273
- }));
274
- setPageIndex(1);
275
- });
276
- },
277
- setColFilter: (val) => {
278
- setColFilters(val);
279
- },
280
- setShowColFilter: (val) => {
281
- setShowColFilter(val);
282
- },
283
- setContainerWidth: (w) => {
284
- setContainerWidth(w);
285
- },
286
- resizeColumn: (colId, delta) => {
287
- const col = columns().find((c) => c.id === colId);
288
- if (col) {
289
- if (col.isFlex()) {
290
- const fixedSpace = columns().filter((c) => !c.isFlex()).reduce((a, b) => a + b.width(), 0);
291
- const flexCount = columns().filter((c) => c.isFlex()).length;
292
- const currentFlexWidth = Math.max(col.original.minWidth ?? 150, (containerWidth() - fixedSpace) / flexCount);
293
- col.setColWidth(currentFlexWidth);
294
- col.setFlex(false);
295
- }
296
- col.setColWidth(Math.max(col.original.minWidth ?? 50, col.width() + delta));
297
- }
298
- }
50
+ visibleRows
299
51
  };
300
52
  }
301
53
  //#endregion
@@ -0,0 +1,65 @@
1
+ "use client";
2
+ const require_utils = require("../utils.cjs");
3
+ let _cerberus_design_signals = require("@cerberus-design/signals");
4
+ //#region src/stores/data.ts
5
+ function createDataStore(options) {
6
+ const [rows, setRows] = (0, _cerberus_design_signals.createSignal)(options.data);
7
+ const [rowSize] = (0, _cerberus_design_signals.createSignal)(require_utils.determineRowHeight(options.rowSize));
8
+ const [columns] = (0, _cerberus_design_signals.createSignal)(createInitColumns(options));
9
+ return {
10
+ columns,
11
+ orderedColumns: (0, _cerberus_design_signals.createComputed)(() => {
12
+ const left = [];
13
+ const center = [];
14
+ const right = [];
15
+ columns().forEach((col) => {
16
+ const pin = col.pinned();
17
+ if (pin === "left") left.push(col);
18
+ else if (pin === "right") right.push(col);
19
+ else center.push(col);
20
+ });
21
+ return [
22
+ ...left,
23
+ ...center,
24
+ ...right
25
+ ];
26
+ }),
27
+ rows,
28
+ rowSize,
29
+ updateData: (newData) => {
30
+ setRows(newData);
31
+ }
32
+ };
33
+ }
34
+ function createInitColumns(options) {
35
+ return options.columns.map((col) => {
36
+ const pinnable = Boolean(col.features?.pinning);
37
+ const filterable = Boolean(col.features?.filter);
38
+ const sortable = Boolean(col.features?.sort);
39
+ const hasFeatures = pinnable || filterable || sortable;
40
+ const minWForFeatures = 100;
41
+ let finalWidth = col.width ?? 150;
42
+ if (hasFeatures && col.width && col.width < minWForFeatures) finalWidth = minWForFeatures;
43
+ const [isVisible] = (0, _cerberus_design_signals.createSignal)(true);
44
+ const [isFlex, setFlex] = (0, _cerberus_design_signals.createSignal)(col.width === void 0);
45
+ const [pinned, setPinned] = (0, _cerberus_design_signals.createSignal)(col.features?.pinning?.defaultPosition ?? false);
46
+ const [width, setColWidth] = (0, _cerberus_design_signals.createSignal)(finalWidth);
47
+ return {
48
+ id: col.id,
49
+ isFlex,
50
+ isVisible,
51
+ original: col,
52
+ pinned,
53
+ width,
54
+ getValue: col.accessor,
55
+ pinnable,
56
+ filterable,
57
+ sortable,
58
+ setFlex,
59
+ setPinned,
60
+ setColWidth
61
+ };
62
+ });
63
+ }
64
+ //#endregion
65
+ exports.createDataStore = createDataStore;
@@ -0,0 +1,10 @@
1
+ import { Accessor } from '@cerberus-design/signals';
2
+ import { GridOptions, InternalColumn } from '../types';
3
+ export type DataStore<TData> = {
4
+ columns: Accessor<InternalColumn<TData>[]>;
5
+ orderedColumns: Accessor<InternalColumn<TData>[]>;
6
+ rows: Accessor<TData[]>;
7
+ rowSize: Accessor<number>;
8
+ updateData: (data: TData[]) => void;
9
+ };
10
+ export declare function createDataStore<TData>(options: GridOptions<TData>): DataStore<TData>;
@@ -0,0 +1,10 @@
1
+ import { Accessor } from '@cerberus-design/signals';
2
+ import { GridOptions, InternalColumn } from '../types';
3
+ export type DataStore<TData> = {
4
+ columns: Accessor<InternalColumn<TData>[]>;
5
+ orderedColumns: Accessor<InternalColumn<TData>[]>;
6
+ rows: Accessor<TData[]>;
7
+ rowSize: Accessor<number>;
8
+ updateData: (data: TData[]) => void;
9
+ };
10
+ export declare function createDataStore<TData>(options: GridOptions<TData>): DataStore<TData>;
@@ -0,0 +1,65 @@
1
+ "use client";
2
+ import { determineRowHeight } from "../utils.js";
3
+ import { createComputed, createSignal } from "@cerberus-design/signals";
4
+ //#region src/stores/data.ts
5
+ function createDataStore(options) {
6
+ const [rows, setRows] = createSignal(options.data);
7
+ const [rowSize] = createSignal(determineRowHeight(options.rowSize));
8
+ const [columns] = createSignal(createInitColumns(options));
9
+ return {
10
+ columns,
11
+ orderedColumns: createComputed(() => {
12
+ const left = [];
13
+ const center = [];
14
+ const right = [];
15
+ columns().forEach((col) => {
16
+ const pin = col.pinned();
17
+ if (pin === "left") left.push(col);
18
+ else if (pin === "right") right.push(col);
19
+ else center.push(col);
20
+ });
21
+ return [
22
+ ...left,
23
+ ...center,
24
+ ...right
25
+ ];
26
+ }),
27
+ rows,
28
+ rowSize,
29
+ updateData: (newData) => {
30
+ setRows(newData);
31
+ }
32
+ };
33
+ }
34
+ function createInitColumns(options) {
35
+ return options.columns.map((col) => {
36
+ const pinnable = Boolean(col.features?.pinning);
37
+ const filterable = Boolean(col.features?.filter);
38
+ const sortable = Boolean(col.features?.sort);
39
+ const hasFeatures = pinnable || filterable || sortable;
40
+ const minWForFeatures = 100;
41
+ let finalWidth = col.width ?? 150;
42
+ if (hasFeatures && col.width && col.width < minWForFeatures) finalWidth = minWForFeatures;
43
+ const [isVisible] = createSignal(true);
44
+ const [isFlex, setFlex] = createSignal(col.width === void 0);
45
+ const [pinned, setPinned] = createSignal(col.features?.pinning?.defaultPosition ?? false);
46
+ const [width, setColWidth] = createSignal(finalWidth);
47
+ return {
48
+ id: col.id,
49
+ isFlex,
50
+ isVisible,
51
+ original: col,
52
+ pinned,
53
+ width,
54
+ getValue: col.accessor,
55
+ pinnable,
56
+ filterable,
57
+ sortable,
58
+ setFlex,
59
+ setPinned,
60
+ setColWidth
61
+ };
62
+ });
63
+ }
64
+ //#endregion
65
+ export { createDataStore };
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ const require_const = require("../const.cjs");
3
+ const require_utils = require("../utils.cjs");
4
+ let _cerberus_design_signals = require("@cerberus-design/signals");
5
+ //#region src/stores/filter.ts
6
+ function createFilterStore(dataStore) {
7
+ const [showColFilter, setShowColFilter] = (0, _cerberus_design_signals.createSignal)(false);
8
+ const [globalFilter, setGlobalFilter] = (0, _cerberus_design_signals.createSignal)({
9
+ operator: require_const.OPERATORS.contains,
10
+ value: null
11
+ });
12
+ const [colFilters, setColFilters] = (0, _cerberus_design_signals.createSignal)({
13
+ active: [],
14
+ filters: {},
15
+ editing: null
16
+ });
17
+ return {
18
+ showColFilter,
19
+ globalFilter,
20
+ colFilters,
21
+ filteredRows: (0, _cerberus_design_signals.createComputed)(() => {
22
+ const rows = dataStore.rows();
23
+ const columns = dataStore.columns();
24
+ let result = [...rows];
25
+ const gFilter = globalFilter();
26
+ const cFilters = colFilters();
27
+ if (cFilters.active.length > 0) result = result.filter((row) => {
28
+ return cFilters.active.every((filterId) => {
29
+ const col = columns.find((c) => c.id === filterId);
30
+ const filter = cFilters.filters[filterId];
31
+ if (!col || !col.filterable) return true;
32
+ const customFn = typeof col.original.features?.filter === "function" ? col.original.features.filter : void 0;
33
+ if (customFn) return customFn(row, col.id, filter.value);
34
+ return require_utils.applyFilterOperator(col.getValue(row), filter.value, filter.operator);
35
+ });
36
+ });
37
+ if (gFilter.value) result = result.filter((row) => {
38
+ return columns.some((col) => {
39
+ if (!col.filterable) return false;
40
+ return require_utils.applyFilterOperator(col.getValue(row), gFilter.value, gFilter.operator);
41
+ });
42
+ });
43
+ return result;
44
+ }),
45
+ setGlobalFilter,
46
+ setColFilter: (val) => setColFilters(val),
47
+ setShowColFilter
48
+ };
49
+ }
50
+ //#endregion
51
+ exports.createFilterStore = createFilterStore;
@@ -0,0 +1,13 @@
1
+ import { Accessor, Setter } from '@cerberus-design/signals';
2
+ import { BaseFilterState, ColumnFilterState } from '../types';
3
+ import { DataStore } from './data';
4
+ export interface FilterStore<TData> {
5
+ showColFilter: Accessor<boolean>;
6
+ globalFilter: Accessor<BaseFilterState>;
7
+ colFilters: Accessor<ColumnFilterState>;
8
+ filteredRows: Accessor<TData[]>;
9
+ setGlobalFilter: Setter<BaseFilterState>;
10
+ setColFilter: Setter<ColumnFilterState>;
11
+ setShowColFilter: Setter<boolean>;
12
+ }
13
+ export declare function createFilterStore<TData>(dataStore: DataStore<TData>): FilterStore<TData>;
@@ -0,0 +1,13 @@
1
+ import { Accessor, Setter } from '@cerberus-design/signals';
2
+ import { BaseFilterState, ColumnFilterState } from '../types';
3
+ import { DataStore } from './data';
4
+ export interface FilterStore<TData> {
5
+ showColFilter: Accessor<boolean>;
6
+ globalFilter: Accessor<BaseFilterState>;
7
+ colFilters: Accessor<ColumnFilterState>;
8
+ filteredRows: Accessor<TData[]>;
9
+ setGlobalFilter: Setter<BaseFilterState>;
10
+ setColFilter: Setter<ColumnFilterState>;
11
+ setShowColFilter: Setter<boolean>;
12
+ }
13
+ export declare function createFilterStore<TData>(dataStore: DataStore<TData>): FilterStore<TData>;
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ import { OPERATORS } from "../const.js";
3
+ import { applyFilterOperator } from "../utils.js";
4
+ import { createComputed, createSignal } from "@cerberus-design/signals";
5
+ //#region src/stores/filter.ts
6
+ function createFilterStore(dataStore) {
7
+ const [showColFilter, setShowColFilter] = createSignal(false);
8
+ const [globalFilter, setGlobalFilter] = createSignal({
9
+ operator: OPERATORS.contains,
10
+ value: null
11
+ });
12
+ const [colFilters, setColFilters] = createSignal({
13
+ active: [],
14
+ filters: {},
15
+ editing: null
16
+ });
17
+ return {
18
+ showColFilter,
19
+ globalFilter,
20
+ colFilters,
21
+ filteredRows: createComputed(() => {
22
+ const rows = dataStore.rows();
23
+ const columns = dataStore.columns();
24
+ let result = [...rows];
25
+ const gFilter = globalFilter();
26
+ const cFilters = colFilters();
27
+ if (cFilters.active.length > 0) result = result.filter((row) => {
28
+ return cFilters.active.every((filterId) => {
29
+ const col = columns.find((c) => c.id === filterId);
30
+ const filter = cFilters.filters[filterId];
31
+ if (!col || !col.filterable) return true;
32
+ const customFn = typeof col.original.features?.filter === "function" ? col.original.features.filter : void 0;
33
+ if (customFn) return customFn(row, col.id, filter.value);
34
+ return applyFilterOperator(col.getValue(row), filter.value, filter.operator);
35
+ });
36
+ });
37
+ if (gFilter.value) result = result.filter((row) => {
38
+ return columns.some((col) => {
39
+ if (!col.filterable) return false;
40
+ return applyFilterOperator(col.getValue(row), gFilter.value, gFilter.operator);
41
+ });
42
+ });
43
+ return result;
44
+ }),
45
+ setGlobalFilter,
46
+ setColFilter: (val) => setColFilters(val),
47
+ setShowColFilter
48
+ };
49
+ }
50
+ //#endregion
51
+ export { createFilterStore };
@@ -0,0 +1,11 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_data = require("./data.cjs");
3
+ const require_pagination = require("./pagination.cjs");
4
+ const require_filter = require("./filter.cjs");
5
+ const require_sort = require("./sort.cjs");
6
+ const require_layout = require("./layout.cjs");
7
+ exports.createDataStore = require_data.createDataStore;
8
+ exports.createFilterStore = require_filter.createFilterStore;
9
+ exports.createLayoutStore = require_layout.createLayoutStore;
10
+ exports.createPaginationStore = require_pagination.createPaginationStore;
11
+ exports.createSortStore = require_sort.createSortStore;
@@ -0,0 +1,5 @@
1
+ export * from './data';
2
+ export * from './pagination';
3
+ export * from './filter';
4
+ export * from './sort';
5
+ export * from './layout';
@@ -0,0 +1,5 @@
1
+ export * from './data';
2
+ export * from './pagination';
3
+ export * from './filter';
4
+ export * from './sort';
5
+ export * from './layout';
@@ -0,0 +1,6 @@
1
+ import { createDataStore } from "./data.js";
2
+ import { createPaginationStore } from "./pagination.js";
3
+ import { createFilterStore } from "./filter.js";
4
+ import { createSortStore } from "./sort.js";
5
+ import { createLayoutStore } from "./layout.js";
6
+ export { createDataStore, createFilterStore, createLayoutStore, createPaginationStore, createSortStore };