@omnigrid/selection-plugin 0.1.2 → 0.1.4

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
@@ -5,21 +5,44 @@ Headless row/column selection plugin for OmniGrid.
5
5
  ## What it provides
6
6
 
7
7
  - Row selection (single, multi, range, checkbox toggle)
8
+ - Disabled rows marked by `isRowSelectable` with the `omnigrid-row-unselectable` class
8
9
  - Cell selection with range drag
9
10
  - Keyboard navigation support (Shift+Click, Ctrl+Click)
10
11
  - API to read/modify selection state programmatically
11
12
 
13
+ ## Styling
14
+
15
+ Selected rows receive the `omnigrid-row-selected` CSS class. The plugin does
16
+ **not** set inline colors — the concrete selected-row color is owned by the
17
+ active theme (for example, `@omnigrid/default-theme` and `@omnigrid/mint-theme`
18
+ define `--omnigrid-cell-selected-background` and `--omnigrid-checkbox-accent`
19
+ for light and dark modes). Rows rejected by `isRowSelectable` receive
20
+ `omnigrid-row-unselectable` and use theme-provided muted background and text
21
+ colors via `--omnigrid-cell-unselectable-background` and
22
+ `--omnigrid-cell-unselectable-color`.
23
+
12
24
  ## Usage
13
25
 
14
26
  ```ts
15
- import { selectionPlugin } from "@omnigrid/selection-plugin";
16
-
17
- const grid = new Grid({
18
- plugins: [selectionPlugin()],
19
- // ...
27
+ import { SelectionPlugin } from "@omnigrid/selection-plugin";
28
+
29
+ const plugin = new SelectionPlugin({
30
+ mode: "multiple",
31
+ onSelectionChange: ({ selectedRowIds, selectedRows }) => {
32
+ console.log("Selected count:", selectedRows.length);
33
+ console.log("Selected IDs:", selectedRowIds);
34
+ console.log("Selected records:", selectedRows);
35
+ },
20
36
  });
37
+
38
+ // Read the current selection at any time:
39
+ const selection = plugin.getSelectionState();
21
40
  ```
22
41
 
42
+ `onSelectionChange` runs when selection changes and receives the current row
43
+ IDs and row records. `getSelectedRowIds()`, `getSelectedRows()`, and
44
+ `getSelectionState()` provide synchronous access to the current selection.
45
+
23
46
  ## Links
24
47
 
25
48
  - **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/plugins/base/selection
package/dist/index.d.ts CHANGED
@@ -1,27 +1,58 @@
1
- import type { CheckboxRenderParams, GridApi, GridPlugin, RowId, RowRenderParams, RowStyle } from "@omnigrid/core";
1
+ import type { CheckboxRenderParams, GridApi, GridPlugin, RowId, RowRenderParams } from "@omnigrid/core";
2
+ /** Selection behavior supported by the plugin. */
2
3
  export type SelectionMode = "single" | "multiple";
4
+ /**
5
+ * CSS class applied to selected rows. The concrete selected-row color is
6
+ * owned by the active theme (e.g. `@omnigrid/default-theme` defines
7
+ * `--omnigrid-cell-selected-background`); the plugin only marks the row.
8
+ */
9
+ export declare const SELECTED_ROW_CLASS = "omnigrid-row-selected";
10
+ /** Applied to rows rejected by `isRowSelectable`; themes supply muted colors. */
11
+ export declare const UNSELECTABLE_ROW_CLASS = "omnigrid-row-unselectable";
12
+ /** Values provided to a custom checkbox renderer. */
3
13
  export interface SelectionRendererParams<T> extends CheckboxRenderParams {
14
+ /** Row data for a row checkbox; absent for the header checkbox. */
4
15
  data?: T;
16
+ /** Row identifier for a row checkbox. */
5
17
  rowId?: RowId;
18
+ /** Row index for a row checkbox. */
6
19
  index?: number;
20
+ /** Whether the renderer is being called for the header checkbox. */
7
21
  header: boolean;
8
22
  }
23
+ /** Renderer used to customize selection checkboxes. */
9
24
  export type SelectionRenderer<T> = (params: SelectionRendererParams<T>) => unknown;
25
+ /** Configuration for the selection plugin. */
10
26
  export interface SelectionPluginOptions<T> {
27
+ /** Supports selecting one row or multiple rows. @default "single" */
11
28
  mode?: SelectionMode;
29
+ /** Restricts selection changes to checkbox interaction. @default false */
12
30
  checkboxOnly?: boolean;
31
+ /** In multiple mode, clicking a row replaces the current selection. @default false */
13
32
  replaceSelectionOnClick?: boolean;
33
+ /** Adds a checkbox to each row. @default false */
14
34
  showRowCheckboxes?: boolean;
35
+ /** Adds a select-all checkbox to the selection column header. @default false */
15
36
  showHeaderCheckbox?: boolean;
37
+ /** Determines whether a row can be selected. */
16
38
  isRowSelectable?: (row: T, index: number) => boolean;
39
+ /** Renders checkbox UI; defaults to the OmniGrid checkbox renderer. */
17
40
  checkboxRenderer?: SelectionRenderer<T>;
41
+ /** Called when the selected rows change. */
42
+ onSelectionChange?: (state: SelectionState<T>) => void;
43
+ /** ID for the automatically inserted checkbox column. @default "__omnigrid_selection__" */
18
44
  selectionColumnId?: string;
45
+ /** Width in pixels of the automatically inserted checkbox column. @default 44 */
19
46
  selectionColumnWidth?: number;
20
47
  }
