@omnigrid/selection-plugin 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KRZK Apps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,64 @@
1
+ import type { CheckboxRenderParams, GridApi, GridPlugin, RowId, RowRenderParams, RowStyle } from "@omnigrid/core";
2
+ export type SelectionMode = "single" | "multiple";
3
+ export interface SelectionRendererParams<T> extends CheckboxRenderParams {
4
+ data?: T;
5
+ rowId?: RowId;
6
+ index?: number;
7
+ header: boolean;
8
+ }
9
+ export type SelectionRenderer<T> = (params: SelectionRendererParams<T>) => unknown;
10
+ export interface SelectionPluginOptions<T> {
11
+ mode?: SelectionMode;
12
+ checkboxOnly?: boolean;
13
+ replaceSelectionOnClick?: boolean;
14
+ showRowCheckboxes?: boolean;
15
+ showHeaderCheckbox?: boolean;
16
+ isRowSelectable?: (row: T, index: number) => boolean;
17
+ checkboxRenderer?: SelectionRenderer<T>;
18
+ selectionColumnId?: string;
19
+ selectionColumnWidth?: number;
20
+ }
21
+ export interface SelectionState<T> {
22
+ selectedRowIds: RowId[];
23
+ selectedRows: T[];
24
+ }
25
+ export declare class SelectionPlugin<T> implements GridPlugin<T> {
26
+ readonly name = "@omnigrid/selection-plugin";
27
+ private api?;
28
+ private mode;
29
+ private checkboxOnly;
30
+ private replaceSelectionOnClick;
31
+ private showRowCheckboxes;
32
+ private showHeaderCheckbox;
33
+ private isRowSelectable;
34
+ private checkboxRenderer;
35
+ private readonly selectionColumnId;
36
+ private readonly selectionColumnWidth;
37
+ private readonly selectedRowIds;
38
+ private anchorIndex?;
39
+ private unregisterRowClick?;
40
+ constructor(options?: SelectionPluginOptions<T>);
41
+ register(api: GridApi<T>): () => void;
42
+ getSelectedRowIds(): RowId[];
43
+ getSelectedRows(): T[];
44
+ getSelectionState(): SelectionState<T>;
45
+ isSelected(rowId: RowId): boolean;
46
+ setSelectedRowIds(rowIds: RowId[]): void;
47
+ clearSelection(): void;
48
+ toggleRow(rowId: RowId, index: number, modifiers?: {
49
+ shiftKey?: boolean;
50
+ ctrlKey?: boolean;
51
+ }): void;
52
+ getRowStyle({ id }: RowRenderParams<T>): RowStyle | undefined;
53
+ private handleRowClick;
54
+ private applySelection;
55
+ private updateColumns;
56
+ private renderCheckbox;
57
+ private hasSelectableRows;
58
+ private isAllSelectableRowsSelected;
59
+ private hasSomeSelectableRowsSelected;
60
+ private toggleAll;
61
+ private refresh;
62
+ private validateSelection;
63
+ updateOptions(newOptions: Partial<SelectionPluginOptions<T>>): void;
64
+ }
package/dist/index.js ADDED
@@ -0,0 +1,244 @@
1
+ const CHECKBOX_COLUMN_DEFAULT_ID = "__omnigrid_selection__";
2
+ function defaultCheckboxRenderer(params) {
3
+ return { type: "@omnigrid/checkbox", ...params };
4
+ }
5
+ export class SelectionPlugin {
6
+ constructor(options = {}) {
7
+ this.name = "@omnigrid/selection-plugin";
8
+ this.selectedRowIds = new Set();
9
+ this.mode = options.mode ?? "single";
10
+ this.checkboxOnly = options.checkboxOnly ?? false;
11
+ this.replaceSelectionOnClick = options.replaceSelectionOnClick ?? false;
12
+ this.showRowCheckboxes = options.showRowCheckboxes ?? false;
13
+ this.showHeaderCheckbox = options.showHeaderCheckbox ?? false;
14
+ this.isRowSelectable = options.isRowSelectable ?? (() => true);
15
+ this.checkboxRenderer = options.checkboxRenderer ?? defaultCheckboxRenderer;
16
+ this.selectionColumnId = options.selectionColumnId ?? CHECKBOX_COLUMN_DEFAULT_ID;
17
+ this.selectionColumnWidth = options.selectionColumnWidth ?? 44;
18
+ }
19
+ register(api) {
20
+ this.api = api;
21
+ this.unregisterRowClick = api.on("rowClick", (event) => this.handleRowClick(event));
22
+ this.updateColumns();
23
+ return () => {
24
+ this.unregisterRowClick?.();
25
+ this.unregisterRowClick = undefined;
26
+ this.api = undefined;
27
+ };
28
+ }
29
+ getSelectedRowIds() {
30
+ return [...this.selectedRowIds];
31
+ }
32
+ getSelectedRows() {
33
+ if (!this.api)
34
+ return [];
35
+ return this.api.getProcessedData().filter((row, index) => this.selectedRowIds.has(this.api.getRowId(row, index)));
36
+ }
37
+ getSelectionState() {
38
+ return { selectedRowIds: this.getSelectedRowIds(), selectedRows: this.getSelectedRows() };
39
+ }
40
+ isSelected(rowId) {
41
+ return this.selectedRowIds.has(rowId);
42
+ }
43
+ setSelectedRowIds(rowIds) {
44
+ this.selectedRowIds.clear();
45
+ rowIds.forEach((rowId) => this.selectedRowIds.add(rowId));
46
+ this.refresh();
47
+ }
48
+ clearSelection() {
49
+ this.setSelectedRowIds([]);
50
+ }
51
+ toggleRow(rowId, index, modifiers = {}) {
52
+ if (!this.api)
53
+ return;
54
+ const row = this.api.getProcessedData()[index];
55
+ if (!row || !this.isRowSelectable(row, index))
56
+ return;
57
+ if (this.mode === "single" && !this.replaceSelectionOnClick && this.selectedRowIds.has(rowId) && !modifiers.shiftKey) {
58
+ this.selectedRowIds.delete(rowId);
59
+ this.anchorIndex = index;
60
+ this.refresh();
61
+ return;
62
+ }
63
+ this.applySelection(rowId, index, modifiers);
64
+ }
65
+ getRowStyle({ id }) {
66
+ return this.selectedRowIds.has(id) ? { backgroundColor: "#dceffd" } : undefined;
67
+ }
68
+ handleRowClick(event) {
69
+ if (this.checkboxOnly)
70
+ return;
71
+ this.applySelection(event.id, event.index, event);
72
+ }
73
+ applySelection(rowId, index, modifiers) {
74
+ if (!this.api)
75
+ return;
76
+ const processedData = this.api.getProcessedData();
77
+ const row = processedData[index];
78
+ if (!row || !this.isRowSelectable(row, index))
79
+ return;
80
+ if (this.mode === "single" && !this.replaceSelectionOnClick && this.selectedRowIds.has(rowId) && !modifiers.shiftKey) {
81
+ this.selectedRowIds.delete(rowId);
82
+ this.anchorIndex = index;
83
+ this.refresh();
84
+ return;
85
+ }
86
+ const useRange = modifiers.shiftKey === true && this.anchorIndex !== undefined;
87
+ if (useRange) {
88
+ const start = Math.min(this.anchorIndex, index);
89
+ const end = Math.max(this.anchorIndex, index);
90
+ if (!modifiers.ctrlKey)
91
+ this.selectedRowIds.clear();
92
+ for (let rowIndex = start; rowIndex <= end; rowIndex += 1) {
93
+ const rangeRow = processedData[rowIndex];
94
+ if (rangeRow && this.isRowSelectable(rangeRow, rowIndex)) {
95
+ this.selectedRowIds.add(this.api.getRowId(rangeRow, rowIndex));
96
+ }
97
+ }
98
+ }
99
+ else if (this.replaceSelectionOnClick && !modifiers.ctrlKey) {
100
+ this.selectedRowIds.clear();
101
+ this.selectedRowIds.add(rowId);
102
+ }
103
+ else if (this.mode === "multiple" || modifiers.ctrlKey) {
104
+ if (this.selectedRowIds.has(rowId))
105
+ this.selectedRowIds.delete(rowId);
106
+ else
107
+ this.selectedRowIds.add(rowId);
108
+ }
109
+ else {
110
+ this.selectedRowIds.clear();
111
+ this.selectedRowIds.add(rowId);
112
+ }
113
+ this.anchorIndex = index;
114
+ this.refresh();
115
+ }
116
+ updateColumns() {
117
+ if (!this.api)
118
+ return;
119
+ const columns = this.api.getState().columns.filter((column) => column.id !== this.selectionColumnId);
120
+ if (!this.showRowCheckboxes && !this.showHeaderCheckbox) {
121
+ this.api.setColumns(columns);
122
+ return;
123
+ }
124
+ const hasPinnedLeft = columns.some((column) => column.pinned === "left");
125
+ const selectionColumn = {
126
+ id: this.selectionColumnId,
127
+ header: "",
128
+ width: this.selectionColumnWidth,
129
+ minWidth: this.selectionColumnWidth,
130
+ maxWidth: this.selectionColumnWidth,
131
+ sortable: false,
132
+ stopHeaderClick: true,
133
+ stopRowClick: true,
134
+ // The checkbox column must stay in view while scrolling horizontally.
135
+ // When the table already has left-pinned columns, pin the checkbox
136
+ // column to the left (as the very first pinned column) so it never
137
+ // scrolls away together with its pinned peers. With no left-pinned
138
+ // columns the checkbox column stays as the first scrollable column.
139
+ pinned: hasPinnedLeft ? "left" : undefined,
140
+ headerRenderer: () => this.renderCheckbox({
141
+ checked: this.isAllSelectableRowsSelected(),
142
+ indeterminate: this.hasSomeSelectableRowsSelected() && !this.isAllSelectableRowsSelected(),
143
+ disabled: !this.hasSelectableRows(),
144
+ ariaLabel: "Select all rows",
145
+ header: true,
146
+ onChange: (event) => this.toggleAll(event),
147
+ }),
148
+ cellRenderer: ({ data, id, index }) => this.renderCheckbox({
149
+ checked: id !== undefined && this.selectedRowIds.has(id),
150
+ indeterminate: false,
151
+ disabled: index === undefined || !this.isRowSelectable(data, index),
152
+ ariaLabel: "Select row",
153
+ data,
154
+ rowId: id,
155
+ index,
156
+ header: false,
157
+ onChange: (event) => id !== undefined && index !== undefined && this.toggleRow(id, index, event),
158
+ }),
159
+ };
160
+ this.api.setColumns([selectionColumn, ...columns]);
161
+ }
162
+ renderCheckbox(params) {
163
+ if ((params.header && !this.showHeaderCheckbox) || (!params.header && !this.showRowCheckboxes))
164
+ return "";
165
+ return this.checkboxRenderer(params);
166
+ }
167
+ hasSelectableRows() {
168
+ if (!this.api)
169
+ return false;
170
+ return this.api.getState().data.some((row, index) => this.isRowSelectable(row, index));
171
+ }
172
+ isAllSelectableRowsSelected() {
173
+ if (!this.api)
174
+ return false;
175
+ const processedData = this.api.getProcessedData();
176
+ const selectableRows = processedData.filter((row, index) => this.isRowSelectable(row, index));
177
+ return (selectableRows.length > 0 &&
178
+ processedData.every((row, index) => !this.isRowSelectable(row, index) || this.selectedRowIds.has(this.api.getRowId(row, index))));
179
+ }
180
+ hasSomeSelectableRowsSelected() {
181
+ if (!this.api)
182
+ return false;
183
+ return this.api.getProcessedData().some((row, index) => this.isRowSelectable(row, index) && this.selectedRowIds.has(this.api.getRowId(row, index)));
184
+ }
185
+ toggleAll(modifiers) {
186
+ if (!this.api)
187
+ return;
188
+ const processedData = this.api.getProcessedData();
189
+ if (this.isAllSelectableRowsSelected()) {
190
+ processedData.forEach((row, index) => {
191
+ if (this.isRowSelectable(row, index))
192
+ this.selectedRowIds.delete(this.api.getRowId(row, index));
193
+ });
194
+ }
195
+ else {
196
+ processedData.forEach((row, index) => {
197
+ if (this.isRowSelectable(row, index))
198
+ this.selectedRowIds.add(this.api.getRowId(row, index));
199
+ });
200
+ }
201
+ this.refresh();
202
+ }
203
+ refresh() {
204
+ if (!this.api || this.api.isDestroyed())
205
+ return;
206
+ this.api.setData(this.api.getState().data);
207
+ }
208
+ validateSelection() {
209
+ if (!this.api)
210
+ return;
211
+ const processedData = this.api.getProcessedData();
212
+ let hasChanges = false;
213
+ for (const rowId of this.selectedRowIds) {
214
+ const index = processedData.findIndex((row, idx) => this.api.getRowId(row, idx) === rowId);
215
+ const row = index !== -1 ? processedData[index] : undefined;
216
+ if (row && index !== -1 && !this.isRowSelectable(row, index)) {
217
+ this.selectedRowIds.delete(rowId);
218
+ hasChanges = true;
219
+ }
220
+ }
221
+ if (hasChanges) {
222
+ this.refresh();
223
+ }
224
+ }
225
+ updateOptions(newOptions) {
226
+ if (newOptions.mode !== undefined)
227
+ this.mode = newOptions.mode;
228
+ if (newOptions.checkboxOnly !== undefined)
229
+ this.checkboxOnly = newOptions.checkboxOnly;
230
+ if (newOptions.replaceSelectionOnClick !== undefined)
231
+ this.replaceSelectionOnClick = newOptions.replaceSelectionOnClick;
232
+ if (newOptions.showRowCheckboxes !== undefined)
233
+ this.showRowCheckboxes = newOptions.showRowCheckboxes;
234
+ if (newOptions.showHeaderCheckbox !== undefined)
235
+ this.showHeaderCheckbox = newOptions.showHeaderCheckbox;
236
+ if (newOptions.isRowSelectable !== undefined)
237
+ this.isRowSelectable = newOptions.isRowSelectable;
238
+ if (newOptions.checkboxRenderer !== undefined)
239
+ this.checkboxRenderer = newOptions.checkboxRenderer;
240
+ this.updateColumns();
241
+ this.validateSelection();
242
+ this.refresh();
243
+ }
244
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@omnigrid/selection-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Headless row selection plugin for OmniGrid",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "npx tsc -p tsconfig.json",
20
+ "lint": "npx tsc --noEmit -p tsconfig.json",
21
+ "test": "vitest run",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "dependencies": {
25
+ "@omnigrid/core": "0.1.0"
26
+ }
27
+ }