@omnigrid/selection-plugin 0.1.3 → 0.1.5
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 +54 -54
- package/dist/index.d.ts +55 -0
- package/dist/index.js +40 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
# @omnigrid/selection-plugin
|
|
2
|
-
|
|
3
|
-
Headless row/column selection plugin for OmniGrid.
|
|
4
|
-
|
|
5
|
-
## What it provides
|
|
6
|
-
|
|
7
|
-
- Row selection (single, multi, range, checkbox toggle)
|
|
8
|
-
- Disabled rows marked by `isRowSelectable` with the `omnigrid-row-unselectable` class
|
|
9
|
-
- Cell selection with range drag
|
|
10
|
-
- Keyboard navigation support (Shift+Click, Ctrl+Click)
|
|
11
|
-
- API to read/modify selection state programmatically
|
|
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
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
```ts
|
|
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
|
-
},
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// Read the current selection at any time:
|
|
39
|
-
const selection = plugin.getSelectionState();
|
|
40
|
-
```
|
|
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
|
-
|
|
46
|
-
## Links
|
|
47
|
-
|
|
48
|
-
- **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/plugins/base/selection
|
|
49
|
-
- **Demo:** https://omnigrid-demo.vercel.app/
|
|
50
|
-
- **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
|
|
51
|
-
|
|
52
|
-
## License
|
|
53
|
-
|
|
54
|
-
MIT © KRZK Apps
|
|
1
|
+
# @omnigrid/selection-plugin
|
|
2
|
+
|
|
3
|
+
Headless row/column selection plugin for OmniGrid.
|
|
4
|
+
|
|
5
|
+
## What it provides
|
|
6
|
+
|
|
7
|
+
- Row selection (single, multi, range, checkbox toggle)
|
|
8
|
+
- Disabled rows marked by `isRowSelectable` with the `omnigrid-row-unselectable` class
|
|
9
|
+
- Cell selection with range drag
|
|
10
|
+
- Keyboard navigation support (Shift+Click, Ctrl+Click)
|
|
11
|
+
- API to read/modify selection state programmatically
|
|
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
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ts
|
|
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
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Read the current selection at any time:
|
|
39
|
+
const selection = plugin.getSelectionState();
|
|
40
|
+
```
|
|
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
|
+
|
|
46
|
+
## Links
|
|
47
|
+
|
|
48
|
+
- **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/plugins/base/selection
|
|
49
|
+
- **Demo:** https://omnigrid-demo.vercel.app/
|
|
50
|
+
- **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT © KRZK Apps
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
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";
|
|
3
4
|
/**
|
|
4
5
|
* CSS class applied to selected rows. The concrete selected-row color is
|
|
@@ -8,29 +9,50 @@ export type SelectionMode = "single" | "multiple";
|
|
|
8
9
|
export declare const SELECTED_ROW_CLASS = "omnigrid-row-selected";
|
|
9
10
|
/** Applied to rows rejected by `isRowSelectable`; themes supply muted colors. */
|
|
10
11
|
export declare const UNSELECTABLE_ROW_CLASS = "omnigrid-row-unselectable";
|
|
12
|
+
/** Values provided to a custom checkbox renderer. */
|
|
11
13
|
export interface SelectionRendererParams<T> extends CheckboxRenderParams {
|
|
14
|
+
/** Row data for a row checkbox; absent for the header checkbox. */
|
|
12
15
|
data?: T;
|
|
16
|
+
/** Row identifier for a row checkbox. */
|
|
13
17
|
rowId?: RowId;
|
|
18
|
+
/** Row index for a row checkbox. */
|
|
14
19
|
index?: number;
|
|
20
|
+
/** Whether the renderer is being called for the header checkbox. */
|
|
15
21
|
header: boolean;
|
|
16
22
|
}
|
|
23
|
+
/** Renderer used to customize selection checkboxes. */
|
|
17
24
|
export type SelectionRenderer<T> = (params: SelectionRendererParams<T>) => unknown;
|
|
25
|
+
/** Configuration for the selection plugin. */
|
|
18
26
|
export interface SelectionPluginOptions<T> {
|
|
27
|
+
/** Supports selecting one row or multiple rows. @default "single" */
|
|
19
28
|
mode?: SelectionMode;
|
|
29
|
+
/** Restricts selection changes to checkbox interaction. @default false */
|
|
20
30
|
checkboxOnly?: boolean;
|
|
31
|
+
/** In multiple mode, clicking a row replaces the current selection. @default false */
|
|
21
32
|
replaceSelectionOnClick?: boolean;
|
|
33
|
+
/** Adds a checkbox to each row. @default false */
|
|
22
34
|
showRowCheckboxes?: boolean;
|
|
35
|
+
/** Adds a select-all checkbox to the selection column header. @default false */
|
|
23
36
|
showHeaderCheckbox?: boolean;
|
|
37
|
+
/** Determines whether a row can be selected. */
|
|
24
38
|
isRowSelectable?: (row: T, index: number) => boolean;
|
|
39
|
+
/** Renders checkbox UI; defaults to the OmniGrid checkbox renderer. */
|
|
25
40
|
checkboxRenderer?: SelectionRenderer<T>;
|
|
41
|
+
/** Called when the selected rows change. */
|
|
26
42
|
onSelectionChange?: (state: SelectionState<T>) => void;
|
|
43
|
+
/** ID for the automatically inserted checkbox column. @default "__omnigrid_selection__" */
|
|
27
44
|
selectionColumnId?: string;
|
|
45
|
+
/** Width in pixels of the automatically inserted checkbox column. @default 44 */
|
|
28
46
|
selectionColumnWidth?: number;
|
|
29
47
|
}
|
|
48
|
+
/** Selected row identifiers and their corresponding row data. */
|
|
30
49
|
export interface SelectionState<T> {
|
|
50
|
+
/** IDs of selected rows. */
|
|
31
51
|
selectedRowIds: RowId[];
|
|
52
|
+
/** Data objects for selected rows. */
|
|
32
53
|
selectedRows: T[];
|
|
33
54
|
}
|
|
55
|
+
/** Adds row selection and optional checkbox controls to a grid. */
|
|
34
56
|
export declare class SelectionPlugin<T> implements GridPlugin<T> {
|
|
35
57
|
readonly name = "@omnigrid/selection-plugin";
|
|
36
58
|
private api?;
|
|
@@ -49,16 +71,45 @@ export declare class SelectionPlugin<T> implements GridPlugin<T> {
|
|
|
49
71
|
private unregisterRowClick?;
|
|
50
72
|
constructor(options?: SelectionPluginOptions<T>);
|
|
51
73
|
register(api: GridApi<T>): () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the IDs of all currently selected rows.
|
|
76
|
+
* @api
|
|
77
|
+
*/
|
|
52
78
|
getSelectedRowIds(): RowId[];
|
|
79
|
+
/**
|
|
80
|
+
* Returns the data objects of all currently selected rows.
|
|
81
|
+
* @api
|
|
82
|
+
*/
|
|
53
83
|
getSelectedRows(): T[];
|
|
84
|
+
/**
|
|
85
|
+
* Returns selected row IDs and row data together.
|
|
86
|
+
* @api
|
|
87
|
+
*/
|
|
54
88
|
getSelectionState(): SelectionState<T>;
|
|
89
|
+
/**
|
|
90
|
+
* Checks whether a row ID is currently selected.
|
|
91
|
+
* @api
|
|
92
|
+
*/
|
|
55
93
|
isSelected(rowId: RowId): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Replaces the current selection with the supplied row IDs.
|
|
96
|
+
* @api
|
|
97
|
+
*/
|
|
56
98
|
setSelectedRowIds(rowIds: RowId[]): void;
|
|
99
|
+
/**
|
|
100
|
+
* Clears all selected rows.
|
|
101
|
+
* @api
|
|
102
|
+
*/
|
|
57
103
|
clearSelection(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Toggles a row's selection. Shift and Ctrl modifiers control range and additive selection.
|
|
106
|
+
* @api
|
|
107
|
+
*/
|
|
58
108
|
toggleRow(rowId: RowId, index: number, modifiers?: {
|
|
59
109
|
shiftKey?: boolean;
|
|
60
110
|
ctrlKey?: boolean;
|
|
61
111
|
}): void;
|
|
112
|
+
/** Returns selection-related CSS classes for a row. */
|
|
62
113
|
getRowClass({ id, index, data }: RowRenderParams<T>): string | undefined;
|
|
63
114
|
private handleRowClick;
|
|
64
115
|
private applySelection;
|
|
@@ -70,5 +121,9 @@ export declare class SelectionPlugin<T> implements GridPlugin<T> {
|
|
|
70
121
|
private toggleAll;
|
|
71
122
|
private refresh;
|
|
72
123
|
private validateSelection;
|
|
124
|
+
/**
|
|
125
|
+
* Updates plugin options at runtime.
|
|
126
|
+
* @api
|
|
127
|
+
*/
|
|
73
128
|
updateOptions(newOptions: Partial<SelectionPluginOptions<T>>): void;
|
|
74
129
|
}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const CHECKBOX_COLUMN_DEFAULT_ID = "__omnigrid_selection__";
|
|
|
10
10
|
function defaultCheckboxRenderer(params) {
|
|
11
11
|
return { type: "@omnigrid/checkbox", ...params };
|
|
12
12
|
}
|
|
13
|
+
/** Adds row selection and optional checkbox controls to a grid. */
|
|
13
14
|
export class SelectionPlugin {
|
|
14
15
|
constructor(options = {}) {
|
|
15
16
|
this.name = "@omnigrid/selection-plugin";
|
|
@@ -32,31 +33,65 @@ export class SelectionPlugin {
|
|
|
32
33
|
return () => {
|
|
33
34
|
this.unregisterRowClick?.();
|
|
34
35
|
this.unregisterRowClick = undefined;
|
|
36
|
+
const api = this.api;
|
|
37
|
+
if (api && !api.isDestroyed()) {
|
|
38
|
+
api.setColumns(api.getState().columns.filter((column) => column.id !== this.selectionColumnId));
|
|
39
|
+
}
|
|
40
|
+
this.selectedRowIds.clear();
|
|
41
|
+
this.anchorIndex = undefined;
|
|
35
42
|
this.api = undefined;
|
|
36
43
|
};
|
|
37
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the IDs of all currently selected rows.
|
|
47
|
+
* @api
|
|
48
|
+
*/
|
|
38
49
|
getSelectedRowIds() {
|
|
39
50
|
return [...this.selectedRowIds];
|
|
40
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns the data objects of all currently selected rows.
|
|
54
|
+
* @api
|
|
55
|
+
*/
|
|
41
56
|
getSelectedRows() {
|
|
42
57
|
if (!this.api)
|
|
43
58
|
return [];
|
|
44
59
|
return this.api.getProcessedData().filter((row, index) => this.selectedRowIds.has(this.api.getRowId(row, index)));
|
|
45
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns selected row IDs and row data together.
|
|
63
|
+
* @api
|
|
64
|
+
*/
|
|
46
65
|
getSelectionState() {
|
|
47
66
|
return { selectedRowIds: this.getSelectedRowIds(), selectedRows: this.getSelectedRows() };
|
|
48
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Checks whether a row ID is currently selected.
|
|
70
|
+
* @api
|
|
71
|
+
*/
|
|
49
72
|
isSelected(rowId) {
|
|
50
73
|
return this.selectedRowIds.has(rowId);
|
|
51
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Replaces the current selection with the supplied row IDs.
|
|
77
|
+
* @api
|
|
78
|
+
*/
|
|
52
79
|
setSelectedRowIds(rowIds) {
|
|
53
80
|
this.selectedRowIds.clear();
|
|
54
81
|
rowIds.forEach((rowId) => this.selectedRowIds.add(rowId));
|
|
55
82
|
this.refresh();
|
|
56
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Clears all selected rows.
|
|
86
|
+
* @api
|
|
87
|
+
*/
|
|
57
88
|
clearSelection() {
|
|
58
89
|
this.setSelectedRowIds([]);
|
|
59
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Toggles a row's selection. Shift and Ctrl modifiers control range and additive selection.
|
|
93
|
+
* @api
|
|
94
|
+
*/
|
|
60
95
|
toggleRow(rowId, index, modifiers = {}) {
|
|
61
96
|
if (!this.api)
|
|
62
97
|
return;
|
|
@@ -71,6 +106,7 @@ export class SelectionPlugin {
|
|
|
71
106
|
}
|
|
72
107
|
this.applySelection(rowId, index, modifiers);
|
|
73
108
|
}
|
|
109
|
+
/** Returns selection-related CSS classes for a row. */
|
|
74
110
|
getRowClass({ id, index, data }) {
|
|
75
111
|
const classes = [];
|
|
76
112
|
if (this.selectedRowIds.has(id))
|
|
@@ -237,6 +273,10 @@ export class SelectionPlugin {
|
|
|
237
273
|
this.refresh();
|
|
238
274
|
}
|
|
239
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Updates plugin options at runtime.
|
|
278
|
+
* @api
|
|
279
|
+
*/
|
|
240
280
|
updateOptions(newOptions) {
|
|
241
281
|
if (newOptions.mode !== undefined)
|
|
242
282
|
this.mode = newOptions.mode;
|