@svgrid/grid 2.6.21 → 2.6.23
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/CHANGELOG.md +62 -0
- package/README.md +22 -0
- package/dist/GridMenus.svelte +201 -18
- package/dist/SvAutoComplete.svelte +4 -0
- package/dist/SvComboBox.svelte +5 -1
- package/dist/SvDateRangeInput.svelte +25 -3
- package/dist/SvDropDownList.svelte +4 -0
- package/dist/SvForm.svelte +8 -8
- package/dist/SvGrid.controller.svelte.d.ts +61 -9
- package/dist/SvGrid.controller.svelte.js +363 -123
- package/dist/SvGrid.css +222 -1
- package/dist/SvGrid.helpers.d.ts +55 -0
- package/dist/SvGrid.helpers.js +83 -0
- package/dist/SvGrid.svelte +255 -56
- package/dist/SvGrid.types.d.ts +146 -1
- package/dist/SvGridCellEditor.svelte +99 -17
- package/dist/SvGridSelect.svelte +6 -2
- package/dist/SvGridSelect.svelte.d.ts +1 -1
- package/dist/SvListBox.svelte +8 -0
- package/dist/SvMultiSelect.svelte +6 -2
- package/dist/SvMultiSelect.svelte.d.ts +1 -1
- package/dist/SvNumberInput.svelte +4 -0
- package/dist/SvRichCell.svelte +125 -0
- package/dist/SvRichCell.svelte.d.ts +15 -0
- package/dist/SvTextArea.svelte +5 -1
- package/dist/SvTreeSelect.svelte +6 -2
- package/dist/SvTreeSelect.svelte.d.ts +1 -1
- package/dist/builtin-editors.d.ts +2 -2
- package/dist/builtin-editors.js +19 -3
- package/dist/cdn/GridMenus-BzWXQjv3.js +600 -0
- package/dist/cdn/GridMenus-xr_rHx8F.js +601 -0
- package/dist/cdn/SvDateRangeInput-CaOuMs8O.js +198 -0
- package/dist/cdn/SvDateRangeInput-DMLKmGEc.js +199 -0
- package/dist/cdn/SvDateTimePicker-CCbDZNZB.js +816 -0
- package/dist/cdn/SvDateTimePicker-sonaH0oh.js +812 -0
- package/dist/cdn/SvGridCellEditor-BAPSC7EL.js +550 -0
- package/dist/cdn/SvGridCellEditor-BwzmUWtL.js +549 -0
- package/dist/cdn/column-resize-DsfNXMom.js +102 -0
- package/dist/cdn/date-format-BNii4zeD.js +1393 -0
- package/dist/cdn/date-format-BnnHlqGw.js +1395 -0
- package/dist/cdn/{editor-registry-CiI0xlKg.js → editor-registry-BhgE3S84.js} +51 -25
- package/dist/cdn/row-resize-BRcimkUT.js +95 -0
- package/dist/cdn/{src-DBel9wRZ.js → src-DGo7IHug.js} +10017 -9488
- package/dist/cdn/{src-BYq-qyrp.js → src-skQN5eqh.js} +7829 -7300
- package/dist/cdn/svgrid.js +14 -10
- package/dist/cdn/svgrid.svelte-external.js +14 -10
- package/dist/cell-values.d.ts +16 -0
- package/dist/cell-values.js +28 -0
- package/dist/clipboard.d.ts +22 -0
- package/dist/clipboard.js +405 -27
- package/dist/column-groups.js +1 -1
- package/dist/column-resize.d.ts +46 -0
- package/dist/column-resize.js +205 -0
- package/dist/columns.d.ts +0 -3
- package/dist/columns.js +0 -57
- package/dist/core.d.ts +19 -4
- package/dist/core.js +460 -119
- package/dist/editing.js +12 -7
- package/dist/editors/cell-editors.d.ts +11 -0
- package/dist/editors/cell-editors.js +18 -1
- package/dist/filtering/excel-filters.js +28 -0
- package/dist/grid-messages.d.ts +15 -0
- package/dist/grid-messages.js +15 -0
- package/dist/group-display.d.ts +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +10 -0
- package/dist/list-option.d.ts +6 -0
- package/dist/menus.js +41 -8
- package/dist/row-resize.d.ts +11 -0
- package/dist/row-resize.js +7 -1
- package/dist/sanitize-html.d.ts +37 -0
- package/dist/sanitize-html.js +234 -0
- package/dist/selection-bar-view.svelte.d.ts +25 -0
- package/dist/selection-bar-view.svelte.js +13 -0
- package/dist/selection.d.ts +2 -1
- package/dist/selection.js +133 -8
- package/dist/spreadsheet.d.ts +1 -1
- package/dist/spreadsheet.js +1 -1
- package/package.json +1 -1
- package/src/GridMenus.svelte +201 -18
- package/src/SvAutoComplete.svelte +4 -0
- package/src/SvComboBox.svelte +5 -1
- package/src/SvDateRangeInput.svelte +25 -3
- package/src/SvDropDownList.svelte +4 -0
- package/src/SvForm.svelte +8 -8
- package/src/SvGrid.controller.svelte.ts +404 -133
- package/src/SvGrid.css +222 -1
- package/src/SvGrid.helpers.ts +87 -0
- package/src/SvGrid.svelte +255 -56
- package/src/SvGrid.types.ts +153 -1
- package/src/SvGridCellEditor.svelte +99 -17
- package/src/SvGridSelect.svelte +6 -2
- package/src/SvListBox.svelte +8 -0
- package/src/SvMultiSelect.svelte +6 -2
- package/src/SvNumberInput.svelte +4 -0
- package/src/SvRichCell.svelte +125 -0
- package/src/SvTextArea.svelte +5 -1
- package/src/SvTreeSelect.svelte +6 -2
- package/src/builtin-editors.test.ts +18 -1
- package/src/builtin-editors.ts +19 -3
- package/src/cell-values.ts +30 -0
- package/src/clipboard.test.ts +93 -20
- package/src/clipboard.ts +433 -33
- package/src/column-groups.ts +1 -1
- package/src/column-resize.test.ts +381 -0
- package/src/column-resize.ts +227 -0
- package/src/columns.test.ts +0 -103
- package/src/columns.ts +0 -58
- package/src/core.aggregate.test.ts +134 -0
- package/src/core.filter.test.ts +156 -0
- package/src/core.grouping.test.ts +146 -0
- package/src/core.row-shape.test.ts +119 -0
- package/src/core.rowmodel-cache.test.ts +121 -0
- package/src/core.sort.test.ts +293 -0
- package/src/core.ts +516 -119
- package/src/date-string-column.test.ts +87 -0
- package/src/editing.test.ts +25 -0
- package/src/editing.ts +12 -9
- package/src/editor-block-prop.test.ts +132 -0
- package/src/editors/cell-editors.ts +27 -1
- package/src/filtering/excel-filters.ts +30 -0
- package/src/filtering/normalize-fast-path.test.ts +104 -0
- package/src/grid-messages.ts +34 -0
- package/src/group-display.ts +1 -1
- package/src/index.ts +25 -1
- package/src/list-option.ts +6 -0
- package/src/menus.test.ts +43 -0
- package/src/menus.ts +38 -8
- package/src/move-cells.test.ts +850 -0
- package/src/number-editor-literal.test.ts +84 -0
- package/src/number-editor.grid.test.ts +160 -0
- package/src/resize-props.test.ts +361 -0
- package/src/row-resize.test.ts +31 -0
- package/src/row-resize.ts +21 -3
- package/src/sanitize-html.test.ts +182 -0
- package/src/sanitize-html.ts +235 -0
- package/src/selection-bar-view.svelte.ts +36 -0
- package/src/selection.test.ts +157 -0
- package/src/selection.ts +135 -8
- package/src/spreadsheet.ts +1 -1
- package/src/svgrid.filter-menu-listbox.svelte.test.ts +56 -10
- package/src/svgrid.in-suggest-snapshot.svelte.test.ts +91 -0
- package/src/svgrid.menu-scroll-close.test.ts +204 -0
- package/src/svgrid.selection-bar-seam.test.ts +185 -0
- package/src/svgrid.upsell-license.test.ts +117 -0
- package/dist/cdn/GridMenus-BfTAKn84.js +0 -488
- package/dist/cdn/GridMenus-C3bJd7w8.js +0 -489
- package/dist/cdn/SvDateTimePicker-CHnUkWcH.js +0 -2206
- package/dist/cdn/SvDateTimePicker-CRQH_U7E.js +0 -2201
- package/dist/cdn/SvGridCellEditor-BGUCzuPB.js +0 -523
- package/dist/cdn/SvGridCellEditor-G8IMWIws.js +0 -522
package/src/core.ts
CHANGED
|
@@ -210,27 +210,69 @@ export function applyGroupAggregate<TData extends RowData>(
|
|
|
210
210
|
columnId: string,
|
|
211
211
|
rows: ReadonlyArray<Row<TData>>,
|
|
212
212
|
): unknown {
|
|
213
|
-
|
|
213
|
+
// One pass, no intermediate arrays.
|
|
214
|
+
//
|
|
215
|
+
// This used to build a `raw` array, then a coerced one, then a filtered one -
|
|
216
|
+
// three allocations per aggregated column PER GROUP - before reducing. On a
|
|
217
|
+
// 100k-row grid grouped two levels deep, aggregation was about two thirds of
|
|
218
|
+
// the total grouping cost (213ms with three aggregators against 81ms with
|
|
219
|
+
// none), and each additional aggregated column added roughly 80ms.
|
|
220
|
+
//
|
|
221
|
+
// `count` first: it never needs to look at a value at all.
|
|
222
|
+
if (agg === 'count') return rows.length
|
|
223
|
+
|
|
224
|
+
if (agg === 'first') {
|
|
225
|
+
return rows.length ? rows[0]!.getCellValueByColumnId(columnId) : undefined
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (agg === 'countDistinct') {
|
|
229
|
+
const seen = new Set<string>()
|
|
230
|
+
for (const row of rows) seen.add(String(row.getCellValueByColumnId(columnId) ?? ''))
|
|
231
|
+
return seen.size
|
|
232
|
+
}
|
|
233
|
+
|
|
214
234
|
if (typeof agg === 'function') {
|
|
215
|
-
|
|
235
|
+
// Custom aggregators keep their contract: the finite numbers, then the
|
|
236
|
+
// original row objects. Note `Number(null)` is 0 and therefore finite, so
|
|
237
|
+
// nulls DO reach the callback as zeros - long-standing behaviour.
|
|
238
|
+
const nums: number[] = []
|
|
239
|
+
for (const row of rows) {
|
|
240
|
+
const n = Number(row.getCellValueByColumnId(columnId))
|
|
241
|
+
if (Number.isFinite(n)) nums.push(n)
|
|
242
|
+
}
|
|
216
243
|
return agg(nums, rows.map((r) => r.original))
|
|
217
244
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
245
|
+
|
|
246
|
+
// sum / avg / min / max / extent share one accumulation pass.
|
|
247
|
+
let count = 0
|
|
248
|
+
let sum = 0
|
|
249
|
+
let min = Infinity
|
|
250
|
+
let max = -Infinity
|
|
251
|
+
for (const row of rows) {
|
|
252
|
+
const n = Number(row.getCellValueByColumnId(columnId))
|
|
253
|
+
if (!Number.isFinite(n)) continue
|
|
254
|
+
count++
|
|
255
|
+
// Left-to-right, matching the previous `reduce`, so float rounding is
|
|
256
|
+
// bit-identical rather than merely close.
|
|
257
|
+
sum += n
|
|
258
|
+
// Math.min/max on scalars rather than `<`, which differs on -0, and rather
|
|
259
|
+
// than the old `Math.min(...nums)` - spreading a whole group throws
|
|
260
|
+
// RangeError once the bucket is big enough to exhaust the argument stack.
|
|
261
|
+
min = Math.min(min, n)
|
|
262
|
+
max = Math.max(max, n)
|
|
263
|
+
}
|
|
264
|
+
if (!count) return undefined
|
|
223
265
|
switch (agg) {
|
|
224
266
|
case 'sum':
|
|
225
|
-
return
|
|
267
|
+
return sum
|
|
226
268
|
case 'avg':
|
|
227
|
-
return
|
|
269
|
+
return sum / count
|
|
228
270
|
case 'min':
|
|
229
|
-
return
|
|
271
|
+
return min
|
|
230
272
|
case 'max':
|
|
231
|
-
return
|
|
273
|
+
return max
|
|
232
274
|
case 'extent':
|
|
233
|
-
return `${
|
|
275
|
+
return `${min} – ${max}`
|
|
234
276
|
default:
|
|
235
277
|
return undefined
|
|
236
278
|
}
|
|
@@ -256,7 +298,7 @@ export type ColumnDef<TFeatures extends TableFeatures, TData extends RowData> =
|
|
|
256
298
|
columns?: Array<ColumnDef<TFeatures, TData>>
|
|
257
299
|
/**
|
|
258
300
|
* Declarative cell spanning (merged cells). Return how many COLUMNS this
|
|
259
|
-
* cell spans to the right (1 = no span). Value-driven
|
|
301
|
+
* cell spans to the right (1 = no span). Value-driven. Feed
|
|
260
302
|
* `spansToMerges(rows, columns)` into `spreadsheetLayout` to apply - it uses
|
|
261
303
|
* the same real `colspan`/`rowspan` merge engine (no separate code path).
|
|
262
304
|
*/
|
|
@@ -295,7 +337,7 @@ export type ColumnDef<TFeatures extends TableFeatures, TData extends RowData> =
|
|
|
295
337
|
columnGroupShow?: 'open' | 'closed'
|
|
296
338
|
/**
|
|
297
339
|
* For a GROUP column (one with `columns: [...]`): start the group expanded.
|
|
298
|
-
* Defaults to `false` (collapsed),
|
|
340
|
+
* Defaults to `false` (collapsed), the conventional default - so only the always-on
|
|
299
341
|
* and `columnGroupShow: 'closed'` children show until the user expands it.
|
|
300
342
|
*/
|
|
301
343
|
openByDefault?: boolean
|
|
@@ -340,7 +382,7 @@ export type ColumnDef<TFeatures extends TableFeatures, TData extends RowData> =
|
|
|
340
382
|
*/
|
|
341
383
|
tooltip?: string | ((ctx: CellContext<TData>) => string | null | undefined)
|
|
342
384
|
/**
|
|
343
|
-
* Declarative per-cell validation
|
|
385
|
+
* Declarative per-cell validation. Runs for EVERY
|
|
344
386
|
* rendered cell - including values already present in `data` on load, not
|
|
345
387
|
* just on edit - so bad data is flagged immediately. Invalid cells get the
|
|
346
388
|
* `sv-grid-cell-invalid` class (red highlight) and the returned message as
|
|
@@ -379,7 +421,7 @@ export type ColumnDef<TFeatures extends TableFeatures, TData extends RowData> =
|
|
|
379
421
|
* Transform the committed edit value before it is written to the row.
|
|
380
422
|
* Runs after the built-in per-`editorType` coercion, so `newValue` is
|
|
381
423
|
* already type-parsed; return the final value to store (e.g. round a
|
|
382
|
-
* number, uppercase a code, look up an id).
|
|
424
|
+
* number, uppercase a code, look up an id). A `valueParser` hook.
|
|
383
425
|
*/
|
|
384
426
|
valueParser?: (params: ValueParserParams<TData>) => unknown
|
|
385
427
|
/**
|
|
@@ -466,6 +508,21 @@ export type ColumnDef<TFeatures extends TableFeatures, TData extends RowData> =
|
|
|
466
508
|
sparkline?: SparklineConfig
|
|
467
509
|
/** Initial column width in pixels. Falls back to the grid's `columnWidth` prop. */
|
|
468
510
|
width?: number
|
|
511
|
+
/**
|
|
512
|
+
* Whether the user may resize this column. Only consulted when the grid has
|
|
513
|
+
* `columnResize` on - it narrows that, it does not enable anything.
|
|
514
|
+
*
|
|
515
|
+
* `false` removes the column's drag handle entirely, so pointer drag, the
|
|
516
|
+
* keyboard arrows and double-click-to-autosize are all gone with it, and the
|
|
517
|
+
* column menu drops its Autosize item. Use it for the columns whose width is
|
|
518
|
+
* part of the layout rather than a preference: a row-number gutter, a
|
|
519
|
+
* checkbox column, a fixed icon column.
|
|
520
|
+
*
|
|
521
|
+
* Programmatic sizing is unaffected - `api.autosizeColumn()`,
|
|
522
|
+
* `api.setColumnWidth()` and `fitColumns` all still apply, the same way they
|
|
523
|
+
* do when `columnResize` is off. This governs the user affordance only.
|
|
524
|
+
*/
|
|
525
|
+
resizable?: boolean
|
|
469
526
|
/**
|
|
470
527
|
* Initial visibility. Set `false` to start the column hidden while still
|
|
471
528
|
* listing it in the Choose Columns UI for the user to re-enable. Applied
|
|
@@ -689,6 +746,143 @@ export const filterFns = {
|
|
|
689
746
|
equals: (value: unknown, query: unknown) => value === query,
|
|
690
747
|
}
|
|
691
748
|
|
|
749
|
+
/**
|
|
750
|
+
* Everything a base row needs that is the same for every row in the table.
|
|
751
|
+
*
|
|
752
|
+
* One object per table, referenced by every row, instead of one closure scope
|
|
753
|
+
* per row. See {@link BASE_ROW_METHODS}.
|
|
754
|
+
*/
|
|
755
|
+
type BaseRowCtx<TData extends RowData> = {
|
|
756
|
+
grid: SvGrid<TData>
|
|
757
|
+
store: { state: Record<string, any> }
|
|
758
|
+
columns: Array<Column<TData>>
|
|
759
|
+
columnCount: number
|
|
760
|
+
columnIndexById: Map<string, number>
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Keys for a base row's private fields.
|
|
765
|
+
*
|
|
766
|
+
* Symbols, not string keys, and that is load-bearing. A row's shared methods
|
|
767
|
+
* need a pointer back to the table, but `_ctx` as a normal property made every
|
|
768
|
+
* row serialise the entire grid: `JSON.stringify(oneRow)` grew with the dataset
|
|
769
|
+
* (981 chars at 3 rows, 67,719 at 3,000) because `options.data` is reachable
|
|
770
|
+
* through it, so stringifying a row model was quadratic. Rows used to serialise
|
|
771
|
+
* to a small constant and must again.
|
|
772
|
+
*
|
|
773
|
+
* A symbol key is invisible to `JSON.stringify`, `Object.keys` and `for...in`,
|
|
774
|
+
* yet IS copied by object spread - which matters because several row models
|
|
775
|
+
* legitimately do `{ ...row, depth }` and the clone needs these to work.
|
|
776
|
+
* Non-enumerable string keys would have hidden them from JSON but also from the
|
|
777
|
+
* spread, silently breaking every cloned row.
|
|
778
|
+
*/
|
|
779
|
+
const ROW_CTX = Symbol('svgrid.row.ctx')
|
|
780
|
+
const ROW_VALUES = Symbol('svgrid.row.values')
|
|
781
|
+
const ROW_CELLS = Symbol('svgrid.row.cells')
|
|
782
|
+
|
|
783
|
+
/** A base row's private fields, on top of the public {@link Row} surface. */
|
|
784
|
+
type BaseRowState<TData extends RowData> = Row<TData> & {
|
|
785
|
+
[ROW_CTX]: BaseRowCtx<TData>
|
|
786
|
+
[ROW_VALUES]: Array<unknown> | null
|
|
787
|
+
[ROW_CELLS]: Array<Cell<TData>> | null
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* The methods every base row carries, defined ONCE and assigned by reference.
|
|
792
|
+
*
|
|
793
|
+
* Rows used to be built as object literals whose methods were closures, which
|
|
794
|
+
* meant a 100k-row grid allocated 700k closures and a closure scope per row
|
|
795
|
+
* before painting anything. Measured at 100k x 9: 13.8 ms and 56.5 MB to build,
|
|
796
|
+
* against 2.0 ms and 14.5 MB for this shape - the single largest cost in
|
|
797
|
+
* mounting a large grid.
|
|
798
|
+
*
|
|
799
|
+
* They read their row through `this` rather than a captured variable, which is
|
|
800
|
+
* why they can be shared. Note they are assigned as OWN properties rather than
|
|
801
|
+
* put on a prototype: `Row` is public, several row models legitimately do
|
|
802
|
+
* `{ ...row, depth }`, and a spread copies own properties but not a prototype.
|
|
803
|
+
* A class here would silently strip every method off a cloned row.
|
|
804
|
+
*/
|
|
805
|
+
const BASE_ROW_METHODS = {
|
|
806
|
+
getCanExpand(this: BaseRowState<RowData>) {
|
|
807
|
+
return false
|
|
808
|
+
},
|
|
809
|
+
getIsExpanded(this: BaseRowState<RowData>) {
|
|
810
|
+
return Boolean((this[ROW_CTX].store.state.expanded ?? {})[this.id])
|
|
811
|
+
},
|
|
812
|
+
toggleExpanded(this: BaseRowState<RowData>) {
|
|
813
|
+
const id = this.id
|
|
814
|
+
this[ROW_CTX].grid.setExpanded((prev) => ({ ...prev, [id]: !prev[id] }))
|
|
815
|
+
},
|
|
816
|
+
getIsSelected(this: BaseRowState<RowData>) {
|
|
817
|
+
return Boolean((this[ROW_CTX].store.state.rowSelection ?? {})[this.id])
|
|
818
|
+
},
|
|
819
|
+
toggleSelected(this: BaseRowState<RowData>) {
|
|
820
|
+
const id = this.id
|
|
821
|
+
this[ROW_CTX].grid.setRowSelection((prev) => ({ ...prev, [id]: !prev[id] }))
|
|
822
|
+
},
|
|
823
|
+
getAllCells(this: BaseRowState<RowData>) {
|
|
824
|
+
return this[ROW_CELLS] ?? buildBaseRowCells(this)
|
|
825
|
+
},
|
|
826
|
+
getCellValueByColumnId(this: BaseRowState<RowData>, columnId: string) {
|
|
827
|
+
const idx = this[ROW_CTX].columnIndexById.get(columnId)
|
|
828
|
+
if (idx === undefined) return undefined
|
|
829
|
+
if (!this[ROW_VALUES]) this[ROW_VALUES] = baseRowValues(this)
|
|
830
|
+
return this[ROW_VALUES][idx]
|
|
831
|
+
},
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Materialise one row's `Cell[]`, memoised on the row.
|
|
836
|
+
*
|
|
837
|
+
* A free function taking the row rather than a method using `this`, because the
|
|
838
|
+
* cell closures need a stable reference to it and aliasing `this` inside a
|
|
839
|
+
* method is exactly the pattern that produces `self`/`that` bugs.
|
|
840
|
+
*/
|
|
841
|
+
function buildBaseRowCells<TData extends RowData>(row: BaseRowState<TData>): Array<Cell<TData>> {
|
|
842
|
+
const { columns, columnCount, grid } = row[ROW_CTX]
|
|
843
|
+
const built = new Array<Cell<TData>>(columnCount)
|
|
844
|
+
for (let i = 0; i < columnCount; i++) {
|
|
845
|
+
const column = columns[i]!
|
|
846
|
+
const colIndex = i
|
|
847
|
+
const cell: Cell<TData> = {
|
|
848
|
+
id: `${row.id}_${column.id}`,
|
|
849
|
+
row,
|
|
850
|
+
column,
|
|
851
|
+
getValue: () => {
|
|
852
|
+
if (!row[ROW_VALUES]) row[ROW_VALUES] = baseRowValues(row)
|
|
853
|
+
return row[ROW_VALUES][colIndex]
|
|
854
|
+
},
|
|
855
|
+
getContext: () => ({
|
|
856
|
+
cell,
|
|
857
|
+
row,
|
|
858
|
+
column,
|
|
859
|
+
table: grid,
|
|
860
|
+
getValue: () => cell.getValue(),
|
|
861
|
+
}),
|
|
862
|
+
}
|
|
863
|
+
built[i] = cell
|
|
864
|
+
}
|
|
865
|
+
row[ROW_CELLS] = built
|
|
866
|
+
return built
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Resolve every column's value for one row. Kept lazy: a 100k-row grid showing
|
|
871
|
+
* twenty rows must not materialise 900k values to paint.
|
|
872
|
+
*/
|
|
873
|
+
function baseRowValues<TData extends RowData>(row: BaseRowState<TData>): Array<unknown> {
|
|
874
|
+
const { columns, columnCount } = row[ROW_CTX]
|
|
875
|
+
const original = row.original as Record<string, unknown>
|
|
876
|
+
const values = new Array<unknown>(columnCount)
|
|
877
|
+
for (let i = 0; i < columnCount; i++) {
|
|
878
|
+
const def = columns[i]!.columnDef
|
|
879
|
+
if (def.fieldFn) values[i] = def.fieldFn(original as TData)
|
|
880
|
+
else if (def.field) values[i] = original[def.field]
|
|
881
|
+
else values[i] = undefined
|
|
882
|
+
}
|
|
883
|
+
return values
|
|
884
|
+
}
|
|
885
|
+
|
|
692
886
|
/**
|
|
693
887
|
* One stage of the row pipeline: takes the rows produced so far and returns the
|
|
694
888
|
* next set. Stages compose in the order given to `_rowModels`, so filtering
|
|
@@ -714,15 +908,26 @@ export function createFilteredRowModel<TData extends RowData>(): RowModelFactory
|
|
|
714
908
|
return ({ table, rows }) => {
|
|
715
909
|
const filters: ColumnFiltersState = table.getState().columnFilters ?? []
|
|
716
910
|
if (!filters.length) return rows
|
|
911
|
+
|
|
912
|
+
// Resolve each filter's match function once, outside the row loop.
|
|
913
|
+
const compiled = filters.map((filter) => ({
|
|
914
|
+
id: filter.id,
|
|
915
|
+
value: filter.value,
|
|
916
|
+
fn: filter.fn ? filterFns[filter.fn] : filterFns.includesString,
|
|
917
|
+
}))
|
|
918
|
+
|
|
717
919
|
return rows.filter((row) => {
|
|
718
|
-
|
|
719
|
-
const
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
920
|
+
for (let i = 0; i < compiled.length; i++) {
|
|
921
|
+
const filter = compiled[i]!
|
|
922
|
+
// `getCellValueByColumnId` rather than `getAllCells().find(...)`.
|
|
923
|
+
// Both read the same lazily-built `cachedValues` array, but the latter
|
|
924
|
+
// also builds and caches the row's whole `Cell[]` - one object per
|
|
925
|
+
// column - purely to reach one field. On a 100k-row grid that is
|
|
926
|
+
// 100,000 cell arrays the filter never looks at again, and it defeats
|
|
927
|
+
// the laziness the row factory exists to provide.
|
|
928
|
+
if (!filter.fn(row.getCellValueByColumnId(filter.id), filter.value as any)) return false
|
|
929
|
+
}
|
|
930
|
+
return true
|
|
726
931
|
})
|
|
727
932
|
}
|
|
728
933
|
}
|
|
@@ -756,6 +961,9 @@ export function createGroupedRowModel<TData extends RowData>(): RowModelFactory<
|
|
|
756
961
|
levelIndex: number,
|
|
757
962
|
depth: number,
|
|
758
963
|
idPrefix: string,
|
|
964
|
+
/** Grouping columns already fixed by an ancestor bucket, and their raw
|
|
965
|
+
* values - `undefined` where that bucket mixed several. */
|
|
966
|
+
fixedValues: ReadonlyMap<string, unknown>,
|
|
759
967
|
): Array<Row<TData>> {
|
|
760
968
|
if (levelIndex >= grouping.length) {
|
|
761
969
|
// Leaves: actual data rows, with their nesting depth recorded.
|
|
@@ -764,27 +972,60 @@ export function createGroupedRowModel<TData extends RowData>(): RowModelFactory<
|
|
|
764
972
|
const groupKey = grouping[levelIndex]
|
|
765
973
|
if (!groupKey) return input
|
|
766
974
|
|
|
767
|
-
|
|
975
|
+
// Buckets carry the RAW grouping value alongside the rows, plus whether
|
|
976
|
+
// the bucket saw more than one distinct raw value. Both are needed to let
|
|
977
|
+
// deeper levels skip re-scanning this column: buckets are keyed by
|
|
978
|
+
// `String(value ?? '')`, so `null`, `undefined` and `''` collapse into one
|
|
979
|
+
// bucket, and a scan of such a bucket would report disagreement. Tracking
|
|
980
|
+
// it here costs one comparison per row and keeps the shortcut honest.
|
|
981
|
+
type Bucket = { rows: Array<Row<TData>>; raw: unknown; mixed: boolean }
|
|
982
|
+
const buckets = new Map<string, Bucket>()
|
|
768
983
|
for (const row of input) {
|
|
769
984
|
const value = row.getCellValueByColumnId(groupKey)
|
|
770
985
|
const key = String(value ?? '')
|
|
771
|
-
const
|
|
772
|
-
|
|
773
|
-
|
|
986
|
+
const bucket = buckets.get(key)
|
|
987
|
+
if (bucket) {
|
|
988
|
+
bucket.rows.push(row)
|
|
989
|
+
if (!bucket.mixed && bucket.raw !== value) bucket.mixed = true
|
|
990
|
+
} else {
|
|
991
|
+
buckets.set(key, { rows: [row], raw: value, mixed: false })
|
|
992
|
+
}
|
|
774
993
|
}
|
|
775
994
|
|
|
776
995
|
const groupRows: Array<Row<TData>> = []
|
|
777
996
|
let index = 0
|
|
778
|
-
buckets.forEach((
|
|
997
|
+
buckets.forEach((bucket, key) => {
|
|
998
|
+
const children = bucket.rows
|
|
779
999
|
const id = `${idPrefix}_${groupKey}_${key}`
|
|
780
|
-
|
|
1000
|
+
// Record this column as fixed for deeper levels ONLY when the bucket is
|
|
1001
|
+
// homogeneous. If all rows here share a raw value, so does every subset
|
|
1002
|
+
// of them, which is what makes the shortcut sound.
|
|
1003
|
+
//
|
|
1004
|
+
// A MIXED bucket must not be recorded at all - not even as "undefined".
|
|
1005
|
+
// `null`, `undefined` and `''` share a bucket key, so a mixed bucket can
|
|
1006
|
+
// still split into homogeneous children one level down, and those
|
|
1007
|
+
// children have a real shared value that a scan would find. Marking the
|
|
1008
|
+
// column resolved here would hand them the parent's disagreement.
|
|
1009
|
+
const nextFixed = bucket.mixed ? fixedValues : new Map(fixedValues).set(groupKey, bucket.raw)
|
|
1010
|
+
const subRows = buildGroups(children, levelIndex + 1, depth + 1, id, nextFixed)
|
|
781
1011
|
const isDeepest = levelIndex + 1 >= grouping.length
|
|
782
1012
|
const leafCount = isDeepest
|
|
783
1013
|
? subRows.length
|
|
784
1014
|
: subRows.reduce((sum, sub) => sum + (sub.leafCount ?? 0), 0)
|
|
785
1015
|
|
|
1016
|
+
// Every grouping column ABOVE this level is already resolved: bucketing
|
|
1017
|
+
// by it is what made it constant, so scanning the children to rediscover
|
|
1018
|
+
// it is pure waste. Only the current level's key short-circuited before,
|
|
1019
|
+
// so a second-level group walked all of its children to re-derive the
|
|
1020
|
+
// first level's value - about 100,000 reads on the 100k x 9 two-level
|
|
1021
|
+
// case, for an answer already in hand.
|
|
1022
|
+
//
|
|
1023
|
+
// The current level still returns the stringified bucket key rather than
|
|
1024
|
+
// the raw value, because that is what it has always returned and the
|
|
1025
|
+
// group row's display depends on it.
|
|
786
1026
|
const resolveColumnValue = (columnId: string): unknown => {
|
|
787
1027
|
if (columnId === groupKey) return key
|
|
1028
|
+
if (fixedValues.has(columnId)) return fixedValues.get(columnId)
|
|
788
1029
|
let resolved: unknown
|
|
789
1030
|
let hasResolved = false
|
|
790
1031
|
for (const child of children) {
|
|
@@ -841,7 +1082,7 @@ export function createGroupedRowModel<TData extends RowData>(): RowModelFactory<
|
|
|
841
1082
|
return groupRows
|
|
842
1083
|
}
|
|
843
1084
|
|
|
844
|
-
return buildGroups(rows, 0, 0, 'group')
|
|
1085
|
+
return buildGroups(rows, 0, 0, 'group', new Map())
|
|
845
1086
|
}
|
|
846
1087
|
}
|
|
847
1088
|
/**
|
|
@@ -996,33 +1237,211 @@ export function createExpandedRowModel<TData extends RowData>(): RowModelFactory
|
|
|
996
1237
|
export function createSortedRowModel<TData extends RowData>(
|
|
997
1238
|
localSortFns: typeof sortFns = sortFns,
|
|
998
1239
|
): RowModelFactory<TData> {
|
|
999
|
-
return ({ table, rows })
|
|
1240
|
+
return function sortedRowModelStage({ table, rows }) {
|
|
1000
1241
|
const sorting = table.getState().sorting ?? []
|
|
1001
1242
|
if (!sorting.length) return rows
|
|
1002
1243
|
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1244
|
+
// Resolve every clause ONCE, before sorting.
|
|
1245
|
+
//
|
|
1246
|
+
// This used to live inside the comparator, so `getAllColumns().find(...)`
|
|
1247
|
+
// ran per comparison per clause: a single-clause sort of 100k rows made
|
|
1248
|
+
// 1,528,947 array scans, and a three-clause sort made 3,933,751 (measured;
|
|
1249
|
+
// `pnpm bench --case=sort-1col`). The comparator is called O(n log n)
|
|
1250
|
+
// times, so anything inside it that is not O(1) sets the cost of the sort.
|
|
1251
|
+
const allColumns = table.getAllColumns()
|
|
1252
|
+
const clauses: Array<{
|
|
1253
|
+
keys: Array<any>
|
|
1254
|
+
desc: boolean
|
|
1255
|
+
compare: (a: any, b: any) => number
|
|
1256
|
+
}> = []
|
|
1257
|
+
|
|
1258
|
+
for (const clause of sorting) {
|
|
1259
|
+
const column = allColumns.find((col) => col.id === clause.id)
|
|
1260
|
+
if (!column) continue
|
|
1261
|
+
const editorType = column.columnDef.editorType
|
|
1262
|
+
const comparator =
|
|
1263
|
+
editorType === 'number'
|
|
1264
|
+
? localSortFns.number
|
|
1265
|
+
: editorType === 'date' || editorType === 'datetime'
|
|
1266
|
+
? localSortFns.date
|
|
1267
|
+
: localSortFns.auto
|
|
1268
|
+
|
|
1269
|
+
// Precompute one sort key per row, so the comparator reads an array slot
|
|
1270
|
+
// instead of walking the row's column index on every comparison. For the
|
|
1271
|
+
// three built-in comparators the key is also cheaper to compare than the
|
|
1272
|
+
// raw value: a timestamp rather than two `new Date()` allocations, a
|
|
1273
|
+
// number rather than two `Number()` coercions, a collator rather than a
|
|
1274
|
+
// fresh one per `localeCompare` call.
|
|
1275
|
+
//
|
|
1276
|
+
// The identity checks against `sortFns` matter: `localSortFns` is a
|
|
1277
|
+
// public parameter, so a caller can substitute their own comparators.
|
|
1278
|
+
// When they have, we fall through to calling their function with the raw
|
|
1279
|
+
// values - still hoisted, just not specialised.
|
|
1280
|
+
const columnId = column.id
|
|
1281
|
+
const n = rows.length
|
|
1282
|
+
let keys: Array<any> = new Array(n)
|
|
1283
|
+
let compare: (a: any, b: any) => number
|
|
1284
|
+
|
|
1285
|
+
if (comparator === sortFns.number) {
|
|
1286
|
+
for (let i = 0; i < n; i++) keys[i] = Number(rows[i]!.getCellValueByColumnId(columnId) ?? 0)
|
|
1287
|
+
compare = compareNumericKeys
|
|
1288
|
+
} else if (comparator === sortFns.date) {
|
|
1289
|
+
for (let i = 0; i < n; i++) {
|
|
1290
|
+
keys[i] = new Date(rows[i]!.getCellValueByColumnId(columnId) as any).getTime()
|
|
1291
|
+
}
|
|
1292
|
+
compare = compareNumericKeys
|
|
1293
|
+
} else if (comparator === sortFns.auto) {
|
|
1294
|
+
const strings: string[] = new Array(n)
|
|
1295
|
+
// Decide whether ranking is worth attempting BEFORE paying for it.
|
|
1296
|
+
//
|
|
1297
|
+
// Building the distinct set and then discarding it costs about 9 ms on
|
|
1298
|
+
// a 100k-row column where nearly every value is unique, and ranking
|
|
1299
|
+
// saves about 19 ms where they repeat - so guessing wrong in either
|
|
1300
|
+
// direction is measurable. A small stride sample answers it for well
|
|
1301
|
+
// under a millisecond.
|
|
1302
|
+
//
|
|
1303
|
+
// Strided rather than the first N rows: data arrives sorted or
|
|
1304
|
+
// clustered often enough that a prefix is a bad estimator of the whole
|
|
1305
|
+
// column. Reading every k-th row is no more expensive and does not care
|
|
1306
|
+
// how the rows are arranged.
|
|
1307
|
+
const rankLimit = n >> 1
|
|
1308
|
+
let distinct: Set<string> | null = null
|
|
1309
|
+
if (n > 0) {
|
|
1310
|
+
const sampleTarget = Math.min(n, 256)
|
|
1311
|
+
const stride = Math.max(1, Math.floor(n / sampleTarget))
|
|
1312
|
+
const sample = new Set<string>()
|
|
1313
|
+
let sampled = 0
|
|
1314
|
+
for (let i = 0; i < n; i += stride) {
|
|
1315
|
+
sample.add(String(rows[i]!.getCellValueByColumnId(columnId)))
|
|
1316
|
+
sampled++
|
|
1317
|
+
}
|
|
1318
|
+
// Only attempt ranking when the sample suggests real repetition.
|
|
1319
|
+
if (sample.size * 2 <= sampled) distinct = new Set()
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
for (let i = 0; i < n; i++) {
|
|
1323
|
+
const s = String(rows[i]!.getCellValueByColumnId(columnId))
|
|
1324
|
+
strings[i] = s
|
|
1325
|
+
if (distinct) {
|
|
1326
|
+
distinct.add(s)
|
|
1327
|
+
if (distinct.size > rankLimit) distinct = null
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// Collation is by far the most expensive comparison we do - a CPU
|
|
1332
|
+
// profile of a 100k text sort put 65% of the whole operation inside the
|
|
1333
|
+
// collator. But a column's DISTINCT values are usually far fewer than
|
|
1334
|
+
// its rows (statuses, regions, categories, owners), so rank the
|
|
1335
|
+
// distinct values once and sort by rank afterwards. That turns
|
|
1336
|
+
// O(n log n) collator calls into O(u log u), where u is the number of
|
|
1337
|
+
// distinct values, and the resulting order is identical because rank is
|
|
1338
|
+
// a monotone relabelling of the collated order - equal strings share a
|
|
1339
|
+
// rank, so ties still fall through to the stable sort exactly as before.
|
|
1340
|
+
//
|
|
1341
|
+
// Guarded on the uniqueness ratio: when nearly every value is distinct
|
|
1342
|
+
// the ranking pass cannot save any collator calls and would just add an
|
|
1343
|
+
// O(n) Map build, so that case keeps comparing directly.
|
|
1344
|
+
if (distinct) {
|
|
1345
|
+
const ordered = Array.from(distinct).sort(compareCollatedKeys)
|
|
1346
|
+
const rankOf = new Map<string, number>()
|
|
1347
|
+
for (let i = 0; i < ordered.length; i++) rankOf.set(ordered[i]!, i)
|
|
1348
|
+
for (let i = 0; i < n; i++) keys[i] = rankOf.get(strings[i]!)!
|
|
1349
|
+
compare = compareNumericKeys
|
|
1350
|
+
} else {
|
|
1351
|
+
keys = strings
|
|
1352
|
+
compare = compareCollatedKeys
|
|
1353
|
+
}
|
|
1354
|
+
} else {
|
|
1355
|
+
for (let i = 0; i < n; i++) keys[i] = rows[i]!.getCellValueByColumnId(columnId)
|
|
1356
|
+
compare = comparator
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
clauses.push({ keys, desc: clause.desc, compare })
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
if (!clauses.length) return rows
|
|
1363
|
+
|
|
1364
|
+
// Sort an index array, then materialise. `Array.prototype.sort` is stable,
|
|
1365
|
+
// so equal keys keep their original relative order exactly as the previous
|
|
1366
|
+
// `[...rows].sort(...)` did.
|
|
1367
|
+
const order = new Array<number>(rows.length)
|
|
1368
|
+
for (let i = 0; i < order.length; i++) order[i] = i
|
|
1369
|
+
|
|
1370
|
+
// Single-clause sorts get a specialised comparator.
|
|
1371
|
+
//
|
|
1372
|
+
// Most sorts are one column, and that path runs O(n log n) times - 1.66
|
|
1373
|
+
// million comparisons for 100k rows. The general loop pays a clause-array
|
|
1374
|
+
// index, three property loads and an indirect call on every one of them,
|
|
1375
|
+
// none of which vary once the clause list is fixed. Hoisting them into a
|
|
1376
|
+
// closure and, for the numeric comparator, inlining the subtraction removes
|
|
1377
|
+
// the call entirely.
|
|
1378
|
+
//
|
|
1379
|
+
// `keys[ib] - keys[ia]` for descending is exactly `-(keys[ia] - keys[ib])`
|
|
1380
|
+
// as far as sorting is concerned: both are NaN for unorderable values,
|
|
1381
|
+
// which the spec coerces to 0, and they differ only in producing 0 versus
|
|
1382
|
+
// -0 for equal keys, which sorts identically.
|
|
1383
|
+
if (clauses.length === 1) {
|
|
1384
|
+
const { keys, compare, desc } = clauses[0]!
|
|
1385
|
+
if (compare === compareNumericKeys) {
|
|
1386
|
+
order.sort(
|
|
1387
|
+
desc
|
|
1388
|
+
? function compareOneNumericDesc(ia, ib) { return keys[ib] - keys[ia] }
|
|
1389
|
+
: function compareOneNumericAsc(ia, ib) { return keys[ia] - keys[ib] },
|
|
1390
|
+
)
|
|
1391
|
+
} else {
|
|
1392
|
+
order.sort(
|
|
1393
|
+
desc
|
|
1394
|
+
? function compareOneDesc(ia, ib) { return -compare(keys[ia], keys[ib]) }
|
|
1395
|
+
: function compareOneAsc(ia, ib) { return compare(keys[ia], keys[ib]) },
|
|
1017
1396
|
)
|
|
1018
|
-
if (result !== 0) return clause.desc ? -result : result
|
|
1019
1397
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1398
|
+
} else {
|
|
1399
|
+
order.sort(function compareRowsByClauses(ia, ib) {
|
|
1400
|
+
for (let k = 0; k < clauses.length; k++) {
|
|
1401
|
+
const clause = clauses[k]!
|
|
1402
|
+
const result = clause.compare(clause.keys[ia], clause.keys[ib])
|
|
1403
|
+
if (result !== 0) return clause.desc ? -result : result
|
|
1404
|
+
}
|
|
1405
|
+
return 0
|
|
1406
|
+
})
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
const sorted = new Array<Row<TData>>(rows.length)
|
|
1410
|
+
for (let i = 0; i < order.length; i++) sorted[i] = rows[order[i]!]!
|
|
1022
1411
|
return sorted
|
|
1023
1412
|
}
|
|
1024
1413
|
}
|
|
1025
1414
|
|
|
1415
|
+
/**
|
|
1416
|
+
* Numeric key comparison for the built-in `number` and `date` comparators.
|
|
1417
|
+
* Subtraction rather than `<`/`>` on purpose: it reproduces the originals
|
|
1418
|
+
* exactly, NaN included. An unparseable date or a non-numeric value yields NaN,
|
|
1419
|
+
* and the sort spec turns a NaN comparison result into 0 (SortCompare coerces
|
|
1420
|
+
* it), which is the behaviour callers already depend on.
|
|
1421
|
+
*/
|
|
1422
|
+
function compareNumericKeys(a: number, b: number): number {
|
|
1423
|
+
return a - b
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* Text key comparison for the built-in `auto` comparator.
|
|
1428
|
+
*
|
|
1429
|
+
* `localeCompare`, NOT a hoisted `Intl.Collator`. The specification defines
|
|
1430
|
+
* `localeCompare` with no locale or options as constructing a default collator
|
|
1431
|
+
* per call, so hoisting one looks like the obvious optimisation - and it is
|
|
1432
|
+
* measurably slower. V8 fast-paths `String.prototype.localeCompare` for the
|
|
1433
|
+
* default locale; going through a collator object misses that path. Measured
|
|
1434
|
+
* sorting 100k strings: 33 ms via `localeCompare` against 83 ms via a cached
|
|
1435
|
+
* collator on ASCII, 58 ms against 99 ms with accents mixed in, and the two
|
|
1436
|
+
* produce byte-identical orderings across all 100k positions.
|
|
1437
|
+
*
|
|
1438
|
+
* Left as its own function so the sort path has one place to change if that
|
|
1439
|
+
* ever stops being true. Re-measure before "optimising" this again.
|
|
1440
|
+
*/
|
|
1441
|
+
function compareCollatedKeys(a: string, b: string): number {
|
|
1442
|
+
return a.localeCompare(b)
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1026
1445
|
/**
|
|
1027
1446
|
* Everything {@link createSvGridCore} accepts: the data and columns, the
|
|
1028
1447
|
* features and row models that make up the pipeline, and an `on*Change`
|
|
@@ -1128,7 +1547,6 @@ export function createSvGridCore<TFeatures extends TableFeatures, TData extends
|
|
|
1128
1547
|
pagination: PaginationState | undefined
|
|
1129
1548
|
grouping: GroupingState | undefined
|
|
1130
1549
|
expanded: ExpandedState | undefined
|
|
1131
|
-
rowSelection: RowSelectionState | undefined
|
|
1132
1550
|
} | null = null
|
|
1133
1551
|
|
|
1134
1552
|
const grid = {
|
|
@@ -1337,91 +1755,71 @@ export function createSvGridCore<TFeatures extends TableFeatures, TData extends
|
|
|
1337
1755
|
for (let i = 0; i < columns.length; i++) columnIndexById.set(columns[i]!.id, i)
|
|
1338
1756
|
const columnCount = columns.length
|
|
1339
1757
|
|
|
1758
|
+
// One shared context for every row in this table, so a row carries a
|
|
1759
|
+
// pointer rather than a closure scope. See BASE_ROW_METHODS.
|
|
1760
|
+
const rowCtx: BaseRowCtx<TData> = {
|
|
1761
|
+
grid: grid as SvGrid<TData>,
|
|
1762
|
+
store,
|
|
1763
|
+
columns,
|
|
1764
|
+
columnCount,
|
|
1765
|
+
columnIndexById,
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1340
1768
|
cachedBaseRows = new Array(options.data.length)
|
|
1341
1769
|
const getRowId = options.getRowId
|
|
1770
|
+
const m = BASE_ROW_METHODS as unknown as {
|
|
1771
|
+
getCanExpand: Row<TData>['getCanExpand']
|
|
1772
|
+
getIsExpanded: Row<TData>['getIsExpanded']
|
|
1773
|
+
toggleExpanded: Row<TData>['toggleExpanded']
|
|
1774
|
+
getIsSelected: Row<TData>['getIsSelected']
|
|
1775
|
+
toggleSelected: Row<TData>['toggleSelected']
|
|
1776
|
+
getAllCells: Row<TData>['getAllCells']
|
|
1777
|
+
getCellValueByColumnId: Row<TData>['getCellValueByColumnId']
|
|
1778
|
+
}
|
|
1342
1779
|
for (let index = 0; index < options.data.length; index++) {
|
|
1343
1780
|
const original = options.data[index]!
|
|
1344
|
-
|
|
1345
|
-
//
|
|
1346
|
-
//
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
let cachedCells: Array<Cell<TData>> | null = null
|
|
1350
|
-
|
|
1351
|
-
function computeValues(): Array<unknown> {
|
|
1352
|
-
const values = new Array<unknown>(columnCount)
|
|
1353
|
-
for (let i = 0; i < columnCount; i++) {
|
|
1354
|
-
const column = columns[i]!
|
|
1355
|
-
if (column.columnDef.fieldFn) {
|
|
1356
|
-
values[i] = column.columnDef.fieldFn(original)
|
|
1357
|
-
} else if (column.columnDef.field) {
|
|
1358
|
-
values[i] = (original as any)[column.columnDef.field]
|
|
1359
|
-
} else {
|
|
1360
|
-
values[i] = undefined
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
return values
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
const row: Row<TData> = {
|
|
1367
|
-
id,
|
|
1781
|
+
// `_values` and `_cells` stay null until something reads them - a
|
|
1782
|
+
// 100k-row grid showing twenty rows must not materialise every row's
|
|
1783
|
+
// values or cell objects to paint.
|
|
1784
|
+
const row: BaseRowState<TData> = {
|
|
1785
|
+
id: getRowId ? getRowId(original, index) : String(index),
|
|
1368
1786
|
index,
|
|
1369
1787
|
original,
|
|
1370
1788
|
depth: 0,
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
if (cachedCells) return cachedCells
|
|
1382
|
-
const built = new Array<Cell<TData>>(columnCount)
|
|
1383
|
-
for (let i = 0; i < columnCount; i++) {
|
|
1384
|
-
const column = columns[i]!
|
|
1385
|
-
const colIndex = i
|
|
1386
|
-
const cell: Cell<TData> = {
|
|
1387
|
-
id: `${id}_${column.id}`,
|
|
1388
|
-
row,
|
|
1389
|
-
column,
|
|
1390
|
-
getValue: () => {
|
|
1391
|
-
if (!cachedValues) cachedValues = computeValues()
|
|
1392
|
-
return cachedValues[colIndex]
|
|
1393
|
-
},
|
|
1394
|
-
getContext: () => ({
|
|
1395
|
-
cell,
|
|
1396
|
-
row,
|
|
1397
|
-
column,
|
|
1398
|
-
table: grid,
|
|
1399
|
-
getValue: () => cell.getValue(),
|
|
1400
|
-
}),
|
|
1401
|
-
}
|
|
1402
|
-
built[i] = cell
|
|
1403
|
-
}
|
|
1404
|
-
cachedCells = built
|
|
1405
|
-
return built
|
|
1406
|
-
},
|
|
1407
|
-
getCellValueByColumnId: (columnId: string) => {
|
|
1408
|
-
const idx = columnIndexById.get(columnId)
|
|
1409
|
-
if (idx === undefined) return undefined
|
|
1410
|
-
if (!cachedValues) cachedValues = computeValues()
|
|
1411
|
-
return cachedValues[idx]
|
|
1412
|
-
},
|
|
1789
|
+
[ROW_CTX]: rowCtx,
|
|
1790
|
+
[ROW_VALUES]: null,
|
|
1791
|
+
[ROW_CELLS]: null,
|
|
1792
|
+
getCanExpand: m.getCanExpand,
|
|
1793
|
+
getIsExpanded: m.getIsExpanded,
|
|
1794
|
+
toggleExpanded: m.toggleExpanded,
|
|
1795
|
+
getIsSelected: m.getIsSelected,
|
|
1796
|
+
toggleSelected: m.toggleSelected,
|
|
1797
|
+
getAllCells: m.getAllCells,
|
|
1798
|
+
getCellValueByColumnId: m.getCellValueByColumnId,
|
|
1413
1799
|
}
|
|
1414
1800
|
cachedBaseRows[index] = row
|
|
1415
1801
|
}
|
|
1416
1802
|
}
|
|
1417
1803
|
|
|
1804
|
+
// Only the slices a pipeline stage actually READS belong in this key.
|
|
1805
|
+
//
|
|
1806
|
+
// `rowSelection` used to be here, which meant ticking one checkbox on a
|
|
1807
|
+
// 100k-row grid re-filtered and re-sorted the entire dataset to rebuild a
|
|
1808
|
+
// row array that was identical by construction. Nothing reads it: the two
|
|
1809
|
+
// consumers are `getIsSelected` closures (on data rows and on group rows)
|
|
1810
|
+
// that read `store.state` when called, so they observe a selection change
|
|
1811
|
+
// without the model being rebuilt.
|
|
1812
|
+
//
|
|
1813
|
+
// `_rowModels` is a closed set of six named slots, so no consumer stage
|
|
1814
|
+
// can be inserted that might read selection. A caller CAN supply a custom
|
|
1815
|
+
// function for one of those slots; if one ever needs a slice that is not
|
|
1816
|
+
// listed here, add it here rather than reinstating all of them.
|
|
1418
1817
|
const currentSlices = {
|
|
1419
1818
|
sorting: store.state.sorting,
|
|
1420
1819
|
columnFilters: store.state.columnFilters,
|
|
1421
1820
|
pagination: store.state.pagination,
|
|
1422
1821
|
grouping: store.state.grouping,
|
|
1423
1822
|
expanded: store.state.expanded,
|
|
1424
|
-
rowSelection: store.state.rowSelection,
|
|
1425
1823
|
}
|
|
1426
1824
|
if (
|
|
1427
1825
|
cachedRowModel &&
|
|
@@ -1431,8 +1829,7 @@ export function createSvGridCore<TFeatures extends TableFeatures, TData extends
|
|
|
1431
1829
|
cachedSlices?.columnFilters === currentSlices.columnFilters &&
|
|
1432
1830
|
cachedSlices?.pagination === currentSlices.pagination &&
|
|
1433
1831
|
cachedSlices?.grouping === currentSlices.grouping &&
|
|
1434
|
-
cachedSlices?.expanded === currentSlices.expanded
|
|
1435
|
-
cachedSlices?.rowSelection === currentSlices.rowSelection
|
|
1832
|
+
cachedSlices?.expanded === currentSlices.expanded
|
|
1436
1833
|
) {
|
|
1437
1834
|
return cachedRowModel
|
|
1438
1835
|
}
|