@nanahoshi/mona-ui 0.26.1 → 0.26.3

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.
@@ -0,0 +1,196 @@
1
+ # Selection
2
+
3
+ > Select single or multiple rows by key, with checkboxes, row clicks and the keyboard.
4
+
5
+ Entry point: `@nanahoshi/mona-ui/grid`
6
+
7
+ **Selector:** `mona-grid[monaGridSelectable]`
8
+
9
+ `monaGridSelectable` lets people select one row or several, with built-in checkboxes, row clicks or the keyboard. The selection is a list of row keys you bind with `[(selectedKeys)]`, so it survives paging, sorting and reloading the data.
10
+
11
+ ## Overview
12
+
13
+ Selection state always lives in `selectedKeys`. Checkboxes, row clicks, keys and your own code all change the same list, and `selectedKeysChange` reports the changes people make.
14
+
15
+ `selectBy` chooses what identifies a row: a field name or a function. Without it, rows are selected by the grid's `rowKey`, so the selection survives a refetch; without either, by the data item itself.
16
+
17
+ **Use something else when:**
18
+
19
+ - People pick one value to fill a field. Use a [Dropdown List](dropdown-list.md) or [List Box](list-box.md).
20
+ - The selection drives actions on the rows, such as deleting them: keep the selection, and put the actions in a [toolbar](grid.md#toolbar-and-the-columns-button) that reads `selectedKeys`.
21
+
22
+ ## Import & Quick Start
23
+
24
+ ```typescript
25
+ import { GridColumnComponent, GridComponent, GridSelectableDirective } from "@nanahoshi/mona-ui/grid";
26
+ ```
27
+
28
+ ```typescript
29
+ protected readonly selectedIds = signal<number[]>([]);
30
+ ```
31
+
32
+ ```html
33
+ <mona-grid
34
+ aria-label="Orders"
35
+ rowKey="orderId"
36
+ [data]="orders()"
37
+ [monaGridSelectable]="{ mode: 'multiple', showCheckboxes: true }"
38
+ [(selectedKeys)]="selectedIds">
39
+ ...
40
+ </mona-grid>
41
+ ```
42
+
43
+ ## Examples
44
+
45
+ ### Checkbox Selection
46
+
47
+ `selection-example.component.ts`
48
+
49
+ ```typescript
50
+ import { Component, signal } from "@angular/core";
51
+ import { GridColumnComponent, GridComponent, GridSelectableDirective } from "@nanahoshi/mona-ui/grid";
52
+
53
+ @Component({
54
+ selector: "app-grid-selection-selection-example",
55
+ imports: [GridColumnComponent, GridComponent, GridSelectableDirective],
56
+ templateUrl: "./selection-example.component.html"
57
+ })
58
+ export class GridSelectionExampleComponent {
59
+ protected readonly members = [
60
+ { id: 1, name: "Amara Okafor", role: "Designer", location: "Lagos" },
61
+ { id: 2, name: "Lukas Weber", role: "Engineer", location: "Berlin" },
62
+ { id: 3, name: "Mei Tanaka", role: "Engineer", location: "Osaka" },
63
+ { id: 4, name: "Diego Ramos", role: "Product manager", location: "Lima" },
64
+ { id: 5, name: "Sofia Rossi", role: "Researcher", location: "Turin" },
65
+ { id: 6, name: "Noah Clarke", role: "Engineer", location: "Toronto" }
66
+ ];
67
+ protected readonly selectedIds = signal<number[]>([2]);
68
+
69
+ /** Names each row's checkbox after the person, instead of "Select row 3". */
70
+ protected readonly rowLabel = (row: Record<PropertyKey, unknown>): string => `Select ${String(row["name"])}`;
71
+ }
72
+ ```
73
+
74
+ `selection-example.component.html`
75
+
76
+ ```html
77
+ <div class="flex flex-col gap-2">
78
+ <mona-grid
79
+ aria-label="Team members"
80
+ rowKey="id"
81
+ [data]="members"
82
+ [monaGridSelectable]="{ mode: 'multiple', showCheckboxes: true, rowAriaLabel: rowLabel }"
83
+ [pageSize]="5"
84
+ [(selectedKeys)]="selectedIds">
85
+ <mona-grid-column field="name" title="Name" />
86
+ <mona-grid-column field="role" title="Role" />
87
+ <mona-grid-column field="location" title="Location" />
88
+ </mona-grid>
89
+ <span aria-live="polite" class="text-sm">{{ selectedIds().length }} selected</span>
90
+ </div>
91
+ ```
92
+
93
+ `rowAriaLabel` names each checkbox after its row, so a screen reader says "Select Lukas Weber" instead of "Select row 2".
94
+
95
+ ### Options
96
+
97
+ | Option | Default | What it does |
98
+ | :--- | :--- | :--- |
99
+ | `enabled` | `true` | Turns selection on or off without removing the directive. |
100
+ | `mode` | `"single"` | `"single"` or `"multiple"`. |
101
+ | `showCheckboxes` | `false` | Shows the built-in checkbox column. |
102
+ | `showSelectAll` | `true` | Shows the select-all checkbox in that column's header, in multiple mode. |
103
+ | `selectAllScope` | `"page"` | Which rows the select-all checkbox affects: `"page"`, the rows on the current page, or `"view"`, every row in the filtered and sorted view. Virtual scrolling has no pages, so `"page"` means the whole view there. |
104
+ | `selectOnRowClick` | `false` | Whether a click on the row itself changes the selection. |
105
+ | `rowAriaLabel` | `"Select row 3"` | A function from the row to its checkbox's name. |
106
+
107
+ ### Selecting from Code
108
+
109
+ Set `selectedKeys` to select rows yourself, such as after a search or to restore a saved selection. `selectedKeysChange` isn't emitted for a value set from code.
110
+
111
+ ```typescript
112
+ protected selectOverdue(): void {
113
+ this.selectedIds.set(this.orders().filter(order => order.overdue).map(order => order.orderId));
114
+ }
115
+ ```
116
+
117
+ ## Behavior
118
+
119
+ ### Row clicks
120
+
121
+ Off by default, so clicking a row's content (a link, a button, an editable cell) never silently discards the selection. With `selectOnRowClick`: in single mode, clicking a row replaces the selection; in multiple mode, a plain click selects just that row, Ctrl/Cmd-click toggles one row without disturbing the rest, and Shift-click selects the range from the last clicked row. Checkbox clicks always toggle their row, whatever `selectOnRowClick` says.
122
+
123
+ ### The checkbox column
124
+
125
+ It's fixed at the start of the grid, stays put while the grid scrolls sideways, and never takes part in sorting, filtering, grouping, resizing, reordering or the Columns popup. The header checkbox is checked when every row in scope is selected, and mixed when only some are. Selecting or clearing everything in scope keeps the keys of rows outside it.
126
+
127
+ ### Stable keys
128
+
129
+ Provide a stable `rowKey` or `selectBy` when the selection must survive data reloads, paging or server requests. With server binding, select-all selects the loaded rows only.
130
+
131
+ ## Accessibility
132
+
133
+ Selected rows carry `aria-selected`, and the grid says it allows several with `aria-multiselectable`. Name the checkboxes with `rowAriaLabel` when the position alone wouldn't tell people which row they're on.
134
+
135
+ ### Keyboard
136
+
137
+ | Key | Action |
138
+ | --- | --- |
139
+ | `Space` | Toggles the focused row's selection. |
140
+ | `Shift+Space` | Selects the rows from the last selected row to this one. |
141
+ | `Shift+ArrowDown` / `Shift+ArrowUp` | Moves to the next/previous row and extends the selection to it. |
142
+ | `Ctrl+A` / `Cmd+A` | In multiple mode, selects every row in the select-all scope. |
143
+
144
+ The rest of the grid's keys are on [Grid Overview](grid.md#keyboard).
145
+
146
+ ### ARIA
147
+
148
+ | Attribute | When present | Value |
149
+ | --- | --- | --- |
150
+ | `aria-multiselectable` | On the tabular content region | `"true"` with multiple selection |
151
+ | `aria-selected` | On a row, while `monaGridSelectable` is enabled | Whether the row is selected |
152
+ | `aria-label` | On a row's checkbox / the select-all checkbox | `rowAriaLabel`'s text or `"Select row 3"` (`selectRow`) / `selectAllRows` |
153
+
154
+ ## API
155
+
156
+ Everything below is imported from `@nanahoshi/mona-ui/grid`.
157
+
158
+ ### `GridSelectableDirective`
159
+
160
+ **Selector:** `mona-grid[monaGridSelectable]`
161
+
162
+ #### Inputs
163
+
164
+ | Name | Type | Default | Description |
165
+ | --- | --- | --- | --- |
166
+ | `monaGridSelectable` | `SelectableOptions \| ""` | `""` | Enables row selection on the grid. Pass a `SelectableOptions` object to configure single or multiple selection mode. Selection is on unless the object sets `enabled: false`. |
167
+ | `selectBy` | `GridKeySelector<unknown>` | `""` | Field name or function that gives a row's selection key. Without it, rows are selected by the grid's `rowKey` value, or by the data item when there's no `rowKey`. |
168
+ | `selectedKeys` | `Iterable<unknown>` | `[]` | Currently selected row keys. |
169
+
170
+ #### Outputs
171
+
172
+ | Name | Type | Description |
173
+ | --- | --- | --- |
174
+ | `selectedKeysChange` | `unknown[]` | Emitted when the user changes the selection. Not emitted for a `selectedKeys` value set from code. |
175
+
176
+ ### Types
177
+
178
+ #### `SelectableOptions`
179
+
180
+ | Name | Type | Description |
181
+ | --- | --- | --- |
182
+ | `enabled?` | `boolean` | Enables row selection. |
183
+ | `mode?` | `"single" \| "multiple"` | Controls whether one or multiple rows can be selected. |
184
+ | `rowAriaLabel?` | `(rowData: Record<PropertyKey, unknown>) => string` | Derives the accessible label for a row's selection checkbox from its data. When omitted, the checkbox falls back to a positional label (e.g. "Select row 3"). |
185
+ | `selectAllScope?` | `"page" \| "view"` | Determines which rows are affected by the header checkbox. - "page": rows on the current paginated page - "view": all rows in the current filtered and sorted view For virtual scrolling, "page" resolves to the complete current view because virtual scrolling does not have logical pages. |
186
+ | `selectOnRowClick?` | `boolean` | Whether clicking the row itself changes selection. Off by default so that clicking a row to interact with its content (a link, a button, an editable cell) never silently discards the current selection. Set this to true to also select rows via a plain click, in addition to checkboxes or application code. |
187
+ | `showCheckboxes?` | `boolean` | Displays the built-in checkbox selection column. |
188
+ | `showSelectAll?` | `boolean` | Displays a select-all checkbox in the selection-column header. Only applies to multiple-selection mode. |
189
+
190
+ #### `GridKeySelector`
191
+
192
+ The field holding a row's key, or a function returning it.
193
+
194
+ ```typescript
195
+ type GridKeySelector<TData, TKey = unknown> = string | Selector<TData, TKey> | null;
196
+ ```