@omnigrid/core 0.1.35 → 0.1.36

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/README.md CHANGED
@@ -30,7 +30,9 @@ adapter-native scroll adjustment without moving scroll coordinates into the Stor
30
30
  ```ts
31
31
  interface GridOptions<T> {
32
32
  data?: T[] | null; // row data; null/omitted means initial loading
33
- columns: ColumnDef<T>[]; // required
33
+ columns?: ColumnDef<T>[]; // inferred from the first row when omitted
34
+ autoColumnHeaderMode?: "as-is" | "title-case"; // default: "as-is"
35
+ getColumnHeader?: (field: string) => string; // custom formatter; takes precedence over autoColumnHeaderMode
34
36
  getRowId?: (row: T, index: number) => RowId; // default: index-based
35
37
  rowHeight?: number; // fixed px row height; default 32 (offset math is O(1))
36
38
  rowOverscan?: number; // extra rows above/below viewport; default 5
@@ -48,6 +50,14 @@ interface GridOptions<T> {
48
50
  }
49
51
  ```
50
52
 
53
+ When `columns` is omitted, the core infers leaf columns from the first row.
54
+ If the initial dataset is empty or pending, it waits for the first non-empty
55
+ `setData` or `appendData`. The inferred schema stays fixed afterward; call
56
+ `setColumns` when the schema changes. An explicit `columns: []` disables
57
+ inference. Headers default to the field key; set `autoColumnHeaderMode` to
58
+ `"title-case"` to split camel-case keys into capitalized words, or provide
59
+ `getColumnHeader` for a custom formatter.
60
+
51
61
  Row callbacks receive `params.rowType` (`"leaf"` or `"group"`). Group rows are synthetic and have no `data`; checking `rowType` narrows the callback parameters.
52
62
 
53
63
  ### `ColumnDef<T>`
@@ -254,7 +264,7 @@ In a UI framework, use the adapter — e.g. `@omnigrid/react`:
254
264
  data={rows}
255
265
  getRowId={(row) => row.id}
256
266
  plugins={plugins}
257
- style={{ height: 480 }}
267
+ height={480}
258
268
  />
