@svgrid/grid 1.0.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.
- package/LICENSE +33 -0
- package/README.md +39 -0
- package/dist/FlexRender.svelte +96 -0
- package/dist/FlexRender.svelte.d.ts +49 -0
- package/dist/SvGrid.svelte +8742 -0
- package/dist/SvGrid.svelte.d.ts +381 -0
- package/dist/SvGridChart.svelte +653 -0
- package/dist/SvGridChart.svelte.d.ts +25 -0
- package/dist/SvGridChart.test.d.ts +1 -0
- package/dist/SvGridChart.test.js +62 -0
- package/dist/SvGridDropdown.svelte +675 -0
- package/dist/SvGridDropdown.svelte.d.ts +40 -0
- package/dist/a11y.contract.test.d.ts +1 -0
- package/dist/a11y.contract.test.js +39 -0
- package/dist/a11y.d.ts +38 -0
- package/dist/a11y.js +37 -0
- package/dist/a11y.test.d.ts +1 -0
- package/dist/a11y.test.js +44 -0
- package/dist/cell-formatting.d.ts +17 -0
- package/dist/cell-formatting.js +141 -0
- package/dist/cell-formatting.test.d.ts +1 -0
- package/dist/cell-formatting.test.js +234 -0
- package/dist/chart-export.d.ts +14 -0
- package/dist/chart-export.js +112 -0
- package/dist/chart.d.ts +226 -0
- package/dist/chart.js +732 -0
- package/dist/chart.test.d.ts +1 -0
- package/dist/chart.test.js +289 -0
- package/dist/collaboration.d.ts +74 -0
- package/dist/collaboration.js +98 -0
- package/dist/collaboration.test.d.ts +1 -0
- package/dist/collaboration.test.js +65 -0
- package/dist/conditional-formatting.d.ts +91 -0
- package/dist/conditional-formatting.js +170 -0
- package/dist/conditional-formatting.test.d.ts +1 -0
- package/dist/conditional-formatting.test.js +87 -0
- package/dist/core.coverage.test.d.ts +1 -0
- package/dist/core.coverage.test.js +186 -0
- package/dist/core.d.ts +370 -0
- package/dist/core.js +568 -0
- package/dist/core.performance.test.d.ts +1 -0
- package/dist/core.performance.test.js +29 -0
- package/dist/createGrid.svelte.d.ts +6 -0
- package/dist/createGrid.svelte.js +17 -0
- package/dist/createGrid.test.d.ts +1 -0
- package/dist/createGrid.test.js +9 -0
- package/dist/createGridState.svelte.d.ts +3 -0
- package/dist/createGridState.svelte.js +13 -0
- package/dist/editors/cell-editors.d.ts +23 -0
- package/dist/editors/cell-editors.js +97 -0
- package/dist/editors/cell-editors.test.d.ts +1 -0
- package/dist/editors/cell-editors.test.js +75 -0
- package/dist/fill-patterns.d.ts +30 -0
- package/dist/fill-patterns.js +207 -0
- package/dist/fill-patterns.test.d.ts +1 -0
- package/dist/fill-patterns.test.js +81 -0
- package/dist/filtering/excel-filters.d.ts +26 -0
- package/dist/filtering/excel-filters.js +64 -0
- package/dist/filtering/excel-filters.test.d.ts +1 -0
- package/dist/filtering/excel-filters.test.js +116 -0
- package/dist/filtering/locale-filter.test.d.ts +13 -0
- package/dist/filtering/locale-filter.test.js +189 -0
- package/dist/flex-render.d.ts +1 -0
- package/dist/flex-render.js +2 -0
- package/dist/flex-render.test.d.ts +1 -0
- package/dist/flex-render.test.js +142 -0
- package/dist/group-aggregate.test.d.ts +1 -0
- package/dist/group-aggregate.test.js +32 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +27 -0
- package/dist/keyboard.d.ts +8 -0
- package/dist/keyboard.js +91 -0
- package/dist/keyboard.test.d.ts +1 -0
- package/dist/keyboard.test.js +27 -0
- package/dist/merge-objects.d.ts +9 -0
- package/dist/merge-objects.js +28 -0
- package/dist/named-views.d.ts +42 -0
- package/dist/named-views.js +80 -0
- package/dist/named-views.test.d.ts +1 -0
- package/dist/named-views.test.js +57 -0
- package/dist/new-features.test.d.ts +1 -0
- package/dist/new-features.test.js +183 -0
- package/dist/render-component.d.ts +13 -0
- package/dist/render-component.js +14 -0
- package/dist/render-component.test.d.ts +1 -0
- package/dist/render-component.test.js +41 -0
- package/dist/server-data-source.d.ts +76 -0
- package/dist/server-data-source.js +83 -0
- package/dist/server-data-source.test.d.ts +1 -0
- package/dist/server-data-source.test.js +81 -0
- package/dist/sparkline.d.ts +56 -0
- package/dist/sparkline.js +97 -0
- package/dist/sparkline.test.d.ts +1 -0
- package/dist/sparkline.test.js +52 -0
- package/dist/static-functions.d.ts +1 -0
- package/dist/static-functions.js +1 -0
- package/dist/subscribe.d.ts +9 -0
- package/dist/subscribe.js +31 -0
- package/dist/subscribe.test.d.ts +1 -0
- package/dist/subscribe.test.js +93 -0
- package/dist/sv-grid-scrollbar.d.ts +1 -0
- package/dist/sv-grid-scrollbar.js +310 -0
- package/dist/svgrid-wrapper.types.d.ts +341 -0
- package/dist/svgrid-wrapper.types.js +1 -0
- package/dist/svgrid.api-extensions.test.d.ts +14 -0
- package/dist/svgrid.api-extensions.test.js +334 -0
- package/dist/svgrid.api.test.d.ts +15 -0
- package/dist/svgrid.api.test.js +169 -0
- package/dist/svgrid.behavior.test.d.ts +15 -0
- package/dist/svgrid.behavior.test.js +605 -0
- package/dist/svgrid.column-reorder.test.d.ts +15 -0
- package/dist/svgrid.column-reorder.test.js +224 -0
- package/dist/svgrid.features.test.d.ts +1 -0
- package/dist/svgrid.features.test.js +129 -0
- package/dist/svgrid.interaction.test.d.ts +9 -0
- package/dist/svgrid.interaction.test.js +318 -0
- package/dist/svgrid.locale-filtering.test.d.ts +15 -0
- package/dist/svgrid.locale-filtering.test.js +261 -0
- package/dist/svgrid.new-features.wrapper.test.d.ts +1 -0
- package/dist/svgrid.new-features.wrapper.test.js +136 -0
- package/dist/svgrid.row-pinning.test.d.ts +14 -0
- package/dist/svgrid.row-pinning.test.js +250 -0
- package/dist/svgrid.set-filter.test.d.ts +17 -0
- package/dist/svgrid.set-filter.test.js +268 -0
- package/dist/svgrid.wrapper.test.d.ts +1 -0
- package/dist/svgrid.wrapper.test.js +33 -0
- package/dist/test-setup.d.ts +1 -0
- package/dist/test-setup.js +29 -0
- package/dist/transaction.test.d.ts +1 -0
- package/dist/transaction.test.js +89 -0
- package/dist/virtualization/column-virtualizer.d.ts +20 -0
- package/dist/virtualization/column-virtualizer.js +19 -0
- package/dist/virtualization/column-virtualizer.test.d.ts +1 -0
- package/dist/virtualization/column-virtualizer.test.js +25 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +11 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +20 -0
- package/dist/virtualization/types.d.ts +28 -0
- package/dist/virtualization/types.js +1 -0
- package/dist/virtualization/virtualizer.d.ts +13 -0
- package/dist/virtualization/virtualizer.js +232 -0
- package/dist/virtualization/virtualizer.test.d.ts +1 -0
- package/dist/virtualization/virtualizer.test.js +41 -0
- package/package.json +69 -0
- package/src/FlexRender.svelte +96 -0
- package/src/SvGrid.svelte +8742 -0
- package/src/SvGridChart.svelte +653 -0
- package/src/SvGridChart.test.ts +69 -0
- package/src/SvGridDropdown.svelte +675 -0
- package/src/a11y.contract.test.ts +49 -0
- package/src/a11y.test.ts +59 -0
- package/src/a11y.ts +59 -0
- package/src/cell-formatting.test.ts +273 -0
- package/src/cell-formatting.ts +169 -0
- package/src/chart-export.ts +144 -0
- package/src/chart.test.ts +311 -0
- package/src/chart.ts +934 -0
- package/src/collaboration.test.ts +74 -0
- package/src/collaboration.ts +161 -0
- package/src/conditional-formatting.test.ts +107 -0
- package/src/conditional-formatting.ts +239 -0
- package/src/core.coverage.test.ts +230 -0
- package/src/core.performance.test.ts +30 -0
- package/src/core.ts +991 -0
- package/src/createGrid.svelte.ts +42 -0
- package/src/createGrid.test.ts +10 -0
- package/src/createGridState.svelte.ts +17 -0
- package/src/editors/cell-editors.test.ts +80 -0
- package/src/editors/cell-editors.ts +127 -0
- package/src/fill-patterns.test.ts +93 -0
- package/src/fill-patterns.ts +229 -0
- package/src/filtering/excel-filters.test.ts +131 -0
- package/src/filtering/excel-filters.ts +101 -0
- package/src/filtering/locale-filter.test.ts +220 -0
- package/src/flex-render.test.ts +143 -0
- package/src/flex-render.ts +3 -0
- package/src/group-aggregate.test.ts +48 -0
- package/src/index.ts +159 -0
- package/src/keyboard.test.ts +59 -0
- package/src/keyboard.ts +97 -0
- package/src/merge-objects.ts +48 -0
- package/src/named-views.test.ts +66 -0
- package/src/named-views.ts +120 -0
- package/src/new-features.test.ts +217 -0
- package/src/render-component.test.ts +51 -0
- package/src/render-component.ts +28 -0
- package/src/server-data-source.test.ts +88 -0
- package/src/server-data-source.ts +163 -0
- package/src/sparkline.test.ts +59 -0
- package/src/sparkline.ts +164 -0
- package/src/static-functions.ts +11 -0
- package/src/subscribe.test.ts +103 -0
- package/src/subscribe.ts +38 -0
- package/src/sv-grid-scrollbar.ts +347 -0
- package/src/svgrid-wrapper.types.ts +382 -0
- package/src/svgrid.api-extensions.test.ts +362 -0
- package/src/svgrid.api.test.ts +192 -0
- package/src/svgrid.behavior.test.ts +657 -0
- package/src/svgrid.column-reorder.test.ts +234 -0
- package/src/svgrid.features.test.ts +157 -0
- package/src/svgrid.interaction.test.ts +355 -0
- package/src/svgrid.locale-filtering.test.ts +259 -0
- package/src/svgrid.new-features.wrapper.test.ts +164 -0
- package/src/svgrid.row-pinning.test.ts +266 -0
- package/src/svgrid.set-filter.test.ts +270 -0
- package/src/svgrid.wrapper.test.ts +35 -0
- package/src/test-setup.ts +37 -0
- package/src/transaction.test.ts +100 -0
- package/src/virtualization/column-virtualizer.test.ts +27 -0
- package/src/virtualization/column-virtualizer.ts +30 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +24 -0
- package/src/virtualization/types.ts +30 -0
- package/src/virtualization/virtualizer.test.ts +47 -0
- package/src/virtualization/virtualizer.ts +270 -0
package/dist/keyboard.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export function getKeyboardIntent(event) {
|
|
2
|
+
// Ctrl+Home / Ctrl+End come before plain Home / End so the modifier wins.
|
|
3
|
+
if (event.ctrlKey && event.key === 'Home')
|
|
4
|
+
return 'gridStart';
|
|
5
|
+
if (event.ctrlKey && event.key === 'End')
|
|
6
|
+
return 'gridEnd';
|
|
7
|
+
if (event.key === 'ArrowLeft')
|
|
8
|
+
return 'moveLeft';
|
|
9
|
+
if (event.key === 'ArrowRight')
|
|
10
|
+
return 'moveRight';
|
|
11
|
+
if (event.key === 'ArrowUp')
|
|
12
|
+
return 'moveUp';
|
|
13
|
+
if (event.key === 'ArrowDown')
|
|
14
|
+
return 'moveDown';
|
|
15
|
+
if (event.key === 'Home')
|
|
16
|
+
return 'rowStart';
|
|
17
|
+
if (event.key === 'End')
|
|
18
|
+
return 'rowEnd';
|
|
19
|
+
if (event.key === 'PageUp')
|
|
20
|
+
return 'pageUp';
|
|
21
|
+
if (event.key === 'PageDown')
|
|
22
|
+
return 'pageDown';
|
|
23
|
+
// Excel-style data-entry navigation:
|
|
24
|
+
// Enter → move down (Shift+Enter → up)
|
|
25
|
+
// Tab → move right (Shift+Tab → left), wraps at row boundaries
|
|
26
|
+
// Delete → clear contents of the selected cells
|
|
27
|
+
// F2 / Space → start editing the active cell
|
|
28
|
+
if (event.key === 'Enter')
|
|
29
|
+
return event.shiftKey ? 'moveUp' : 'moveDown';
|
|
30
|
+
if (event.key === 'Tab')
|
|
31
|
+
return event.shiftKey ? 'tabPrev' : 'tabNext';
|
|
32
|
+
if (event.key === 'F2')
|
|
33
|
+
return 'activate';
|
|
34
|
+
if (event.key === ' ')
|
|
35
|
+
return 'activate';
|
|
36
|
+
if (event.key === 'Delete')
|
|
37
|
+
return 'clearCells';
|
|
38
|
+
return 'noop';
|
|
39
|
+
}
|
|
40
|
+
export function getNextActiveCell(current, intent, bounds) {
|
|
41
|
+
const pageSize = bounds.pageSize ?? 10;
|
|
42
|
+
let rowIndex = current.rowIndex;
|
|
43
|
+
let colIndex = current.colIndex;
|
|
44
|
+
if (intent === 'moveLeft')
|
|
45
|
+
colIndex -= 1;
|
|
46
|
+
if (intent === 'moveRight')
|
|
47
|
+
colIndex += 1;
|
|
48
|
+
if (intent === 'moveUp')
|
|
49
|
+
rowIndex -= 1;
|
|
50
|
+
if (intent === 'moveDown')
|
|
51
|
+
rowIndex += 1;
|
|
52
|
+
if (intent === 'tabNext') {
|
|
53
|
+
// Right one column; if we fall off the right edge, wrap to the
|
|
54
|
+
// first column of the next row - Excel's "data entry" behavior.
|
|
55
|
+
colIndex += 1;
|
|
56
|
+
if (colIndex > bounds.maxCol) {
|
|
57
|
+
colIndex = 0;
|
|
58
|
+
rowIndex += 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (intent === 'tabPrev') {
|
|
62
|
+
colIndex -= 1;
|
|
63
|
+
if (colIndex < 0) {
|
|
64
|
+
colIndex = bounds.maxCol;
|
|
65
|
+
rowIndex -= 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (intent === 'rowStart')
|
|
69
|
+
colIndex = 0;
|
|
70
|
+
if (intent === 'rowEnd')
|
|
71
|
+
colIndex = bounds.maxCol;
|
|
72
|
+
if (intent === 'gridStart') {
|
|
73
|
+
rowIndex = 0;
|
|
74
|
+
colIndex = 0;
|
|
75
|
+
}
|
|
76
|
+
if (intent === 'gridEnd') {
|
|
77
|
+
rowIndex = bounds.maxRow;
|
|
78
|
+
colIndex = bounds.maxCol;
|
|
79
|
+
}
|
|
80
|
+
if (intent === 'pageUp')
|
|
81
|
+
rowIndex -= pageSize;
|
|
82
|
+
if (intent === 'pageDown')
|
|
83
|
+
rowIndex += pageSize;
|
|
84
|
+
rowIndex = Math.min(Math.max(rowIndex, 0), Math.max(bounds.maxRow, 0));
|
|
85
|
+
colIndex = Math.min(Math.max(colIndex, 0), Math.max(bounds.maxCol, 0));
|
|
86
|
+
return {
|
|
87
|
+
rowIndex,
|
|
88
|
+
colIndex,
|
|
89
|
+
cellId: current.cellId,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getKeyboardIntent, getNextActiveCell } from './keyboard';
|
|
3
|
+
describe('keyboard navigation engine', () => {
|
|
4
|
+
it('maps keys to navigation intents', () => {
|
|
5
|
+
expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: 'ArrowLeft' }))).toBe('moveLeft');
|
|
6
|
+
expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: 'Home', ctrlKey: true }))).toBe('gridStart');
|
|
7
|
+
expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: ' ' }))).toBe('activate');
|
|
8
|
+
});
|
|
9
|
+
it('computes next active cell within bounds', () => {
|
|
10
|
+
const next = getNextActiveCell({ rowIndex: 1, colIndex: 1, cellId: null }, 'moveDown', { maxRow: 2, maxCol: 3, pageSize: 10 });
|
|
11
|
+
expect(next.rowIndex).toBe(2);
|
|
12
|
+
expect(next.colIndex).toBe(1);
|
|
13
|
+
});
|
|
14
|
+
it('handles row and grid boundary intents', () => {
|
|
15
|
+
const rowEnd = getNextActiveCell({ rowIndex: 2, colIndex: 1, cellId: null }, 'rowEnd', { maxRow: 2, maxCol: 4, pageSize: 2 });
|
|
16
|
+
expect(rowEnd.colIndex).toBe(4);
|
|
17
|
+
const gridStart = getNextActiveCell({ rowIndex: 2, colIndex: 4, cellId: null }, 'gridStart', { maxRow: 2, maxCol: 4, pageSize: 2 });
|
|
18
|
+
expect(gridStart.rowIndex).toBe(0);
|
|
19
|
+
expect(gridStart.colIndex).toBe(0);
|
|
20
|
+
});
|
|
21
|
+
it('clamps page navigation to valid bounds', () => {
|
|
22
|
+
const pageUp = getNextActiveCell({ rowIndex: 1, colIndex: 0, cellId: null }, 'pageUp', { maxRow: 20, maxCol: 2, pageSize: 10 });
|
|
23
|
+
const pageDown = getNextActiveCell({ rowIndex: 18, colIndex: 0, cellId: null }, 'pageDown', { maxRow: 20, maxCol: 2, pageSize: 10 });
|
|
24
|
+
expect(pageUp.rowIndex).toBe(0);
|
|
25
|
+
expect(pageDown.rowIndex).toBe(20);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges objects together while keeping their getters alive.
|
|
3
|
+
* Taken from SolidJS:
|
|
4
|
+
* https://github.com/solidjs/solid/blob/24abc825c0996fd2bc8c1de1491efe9a7e743aff/packages/solid/src/server/rendering.ts#L82-L115
|
|
5
|
+
*/
|
|
6
|
+
export declare function mergeObjects<T>(source: T): T;
|
|
7
|
+
export declare function mergeObjects<T, U>(source: T, source1: U): T & U;
|
|
8
|
+
export declare function mergeObjects<T, U, V>(source: T, source1: U, source2: V): T & U & V;
|
|
9
|
+
export declare function mergeObjects<T, U, V, W>(source: T, source1: U, source2: V, source3: W): T & U & V & W;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function mergeObjects(...sources) {
|
|
2
|
+
const target = {};
|
|
3
|
+
for (let source of sources) {
|
|
4
|
+
if (typeof source === 'function')
|
|
5
|
+
source = source();
|
|
6
|
+
if (source) {
|
|
7
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
8
|
+
for (const key in descriptors) {
|
|
9
|
+
if (key in target)
|
|
10
|
+
continue;
|
|
11
|
+
Object.defineProperty(target, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get() {
|
|
14
|
+
for (let i = sources.length - 1; i >= 0; i--) {
|
|
15
|
+
let scopedSource = sources[i];
|
|
16
|
+
if (typeof scopedSource === 'function')
|
|
17
|
+
scopedSource = scopedSource();
|
|
18
|
+
const value = (scopedSource || {})[key];
|
|
19
|
+
if (value !== undefined)
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named views. A thin, packaged manager on top of `api.getState()` /
|
|
3
|
+
* `api.setState()` so the "save / restore named layouts" feature every
|
|
4
|
+
* enterprise grid ships is one import instead of hand-rolled glue.
|
|
5
|
+
*
|
|
6
|
+
* Storage is pluggable: `localStorageViews(key)` persists per browser, or
|
|
7
|
+
* pass your own adapter to sync views to a server / per-user account. The
|
|
8
|
+
* manager itself is pure and synchronous, so it is trivially testable.
|
|
9
|
+
*/
|
|
10
|
+
import type { SvGridViewState } from './svgrid-wrapper.types';
|
|
11
|
+
export type SavedView = {
|
|
12
|
+
name: string;
|
|
13
|
+
state: Partial<SvGridViewState>;
|
|
14
|
+
createdAt: number;
|
|
15
|
+
};
|
|
16
|
+
export type ViewStorage = {
|
|
17
|
+
read(): SavedView[];
|
|
18
|
+
write(views: SavedView[]): void;
|
|
19
|
+
};
|
|
20
|
+
/** Minimal grid handle the manager needs - `SvGridApi` satisfies it. */
|
|
21
|
+
export type ViewStateHost = {
|
|
22
|
+
getState(): SvGridViewState;
|
|
23
|
+
setState(state: Partial<SvGridViewState>): void;
|
|
24
|
+
};
|
|
25
|
+
export type NamedViews = {
|
|
26
|
+
list(): SavedView[];
|
|
27
|
+
/** Capture the grid's current state under `name` (overwrites a duplicate). */
|
|
28
|
+
save(name: string): SavedView;
|
|
29
|
+
/** Apply a saved view to the grid. Returns false if the name is unknown. */
|
|
30
|
+
load(name: string): boolean;
|
|
31
|
+
/** Delete a saved view. Returns false if the name is unknown. */
|
|
32
|
+
remove(name: string): boolean;
|
|
33
|
+
rename(from: string, to: string): boolean;
|
|
34
|
+
has(name: string): boolean;
|
|
35
|
+
};
|
|
36
|
+
/** In-memory storage (default). State lives only as long as the manager. */
|
|
37
|
+
export declare function memoryViews(initial?: SavedView[]): ViewStorage;
|
|
38
|
+
/** localStorage-backed storage. Safe to construct in SSR (no-ops there). */
|
|
39
|
+
export declare function localStorageViews(key: string): ViewStorage;
|
|
40
|
+
export declare function createNamedViews(host: ViewStateHost, options?: {
|
|
41
|
+
storage?: ViewStorage;
|
|
42
|
+
}): NamedViews;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** In-memory storage (default). State lives only as long as the manager. */
|
|
2
|
+
export function memoryViews(initial = []) {
|
|
3
|
+
let views = [...initial];
|
|
4
|
+
return {
|
|
5
|
+
read: () => views,
|
|
6
|
+
write: (next) => {
|
|
7
|
+
views = next;
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** localStorage-backed storage. Safe to construct in SSR (no-ops there). */
|
|
12
|
+
export function localStorageViews(key) {
|
|
13
|
+
const available = typeof localStorage !== 'undefined';
|
|
14
|
+
return {
|
|
15
|
+
read() {
|
|
16
|
+
if (!available)
|
|
17
|
+
return [];
|
|
18
|
+
try {
|
|
19
|
+
const raw = localStorage.getItem(key);
|
|
20
|
+
const parsed = raw ? JSON.parse(raw) : [];
|
|
21
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
write(views) {
|
|
28
|
+
if (!available)
|
|
29
|
+
return;
|
|
30
|
+
try {
|
|
31
|
+
localStorage.setItem(key, JSON.stringify(views));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
/* quota / private mode - ignore */
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function createNamedViews(host, options = {}) {
|
|
40
|
+
const storage = options.storage ?? memoryViews();
|
|
41
|
+
const all = () => storage.read();
|
|
42
|
+
const find = (name) => all().find((v) => v.name === name);
|
|
43
|
+
return {
|
|
44
|
+
list: () => all().slice().sort((a, b) => a.createdAt - b.createdAt),
|
|
45
|
+
has: (name) => Boolean(find(name)),
|
|
46
|
+
save(name) {
|
|
47
|
+
const view = {
|
|
48
|
+
name,
|
|
49
|
+
state: host.getState(),
|
|
50
|
+
createdAt: find(name)?.createdAt ?? Date.now(),
|
|
51
|
+
};
|
|
52
|
+
storage.write([...all().filter((v) => v.name !== name), view]);
|
|
53
|
+
return view;
|
|
54
|
+
},
|
|
55
|
+
load(name) {
|
|
56
|
+
const view = find(name);
|
|
57
|
+
if (!view)
|
|
58
|
+
return false;
|
|
59
|
+
host.setState(view.state);
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
remove(name) {
|
|
63
|
+
const next = all().filter((v) => v.name !== name);
|
|
64
|
+
if (next.length === all().length)
|
|
65
|
+
return false;
|
|
66
|
+
storage.write(next);
|
|
67
|
+
return true;
|
|
68
|
+
},
|
|
69
|
+
rename(from, to) {
|
|
70
|
+
const view = find(from);
|
|
71
|
+
if (!view || find(to))
|
|
72
|
+
return false;
|
|
73
|
+
storage.write([
|
|
74
|
+
...all().filter((v) => v.name !== from),
|
|
75
|
+
{ ...view, name: to },
|
|
76
|
+
]);
|
|
77
|
+
return true;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createNamedViews, memoryViews } from './named-views';
|
|
3
|
+
function fakeHost(initial = {}) {
|
|
4
|
+
let state = { sorting: [], columnFilters: [], ...initial };
|
|
5
|
+
return {
|
|
6
|
+
getState: () => state,
|
|
7
|
+
setState: vi.fn((s) => {
|
|
8
|
+
state = { ...state, ...s };
|
|
9
|
+
}),
|
|
10
|
+
_peek: () => state,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
describe('createNamedViews', () => {
|
|
14
|
+
it('saves the current state and lists it', () => {
|
|
15
|
+
const host = fakeHost({ sorting: [{ id: 'name', desc: true }] });
|
|
16
|
+
const views = createNamedViews(host, { storage: memoryViews() });
|
|
17
|
+
views.save('By name');
|
|
18
|
+
expect(views.list().map((v) => v.name)).toEqual(['By name']);
|
|
19
|
+
expect(views.has('By name')).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
it('load applies the saved state via setState', () => {
|
|
22
|
+
const host = fakeHost({ sorting: [{ id: 'age', desc: false }] });
|
|
23
|
+
const views = createNamedViews(host, { storage: memoryViews() });
|
|
24
|
+
views.save('snap');
|
|
25
|
+
host.setState({ sorting: [] }); // mutate away
|
|
26
|
+
host.setState.mockClear();
|
|
27
|
+
expect(views.load('snap')).toBe(true);
|
|
28
|
+
expect(host.setState).toHaveBeenCalledWith(expect.objectContaining({ sorting: [{ id: 'age', desc: false }] }));
|
|
29
|
+
expect(views.load('missing')).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
it('save overwrites a duplicate name but keeps its createdAt', () => {
|
|
32
|
+
const host = fakeHost();
|
|
33
|
+
const views = createNamedViews(host, { storage: memoryViews() });
|
|
34
|
+
const a = views.save('v');
|
|
35
|
+
const b = views.save('v');
|
|
36
|
+
expect(views.list()).toHaveLength(1);
|
|
37
|
+
expect(b.createdAt).toBe(a.createdAt);
|
|
38
|
+
});
|
|
39
|
+
it('remove and rename', () => {
|
|
40
|
+
const host = fakeHost();
|
|
41
|
+
const views = createNamedViews(host, { storage: memoryViews() });
|
|
42
|
+
views.save('a');
|
|
43
|
+
views.save('b');
|
|
44
|
+
expect(views.rename('a', 'c')).toBe(true);
|
|
45
|
+
expect(views.rename('c', 'b')).toBe(false); // target exists
|
|
46
|
+
expect(views.remove('c')).toBe(true);
|
|
47
|
+
expect(views.remove('c')).toBe(false);
|
|
48
|
+
expect(views.list().map((v) => v.name)).toEqual(['b']);
|
|
49
|
+
});
|
|
50
|
+
it('persists through a storage adapter', () => {
|
|
51
|
+
const store = memoryViews();
|
|
52
|
+
createNamedViews(fakeHost({ sorting: [{ id: 'x', desc: true }] }), { storage: store }).save('keep');
|
|
53
|
+
// A fresh manager over the same storage sees it.
|
|
54
|
+
const reopened = createNamedViews(fakeHost(), { storage: store });
|
|
55
|
+
expect(reopened.has('keep')).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the headless-engine features shipped alongside the wrapper
|
|
3
|
+
* additions (`getRowId`, the `between` filter operator). Wrapper-level
|
|
4
|
+
* additions are covered by the source-level smoke tests in
|
|
5
|
+
* `svgrid.new-features.wrapper.test.ts` - jsdom + Svelte 5 mount is
|
|
6
|
+
* brittle for tests that only need to confirm props are read.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
import { columnFilteringFeature, createCoreRowModel, createSvGrid, rowSortingFeature, tableFeatures, } from './index';
|
|
10
|
+
import { applyExcelFilter } from './filtering/excel-filters';
|
|
11
|
+
const data = [
|
|
12
|
+
{ id: 'ada', name: 'Ada Lovelace', age: 36 },
|
|
13
|
+
{ id: 'linus', name: 'Linus Torvalds', age: 54 },
|
|
14
|
+
{ id: 'grace', name: 'Grace Hopper', age: 85 },
|
|
15
|
+
];
|
|
16
|
+
const columns = [
|
|
17
|
+
{ field: 'id', header: 'ID' },
|
|
18
|
+
{ field: 'name', header: 'Name' },
|
|
19
|
+
{ field: 'age', header: 'Age' },
|
|
20
|
+
];
|
|
21
|
+
const features = tableFeatures({ rowSortingFeature });
|
|
22
|
+
describe('createSvGrid - getRowId', () => {
|
|
23
|
+
it('falls back to the array index as id when no getRowId is provided', () => {
|
|
24
|
+
const grid = createSvGrid({
|
|
25
|
+
_features: features,
|
|
26
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
27
|
+
columns,
|
|
28
|
+
data,
|
|
29
|
+
});
|
|
30
|
+
const rows = grid.getRowModel().rows;
|
|
31
|
+
expect(rows.map((r) => r.id)).toEqual(['0', '1', '2']);
|
|
32
|
+
});
|
|
33
|
+
it('uses the result of getRowId(row, index) as the row id', () => {
|
|
34
|
+
const grid = createSvGrid({
|
|
35
|
+
_features: features,
|
|
36
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
37
|
+
columns,
|
|
38
|
+
data,
|
|
39
|
+
getRowId: (row) => row.id,
|
|
40
|
+
});
|
|
41
|
+
const rows = grid.getRowModel().rows;
|
|
42
|
+
expect(rows.map((r) => r.id)).toEqual(['ada', 'linus', 'grace']);
|
|
43
|
+
});
|
|
44
|
+
it('passes the row index to getRowId', () => {
|
|
45
|
+
const seen = [];
|
|
46
|
+
createSvGrid({
|
|
47
|
+
_features: features,
|
|
48
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
49
|
+
columns,
|
|
50
|
+
data,
|
|
51
|
+
getRowId: (row, index) => {
|
|
52
|
+
seen.push({ id: row.id, index });
|
|
53
|
+
return row.id;
|
|
54
|
+
},
|
|
55
|
+
}).getRowModel().rows;
|
|
56
|
+
expect(seen).toEqual([
|
|
57
|
+
{ id: 'ada', index: 0 },
|
|
58
|
+
{ id: 'linus', index: 1 },
|
|
59
|
+
{ id: 'grace', index: 2 },
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
it('preserves the same id across `getRowModel()` invocations (stable identity)', () => {
|
|
63
|
+
const grid = createSvGrid({
|
|
64
|
+
_features: features,
|
|
65
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
66
|
+
columns,
|
|
67
|
+
data,
|
|
68
|
+
getRowId: (row) => `r:${row.id}`,
|
|
69
|
+
});
|
|
70
|
+
const first = grid.getRowModel().rows.map((r) => r.id);
|
|
71
|
+
const second = grid.getRowModel().rows.map((r) => r.id);
|
|
72
|
+
expect(first).toEqual(second);
|
|
73
|
+
expect(first).toEqual(['r:ada', 'r:linus', 'r:grace']);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('applyExcelFilter - between operator', () => {
|
|
77
|
+
it('returns true when the value is inside the inclusive range', () => {
|
|
78
|
+
expect(applyExcelFilter(50, { id: 'age', operator: 'between', value: 18, valueTo: 65 })).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
it('is inclusive at the lower bound', () => {
|
|
81
|
+
expect(applyExcelFilter(18, { id: 'age', operator: 'between', value: 18, valueTo: 65 })).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
it('is inclusive at the upper bound', () => {
|
|
84
|
+
expect(applyExcelFilter(65, { id: 'age', operator: 'between', value: 18, valueTo: 65 })).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
it('returns false when the value is below the range', () => {
|
|
87
|
+
expect(applyExcelFilter(17, { id: 'age', operator: 'between', value: 18, valueTo: 65 })).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
it('returns false when the value is above the range', () => {
|
|
90
|
+
expect(applyExcelFilter(66, { id: 'age', operator: 'between', value: 18, valueTo: 65 })).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
it('parses string endpoints as numbers (the wrapper hands strings through)', () => {
|
|
93
|
+
expect(applyExcelFilter('30', { id: 'age', operator: 'between', value: '18', valueTo: '65' })).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
it('works with ISO date strings via lexicographic compare', () => {
|
|
96
|
+
expect(applyExcelFilter('2026-06-15', {
|
|
97
|
+
id: 'd', operator: 'between', value: '2026-01-01', valueTo: '2026-12-31',
|
|
98
|
+
})).toBe(true);
|
|
99
|
+
expect(applyExcelFilter('2025-12-31', {
|
|
100
|
+
id: 'd', operator: 'between', value: '2026-01-01', valueTo: '2026-12-31',
|
|
101
|
+
})).toBe(false);
|
|
102
|
+
expect(applyExcelFilter('2027-01-01', {
|
|
103
|
+
id: 'd', operator: 'between', value: '2026-01-01', valueTo: '2026-12-31',
|
|
104
|
+
})).toBe(false);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('ColumnDef.sortable + ColumnDef.filterable', () => {
|
|
108
|
+
const features = tableFeatures({
|
|
109
|
+
rowSortingFeature,
|
|
110
|
+
columnFilteringFeature,
|
|
111
|
+
});
|
|
112
|
+
function columnsWith(opts) {
|
|
113
|
+
return [
|
|
114
|
+
{ field: 'id', header: 'ID' },
|
|
115
|
+
{ field: 'name', header: 'Name', sortable: opts.nameSortable, filterable: opts.nameFilterable },
|
|
116
|
+
{ field: 'age', header: 'Age', sortable: opts.ageSortable },
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
it('getCanSort() returns true by default when rowSortingFeature is on', () => {
|
|
120
|
+
const grid = createSvGrid({
|
|
121
|
+
_features: features,
|
|
122
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
123
|
+
data,
|
|
124
|
+
columns: columnsWith({}),
|
|
125
|
+
});
|
|
126
|
+
const name = grid.getAllColumns().find((c) => c.id === 'name');
|
|
127
|
+
expect(name?.getCanSort()).toBe(true);
|
|
128
|
+
});
|
|
129
|
+
it('getCanSort() returns false when columnDef.sortable === false', () => {
|
|
130
|
+
const grid = createSvGrid({
|
|
131
|
+
_features: features,
|
|
132
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
133
|
+
data,
|
|
134
|
+
columns: columnsWith({ nameSortable: false }),
|
|
135
|
+
});
|
|
136
|
+
const name = grid.getAllColumns().find((c) => c.id === 'name');
|
|
137
|
+
const age = grid.getAllColumns().find((c) => c.id === 'age');
|
|
138
|
+
expect(name?.getCanSort()).toBe(false);
|
|
139
|
+
expect(age?.getCanSort()).toBe(true);
|
|
140
|
+
});
|
|
141
|
+
it('getCanSort() still returns false even with sortable: true if the feature is OFF', () => {
|
|
142
|
+
const noSortFeatures = tableFeatures({ columnFilteringFeature });
|
|
143
|
+
const grid = createSvGrid({
|
|
144
|
+
_features: noSortFeatures,
|
|
145
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
146
|
+
data,
|
|
147
|
+
columns: [
|
|
148
|
+
{ field: 'name', header: 'Name', sortable: true },
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
const name = grid.getAllColumns().find((c) => c.id === 'name');
|
|
152
|
+
expect(name?.getCanSort()).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
it('getCanFilter() returns false when columnDef.filterable === false', () => {
|
|
155
|
+
const grid = createSvGrid({
|
|
156
|
+
_features: features,
|
|
157
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
158
|
+
data,
|
|
159
|
+
columns: columnsWith({ nameFilterable: false }),
|
|
160
|
+
});
|
|
161
|
+
const name = grid.getAllColumns().find((c) => c.id === 'name');
|
|
162
|
+
const age = grid.getAllColumns().find((c) => c.id === 'age');
|
|
163
|
+
expect(name?.getCanFilter()).toBe(false);
|
|
164
|
+
expect(age?.getCanFilter()).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
it('sortable + filterable are independent', () => {
|
|
167
|
+
const grid = createSvGrid({
|
|
168
|
+
_features: features,
|
|
169
|
+
_rowModels: { coreRowModel: createCoreRowModel() },
|
|
170
|
+
data,
|
|
171
|
+
columns: [
|
|
172
|
+
{ field: 'name', header: 'Name', sortable: false, filterable: true },
|
|
173
|
+
{ field: 'age', header: 'Age', sortable: true, filterable: false },
|
|
174
|
+
],
|
|
175
|
+
});
|
|
176
|
+
const name = grid.getAllColumns().find((c) => c.id === 'name');
|
|
177
|
+
const age = grid.getAllColumns().find((c) => c.id === 'age');
|
|
178
|
+
expect(name?.getCanSort()).toBe(false);
|
|
179
|
+
expect(name?.getCanFilter()).toBe(true);
|
|
180
|
+
expect(age?.getCanSort()).toBe(true);
|
|
181
|
+
expect(age?.getCanFilter()).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Component, ComponentProps, Snippet } from 'svelte';
|
|
2
|
+
export declare class RenderComponentConfig<TComponent extends Component> {
|
|
3
|
+
component: TComponent;
|
|
4
|
+
props?: (ComponentProps<TComponent> | Record<string, never>) | undefined;
|
|
5
|
+
constructor(component: TComponent, props?: (ComponentProps<TComponent> | Record<string, never>) | undefined);
|
|
6
|
+
}
|
|
7
|
+
export declare class RenderSnippetConfig<TProps> {
|
|
8
|
+
snippet: Snippet<[TProps]>;
|
|
9
|
+
params?: TProps | undefined;
|
|
10
|
+
constructor(snippet: Snippet<[TProps]>, params?: TProps | undefined);
|
|
11
|
+
}
|
|
12
|
+
export declare const renderComponent: <TComponent extends Component<any>, TProps extends ComponentProps<TComponent>>(component: TComponent, props?: TProps) => RenderComponentConfig<TComponent>;
|
|
13
|
+
export declare const renderSnippet: <TProps>(snippet: Snippet<[TProps]>, params?: TProps) => RenderSnippetConfig<TProps>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class RenderComponentConfig {
|
|
2
|
+
constructor(component, props) {
|
|
3
|
+
this.component = component;
|
|
4
|
+
this.props = props;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class RenderSnippetConfig {
|
|
8
|
+
constructor(snippet, params) {
|
|
9
|
+
this.snippet = snippet;
|
|
10
|
+
this.params = params;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export const renderComponent = (component, props) => new RenderComponentConfig(component, props);
|
|
14
|
+
export const renderSnippet = (snippet, params) => new RenderSnippetConfig(snippet, params);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for the renderComponent / renderSnippet factories.
|
|
3
|
+
*/
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { RenderComponentConfig, RenderSnippetConfig, renderComponent, renderSnippet, } from './render-component';
|
|
6
|
+
describe('renderComponent', () => {
|
|
7
|
+
it('returns a RenderComponentConfig instance', () => {
|
|
8
|
+
const FakeComponent = (() => null);
|
|
9
|
+
const config = renderComponent(FakeComponent);
|
|
10
|
+
expect(config).toBeInstanceOf(RenderComponentConfig);
|
|
11
|
+
expect(config.component).toBe(FakeComponent);
|
|
12
|
+
expect(config.props).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
it('captures the props payload as-is', () => {
|
|
15
|
+
const FakeComponent = (() => null);
|
|
16
|
+
const config = renderComponent(FakeComponent, { id: 1, label: 'Hello' });
|
|
17
|
+
expect(config.props).toEqual({ id: 1, label: 'Hello' });
|
|
18
|
+
});
|
|
19
|
+
it('reuses the constructor instance fields', () => {
|
|
20
|
+
const FakeComponent = (() => null);
|
|
21
|
+
const a = renderComponent(FakeComponent, { x: 1 });
|
|
22
|
+
const b = renderComponent(FakeComponent, { x: 1 });
|
|
23
|
+
// Different instances - the factory does not memoize.
|
|
24
|
+
expect(a).not.toBe(b);
|
|
25
|
+
expect(a.component).toBe(b.component);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
describe('renderSnippet', () => {
|
|
29
|
+
it('returns a RenderSnippetConfig instance with the captured snippet + params', () => {
|
|
30
|
+
const fakeSnippet = (() => null);
|
|
31
|
+
const config = renderSnippet(fakeSnippet, { value: 42 });
|
|
32
|
+
expect(config).toBeInstanceOf(RenderSnippetConfig);
|
|
33
|
+
expect(config.snippet).toBe(fakeSnippet);
|
|
34
|
+
expect(config.params).toEqual({ value: 42 });
|
|
35
|
+
});
|
|
36
|
+
it('allows an undefined params payload', () => {
|
|
37
|
+
const fakeSnippet = (() => null);
|
|
38
|
+
const config = renderSnippet(fakeSnippet);
|
|
39
|
+
expect(config.params).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
});
|