@svgrid/grid 1.0.2 → 1.1.1
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 +1 -1
- package/README.md +137 -39
- package/dist/GridFooter.svelte +164 -0
- package/dist/GridFooter.svelte.d.ts +29 -0
- package/dist/GridMenus.svelte +648 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +472 -0
- package/dist/SvGrid.controller.svelte.js +1917 -0
- package/dist/SvGrid.css +2012 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.helpers.test.d.ts +1 -0
- package/dist/SvGrid.helpers.test.js +298 -0
- package/dist/SvGrid.svelte +2346 -8742
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +523 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/aligned-grids.d.ts +6 -0
- package/dist/aligned-grids.js +84 -0
- package/dist/aligned-grids.test.d.ts +1 -0
- package/dist/aligned-grids.test.js +75 -0
- package/dist/build-api.coverage.test.d.ts +20 -0
- package/dist/build-api.coverage.test.js +505 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +557 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-render.test.d.ts +1 -0
- package/dist/cell-render.test.js +338 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -0
- package/dist/chart-export.test.d.ts +1 -0
- package/dist/chart-export.test.js +302 -0
- package/dist/chart.coverage.test.d.ts +1 -0
- package/dist/chart.coverage.test.js +748 -0
- package/dist/chart.d.ts +370 -3
- package/dist/chart.js +1135 -42
- package/dist/clipboard.d.ts +15 -0
- package/dist/clipboard.js +420 -0
- package/dist/clipboard.test.d.ts +1 -0
- package/dist/clipboard.test.js +700 -0
- package/dist/collaboration.coverage.test.d.ts +1 -0
- package/dist/collaboration.coverage.test.js +200 -0
- package/dist/column-groups.d.ts +19 -0
- package/dist/column-groups.js +62 -0
- package/dist/column-groups.test.d.ts +1 -0
- package/dist/column-groups.test.js +56 -0
- package/dist/column-types.d.ts +10 -0
- package/dist/column-types.js +63 -0
- package/dist/column-types.test.d.ts +1 -0
- package/dist/column-types.test.js +62 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/columns.test.d.ts +1 -0
- package/dist/columns.test.js +625 -0
- package/dist/core.d.ts +86 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +31 -0
- package/dist/editing.js +529 -0
- package/dist/editing.test.d.ts +1 -0
- package/dist/editing.test.js +732 -0
- package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
- package/dist/editors/cell-editors.coverage.test.js +139 -0
- package/dist/editors/cell-editors.d.ts +1 -1
- package/dist/facet-buckets.d.ts +13 -0
- package/dist/facet-buckets.js +54 -0
- package/dist/facet-buckets.test.d.ts +1 -0
- package/dist/facet-buckets.test.js +296 -0
- package/dist/features.d.ts +5 -0
- package/dist/features.js +30 -0
- package/dist/filter-operators.d.ts +16 -0
- package/dist/filter-operators.js +69 -0
- package/dist/filter-operators.test.d.ts +1 -0
- package/dist/filter-operators.test.js +135 -0
- package/dist/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/hyperformula-adapter.test.d.ts +1 -0
- package/dist/hyperformula-adapter.test.js +205 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.js +8 -2
- package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
- package/dist/keyboard-handlers.coverage.test.js +495 -0
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +202 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +390 -0
- package/dist/menus.test.d.ts +1 -0
- package/dist/menus.test.js +560 -0
- package/dist/named-views.coverage.test.d.ts +1 -0
- package/dist/named-views.coverage.test.js +180 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-drag.d.ts +49 -0
- package/dist/row-drag.js +221 -0
- package/dist/row-drag.test.d.ts +1 -0
- package/dist/row-drag.test.js +142 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/row-resize.test.d.ts +1 -0
- package/dist/row-resize.test.js +329 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +89 -0
- package/dist/scroll-sync.test.d.ts +1 -0
- package/dist/scroll-sync.test.js +290 -0
- package/dist/selection.d.ts +32 -0
- package/dist/selection.js +427 -0
- package/dist/selection.multi-range.test.d.ts +1 -0
- package/dist/selection.multi-range.test.js +55 -0
- package/dist/selection.test.d.ts +1 -0
- package/dist/selection.test.js +647 -0
- package/dist/server-data-source.coverage.test.d.ts +1 -0
- package/dist/server-data-source.coverage.test.js +154 -0
- package/dist/spreadsheet.d.ts +110 -0
- package/dist/spreadsheet.js +242 -0
- package/dist/spreadsheet.test.d.ts +1 -0
- package/dist/spreadsheet.test.js +446 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/sv-grid-scrollbar.js +13 -1
- package/dist/svgrid-wrapper.types.d.ts +31 -1
- package/dist/svgrid.behavior.test.js +20 -0
- package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
- package/dist/svgrid.comments-autocomplete.test.js +96 -0
- package/dist/svgrid.context-menu.test.d.ts +1 -0
- package/dist/svgrid.context-menu.test.js +102 -0
- package/dist/svgrid.interaction.test.js +31 -0
- package/dist/svgrid.new-features.wrapper.test.js +64 -6
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/test-setup.js +9 -3
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +45 -0
- package/dist/virtualization/scroll-scaling.js +99 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +127 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
- package/dist/virtualization/virtualizer.d.ts +7 -0
- package/dist/virtualization/virtualizer.js +30 -0
- package/package.json +2 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +648 -0
- package/src/SvGrid.controller.svelte.ts +2352 -0
- package/src/SvGrid.css +2012 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +2346 -8742
- package/src/SvGrid.types.ts +537 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/aligned-grids.test.ts +80 -0
- package/src/aligned-grids.ts +87 -0
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +683 -0
- package/src/cell-render.test.ts +451 -0
- package/src/cell-render.ts +426 -0
- package/src/cell-values.ts +114 -0
- package/src/chart-export.test.ts +370 -0
- package/src/chart.coverage.test.ts +814 -0
- package/src/chart.ts +1352 -47
- package/src/clipboard.test.ts +780 -0
- package/src/clipboard.ts +552 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/column-groups.test.ts +59 -0
- package/src/column-groups.ts +80 -0
- package/src/column-types.test.ts +68 -0
- package/src/column-types.ts +82 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +86 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +669 -0
- package/src/editors/cell-editors.coverage.test.ts +156 -0
- package/src/editors/cell-editors.ts +1 -0
- package/src/facet-buckets.test.ts +353 -0
- package/src/facet-buckets.ts +67 -0
- package/src/features.ts +128 -0
- package/src/filter-operators.test.ts +174 -0
- package/src/filter-operators.ts +87 -0
- package/src/hyperformula-adapter.test.ts +256 -0
- package/src/hyperformula-adapter.ts +124 -0
- package/src/index.ts +49 -0
- package/src/keyboard-handlers.coverage.test.ts +560 -0
- package/src/keyboard-handlers.ts +353 -0
- package/src/keyboard.ts +97 -97
- package/src/menus.test.ts +620 -0
- package/src/menus.ts +555 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -0
- package/src/row-drag.test.ts +168 -0
- package/src/row-drag.ts +255 -0
- package/src/row-resize.test.ts +369 -0
- package/src/row-resize.ts +171 -0
- package/src/scroll-sync.test.ts +330 -0
- package/src/scroll-sync.ts +218 -0
- package/src/selection.multi-range.test.ts +61 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +579 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +489 -0
- package/src/spreadsheet.ts +304 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +31 -1
- package/src/svgrid.behavior.test.ts +22 -0
- package/src/svgrid.comments-autocomplete.test.ts +112 -0
- package/src/svgrid.context-menu.test.ts +126 -0
- package/src/svgrid.interaction.test.ts +30 -0
- package/src/svgrid.new-features.wrapper.test.ts +67 -6
- package/src/svgrid.wrapper.test.ts +27 -1
- package/src/test-setup.ts +9 -6
- package/src/virtualization/scroll-scaling.test.ts +148 -0
- package/src/virtualization/scroll-scaling.ts +121 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
- package/src/virtualization/virtualizer.ts +26 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// cell-render handlers extracted from the controller. Imperative event handlers
|
|
2
|
+
// reading/writing controller state via the `ctx` handle; the reactive core
|
|
3
|
+
// ($state/$derived/$effect) stays in the controller.
|
|
4
|
+
import {
|
|
5
|
+
applyExcelFilter,
|
|
6
|
+
normalizeForFilter,
|
|
7
|
+
createColumnVirtualizer,
|
|
8
|
+
createCoreRowModel,
|
|
9
|
+
createExpandedRowModel,
|
|
10
|
+
createFilteredRowModel,
|
|
11
|
+
createGroupedRowModel,
|
|
12
|
+
createPaginatedRowModel,
|
|
13
|
+
createSvelteVirtualizer,
|
|
14
|
+
createSortedRowModel,
|
|
15
|
+
createSvGrid,
|
|
16
|
+
filterFns,
|
|
17
|
+
getGridCellA11yProps,
|
|
18
|
+
getGridCellDomId,
|
|
19
|
+
getGridHeaderA11yProps,
|
|
20
|
+
getGridRootA11yProps,
|
|
21
|
+
getGridRowA11yProps,
|
|
22
|
+
parseEditorValue,
|
|
23
|
+
normalizeEditorOptions,
|
|
24
|
+
sortFns,
|
|
25
|
+
tableFeatures,
|
|
26
|
+
rowSortingFeature,
|
|
27
|
+
columnFilteringFeature,
|
|
28
|
+
columnGroupingFeature,
|
|
29
|
+
type CellContext,
|
|
30
|
+
type EditorContext,
|
|
31
|
+
type CellEditorOption,
|
|
32
|
+
type CellEditorType,
|
|
33
|
+
type CellFormatter,
|
|
34
|
+
type CellFormatConfig,
|
|
35
|
+
type Column,
|
|
36
|
+
type ColumnDef,
|
|
37
|
+
type Row,
|
|
38
|
+
type RowData,
|
|
39
|
+
type SvGridApi,
|
|
40
|
+
type TableFeatures,
|
|
41
|
+
} from "./index";
|
|
42
|
+
import "./sv-grid-scrollbar";
|
|
43
|
+
import type { Snippet } from "svelte";
|
|
44
|
+
import { getKeyboardIntent, getNextActiveCell } from "./keyboard";
|
|
45
|
+
import {
|
|
46
|
+
formatNumericWithConfig,
|
|
47
|
+
getDateFormatter,
|
|
48
|
+
resolveDatePattern,
|
|
49
|
+
} from "./cell-formatting";
|
|
50
|
+
import {
|
|
51
|
+
RenderSnippetConfig,
|
|
52
|
+
RenderComponentConfig,
|
|
53
|
+
} from "./render-component";
|
|
54
|
+
import { buildFillPattern } from "./fill-patterns";
|
|
55
|
+
import { buildSparkline, toSparklineValues } from "./sparkline";
|
|
56
|
+
import {
|
|
57
|
+
resolveCellFormat,
|
|
58
|
+
computeColumnStat,
|
|
59
|
+
formatsNeedingStats,
|
|
60
|
+
type ColumnStat,
|
|
61
|
+
type ResolvedCellFormat,
|
|
62
|
+
} from "./conditional-formatting";
|
|
63
|
+
import SvGridDropdown from "./SvGridDropdown.svelte";
|
|
64
|
+
import type {
|
|
65
|
+
Props,
|
|
66
|
+
SelectionPoint,
|
|
67
|
+
SelectionRange,
|
|
68
|
+
CellEditState,
|
|
69
|
+
FilterOperator,
|
|
70
|
+
FilterOption,
|
|
71
|
+
MenuPosition,
|
|
72
|
+
} from "./SvGrid.types";
|
|
73
|
+
import {
|
|
74
|
+
cfTextStyle,
|
|
75
|
+
fmtStat,
|
|
76
|
+
getCellKey,
|
|
77
|
+
resolveClassList,
|
|
78
|
+
toDateInputValue,
|
|
79
|
+
toDateTimeLocalInputValue,
|
|
80
|
+
getEditableInputValue,
|
|
81
|
+
getEditorInputType,
|
|
82
|
+
toValueArray,
|
|
83
|
+
getOptionLabel,
|
|
84
|
+
getOptionColor,
|
|
85
|
+
colorfulChipStyle,
|
|
86
|
+
getEditorClass,
|
|
87
|
+
asDate,
|
|
88
|
+
clampMenuX,
|
|
89
|
+
cssEscape,
|
|
90
|
+
rawToNumber,
|
|
91
|
+
formatFacetNumber,
|
|
92
|
+
formatFacetDate,
|
|
93
|
+
} from "./SvGrid.helpers";
|
|
94
|
+
import { createEditing } from "./editing";
|
|
95
|
+
import { createSelection } from "./selection";
|
|
96
|
+
import { createColumns } from "./columns";
|
|
97
|
+
import { createGridApi } from "./build-api";
|
|
98
|
+
import { createClipboard } from "./clipboard";
|
|
99
|
+
import {
|
|
100
|
+
filterOperatorOptions,
|
|
101
|
+
fallbackOperatorOption,
|
|
102
|
+
TEXT_OPERATORS,
|
|
103
|
+
NUMBER_OPERATORS,
|
|
104
|
+
DATE_OPERATORS,
|
|
105
|
+
CHECKBOX_OPERATORS,
|
|
106
|
+
operatorOption,
|
|
107
|
+
operatorsForColumn,
|
|
108
|
+
defaultOperatorFor,
|
|
109
|
+
operatorLabelFor,
|
|
110
|
+
} from "./filter-operators";
|
|
111
|
+
import {
|
|
112
|
+
type FacetBucket,
|
|
113
|
+
isBucketableColumn,
|
|
114
|
+
buildBuckets,
|
|
115
|
+
isInBucket,
|
|
116
|
+
} from "./facet-buckets";
|
|
117
|
+
import {
|
|
118
|
+
getColumnBaseValue,
|
|
119
|
+
isGroupRow,
|
|
120
|
+
toolPanelHeaderLabel,
|
|
121
|
+
formatSummaryNumeric,
|
|
122
|
+
getColumnAlign,
|
|
123
|
+
getPinnedCellValue,
|
|
124
|
+
getColumnAccessorValue,
|
|
125
|
+
columnDefMatchesId,
|
|
126
|
+
} from "./cell-values";
|
|
127
|
+
|
|
128
|
+
export function createCellRender<
|
|
129
|
+
TFeatures extends TableFeatures = TableFeatures,
|
|
130
|
+
TData extends RowData = RowData,
|
|
131
|
+
>(ctx: any) {
|
|
132
|
+
function cellConditionalFormat(
|
|
133
|
+
row: Row<TData>,
|
|
134
|
+
column: Column<TData>,
|
|
135
|
+
value: unknown,
|
|
136
|
+
): ResolvedCellFormat | null {
|
|
137
|
+
const formats = ctx.props.conditionalFormats;
|
|
138
|
+
if (!formats?.length) return null;
|
|
139
|
+
return resolveCellFormat(
|
|
140
|
+
value,
|
|
141
|
+
row.original,
|
|
142
|
+
column.id,
|
|
143
|
+
formats,
|
|
144
|
+
ctx.conditionalColumnStats.get(column.id) ?? null,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Compute the consumer-supplied row class for one rendered row. */
|
|
149
|
+
function computeRowClass(row: Row<TData>, rowIndex: number): string {
|
|
150
|
+
if (!ctx.props.rowClass) return "";
|
|
151
|
+
return resolveClassList(
|
|
152
|
+
ctx.props.rowClass({ row: row.original as TData, rowIndex }),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Compute the consumer-supplied cell class for one rendered cell. */
|
|
157
|
+
function computeCellClass(row: Row<TData>, column: Column<TData>): string {
|
|
158
|
+
const raw = column.columnDef.cellClass;
|
|
159
|
+
if (raw == null) return "";
|
|
160
|
+
if (typeof raw === "string" || Array.isArray(raw)) {
|
|
161
|
+
return resolveClassList(raw);
|
|
162
|
+
}
|
|
163
|
+
if (typeof raw === "function") {
|
|
164
|
+
// Build a minimal CellContext - the only fields the wrapper-side
|
|
165
|
+
// cellClass author needs are `row` and `column`. Callers can read
|
|
166
|
+
// `ctx.row.original` and `ctx.column.id` exactly like in a `cell`
|
|
167
|
+
// renderer. The other fields are stubbed for compatibility.
|
|
168
|
+
const cellCtx = {
|
|
169
|
+
row,
|
|
170
|
+
column,
|
|
171
|
+
cell: undefined as any,
|
|
172
|
+
table: undefined as any,
|
|
173
|
+
getValue: () => row.getCellValueByColumnId(column.id),
|
|
174
|
+
};
|
|
175
|
+
return resolveClassList(raw(cellCtx as any));
|
|
176
|
+
}
|
|
177
|
+
return "";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Resolve the per-cell tooltip. Column-level `tooltip` field can be a
|
|
182
|
+
* plain string (rendered as `title=`) or a `(ctx) => string` callback
|
|
183
|
+
* for value-dependent text. Returning empty / nullish means no
|
|
184
|
+
* tooltip - the renderer omits the `title=` attribute entirely.
|
|
185
|
+
*/
|
|
186
|
+
function computeCellTooltip(row: Row<TData>, column: Column<TData>): string | null {
|
|
187
|
+
const raw = column.columnDef.tooltip
|
|
188
|
+
if (raw == null) return null
|
|
189
|
+
if (typeof raw === "string") return raw || null
|
|
190
|
+
if (typeof raw === "function") {
|
|
191
|
+
const ctx = {
|
|
192
|
+
row,
|
|
193
|
+
column,
|
|
194
|
+
cell: undefined as any,
|
|
195
|
+
table: undefined as any,
|
|
196
|
+
getValue: () => row.getCellValueByColumnId(column.id),
|
|
197
|
+
}
|
|
198
|
+
const out = (raw as any)(ctx)
|
|
199
|
+
return out ? String(out) : null
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Resolve a per-cell note (a longer comment / annotation). Notes
|
|
206
|
+
* come from the grid's `notes` prop - a `{ [rowId]: { [columnId]: string } }`
|
|
207
|
+
* map - so the consumer keeps note storage. Returning non-empty
|
|
208
|
+
* paints a corner indicator AND becomes the cell's tooltip text.
|
|
209
|
+
*/
|
|
210
|
+
function computeCellNote(row: Row<TData>, column: Column<TData>): string | null {
|
|
211
|
+
// Internal overlay (from the comment editor) wins over props.notes so
|
|
212
|
+
// edits show immediately even when `notes` is controlled. An empty
|
|
213
|
+
// overlay entry means "removed".
|
|
214
|
+
const ov = ctx.noteOverrides?.[row.id]?.[column.id]
|
|
215
|
+
if (ov !== undefined) return ov.trim() ? ov : null
|
|
216
|
+
const map = ctx.props.notes
|
|
217
|
+
if (!map) return null
|
|
218
|
+
const byCol = map[row.id]
|
|
219
|
+
if (!byCol) return null
|
|
220
|
+
const v = byCol[column.id]
|
|
221
|
+
return v && v.trim() ? v : null
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function getColumnEditorOptions(
|
|
225
|
+
column: Column<TData>,
|
|
226
|
+
row?: Row<TData> | null,
|
|
227
|
+
): CellEditorOption[] {
|
|
228
|
+
const def = column.columnDef.editorOptions;
|
|
229
|
+
if (typeof def === "function") {
|
|
230
|
+
// Dynamic per-row: must be re-evaluated because the row's other
|
|
231
|
+
// cells may have just changed (cascade).
|
|
232
|
+
if (!row?.original) return [];
|
|
233
|
+
return normalizeEditorOptions(def(row.original as TData));
|
|
234
|
+
}
|
|
235
|
+
const id = column.id;
|
|
236
|
+
if (
|
|
237
|
+
!ctx.editorOptionsCache[id] ||
|
|
238
|
+
ctx.editorOptionsCache[id + "__src"] !== (def as unknown as object)
|
|
239
|
+
) {
|
|
240
|
+
ctx.editorOptionsCache[id] = normalizeEditorOptions(def);
|
|
241
|
+
(ctx.editorOptionsCache as Record<string, unknown>)[id + "__src"] = def;
|
|
242
|
+
}
|
|
243
|
+
return ctx.editorOptionsCache[id];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Joined display string for list/chips cells. */
|
|
247
|
+
function formatListCellValue(
|
|
248
|
+
column: Column<TData>,
|
|
249
|
+
value: unknown,
|
|
250
|
+
row?: Row<TData> | null,
|
|
251
|
+
): string {
|
|
252
|
+
const options = getColumnEditorOptions(column, row);
|
|
253
|
+
const sep = column.columnDef.editorSeparator ?? ", ";
|
|
254
|
+
if (Array.isArray(value)) {
|
|
255
|
+
return value.map((v) => getOptionLabel(options, v)).join(sep);
|
|
256
|
+
}
|
|
257
|
+
if (value == null || value === "") return "";
|
|
258
|
+
return getOptionLabel(options, value);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function formatCellValue(
|
|
262
|
+
column: Column<TData>,
|
|
263
|
+
value: unknown,
|
|
264
|
+
row: Row<TData>,
|
|
265
|
+
) {
|
|
266
|
+
const formatter = column.columnDef.formatter as
|
|
267
|
+
| CellFormatter<TData>
|
|
268
|
+
| undefined;
|
|
269
|
+
if (typeof formatter === "function") {
|
|
270
|
+
const formatted = formatter({ value, row, column, table: ctx.grid });
|
|
271
|
+
return String(formatted ?? "");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Password columns: mask the stored value with bullets when the cell
|
|
275
|
+
// is in read-only mode. The editor still receives the real string.
|
|
276
|
+
if (column.columnDef.editorType === "password") {
|
|
277
|
+
const s = String(value ?? "");
|
|
278
|
+
return s.length > 0 ? "•".repeat(Math.min(s.length, 12)) : "";
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const formatConfig = column.columnDef.format as
|
|
282
|
+
| CellFormatConfig
|
|
283
|
+
| undefined;
|
|
284
|
+
if (!formatConfig) return String(value ?? "");
|
|
285
|
+
|
|
286
|
+
if (
|
|
287
|
+
formatConfig.type === "number" ||
|
|
288
|
+
formatConfig.type === "currency" ||
|
|
289
|
+
formatConfig.type === "percent"
|
|
290
|
+
) {
|
|
291
|
+
return formatNumericWithConfig(value, {
|
|
292
|
+
type: formatConfig.type,
|
|
293
|
+
locales: formatConfig.locales,
|
|
294
|
+
currency:
|
|
295
|
+
formatConfig.type === "currency"
|
|
296
|
+
? (formatConfig.currency ?? "USD")
|
|
297
|
+
: undefined,
|
|
298
|
+
valueIsPercentPoints:
|
|
299
|
+
formatConfig.type === "percent"
|
|
300
|
+
? formatConfig.valueIsPercentPoints
|
|
301
|
+
: undefined,
|
|
302
|
+
options: formatConfig.options,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (formatConfig.type === "date" || formatConfig.type === "datetime") {
|
|
307
|
+
const parsedDate = asDate(value);
|
|
308
|
+
if (parsedDate) {
|
|
309
|
+
const preset = resolveDatePattern(
|
|
310
|
+
formatConfig.pattern,
|
|
311
|
+
formatConfig.type,
|
|
312
|
+
);
|
|
313
|
+
const merged: Intl.DateTimeFormatOptions =
|
|
314
|
+
preset || formatConfig.options
|
|
315
|
+
? { ...preset, ...formatConfig.options }
|
|
316
|
+
: formatConfig.type === "date"
|
|
317
|
+
? { year: "numeric", month: "2-digit", day: "2-digit" }
|
|
318
|
+
: {
|
|
319
|
+
year: "numeric",
|
|
320
|
+
month: "2-digit",
|
|
321
|
+
day: "2-digit",
|
|
322
|
+
hour: "2-digit",
|
|
323
|
+
minute: "2-digit",
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
return getDateFormatter(formatConfig.locales, merged).format(
|
|
327
|
+
parsedDate,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return String(value ?? "");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Format a cell value for a PINNED row. Same surface as
|
|
337
|
+
* `formatCellValue` but doesn't depend on Row<TData>. Custom
|
|
338
|
+
* `formatter` callbacks are invoked with a `row` of `null` (their
|
|
339
|
+
* type allows it; most don't read it).
|
|
340
|
+
*/
|
|
341
|
+
function formatPinnedValue(
|
|
342
|
+
column: Column<TData>,
|
|
343
|
+
value: unknown,
|
|
344
|
+
): string {
|
|
345
|
+
const def = column.columnDef;
|
|
346
|
+
const formatter = def.formatter as
|
|
347
|
+
| ((ctx: { value: unknown; row: null; column: Column<TData>; table: unknown }) => unknown)
|
|
348
|
+
| undefined;
|
|
349
|
+
if (typeof formatter === "function") {
|
|
350
|
+
return String(
|
|
351
|
+
formatter({ value, row: null, column, table: ctx.grid }) ?? "",
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
if (def.editorType === "password") {
|
|
355
|
+
const s = String(value ?? "");
|
|
356
|
+
return s.length > 0 ? "•".repeat(Math.min(s.length, 12)) : "";
|
|
357
|
+
}
|
|
358
|
+
const formatConfig = def.format as CellFormatConfig | undefined;
|
|
359
|
+
if (!formatConfig) return String(value ?? "");
|
|
360
|
+
if (
|
|
361
|
+
formatConfig.type === "number" ||
|
|
362
|
+
formatConfig.type === "currency" ||
|
|
363
|
+
formatConfig.type === "percent"
|
|
364
|
+
) {
|
|
365
|
+
return formatNumericWithConfig(value, {
|
|
366
|
+
type: formatConfig.type,
|
|
367
|
+
locales: formatConfig.locales,
|
|
368
|
+
currency:
|
|
369
|
+
formatConfig.type === "currency"
|
|
370
|
+
? (formatConfig.currency ?? "USD")
|
|
371
|
+
: undefined,
|
|
372
|
+
valueIsPercentPoints:
|
|
373
|
+
formatConfig.type === "percent"
|
|
374
|
+
? formatConfig.valueIsPercentPoints
|
|
375
|
+
: undefined,
|
|
376
|
+
options: formatConfig.options,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (formatConfig.type === "date" || formatConfig.type === "datetime") {
|
|
380
|
+
const parsedDate = asDate(value);
|
|
381
|
+
if (parsedDate) {
|
|
382
|
+
const preset = resolveDatePattern(formatConfig.pattern, formatConfig.type);
|
|
383
|
+
const merged: Intl.DateTimeFormatOptions =
|
|
384
|
+
preset || formatConfig.options
|
|
385
|
+
? { ...preset, ...formatConfig.options }
|
|
386
|
+
: formatConfig.type === "date"
|
|
387
|
+
? { year: "numeric", month: "2-digit", day: "2-digit" }
|
|
388
|
+
: { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" };
|
|
389
|
+
return getDateFormatter(formatConfig.locales, merged).format(parsedDate);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return String(value ?? "");
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Resolve `cellClass` for a pinned row. We synthesise a minimal
|
|
397
|
+
* context `{ getValue, value, row: null, column }`; cell-class
|
|
398
|
+
* callbacks that read only the value (the common case) work
|
|
399
|
+
* unchanged.
|
|
400
|
+
*/
|
|
401
|
+
function computePinnedCellClass(
|
|
402
|
+
rowData: TData,
|
|
403
|
+
column: Column<TData>,
|
|
404
|
+
): string {
|
|
405
|
+
const cellClass = column.columnDef.cellClass as
|
|
406
|
+
| ((ctx: { getValue: () => unknown; value: unknown; row: null; column: Column<TData> }) => string | undefined | null)
|
|
407
|
+
| undefined;
|
|
408
|
+
if (typeof cellClass !== "function") return "";
|
|
409
|
+
const value = getPinnedCellValue(rowData, column);
|
|
410
|
+
const out = cellClass({ getValue: () => value, value, row: null, column });
|
|
411
|
+
return out ? String(out) : "";
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return {
|
|
415
|
+
cellConditionalFormat,
|
|
416
|
+
computeRowClass,
|
|
417
|
+
computeCellClass,
|
|
418
|
+
computeCellTooltip,
|
|
419
|
+
computeCellNote,
|
|
420
|
+
getColumnEditorOptions,
|
|
421
|
+
formatListCellValue,
|
|
422
|
+
formatCellValue,
|
|
423
|
+
formatPinnedValue,
|
|
424
|
+
computePinnedCellClass,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Pure column/cell value access + display-formatting helpers. They operate
|
|
2
|
+
// only on their Column/Row/data arguments (plus the numeric formatter), so
|
|
3
|
+
// they hold no grid state and live outside the controller.
|
|
4
|
+
import type {
|
|
5
|
+
CellFormatConfig,
|
|
6
|
+
Column,
|
|
7
|
+
ColumnDef,
|
|
8
|
+
Row,
|
|
9
|
+
RowData,
|
|
10
|
+
TableFeatures,
|
|
11
|
+
} from "./index";
|
|
12
|
+
import { formatNumericWithConfig } from "./cell-formatting";
|
|
13
|
+
|
|
14
|
+
export function getColumnBaseValue<TData extends RowData>(row: Row<TData>, column: Column<TData>) {
|
|
15
|
+
const def = column.columnDef;
|
|
16
|
+
if (def.accessorFn) return def.accessorFn(row.original);
|
|
17
|
+
if (def.field) return row.original[def.field];
|
|
18
|
+
return row.getCellValueByColumnId(column.id);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isGroupRow<TData extends RowData>(row: Row<TData>) {
|
|
22
|
+
return typeof row.getCanExpand === "function" && row.getCanExpand();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function toolPanelHeaderLabel<TData extends RowData>(column: Column<TData>): string {
|
|
26
|
+
const h = column.columnDef.header;
|
|
27
|
+
return typeof h === "string" ? h : column.id;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Apply the column's `format` config to a raw numeric summary value
|
|
31
|
+
* (sum / avg / etc.). Mirrors the currency/number/percent branches in
|
|
32
|
+
* `formatCellValue` so a totaled salary column shows "$1,234,567" in
|
|
33
|
+
* the footer instead of "1234567". Per-row `formatter` functions are
|
|
34
|
+
* intentionally NOT invoked here - they may close over row state. */
|
|
35
|
+
export function formatSummaryNumeric<TData extends RowData>(column: Column<TData>, value: number): string {
|
|
36
|
+
const formatConfig = column.columnDef.format as
|
|
37
|
+
| CellFormatConfig
|
|
38
|
+
| undefined;
|
|
39
|
+
if (
|
|
40
|
+
formatConfig &&
|
|
41
|
+
(formatConfig.type === "number" ||
|
|
42
|
+
formatConfig.type === "currency" ||
|
|
43
|
+
formatConfig.type === "percent")
|
|
44
|
+
) {
|
|
45
|
+
return formatNumericWithConfig(value, {
|
|
46
|
+
type: formatConfig.type,
|
|
47
|
+
locales: formatConfig.locales,
|
|
48
|
+
currency:
|
|
49
|
+
formatConfig.type === "currency"
|
|
50
|
+
? (formatConfig.currency ?? "USD")
|
|
51
|
+
: undefined,
|
|
52
|
+
valueIsPercentPoints:
|
|
53
|
+
formatConfig.type === "percent"
|
|
54
|
+
? formatConfig.valueIsPercentPoints
|
|
55
|
+
: undefined,
|
|
56
|
+
options: formatConfig.options,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return String(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Effective horizontal alignment for a column. Honors the explicit
|
|
64
|
+
* `align` prop on the ColumnDef; otherwise picks a sensible default
|
|
65
|
+
* based on `editorType`:
|
|
66
|
+
* number / date / datetime → 'right'
|
|
67
|
+
* checkbox → 'center'
|
|
68
|
+
* everything else → 'left'
|
|
69
|
+
*/
|
|
70
|
+
export function getColumnAlign<TData extends RowData>(column: Column<TData>): "left" | "center" | "right" {
|
|
71
|
+
const explicit = column.columnDef.align;
|
|
72
|
+
if (explicit) return explicit;
|
|
73
|
+
const editorType = column.columnDef.editorType;
|
|
74
|
+
if (
|
|
75
|
+
editorType === "number" ||
|
|
76
|
+
editorType === "date" ||
|
|
77
|
+
editorType === "datetime"
|
|
78
|
+
) {
|
|
79
|
+
return "right";
|
|
80
|
+
}
|
|
81
|
+
if (editorType === "checkbox") return "center";
|
|
82
|
+
return "left";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Read a cell value from a PINNED row. Pinned rows aren't bound to a
|
|
87
|
+
* TanStack Row<TData>, so we resolve the value directly off the raw
|
|
88
|
+
* TData via `accessorFn` or `field`. Mirrors `getColumnBaseValue`'s
|
|
89
|
+
* lookup path minus the Row indirection.
|
|
90
|
+
*/
|
|
91
|
+
export function getPinnedCellValue<TData extends RowData>(
|
|
92
|
+
rowData: TData,
|
|
93
|
+
column: Column<TData>,
|
|
94
|
+
): unknown {
|
|
95
|
+
const def = column.columnDef;
|
|
96
|
+
if (def.accessorFn) return def.accessorFn(rowData);
|
|
97
|
+
const field = def.field as string | undefined;
|
|
98
|
+
if (!field) return undefined;
|
|
99
|
+
return (rowData as unknown as Record<string, unknown>)[field];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function getColumnAccessorValue<TData extends RowData>(rowData: TData, column: Column<TData>) {
|
|
103
|
+
const def = column.columnDef;
|
|
104
|
+
if (def.accessorFn) return def.accessorFn(rowData);
|
|
105
|
+
if (def.field) return rowData[def.field];
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function columnDefMatchesId<TFeatures extends TableFeatures, TData extends RowData>(
|
|
110
|
+
def: ColumnDef<TFeatures, TData>,
|
|
111
|
+
columnId: string,
|
|
112
|
+
): boolean {
|
|
113
|
+
return (def.id ?? def.field ?? null) === columnId;
|
|
114
|
+
}
|