@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
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditional formatting engine. Excel-style declarative rules that color a
|
|
3
|
+
* cell by its value - color scales, in-cell data bars, icon sets, and plain
|
|
4
|
+
* predicate rules. This goes beyond `cellClass(ctx)` (which can only toggle
|
|
5
|
+
* static classes) because color scales and data bars need a value computed
|
|
6
|
+
* against the column's min/max range.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is pure: `resolveCellFormat` takes a value + the column's
|
|
9
|
+
* numeric range and returns the visual primitives, so the `<SvGrid>` render
|
|
10
|
+
* component just paints the result and the logic is unit-testable.
|
|
11
|
+
*/
|
|
12
|
+
const ICON_SETS = {
|
|
13
|
+
// ordered low -> high; bucket i picks icon i
|
|
14
|
+
arrows: ['↓', '→', '↑'],
|
|
15
|
+
traffic: ['🔴', '🟡', '🟢'],
|
|
16
|
+
triangles: ['▼', '◆', '▲'],
|
|
17
|
+
};
|
|
18
|
+
/** Min/max of a column's finite numeric values, or null if none. */
|
|
19
|
+
export function computeColumnStat(values) {
|
|
20
|
+
let min = Number.POSITIVE_INFINITY;
|
|
21
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
22
|
+
let any = false;
|
|
23
|
+
for (const v of values) {
|
|
24
|
+
if (v == null || v === '')
|
|
25
|
+
continue;
|
|
26
|
+
const n = Number(v);
|
|
27
|
+
if (!Number.isFinite(n))
|
|
28
|
+
continue;
|
|
29
|
+
any = true;
|
|
30
|
+
if (n < min)
|
|
31
|
+
min = n;
|
|
32
|
+
if (n > max)
|
|
33
|
+
max = n;
|
|
34
|
+
}
|
|
35
|
+
return any ? { min, max } : null;
|
|
36
|
+
}
|
|
37
|
+
/** Which icon a value lands on, given ascending thresholds. */
|
|
38
|
+
function pickIcon(set, thresholds, n) {
|
|
39
|
+
let bucket = 0;
|
|
40
|
+
for (let i = 0; i < thresholds.length; i += 1) {
|
|
41
|
+
if (n >= thresholds[i])
|
|
42
|
+
bucket = i + 1;
|
|
43
|
+
}
|
|
44
|
+
// Map the bucket onto the available icons (clamp + spread).
|
|
45
|
+
const idx = Math.min(set.length - 1, Math.max(0, bucket));
|
|
46
|
+
return set[idx] ?? set[set.length - 1];
|
|
47
|
+
}
|
|
48
|
+
function clamp01(x) {
|
|
49
|
+
return x < 0 ? 0 : x > 1 ? 1 : x;
|
|
50
|
+
}
|
|
51
|
+
function parseHex(hex) {
|
|
52
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
53
|
+
if (!m)
|
|
54
|
+
return null;
|
|
55
|
+
const int = parseInt(m[1], 16);
|
|
56
|
+
return [(int >> 16) & 255, (int >> 8) & 255, int & 255];
|
|
57
|
+
}
|
|
58
|
+
function toHex(rgb) {
|
|
59
|
+
return ('#' +
|
|
60
|
+
rgb
|
|
61
|
+
.map((c) => Math.round(clamp01(c / 255) * 255).toString(16).padStart(2, '0'))
|
|
62
|
+
.join(''));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Pick a readable text color (near-black or white) for a given hex
|
|
66
|
+
* background, using WCAG relative luminance. Lets a color-scale fill or a
|
|
67
|
+
* tinted rule keep its text legible without the caller hand-picking a color.
|
|
68
|
+
* Returns null when the background isn't a parseable hex (e.g. a CSS var),
|
|
69
|
+
* so the caller can leave the default text color in place.
|
|
70
|
+
*/
|
|
71
|
+
export function contrastText(bg) {
|
|
72
|
+
const rgb = parseHex(bg);
|
|
73
|
+
if (!rgb)
|
|
74
|
+
return null;
|
|
75
|
+
const lin = rgb.map((c) => {
|
|
76
|
+
const s = c / 255;
|
|
77
|
+
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
78
|
+
});
|
|
79
|
+
const L = 0.2126 * lin[0] + 0.7152 * lin[1] + 0.0722 * lin[2];
|
|
80
|
+
return L > 0.45 ? '#1e293b' : '#ffffff';
|
|
81
|
+
}
|
|
82
|
+
/** Linear interpolate two hex colors. Falls back to `a` if parsing fails. */
|
|
83
|
+
export function lerpColor(a, b, t) {
|
|
84
|
+
const ca = parseHex(a);
|
|
85
|
+
const cb = parseHex(b);
|
|
86
|
+
if (!ca || !cb)
|
|
87
|
+
return a;
|
|
88
|
+
const k = clamp01(t);
|
|
89
|
+
return toHex([
|
|
90
|
+
ca[0] + (cb[0] - ca[0]) * k,
|
|
91
|
+
ca[1] + (cb[1] - ca[1]) * k,
|
|
92
|
+
ca[2] + (cb[2] - ca[2]) * k,
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
function colorScaleAt(fmt, t) {
|
|
96
|
+
if (fmt.mid) {
|
|
97
|
+
return t <= 0.5
|
|
98
|
+
? lerpColor(fmt.min, fmt.mid, t / 0.5)
|
|
99
|
+
: lerpColor(fmt.mid, fmt.max, (t - 0.5) / 0.5);
|
|
100
|
+
}
|
|
101
|
+
return lerpColor(fmt.min, fmt.max, t);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Resolve every format that applies to one cell into a single set of visual
|
|
105
|
+
* primitives. Later-listed formats override earlier ones for the same
|
|
106
|
+
* property, so order your `conditionalFormats` from general to specific.
|
|
107
|
+
*/
|
|
108
|
+
export function resolveCellFormat(value, row, columnId, formats, stat) {
|
|
109
|
+
const out = {};
|
|
110
|
+
for (const fmt of formats) {
|
|
111
|
+
if (fmt.columns && !fmt.columns.includes(columnId))
|
|
112
|
+
continue;
|
|
113
|
+
const n = Number(value);
|
|
114
|
+
if (fmt.type === 'rule') {
|
|
115
|
+
if (fmt.when({ value, row })) {
|
|
116
|
+
if (fmt.background) {
|
|
117
|
+
out.background = fmt.background;
|
|
118
|
+
// Auto-pick legible text on the tint unless the rule sets its own.
|
|
119
|
+
if (!fmt.color) {
|
|
120
|
+
const c = contrastText(fmt.background);
|
|
121
|
+
if (c)
|
|
122
|
+
out.color = c;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (fmt.color)
|
|
126
|
+
out.color = fmt.color;
|
|
127
|
+
if (fmt.fontWeight != null)
|
|
128
|
+
out.fontWeight = fmt.fontWeight;
|
|
129
|
+
}
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (value == null || value === '' || !Number.isFinite(n))
|
|
133
|
+
continue;
|
|
134
|
+
if (fmt.type === 'colorScale') {
|
|
135
|
+
const lo = fmt.minValue ?? stat?.min ?? n;
|
|
136
|
+
const hi = fmt.maxValue ?? stat?.max ?? n;
|
|
137
|
+
const t = hi === lo ? 0.5 : clamp01((n - lo) / (hi - lo));
|
|
138
|
+
out.background = colorScaleAt(fmt, t);
|
|
139
|
+
// The scale fills the whole cell, so keep the text legible against it.
|
|
140
|
+
const c = contrastText(out.background);
|
|
141
|
+
if (c)
|
|
142
|
+
out.color = c;
|
|
143
|
+
}
|
|
144
|
+
else if (fmt.type === 'dataBar') {
|
|
145
|
+
const lo = fmt.minValue ?? Math.min(0, stat?.min ?? 0);
|
|
146
|
+
const hi = fmt.maxValue ?? stat?.max ?? n;
|
|
147
|
+
const span = hi - lo || 1;
|
|
148
|
+
const percent = clamp01((n - lo) / span) * 100;
|
|
149
|
+
const negative = n < 0;
|
|
150
|
+
out.dataBar = {
|
|
151
|
+
percent,
|
|
152
|
+
color: negative ? (fmt.negativeColor ?? '#ef4444') : fmt.color,
|
|
153
|
+
fromRight: false,
|
|
154
|
+
};
|
|
155
|
+
if (fmt.showValue === false)
|
|
156
|
+
out.color = 'transparent';
|
|
157
|
+
}
|
|
158
|
+
else if (fmt.type === 'iconSet') {
|
|
159
|
+
const set = ICON_SETS[fmt.set ?? 'arrows'];
|
|
160
|
+
out.icon = pickIcon(set, fmt.thresholds, n);
|
|
161
|
+
if (fmt.iconOnly)
|
|
162
|
+
out.iconOnly = true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
/** Column ids that need a numeric min/max precomputed (colorScale/dataBar). */
|
|
168
|
+
export function formatsNeedingStats(formats) {
|
|
169
|
+
return formats.some((f) => f.type === 'colorScale' || f.type === 'dataBar');
|
|
170
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { computeColumnStat, lerpColor, contrastText, resolveCellFormat, formatsNeedingStats, } from './conditional-formatting';
|
|
3
|
+
describe('contrastText', () => {
|
|
4
|
+
it('returns dark text on light backgrounds, white on dark', () => {
|
|
5
|
+
expect(contrastText('#fde68a')).toBe('#1e293b'); // light yellow
|
|
6
|
+
expect(contrastText('#86efac')).toBe('#1e293b'); // light green
|
|
7
|
+
expect(contrastText('#1d4ed8')).toBe('#ffffff'); // deep blue
|
|
8
|
+
});
|
|
9
|
+
it('returns null for non-hex backgrounds', () => {
|
|
10
|
+
expect(contrastText('var(--x)')).toBeNull();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
describe('computeColumnStat', () => {
|
|
14
|
+
it('returns min/max of finite numbers', () => {
|
|
15
|
+
expect(computeColumnStat([3, 1, 'x', 9, null])).toEqual({ min: 1, max: 9 });
|
|
16
|
+
});
|
|
17
|
+
it('returns null when nothing is numeric', () => {
|
|
18
|
+
expect(computeColumnStat(['a', null, undefined])).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
describe('lerpColor', () => {
|
|
22
|
+
it('interpolates midpoint', () => {
|
|
23
|
+
expect(lerpColor('#000000', '#ffffff', 0.5)).toBe('#808080');
|
|
24
|
+
});
|
|
25
|
+
it('clamps and falls back on bad input', () => {
|
|
26
|
+
expect(lerpColor('#ff0000', '#00ff00', 0)).toBe('#ff0000');
|
|
27
|
+
expect(lerpColor('nope', '#00ff00', 0.5)).toBe('nope');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe('resolveCellFormat', () => {
|
|
31
|
+
const stat = { min: 0, max: 100 };
|
|
32
|
+
it('color scale maps value position to a gradient color', () => {
|
|
33
|
+
const fmts = [
|
|
34
|
+
{ type: 'colorScale', min: '#ff0000', max: '#00ff00' },
|
|
35
|
+
];
|
|
36
|
+
expect(resolveCellFormat(0, {}, 'c', fmts, stat).background).toBe('#ff0000');
|
|
37
|
+
expect(resolveCellFormat(100, {}, 'c', fmts, stat).background).toBe('#00ff00');
|
|
38
|
+
expect(resolveCellFormat(50, {}, 'c', fmts, stat).background).toBe('#808000');
|
|
39
|
+
});
|
|
40
|
+
it('data bar computes a percent and picks negative color', () => {
|
|
41
|
+
const fmts = [
|
|
42
|
+
{ type: 'dataBar', color: '#16a34a', negativeColor: '#ef4444' },
|
|
43
|
+
];
|
|
44
|
+
const pos = resolveCellFormat(50, {}, 'c', fmts, { min: -100, max: 100 });
|
|
45
|
+
expect(pos.dataBar?.percent).toBeCloseTo(75);
|
|
46
|
+
expect(pos.dataBar?.color).toBe('#16a34a');
|
|
47
|
+
const neg = resolveCellFormat(-50, {}, 'c', fmts, { min: -100, max: 100 });
|
|
48
|
+
expect(neg.dataBar?.color).toBe('#ef4444');
|
|
49
|
+
});
|
|
50
|
+
it('icon set buckets by ascending thresholds', () => {
|
|
51
|
+
const fmts = [
|
|
52
|
+
{ type: 'iconSet', set: 'arrows', thresholds: [0, 50] },
|
|
53
|
+
];
|
|
54
|
+
expect(resolveCellFormat(-5, {}, 'c', fmts, null).icon).toBe('↓');
|
|
55
|
+
expect(resolveCellFormat(10, {}, 'c', fmts, null).icon).toBe('→');
|
|
56
|
+
expect(resolveCellFormat(80, {}, 'c', fmts, null).icon).toBe('↑');
|
|
57
|
+
});
|
|
58
|
+
it('rule applies styles only when the predicate matches', () => {
|
|
59
|
+
const fmts = [
|
|
60
|
+
{
|
|
61
|
+
type: 'rule',
|
|
62
|
+
when: ({ value }) => Number(value) < 0,
|
|
63
|
+
background: '#fee2e2',
|
|
64
|
+
color: '#991b1b',
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
expect(resolveCellFormat(-1, {}, 'c', fmts, null).background).toBe('#fee2e2');
|
|
68
|
+
expect(resolveCellFormat(1, {}, 'c', fmts, null).background).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
it('respects the columns scope', () => {
|
|
71
|
+
const fmts = [
|
|
72
|
+
{ type: 'colorScale', min: '#000000', max: '#ffffff', columns: ['other'] },
|
|
73
|
+
];
|
|
74
|
+
expect(resolveCellFormat(50, {}, 'c', fmts, stat).background).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
it('later formats override earlier ones', () => {
|
|
77
|
+
const fmts = [
|
|
78
|
+
{ type: 'rule', when: () => true, background: '#aaa' },
|
|
79
|
+
{ type: 'rule', when: () => true, background: '#bbb' },
|
|
80
|
+
];
|
|
81
|
+
expect(resolveCellFormat(1, {}, 'c', fmts, null).background).toBe('#bbb');
|
|
82
|
+
});
|
|
83
|
+
it('formatsNeedingStats flags colorScale/dataBar only', () => {
|
|
84
|
+
expect(formatsNeedingStats([{ type: 'iconSet', thresholds: [0] }])).toBe(false);
|
|
85
|
+
expect(formatsNeedingStats([{ type: 'dataBar', color: '#000' }])).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Targets uncovered branches in core.ts:
|
|
3
|
+
* - row.getAllCells() / cell.getValue() / cell.getContext()
|
|
4
|
+
* - row.getCellValueByColumnId() (both hit + miss path)
|
|
5
|
+
* - sortFns variants (alphanumeric, basic, datetime)
|
|
6
|
+
* - filterFns variants
|
|
7
|
+
* - grouping aggregations
|
|
8
|
+
* - paginated row model edge cases
|
|
9
|
+
*/
|
|
10
|
+
import { describe, expect, it } from 'vitest';
|
|
11
|
+
import { columnFilteringFeature, columnGroupingFeature, createCoreRowModel, createExpandedRowModel, createFilteredRowModel, createGroupedRowModel, createPaginatedRowModel, createSortedRowModel, createSvGrid, filterFns, rowExpandingFeature, rowPaginationFeature, rowSelectionFeature, rowSortingFeature, sortFns, tableFeatures, } from './index';
|
|
12
|
+
import { isFunction } from './core';
|
|
13
|
+
const features = tableFeatures({
|
|
14
|
+
columnFilteringFeature,
|
|
15
|
+
columnGroupingFeature,
|
|
16
|
+
rowExpandingFeature,
|
|
17
|
+
rowPaginationFeature,
|
|
18
|
+
rowSelectionFeature,
|
|
19
|
+
rowSortingFeature,
|
|
20
|
+
});
|
|
21
|
+
const data = [
|
|
22
|
+
{ id: 1, name: 'Ada', team: 'A', salary: 100, born: '1815-12-10' },
|
|
23
|
+
{ id: 2, name: 'Grace', team: 'B', salary: 200, born: '1906-12-09' },
|
|
24
|
+
{ id: 3, name: 'Alan', team: 'A', salary: 150, born: '1912-06-23' },
|
|
25
|
+
{ id: 4, name: 'Linus', team: 'C', salary: 300, born: '1969-12-28' },
|
|
26
|
+
{ id: 5, name: 'Donald', team: 'A', salary: 90, born: '1938-01-10' },
|
|
27
|
+
];
|
|
28
|
+
const columns = [
|
|
29
|
+
{ field: 'name', header: 'Name' },
|
|
30
|
+
{ field: 'team', header: 'Team' },
|
|
31
|
+
{ field: 'salary', header: 'Salary' },
|
|
32
|
+
{ field: 'born', header: 'Born' },
|
|
33
|
+
];
|
|
34
|
+
function makeGrid(state) {
|
|
35
|
+
return createSvGrid({
|
|
36
|
+
data,
|
|
37
|
+
columns,
|
|
38
|
+
_features: features,
|
|
39
|
+
_rowModels: {
|
|
40
|
+
coreRowModel: createCoreRowModel(),
|
|
41
|
+
filteredRowModel: createFilteredRowModel(),
|
|
42
|
+
sortedRowModel: createSortedRowModel(sortFns),
|
|
43
|
+
groupedRowModel: createGroupedRowModel(),
|
|
44
|
+
expandedRowModel: createExpandedRowModel(),
|
|
45
|
+
paginatedRowModel: createPaginatedRowModel(),
|
|
46
|
+
},
|
|
47
|
+
state,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
describe('core - row / cell lazy getters', () => {
|
|
51
|
+
it('exercises row.getAllCells() and cell.getValue()', () => {
|
|
52
|
+
const grid = makeGrid();
|
|
53
|
+
const rows = grid.getRowModel().rows;
|
|
54
|
+
expect(rows.length).toBeGreaterThan(0);
|
|
55
|
+
for (const row of rows) {
|
|
56
|
+
const cells = row.getAllCells();
|
|
57
|
+
expect(Array.isArray(cells)).toBe(true);
|
|
58
|
+
for (const cell of cells) {
|
|
59
|
+
const value = cell.getValue();
|
|
60
|
+
expect(value).not.toBeUndefined();
|
|
61
|
+
const ctx = cell.getContext();
|
|
62
|
+
expect(ctx).toBeDefined();
|
|
63
|
+
expect(ctx.row).toBe(row);
|
|
64
|
+
expect(ctx.table).toBe(grid);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
it('getCellValueByColumnId returns undefined for unknown columns', () => {
|
|
69
|
+
const grid = makeGrid();
|
|
70
|
+
const row = grid.getRowModel().rows[0];
|
|
71
|
+
expect(row.getCellValueByColumnId('no-such')).toBeUndefined();
|
|
72
|
+
expect(row.getCellValueByColumnId('name')).toBeDefined();
|
|
73
|
+
});
|
|
74
|
+
it('caches cached row reference after first access', () => {
|
|
75
|
+
const grid = makeGrid();
|
|
76
|
+
const a = grid.getRowModel().rows[0];
|
|
77
|
+
const b = grid.getRowModel().rows[0];
|
|
78
|
+
expect(a).toBe(b);
|
|
79
|
+
const cellsA = a.getAllCells();
|
|
80
|
+
const cellsB = a.getAllCells();
|
|
81
|
+
expect(cellsA).toBe(cellsB); // cache hit
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('core - sortFns variants', () => {
|
|
85
|
+
it('auto comparator uses locale string compare', () => {
|
|
86
|
+
expect(sortFns.auto('apple', 'banana')).toBeLessThan(0);
|
|
87
|
+
expect(sortFns.auto('banana', 'apple')).toBeGreaterThan(0);
|
|
88
|
+
expect(sortFns.auto('apple', 'apple')).toBe(0);
|
|
89
|
+
});
|
|
90
|
+
it('number comparator is numeric', () => {
|
|
91
|
+
expect(sortFns.number(1, 2)).toBeLessThan(0);
|
|
92
|
+
expect(sortFns.number(2, 1)).toBeGreaterThan(0);
|
|
93
|
+
expect(sortFns.number(1, 1)).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
it('number comparator coerces null to 0', () => {
|
|
96
|
+
expect(sortFns.number(null, 1)).toBeLessThan(0);
|
|
97
|
+
expect(sortFns.number(null, null)).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
it('date comparator compares ISO strings', () => {
|
|
100
|
+
expect(sortFns.date('2020-01-01', '2021-01-01')).toBeLessThan(0);
|
|
101
|
+
expect(sortFns.date('2021-01-01', '2020-01-01')).toBeGreaterThan(0);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
describe('core - filterFns variants', () => {
|
|
105
|
+
it('includesString matches case-insensitive substrings', () => {
|
|
106
|
+
expect(filterFns.includesString('Hello World', 'world')).toBe(true);
|
|
107
|
+
expect(filterFns.includesString('Hello', 'xyz')).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
it('equals matches identical values', () => {
|
|
110
|
+
expect(filterFns.equals('x', 'x')).toBe(true);
|
|
111
|
+
expect(filterFns.equals('x', 'y')).toBe(false);
|
|
112
|
+
expect(filterFns.equals(1, 1)).toBe(true);
|
|
113
|
+
expect(filterFns.equals(1, '1')).toBe(false);
|
|
114
|
+
});
|
|
115
|
+
it('all exported filterFns are callable', () => {
|
|
116
|
+
for (const key of Object.keys(filterFns)) {
|
|
117
|
+
expect(typeof filterFns[key]).toBe('function');
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
describe('core - grouping with aggregations', () => {
|
|
122
|
+
it('grouping by team produces grouped rows', () => {
|
|
123
|
+
const grid = makeGrid({ grouping: ['team'] });
|
|
124
|
+
const rows = grid.getRowModel().rows;
|
|
125
|
+
expect(rows.length).toBeGreaterThan(0);
|
|
126
|
+
});
|
|
127
|
+
it('explicitly expanding a group shows nested rows', () => {
|
|
128
|
+
const grid = makeGrid({
|
|
129
|
+
grouping: ['team'],
|
|
130
|
+
expanded: { group_team_A: true },
|
|
131
|
+
});
|
|
132
|
+
const rows = grid.getRowModel().rows;
|
|
133
|
+
expect(rows.length).toBeGreaterThan(0);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('core - pagination edge cases', () => {
|
|
137
|
+
it('page 0 with size larger than data set returns the whole set', () => {
|
|
138
|
+
const grid = makeGrid({ pagination: { pageIndex: 0, pageSize: 1000 } });
|
|
139
|
+
const rows = grid.getRowModel().rows;
|
|
140
|
+
expect(rows.length).toBeLessThanOrEqual(data.length);
|
|
141
|
+
});
|
|
142
|
+
it('past-the-end page returns an empty rowset', () => {
|
|
143
|
+
const grid = makeGrid({ pagination: { pageIndex: 999, pageSize: 2 } });
|
|
144
|
+
const rows = grid.getRowModel().rows;
|
|
145
|
+
expect(rows.length).toBe(0);
|
|
146
|
+
});
|
|
147
|
+
it('basic page-2 slicing', () => {
|
|
148
|
+
const grid = makeGrid({ pagination: { pageIndex: 1, pageSize: 2 } });
|
|
149
|
+
const rows = grid.getRowModel().rows;
|
|
150
|
+
expect(rows.length).toBeLessThanOrEqual(2);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
describe('core - selection state', () => {
|
|
154
|
+
it('initialises with an empty selection record', () => {
|
|
155
|
+
const grid = makeGrid();
|
|
156
|
+
expect(grid.getState().rowSelection ?? {}).toEqual({});
|
|
157
|
+
});
|
|
158
|
+
it('stores a selection set via initial state', () => {
|
|
159
|
+
const grid = makeGrid({ rowSelection: { 'row-0': true } });
|
|
160
|
+
expect(grid.getState().rowSelection).toEqual({ 'row-0': true });
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
describe('isFunction helper', () => {
|
|
164
|
+
it('returns true for functions', () => {
|
|
165
|
+
expect(isFunction(() => 0)).toBe(true);
|
|
166
|
+
expect(isFunction(function () { })).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
it('returns false for non-functions', () => {
|
|
169
|
+
expect(isFunction(null)).toBe(false);
|
|
170
|
+
expect(isFunction(undefined)).toBe(false);
|
|
171
|
+
expect(isFunction('string')).toBe(false);
|
|
172
|
+
expect(isFunction(42)).toBe(false);
|
|
173
|
+
expect(isFunction({})).toBe(false);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
describe('core - column visibility + leaf columns', () => {
|
|
177
|
+
it('getAllColumns returns one entry per column def', () => {
|
|
178
|
+
const grid = makeGrid();
|
|
179
|
+
expect(grid.getAllColumns().length).toBe(columns.length);
|
|
180
|
+
});
|
|
181
|
+
it('getVisibleLeafColumns reflects columnVisibility state', () => {
|
|
182
|
+
const grid = makeGrid({ columnVisibility: { name: false } });
|
|
183
|
+
const visible = grid.getVisibleLeafColumns?.() ?? grid.getAllColumns();
|
|
184
|
+
expect(Array.isArray(visible)).toBe(true);
|
|
185
|
+
});
|
|
186
|
+
});
|