259
269
  ```
260
270
 
package/dist/columns.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CellSpanningParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, DefaultColumnDef, RowId, RowSpanLayout } from "./types.js";
1
+ import type { AutoColumnHeaderMode, CellSpanningParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, DefaultColumnDef, RowId, RowSpanLayout } from "./types.js";
2
2
  /**
3
3
  * Runtime narrowing: does the column definition describe a GROUP
4
4
  * (it carries `children`) rather than a leaf column?
@@ -6,6 +6,16 @@ import type { CellSpanningParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport
6
6
  export declare function isColumnGroup<T>(column: ColumnDef<T>): column is ColumnGroupDef<T>;
7
7
  /** Inverse of `isColumnGroup` — handy for filters over the flat working set. */
8
8
  export declare function isColumnLeaf<T>(column: ColumnDef<T>): column is ColumnLeafDef<T>;
9
+ /**
10
+ * Infers flat leaf columns from the own enumerable string keys of the first data row.
11
+ *
12
+ * @param data - Rows used to infer the columns.
13
+ * @param getColumnHeader - Optional formatter for each inferred field key.
14
+ * @param autoColumnHeaderMode - Built-in formatting mode used when no custom formatter is provided.
15
+ * @returns Inferred columns, or an empty array when no object row is available.
16
+ * @api
17
+ */
18
+ export declare function inferColumns<T>(data: T[] | null | undefined, getColumnHeader?: (field: string) => string, autoColumnHeaderMode?: AutoColumnHeaderMode): ColumnLeafDef<T>[];
9
19
  /**
10
20
  * Applies shared defaults to leaf columns while preserving column-level overrides.
11
21
  *
package/dist/columns.js CHANGED
@@ -9,6 +9,35 @@ export function isColumnGroup(column) {
9
9
  export function isColumnLeaf(column) {
10
10
  return !isColumnGroup(column);
11
11
  }
12
+ /**
13
+ * Infers flat leaf columns from the own enumerable string keys of the first data row.
14
+ *
15
+ * @param data - Rows used to infer the columns.
16
+ * @param getColumnHeader - Optional formatter for each inferred field key.
17
+ * @param autoColumnHeaderMode - Built-in formatting mode used when no custom formatter is provided.
18
+ * @returns Inferred columns, or an empty array when no object row is available.
19
+ * @api
20
+ */
21
+ export function inferColumns(data, getColumnHeader, autoColumnHeaderMode = "as-is") {
22
+ const firstRow = data?.[0];
23
+ if (firstRow === null || typeof firstRow !== "object")
24
+ return [];
25
+ return Object.keys(firstRow).map((field) => {
26
+ const header = getColumnHeader
27
+ ? getColumnHeader(field)
28
+ : autoColumnHeaderMode === "title-case"
29
+ ? formatAutoColumnHeader(field)
30
+ : field;
31
+ return { id: field, field, header };
32
+ });
33
+ }
34
+ function formatAutoColumnHeader(field) {
35
+ const words = field
36
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
37
+ .replace(/([A-Z])([A-Z][a-z])/g, "$1 $2")
38
+ .split(" ");
39
+ return words.map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1).toLowerCase()}`).join(" ");
40
+ }
12
41
  /**
13
42
  * Applies shared defaults to leaf columns while preserving column-level overrides.
14
43
  *
package/dist/grid.d.ts CHANGED
@@ -49,6 +49,9 @@ export declare class Grid<T> implements GridApi<T> {
49
49
  private loadError;
50
50
  private overlayState;
51
51
  private overlayManual;
52
+ private autoColumnsPending;
53
+ private readonly autoColumnHeaderMode;
54
+ private readonly getColumnHeader;
52
55
  /** Grid-level `wrapText` flag — applied to every column without an explicit one. */
53
56
  private wrapTextAll;
54
57
  /** Normalized cell flash configuration. `false` when disabled. */
