@nanahoshi/mona-ui 0.26.2 → 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,422 @@
1
+ # State & Export
2
+
3
+ > Save and restore the grid's columns, sort, filter, groups and page size, and export rows as CSV.
4
+
5
+ Entry point: `@nanahoshi/mona-ui/grid`
6
+
7
+ **Selectors:** `mona-grid[monaGridStatePersistence]`, `mona-grid[monaGridExport]`
8
+
9
+ `monaGridStatePersistence` captures what people changed (column order, widths and visibility, sort, filter, groups and page size) as a plain `GridState` object you can store and give back later. `monaGridExport` writes the rows of the current view to a CSV file.
10
+
11
+ ## Overview
12
+
13
+ | Directive | State you bind | Methods |
14
+ | :--- | :--- | :--- |
15
+ | `monaGridStatePersistence` | `[(state)]`: `GridState \| null` | `captureState()`, `loadState(state)` |
16
+ | `monaGridExport` | — | `exportCsv(filename?)` |
17
+
18
+ A `GridState` is JSON-safe, so it can go to `localStorage`, a user profile on your server, or a URL. The grid doesn't store it anywhere itself.
19
+
20
+ **Use something else when:**
21
+
22
+ - You need one value back, such as the sort. Bind that feature's own model (`[(sort)]`, `[(filter)]`, `[(group)]`, `[(pageSize)]`) instead.
23
+ - The export needs every record from a server, or a format other than CSV. Produce it in your application from your data.
24
+
25
+ ## Import & Quick Start
26
+
27
+ ```typescript
28
+ import { GridComponent, GridExportDirective, type GridState, GridStatePersistenceDirective } from "@nanahoshi/mona-ui/grid";
29
+ ```
30
+
31
+ ```typescript
32
+ protected readonly state = signal<GridState | null>(null);
33
+ ```
34
+
35
+ ```html
36
+ <button monaButton type="button" (click)="gridExport.exportCsv('orders.csv')">Export as CSV</button>
37
+ <mona-grid
38
+ #gridExport="monaGridExport"
39
+ aria-label="Orders"
40
+ [data]="orders()"
41
+ monaGridExport
42
+ [monaGridStatePersistence]="{ persistPageSize: true }"
43
+ [(state)]="state">
44
+ ...
45
+ </mona-grid>
46
+ ```
47
+
48
+ ## Examples
49
+
50
+ ### Saving the State in the Browser
51
+
52
+ `saved-state-example.component.ts`
53
+
54
+ ```typescript
55
+ import { Component, effect, signal } from "@angular/core";
56
+ import { ButtonDirective } from "@nanahoshi/mona-ui/button";
57
+ import {
58
+ GridColumnComponent,
59
+ GridComponent,
60
+ GridExportDirective,
61
+ GridSortableDirective,
62
+ type GridState,
63
+ GridStatePersistenceDirective,
64
+ GridToolbarTemplateDirective
65
+ } from "@nanahoshi/mona-ui/grid";
66
+
67
+ const STORAGE_KEY = "orders-grid-state";
68
+
69
+ function readSavedState(): GridState | null {
70
+ try {
71
+ const saved = localStorage.getItem(STORAGE_KEY);
72
+ return saved ? (JSON.parse(saved) as GridState) : null;
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+
78
+ @Component({
79
+ selector: "app-grid-state-export-saved-state-example",
80
+ imports: [
81
+ ButtonDirective,
82
+ GridColumnComponent,
83
+ GridComponent,
84
+ GridExportDirective,
85
+ GridSortableDirective,
86
+ GridStatePersistenceDirective,
87
+ GridToolbarTemplateDirective
88
+ ],
89
+ templateUrl: "./saved-state-example.component.html"
90
+ })
91
+ export class GridSavedStateExampleComponent {
92
+ protected readonly orders = Array.from({ length: 24 }, (_, index) => ({
93
+ orderId: 10248 + index,
94
+ customer: ["Ernst Handel", "Hanari Carnes", "=Island Trading", "Folk och fä HB"][index % 4],
95
+ freight: 12.5 + ((index * 17) % 60)
96
+ }));
97
+ /** Restored on the next visit: sort, column order, widths, visibility and page size. */
98
+ protected readonly state = signal<GridState | null>(readSavedState());
99
+
100
+ public constructor() {
101
+ effect(() => {
102
+ const state = this.state();
103
+ try {
104
+ if (state) {
105
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
106
+ }
107
+ } catch {
108
+ // Storage may be full or blocked; the grid keeps working without it.
109
+ }
110
+ });
111
+ }
112
+ }
113
+ ```
114
+
115
+ `saved-state-example.component.html`
116
+
117
+ ```html
118
+ <mona-grid
119
+ #gridExport="monaGridExport"
120
+ aria-label="Orders"
121
+ rowKey="orderId"
122
+ [data]="orders"
123
+ [monaGridStatePersistence]="{ persistPageSize: true }"
124
+ [pageSize]="5"
125
+ [pageSizeValues]="[5, 10, 20]"
126
+ monaGridExport
127
+ monaGridSortable
128
+ [(state)]="state">
129
+ <ng-template monaGridToolbarTemplate>
130
+ <button monaButton type="button" (click)="gridExport.exportCsv('orders.csv')">Export as CSV</button>
131
+ </ng-template>
132
+ <mona-grid-column field="orderId" title="Order" type="number" />
133
+ <mona-grid-column field="customer" title="Customer" />
134
+ <mona-grid-column field="freight" title="Freight" type="number" />
135
+ </mona-grid>
136
+ ```
137
+
138
+ The example reads a saved state when it starts and writes every new one to `localStorage`. Sort a column, change the page size or move a column, then reload the page: the grid comes back the same. The toolbar's Export button downloads the rows as `orders.csv`; the customer `=Island Trading` is written as `'=Island Trading`, so a spreadsheet doesn't run it as a formula.
139
+
140
+ ### Applying a State from Code
141
+
142
+ `captureState()` returns the current state at once, without waiting for the model to update, and `loadState(state)` applies one and says how it went:
143
+
144
+ ```typescript
145
+ private readonly persistence = viewChild.required(GridStatePersistenceDirective);
146
+
147
+ protected restore(saved: GridState): void {
148
+ const result = this.persistence().loadState(saved);
149
+ if (result.status !== "applied") {
150
+ console.warn("The saved layout no longer fits", result.status, result.missingColumns);
151
+ }
152
+ }
153
+ ```
154
+
155
+ `status` is `"applied"`, `"empty"` (no state), `"rejected-invalid"` (not well formed: a missing array, a duplicate column id, an unknown filter operator, a page size or width that isn't a positive number, and nothing changes), `"rejected-schema"` or `"rejected-version"`. `ignoredColumns` lists saved columns the grid no longer has, and `missingColumns` the grid's columns the state didn't mention.
156
+
157
+ ## Behavior
158
+
159
+ ### What the state holds
160
+
161
+ | Field | Holds |
162
+ | :--- | :--- |
163
+ | `columns` | Each column's `id`, `order`, `width` and `hidden`. The id is the column's `stateKey`, or its `field` without one. |
164
+ | `sort`, `filter`, `group` | The descriptors, as plain values. |
165
+ | `pageSize` | The page size, unless `persistPageSize` is `false`. |
166
+ | `version` | The library's state format, so an older format is rejected (`"rejected-version"`). |
167
+ | `schemaVersion` | The `schemaVersion` option, so a state saved for an older set of columns is rejected (`"rejected-schema"`). |
168
+
169
+ The page offset, the selection, expanded rows and row order aren't part of the state. Give a column a `stateKey` when its `field` is empty or may change, so its saved width and position still find it. The command column's `stateKey` is `"commands"` by default.
170
+
171
+ ### When the state changes
172
+
173
+ The `state` model updates after each change people make, and a column resize is reported once, when the drag ends. A state bound before the grid renders is applied once the columns exist. Setting `state` later applies it, unless it equals what the grid already shows. With server binding, a restored state triggers one `dataStateChange` at `skip: 0`.
174
+
175
+ ### What the export contains
176
+
177
+ `exportCsv(filename?)` exports the visible columns of every row in the current view: filtered and sorted, all pages. With server binding it exports only the loaded page. Dates are written as the local calendar date (`YYYY-MM-DD`), and text starting with `=`, `+`, `-`, `@`, a tab or a carriage return is prefixed with `'` so spreadsheets don't run it as a formula. The column titles form the header row. Formatters and cell templates aren't used: the file has the raw values.
178
+
179
+ ## Accessibility
180
+
181
+ Neither directive adds roles or keys. Put the export command where people expect it, such as a button in the toolbar, and give it a visible label that says it downloads a file. A restored layout changes the column order and the sort that assistive technology reads, so restore it when the page loads rather than while someone is working in the grid.
182
+
183
+ ### Keyboard
184
+
185
+ Neither directive adds keys. The grid's keys are on [Grid Overview](grid.md#keyboard), and the toolbar's on the same page.
186
+
187
+ ## API
188
+
189
+ Everything below is imported from `@nanahoshi/mona-ui/grid`.
190
+
191
+ ### `GridStatePersistenceDirective`
192
+
193
+ **Selector:** `mona-grid[monaGridStatePersistence]`
194
+ **Exported as:** `monaGridStatePersistence`
195
+
196
+ #### Inputs
197
+
198
+ | Name | Type | Default | Description |
199
+ | --- | --- | --- | --- |
200
+ | `monaGridStatePersistence` | `GridStatePersistenceOptions \| ""` | `""` | State persistence options such as schema version and page size persistence. |
201
+ | `state` (two-way) | `GridState \| null` | `null` | The persisted grid state to apply and update when the grid state changes. |
202
+
203
+ #### Outputs
204
+
205
+ | Name | Type | Description |
206
+ | --- | --- | --- |
207
+ | `stateChange` | `GridState \| null` | Emitted when `state` changes. |
208
+
209
+ #### Methods
210
+
211
+ | Signature | Description |
212
+ | --- | --- |
213
+ | `captureState(): GridState` | Captures the grid's current state (columns, sort, filter, group) without waiting for the next change cycle. |
214
+ | `loadState(state: GridState \| null, options: GridStatePersistenceOptions = this.#resolveOptions()): GridStateLoadResult` | Applies a previously captured state to the grid. A state that isn't well formed (a missing array, a duplicate column id, an unknown filter operator, a page size or width that isn't a positive number) returns `rejected-invalid` and changes nothing. |
215
+
216
+ ### `GridExportDirective`
217
+
218
+ **Selector:** `mona-grid[monaGridExport]`
219
+ **Exported as:** `monaGridExport`
220
+
221
+ #### Methods
222
+
223
+ | Signature | Description |
224
+ | --- | --- |
225
+ | `exportCsv(filename?: string): void` | Exports the visible columns of the current view as CSV (only the loaded page with server binding). |
226
+
227
+ ### Types
228
+
229
+ #### `GridColumnState`
230
+
231
+ | Name | Type |
232
+ | --- | --- |
233
+ | `hidden` | `boolean` |
234
+ | `id` | `string` |
235
+ | `order` | `number` |
236
+ | `width` | `number \| null` |
237
+
238
+ #### `GridState`
239
+
240
+ | Name | Type | Description |
241
+ | --- | --- | --- |
242
+ | `columns` | `readonly GridColumnState[]` | Persisted per-column hidden/order/width state. |
243
+ | `filter` | `readonly GridStateCompositeFilterDescriptor[]` | Persisted filter descriptors. |
244
+ | `group` | `readonly GroupDescriptor[]` | Persisted group descriptors. |
245
+ | `pageSize?` | `number` | Persisted page size, unless `persistPageSize` is `false`. |
246
+ | `schemaVersion?` | `number \| string` | Consumer-defined version tag used to reject stale persisted state. |
247
+ | `sort` | `readonly GridStateSortDescriptor[]` | Persisted sort descriptors. |
248
+ | `version` | `GridStateVersion` | The state schema version produced by this version of the library. |
249
+
250
+ #### `GridStateCompositeFilterDescriptor`
251
+
252
+ | Name | Type |
253
+ | --- | --- |
254
+ | `filters` | `readonly (GridStateFilterDescriptor \| GridStateCompositeFilterDescriptor)[]` |
255
+ | `logic` | `FilterLogic` |
256
+
257
+ #### `GridStateFilterDescriptor`
258
+
259
+ | Name | Type |
260
+ | --- | --- |
261
+ | `field` | `string` |
262
+ | `operator` | `Exclude<FilterOperators, "function">` |
263
+ | `value?` | `GridStateFilterValue` |
264
+
265
+ #### `GridStateLoadResult`
266
+
267
+ | Name | Type |
268
+ | --- | --- |
269
+ | `ignoredColumns` | `readonly string[]` |
270
+ | `missingColumns` | `readonly string[]` |
271
+ | `status` | `GridStateLoadStatus` |
272
+
273
+ #### `GridStatePersistenceOptions`
274
+
275
+ | Name | Type |
276
+ | --- | --- |
277
+ | `enabled?` | `boolean` |
278
+ | `persistPageSize?` | `boolean` |
279
+ | `schemaVersion?` | `number \| string` |
280
+
281
+ #### `GridStateSortDescriptor`
282
+
283
+ | Name | Type |
284
+ | --- | --- |
285
+ | `dir` | `SortDirection` |
286
+ | `field` | `string` |
287
+
288
+ #### `GridStateFilterValue`
289
+
290
+ ```typescript
291
+ type GridStateFilterValue = boolean | number | string | null | readonly (boolean | number | string | null)[];
292
+ ```
293
+
294
+ #### `GridStateLoadStatus`
295
+
296
+ The outcome of loading a state. `rejected-invalid`: the state isn't a well-formed `GridState` (a missing
297
+ array, a duplicate column id, an unknown filter operator, a page size or width that isn't a positive
298
+ number), and nothing was changed.
299
+
300
+ ```typescript
301
+ type GridStateLoadStatus = "applied" | "empty" | "rejected-invalid" | "rejected-schema" | "rejected-version";
302
+ ```
303
+
304
+ #### `GridStateVersion`
305
+
306
+ ```typescript
307
+ type GridStateVersion = 1;
308
+ ```
309
+
310
+ #### `ArrayFilterOperators`
311
+
312
+ From `@nanahoshi/mona-ui/query`.
313
+
314
+ ```typescript
315
+ type ArrayFilterOperators = "in" | "notin";
316
+ ```
317
+
318
+ #### `BooleanFilterOperators`
319
+
320
+ From `@nanahoshi/mona-ui/query`.
321
+
322
+ ```typescript
323
+ type BooleanFilterOperators = "istrue" | "isfalse" | NullFilterOperators;
324
+ ```
325
+
326
+ #### `CommonFilterOperators`
327
+
328
+ From `@nanahoshi/mona-ui/query`.
329
+
330
+ ```typescript
331
+ type CommonFilterOperators = "eq" | "neq";
332
+ ```
333
+
334
+ #### `DateFilterOperators`
335
+
336
+ From `@nanahoshi/mona-ui/query`.
337
+
338
+ ```typescript
339
+ type DateFilterOperators = NumericFilterOperators | NullFilterOperators;
340
+ ```
341
+
342
+ #### `EmptyFilterOperators`
343
+
344
+ From `@nanahoshi/mona-ui/query`.
345
+
346
+ ```typescript
347
+ type EmptyFilterOperators = "isempty" | "isnotempty";
348
+ ```
349
+
350
+ #### `FilterLogic`
351
+
352
+ From `@nanahoshi/mona-ui/query`.
353
+
354
+ ```typescript
355
+ type FilterLogic = "and" | "or";
356
+ ```
357
+
358
+ #### `FilterOperators`
359
+
360
+ From `@nanahoshi/mona-ui/query`.
361
+
362
+ ```typescript
363
+ type FilterOperators = | CommonFilterOperators | BooleanFilterOperators | NumericFilterOperators | StringFilterOperators | DateFilterOperators | NullFilterOperators | EmptyFilterOperators | NullOrEmptyFilterOperators | ArrayFilterOperators | RangeFilterOperators | FunctionFilterOperators;
364
+ ```
365
+
366
+ #### `FunctionFilterOperators`
367
+
368
+ From `@nanahoshi/mona-ui/query`.
369
+
370
+ ```typescript
371
+ type FunctionFilterOperators = "function";
372
+ ```
373
+
374
+ #### `NullFilterOperators`
375
+
376
+ From `@nanahoshi/mona-ui/query`.
377
+
378
+ ```typescript
379
+ type NullFilterOperators = "isnull" | "isnotnull";
380
+ ```
381
+
382
+ #### `NullOrEmptyFilterOperators`
383
+
384
+ From `@nanahoshi/mona-ui/query`.
385
+
386
+ ```typescript
387
+ type NullOrEmptyFilterOperators = "isnullorempty" | "isnotnullorempty";
388
+ ```
389
+
390
+ #### `NumericFilterOperators`
391
+
392
+ From `@nanahoshi/mona-ui/query`.
393
+
394
+ ```typescript
395
+ type NumericFilterOperators = CommonFilterOperators | "gt" | "gte" | "lt" | "lte" | NullFilterOperators;
396
+ ```
397
+
398
+ #### `RangeFilterOperators`
399
+
400
+ From `@nanahoshi/mona-ui/query`.
401
+
402
+ ```typescript
403
+ type RangeFilterOperators = "between" | "notbetween";
404
+ ```
405
+
406
+ #### `SortDirection`
407
+
408
+ From `@nanahoshi/mona-ui/query`.
409
+
410
+ ```typescript
411
+ type SortDirection = "asc" | "desc";
412
+ ```
413
+
414
+ #### `StringFilterOperators`
415
+
416
+ From `@nanahoshi/mona-ui/query`.
417
+
418
+ ```typescript
419
+ type StringFilterOperators = | CommonFilterOperators | "startswith" | "endswith" | "contains" | "doesnotcontain" | NullFilterOperators | EmptyFilterOperators | NullOrEmptyFilterOperators;
420
+ ```
421
+
422
+ **Documented elsewhere:** [`GroupDescriptor`](grid-grouping.md)