48
+ /** Selected row identifiers and their corresponding row data. */
21
49
  export interface SelectionState<T> {
50
+ /** IDs of selected rows. */
22
51
  selectedRowIds: RowId[];
52
+ /** Data objects for selected rows. */
23
53
  selectedRows: T[];
24
54
  }
55
+ /** Adds row selection and optional checkbox controls to a grid. */
25
56
  export declare class SelectionPlugin<T> implements GridPlugin<T> {
26
57
  readonly name = "@omnigrid/selection-plugin";
27
58
  private api?;
@@ -32,6 +63,7 @@ export declare class SelectionPlugin<T> implements GridPlugin<T> {
32
63
  private showHeaderCheckbox;
33
64
  private isRowSelectable;
34
65
  private checkboxRenderer;
66
+ private onSelectionChange?;
35
67
  private readonly selectionColumnId;
36
68
  private readonly selectionColumnWidth;
37
69
  private readonly selectedRowIds;
@@ -39,17 +71,46 @@ export declare class SelectionPlugin<T> implements GridPlugin<T> {
39
71
  private unregisterRowClick?;
40
72
  constructor(options?: SelectionPluginOptions<T>);
41
73
  register(api: GridApi<T>): () => void;
74
+ /**
75
+ * Returns the IDs of all currently selected rows.
76
+ * @api
77
+ */
42
78
  getSelectedRowIds(): RowId[];
79
+ /**
80
+ * Returns the data objects of all currently selected rows.
81
+ * @api
82
+ */
43
83
  getSelectedRows(): T[];
84
+ /**
85
+ * Returns selected row IDs and row data together.
86
+ * @api
87
+ */
44
88
  getSelectionState(): SelectionState<T>;
89
+ /**
90
+ * Checks whether a row ID is currently selected.
91
+ * @api
92
+ */
45
93
  isSelected(rowId: RowId): boolean;
94
+ /**
95
+ * Replaces the current selection with the supplied row IDs.
96
+ * @api
97
+ */
46
98
  setSelectedRowIds(rowIds: RowId[]): void;
99
+ /**
100
+ * Clears all selected rows.
101
+ * @api
102
+ */
47
103
  clearSelection(): void;
104
+ /**
105
+ * Toggles a row's selection. Shift and Ctrl modifiers control range and additive selection.
106
+ * @api
107
+ */
48
108
  toggleRow(rowId: RowId, index: number, modifiers?: {
49
109
  shiftKey?: boolean;
50
110
  ctrlKey?: boolean;
51
111
  }): void;
52
- getRowStyle({ id }: RowRenderParams<T>): RowStyle | undefined;
112
+ /** Returns selection-related CSS classes for a row. */
113
+ getRowClass({ id, index, data }: RowRenderParams<T>): string | undefined;
53
114
  private handleRowClick;
54
115
  private applySelection;
55
116
  private updateColumns;
@@ -60,5 +121,9 @@ export declare class SelectionPlugin<T> implements GridPlugin<T> {
60
121
  private toggleAll;
61
122
  private refresh;
62
123
  private validateSelection;
124
+ /**
125
+ * Updates plugin options at runtime.
126
+ * @api
127
+ */
63
128
  updateOptions(newOptions: Partial<SelectionPluginOptions<T>>): void;
64
129
  }
package/dist/index.js CHANGED
@@ -1,7 +1,16 @@
1
+ /**
2
+ * CSS class applied to selected rows. The concrete selected-row color is
3
+ * owned by the active theme (e.g. `@omnigrid/default-theme` defines
4
+ * `--omnigrid-cell-selected-background`); the plugin only marks the row.
5
+ */
6
+ export const SELECTED_ROW_CLASS = "omnigrid-row-selected";
7
+ /** Applied to rows rejected by `isRowSelectable`; themes supply muted colors. */
8
+ export const UNSELECTABLE_ROW_CLASS = "omnigrid-row-unselectable";
1
9
  const CHECKBOX_COLUMN_DEFAULT_ID = "__omnigrid_selection__";
2
10
  function defaultCheckboxRenderer(params) {
3
11
  return { type: "@omnigrid/checkbox", ...params };
4
12
  }
13
+ /** Adds row selection and optional checkbox controls to a grid. */
5
14
  export class SelectionPlugin {
6
15
  constructor(options = {}) {
7
16
  this.name = "@omnigrid/selection-plugin";
@@ -13,6 +22,7 @@ export class SelectionPlugin {
13
22
  this.showHeaderCheckbox = options.showHeaderCheckbox ?? false;
14
23
  this.isRowSelectable = options.isRowSelectable ?? (() => true);
15
24
  this.checkboxRenderer = options.checkboxRenderer ?? defaultCheckboxRenderer;
25
+ this.onSelectionChange = options.onSelectionChange;
16
26
  this.selectionColumnId = options.selectionColumnId ?? CHECKBOX_COLUMN_DEFAULT_ID;
17
27
  this.selectionColumnWidth = options.selectionColumnWidth ?? 44;
18
28
  }
@@ -26,28 +36,56 @@ export class SelectionPlugin {
26
36
  this.api = undefined;
27
37
  };
28
38
  }
39
+ /**
40
+ * Returns the IDs of all currently selected rows.
41
+ * @api
42
+ */
29
43
  getSelectedRowIds() {
30
44
  return [...this.selectedRowIds];
31
45
  }
46
+ /**
47
+ * Returns the data objects of all currently selected rows.
48
+ * @api
49
+ */
32
50
  getSelectedRows() {
33
51
  if (!this.api)
34
52
  return [];
35
53
  return this.api.getProcessedData().filter((row, index) => this.selectedRowIds.has(this.api.getRowId(row, index)));
36
54
  }
55
+ /**
56
+ * Returns selected row IDs and row data together.
57
+ * @api
58
+ */
37
59
  getSelectionState() {
38
60
  return { selectedRowIds: this.getSelectedRowIds(), selectedRows: this.getSelectedRows() };
39
61
  }
62
+ /**
63
+ * Checks whether a row ID is currently selected.
64
+ * @api
65
+ */
40
66
  isSelected(rowId) {
41
67
  return this.selectedRowIds.has(rowId);
42
68
  }
69
+ /**
70
+ * Replaces the current selection with the supplied row IDs.
71
+ * @api
72
+ */
43
73
  setSelectedRowIds(rowIds) {
44
74
  this.selectedRowIds.clear();
45
75
  rowIds.forEach((rowId) => this.selectedRowIds.add(rowId));
46
76
  this.refresh();
47
77
  }
78
+ /**
79
+ * Clears all selected rows.
80
+ * @api
81
+ */
48
82
  clearSelection() {
49
83
  this.setSelectedRowIds([]);
50
84
  }
85
+ /**
86
+ * Toggles a row's selection. Shift and Ctrl modifiers control range and additive selection.
87
+ * @api
88
+ */
51
89
  toggleRow(rowId, index, modifiers = {}) {
52
90
  if (!this.api)
53
91
  return;
@@ -62,8 +100,14 @@ export class SelectionPlugin {
62
100
  }
63
101
  this.applySelection(rowId, index, modifiers);
64
102
  }
65
- getRowStyle({ id }) {
66
- return this.selectedRowIds.has(id) ? { backgroundColor: "#dceffd" } : undefined;
103
+ /** Returns selection-related CSS classes for a row. */
104
+ getRowClass({ id, index, data }) {
105
+ const classes = [];
106
+ if (this.selectedRowIds.has(id))
107
+ classes.push(SELECTED_ROW_CLASS);
108
+ if (!this.isRowSelectable(data, index))
109
+ classes.push(UNSELECTABLE_ROW_CLASS);
110
+ return classes.length > 0 ? classes.join(" ") : undefined;
67
111
  }
68
112
  handleRowClick(event) {
69
113
  if (this.checkboxOnly)
@@ -204,6 +248,7 @@ export class SelectionPlugin {
204
248
  if (!this.api || this.api.isDestroyed())
205
249
  return;
206
250
  this.api.setData(this.api.getState().data);
251
+ this.onSelectionChange?.(this.getSelectionState());
207
252
  }
208
253
  validateSelection() {
209
254
  if (!this.api)
@@ -222,6 +267,10 @@ export class SelectionPlugin {
222
267
  this.refresh();
223
268
  }
224
269
  }
270
+ /**
271
+ * Updates plugin options at runtime.
272
+ * @api
273
+ */
225
274
  updateOptions(newOptions) {
226
275
  if (newOptions.mode !== undefined)
227
276
  this.mode = newOptions.mode;
@@ -237,6 +286,8 @@ export class SelectionPlugin {
237
286
  this.isRowSelectable = newOptions.isRowSelectable;
238
287
  if (newOptions.checkboxRenderer !== undefined)
239
288
  this.checkboxRenderer = newOptions.checkboxRenderer;
289
+ if (newOptions.onSelectionChange !== undefined)
290
+ this.onSelectionChange = newOptions.onSelectionChange;
240
291
  this.updateColumns();
241
292
  this.validateSelection();
242
293
  this.refresh();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/selection-plugin",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Headless row selection plugin for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",