@@ -292,6 +295,7 @@ export declare class Grid<T> implements GridApi<T> {
292
295
  * @api
293
296
  */
294
297
  getRowHeight(rowIndex: number): number;
298
+ private resolvePendingAutoColumns;
295
299
  setData(data: T[] | null | undefined): void;
296
300
  /**
297
301
  * Chunk-append API for incremental data loading (infinite scroll, live
package/dist/grid.js CHANGED
@@ -1,4 +1,4 @@
1
- import { applyDefaultColumnDef, applyWrapText, flattenColumns, getCellValue, getColumnGroups, getRowSpans, resolveColSpan, } from "./columns.js";
1
+ import { applyDefaultColumnDef, applyWrapText, flattenColumns, getCellValue, getColumnGroups, getRowSpans, inferColumns, isColumnGroup, resolveColSpan, } from "./columns.js";
2
2
  import { EventBus } from "./events.js";
3
3
  import { applyFilterModel, applyQuickFilter, isActiveCondition } from "./filtering.js";
4
4
  import { buildGroupTree, computeAggregations, flattenTree } from "./groupingPipeline.js";
@@ -37,6 +37,7 @@ export class Grid {
37
37
  this.hasData = true;
38
38
  this.overlayState = { type: null };
39
39
  this.overlayManual = false;
40
+ this.autoColumnsPending = false;
40
41
  /** Normalized cell flash configuration. `false` when disabled. */
41
42
  this.cellFlash = false;
42
43
  /** Cells currently flashed: key `${rowId}:${columnId}` → expiry timestamp. */
@@ -50,9 +51,13 @@ export class Grid {
50
51
  * (width / height) still go through the Store and DO trigger re-renders.
51
52
  */
52
53
  this.scrollPosition = { scrollTop: 0, scrollLeft: 0 };
54
+ this.autoColumnsPending = options.columns === undefined && (options.data?.length ?? 0) === 0;
55
+ this.autoColumnHeaderMode = options.autoColumnHeaderMode;
56
+ this.getColumnHeader = options.getColumnHeader;
53
57
  const state = {
54
58
  data: options.data ?? [],
55
- columns: applyWrapText(applyDefaultColumnDef(options.columns, options.defaultColumnDef), options.wrapText),
59
+ columns: applyWrapText(applyDefaultColumnDef(options.columns ??
60
+ inferColumns(options.data, options.getColumnHeader, options.autoColumnHeaderMode), options.defaultColumnDef), options.wrapText),
56
61
  viewport: { width: 0, height: 0, scrollTop: 0, scrollLeft: 0 },
57
62
  rowHeight: options.rowHeight ?? 32,
58
63
  groupBy: [],
@@ -901,18 +906,54 @@ export class Grid {
901
906
  getRowHeight(rowIndex) {
902
907
  return this.virtualizer.getRowHeight(rowIndex);
903
908
  }
909
+ resolvePendingAutoColumns(data) {
910
+ if (!this.autoColumnsPending || data.length === 0)
911
+ return undefined;
912
+ const inferredColumns = inferColumns(data, this.getColumnHeader, this.autoColumnHeaderMode);
913
+ const inferredById = new Map(inferredColumns.map((column) => [column.id, column]));
914
+ const currentColumns = this.store.getState().columns;
915
+ const currentIds = new Set();
916
+ const mergedColumns = [];
917
+ currentColumns.forEach((currentColumn) => {
918
+ const inferredColumn = inferredById.get(currentColumn.id);
919
+ if (inferredColumn && !isColumnGroup(currentColumn)) {
920
+ mergedColumns.push({
921
+ ...inferredColumn,
922
+ ...currentColumn,
923
+ id: inferredColumn.id,
924
+ field: inferredColumn.field,
925
+ header: inferredColumn.header,
926
+ });
927
+ }
928
+ else {
929
+ mergedColumns.push(currentColumn);
930
+ }
931
+ currentIds.add(currentColumn.id);
932
+ });
933
+ inferredColumns.forEach((column) => {
934
+ if (!currentIds.has(column.id))
935
+ mergedColumns.push(column);
936
+ });
937
+ this.autoColumnsPending = false;
938
+ return mergedColumns;
939
+ }
904
940
  setData(data) {
905
941
  this.assertActive();
906
942
  this.loadError = undefined;
907
943
  this.hasData = data !== undefined && data !== null;
908
944
  const nextData = data ?? [];
945
+ const inferredColumns = this.resolvePendingAutoColumns(nextData);
909
946
  this.filteredDataCache = null;
910
947
  this.processedDataCache = null;
911
948
  this.groupedRowsCache = null;
912
949
  this.groupedRowsRevision = -1;
913
950
  this.virtualizer.resetAllRowHeights();
914
951
  this.revision += 1;
915
- this.store.setState({ data: nextData });
952
+ const nextState = { data: nextData };
953
+ if (inferredColumns !== undefined) {
954
+ nextState.columns = applyWrapText(applyDefaultColumnDef(inferredColumns, this.defaultColumnDef), this.wrapTextAll);
955
+ }
956
+ this.store.setState(nextState);
916
957
  this.events.emit("dataChange", nextData);
917
958
  this.recomputeOverlay();
918
959
  }
@@ -929,13 +970,18 @@ export class Grid {
929
970
  return;
930
971
  const currentData = this.store.getState().data;
931
972
  const nextData = [...currentData, ...rows];
973
+ const inferredColumns = this.resolvePendingAutoColumns(nextData);
932
974
  this.hasData = true;
933
975
  this.filteredDataCache = null;
934
976
  this.processedDataCache = null;
935
977
  this.groupedRowsCache = null;
936
978
  this.groupedRowsRevision = -1;
937
979
  this.revision += 1;
938
- this.store.setState({ data: nextData });
980
+ const nextState = { data: nextData };
981
+ if (inferredColumns !== undefined) {
982
+ nextState.columns = applyWrapText(applyDefaultColumnDef(inferredColumns, this.defaultColumnDef), this.wrapTextAll);
983
+ }
984
+ this.store.setState(nextState);
939
985
  this.events.emit("dataChange", nextData);
940
986
  this.recomputeOverlay();
941
987
  }
package/dist/index.d.ts CHANGED
@@ -14,6 +14,6 @@ export { applyFilterModel, applyQuickFilter, isActiveCondition, isFilterModelAct
14
14
  export type { ColumnFilterModel, FilterCondition, FilterModel, FilterOperator } from "./filtering.js";
15
15
  export type { UIButtonControl, UICheckboxDescriptor, UIControlBase, UIControlDescriptor, UIGroupControl, UIInputControl, UIPopupAnchor, UIPopupDescriptor, UISelectControl, UISelectOption, UITextBlockControl, UIMultiSelectControl, UIMultiSelectOption, } from "./ui.js";
16
16
  export type { OverlayOptions, OverlayScope, OverlayState, OverlayType } from "./overlays.js";
17
- export type { BlockConfig, CellAlign, AggregationFunc, ColumnHeaderExtension, ColumnHeaderExtensionPosition, CellRenderParams, CellClassRules, CellSpan, CellSpanningParams, CheckboxControl, CheckboxRenderParams, ColSpanDef, ColumnClassRules, ColumnDataType, ColumnDropPosition, DefaultColumnDef, ColumnFilterType, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, ColumnStyle, ColumnStyleParams, DataProcessor, DataRowNode, DomPoolBindings, DomPoolUpdate, DomPoolUpdateOptions, GroupRowNode, GroupRowRenderParams, GroupRowRenderer, CoreGridApi, CoreGridState, GridApi, GridEvents, GridOptions, GridPlugin, GridState, PluginApiExtensions, PluginStateExtensions, ResolvedAggregationFunc, HeaderClassRules, HeaderGroupClassRules, HeaderGroupRenderParams, HeaderRenderParams, HeaderStyle, OverlayComponent, OverlayComponentProps, PooledRow, Range, RowClassRules, RowClickEvent, RowHeightUpdateOptions, RowHost, RowHoverEvent, RowId, RowHierarchy, RowHierarchyScope, RowGroupContext, RowGroupContextChild, RowNode, RowRenderParams, RowSpanLayout, RowStyle, RowTransaction, ScrollPosition, SlotComponentContent, SlotContent, SlotHtmlContent, SlotMount, SlotMountOptions, SlotName, SlotNodeContent, SlotNodeEvent, SlotNodeEventName, SlotPosition, SlotProvider, SlotRenderContext, SortDirection, TransactionResult, TreeRowNode, ViewportData, ViewportState, VirtualItem, } from "./types.js";
18
- export { applyDefaultColumnDef, getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf, getCellValue, getCellSpanningParams, resolveColSpan, getRowSpans, } from "./columns.js";
17
+ export type { BlockConfig, CellAlign, AggregationFunc, AutoColumnHeaderMode, ColumnHeaderExtension, ColumnHeaderExtensionPosition, CellRenderParams, CellClassRules, CellSpan, CellSpanningParams, CheckboxControl, CheckboxRenderParams, ColSpanDef, ColumnClassRules, ColumnDataType, ColumnDropPosition, DefaultColumnDef, ColumnFilterType, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, ColumnStyle, ColumnStyleParams, DataProcessor, DataRowNode, DomPoolBindings, DomPoolUpdate, DomPoolUpdateOptions, GroupRowNode, GroupRowRenderParams, GroupRowRenderer, CoreGridApi, CoreGridState, GridApi, GridEvents, GridOptions, GridPlugin, GridState, PluginApiExtensions, PluginStateExtensions, ResolvedAggregationFunc, HeaderClassRules, HeaderGroupClassRules, HeaderGroupRenderParams, HeaderRenderParams, HeaderStyle, OverlayComponent, OverlayComponentProps, PooledRow, Range, RowClassRules, RowClickEvent, RowHeightUpdateOptions, RowHost, RowHoverEvent, RowId, RowHierarchy, RowHierarchyScope, RowGroupContext, RowGroupContextChild, RowNode, RowRenderParams, RowSpanLayout, RowStyle, RowTransaction, ScrollPosition, SlotComponentContent, SlotContent, SlotHtmlContent, SlotMount, SlotMountOptions, SlotName, SlotNodeContent, SlotNodeEvent, SlotNodeEventName, SlotPosition, SlotProvider, SlotRenderContext, SortDirection, TransactionResult, TreeRowNode, ViewportData, ViewportState, VirtualItem, } from "./types.js";
18
+ export { applyDefaultColumnDef, getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf, getCellValue, getCellSpanningParams, resolveColSpan, getRowSpans, inferColumns, } from "./columns.js";
19
19
  export { Virtualizer, RowHeightRegistry } from "./virtualizer.js";
package/dist/index.js CHANGED
@@ -8,5 +8,5 @@ export { SlotManager } from "./slots.js";
8
8
  export { Store } from "./store.js";
9
9
  export { DataPipeline, PIPELINE_STAGE_ORDER } from "./pipeline.js";
10
10
  export { applyFilterModel, applyQuickFilter, isActiveCondition, isFilterModelActive, matchesColumnFilter, } from "./filtering.js";
11
- export { applyDefaultColumnDef, getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf, getCellValue, getCellSpanningParams, resolveColSpan, getRowSpans, } from "./columns.js";
11
+ export { applyDefaultColumnDef, getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf, getCellValue, getCellSpanningParams, resolveColSpan, getRowSpans, inferColumns, } from "./columns.js";
12
12
  export { Virtualizer, RowHeightRegistry } from "./virtualizer.js";
package/dist/types.d.ts CHANGED
@@ -613,10 +613,36 @@ export interface RowHeightUpdateOptions {
613
613
  */
614
614
  notify?: boolean;
615
615
  }
616
+ /**
617
+ * Built-in formatting modes for headers inferred from row fields.
618
+ * @api
619
+ */
620
+ export type AutoColumnHeaderMode = "as-is" | "title-case";
616
621
  export interface GridOptions<T> {
617
622
  /** Row data; omitted or null means an initial load is pending. */
618
623
  data?: T[] | null;
619
- columns: ColumnDef<T>[];
624
+ /**
625
+ * Column definitions. When omitted, the core infers columns from the first
626
+ * row and waits for data if the initial dataset is empty. The inferred
627
+ * schema stays fixed; call `setColumns` to change it afterward. Pass `[]`
628
+ * to explicitly disable inference.
629
+ * @api
630
+ */
631
+ columns?: ColumnDef<T>[];
632
+ /**
633
+ * Formatting mode for headers of columns inferred by the core. Defaults
634
+ * to `as-is`; `title-case` splits camel-case field names into words and
635
+ * capitalizes each word. Ignored when `columns` is provided.
636
+ * @default as-is
637
+ * @api
638
+ */
639
+ autoColumnHeaderMode?: AutoColumnHeaderMode;
640
+ /**
641
+ * Custom formatter for headers of inferred columns. Takes precedence over
642
+ * `autoColumnHeaderMode` when provided. Ignored when `columns` is provided.
643
+ * @api
644
+ */
645
+ getColumnHeader?: (field: string) => string;
620
646
  /** Properties applied to every leaf column unless overridden. @api */
621
647
  defaultColumnDef?: DefaultColumnDef<T>;
622
648
  /** Returns a stable row ID; defaults to the row's current index. Use a stable ID to preserve row identity when rows are reordered. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/core",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "description": "Framework-agnostic core engine for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",