tadabut 0.4.0 → 0.6.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.
@@ -26,11 +26,30 @@ type EditorInstance<Value> = {
26
26
  /** Remove the element and tear down any widget you created. */
27
27
  readonly destroy: () => void;
28
28
  };
29
- /** For `boolean` columns. `null` shows unchecked; it never returns `null`. */
30
- type CheckboxEditor = BaseEditor & {
29
+ /** What a checkbox writes, for columns that don't hold `true`/`false`. */
30
+ type CheckboxValues<Value> = {
31
+ /** Written when the box is ticked. */
32
+ readonly checked: Value;
33
+ /** Written when it isn't. */
34
+ readonly unchecked: Value;
35
+ };
36
+ /**
37
+ * For `boolean` columns, or `string` and `number` columns with `values`. A
38
+ * value that's neither stays as it is until the box is clicked.
39
+ */
40
+ type CheckboxEditor<Value = boolean> = BaseEditor & {
31
41
  /** Picks this editor. */
32
42
  readonly type: "checkbox";
33
- };
43
+ } & (0 extends 1 & Value ? {
44
+ /** The column is `any`, so `values` can't be checked. */
45
+ readonly values?: CheckboxValues<Value>;
46
+ } : [Value] extends [boolean] ? {
47
+ /** Only for columns that aren't `true`/`false`. */
48
+ readonly values?: never;
49
+ } : {
50
+ /** What to write instead of `true` and `false`, like `{ checked: 'O', unchecked: 'N' }`. */
51
+ readonly values: CheckboxValues<Value>;
52
+ });
34
53
  /**
35
54
  * Bring your own editor (Select2, a date picker library…). Works on any
36
55
  * column, objects included. `create` runs every time a row is opened; call
@@ -59,21 +78,91 @@ type CustomEditor<Value> = BaseEditor & {
59
78
  /** Builds your editor. Runs every time a row opens. */
60
79
  readonly create: (context: EditorContext<Value>) => EditorInstance<Value>;
61
80
  };
81
+ /** The separators a date format can use. */
82
+ type DateSeparator = "/" | "-" | ".";
83
+ /**
84
+ * How a date is written: `dd`, `mm` and `yyyy` in any order, with `/`, `-`
85
+ * or `.` between them.
86
+ *
87
+ * @example
88
+ * 'dd/mm/yyyy'
89
+ */
90
+ type DateFormat = { [S in DateSeparator]: `dd${S}mm${S}yyyy` | `mm${S}dd${S}yyyy` | `yyyy${S}mm${S}dd` | `yyyy${S}dd${S}mm` | `dd${S}yyyy${S}mm` | `mm${S}yyyy${S}dd`; }[DateSeparator];
91
+ /** ISO, what `<input type="date">` and most servers use. */
92
+ declare const defaultDateFormat: DateFormat;
93
+ /**
94
+ * Reads a date written in `format`, and gives it as `'yyyy-mm-dd'`. A time
95
+ * after it is dropped. `null` if it isn't a real date, like `31/02/2024`.
96
+ *
97
+ * @example
98
+ * parseDate('05/01/2024', 'dd/mm/yyyy'); // '2024-01-05'
99
+ */
100
+ declare function parseDate(value: string, format: DateFormat): string | null;
101
+ /**
102
+ * Writes a `'yyyy-mm-dd'` date in `format`. Only give it dates `parseDate`
103
+ * returned: it checks nothing.
104
+ *
105
+ * @example
106
+ * formatDate('2024-01-05', 'dd.mm.yyyy'); // '05.01.2024'
107
+ */
108
+ declare function formatDate(iso: string, format: DateFormat): string;
62
109
  /**
63
- * For `string` columns holding `'YYYY-MM-DD'`. Anything else, like
64
- * `'2024-01-05T00:00:00'`, shows as empty and saves as `null`. Empty gives
65
- * `null`.
110
+ * For `string` columns holding a date written in `format`. A time after the
111
+ * date is dropped. Anything else shows as empty and saves as `null`. Empty
112
+ * gives `null`.
66
113
  *
67
114
  * @example
68
- * { type: 'date', min: '2020-01-01', max: '2030-12-31' }
115
+ * { type: 'date', format: 'dd/mm/yyyy', min: '01/01/2020' }
69
116
  */
70
117
  type DateEditor = BaseEditor & {
71
118
  /** Picks this editor. */
72
119
  readonly type: "date";
73
- /** Earliest date, as `'YYYY-MM-DD'`. */
120
+ /** Earliest date, written in `format`. */
74
121
  readonly min?: string;
75
- /** Latest date, as `'YYYY-MM-DD'`. */
122
+ /** Latest date, written in `format`. */
76
123
  readonly max?: string;
124
+ /**
125
+ * How the date is written in your rows: what the server sends and what
126
+ * `onSave` gets. Defaults to `'yyyy-mm-dd'`.
127
+ */
128
+ readonly format?: DateFormat;
129
+ /** How the cell shows it. Defaults to `format`. */
130
+ readonly display?: DateFormat;
131
+ };
132
+ /** How a money input and its cells look. */
133
+ type MoneyFormat = {
134
+ /** Digits after the point. */
135
+ readonly decimals: number;
136
+ /** Between groups of thousands. */
137
+ readonly separator: string;
138
+ /** The decimal point that's shown. */
139
+ readonly point: string;
140
+ };
141
+ /** `12 500.00`. */
142
+ declare const defaultMoneyFormat: MoneyFormat;
143
+ /** `12500` → `'12 500.00'`, `null` → `''`. */
144
+ declare function formatMoney(value: number | null, format: MoneyFormat): string;
145
+ /**
146
+ * For `number` columns that hold money. Shows `12 500.00` while typing and in
147
+ * the cells. The row keeps a plain number.
148
+ *
149
+ * @example
150
+ * { type: 'money', min: 0 }
151
+ * { type: 'money', separator: '.', point: ',' } // 12.500,00
152
+ */
153
+ type MoneyEditor = BaseEditor & {
154
+ /** Picks this editor. */
155
+ readonly type: "money";
156
+ /** Smallest value allowed. */
157
+ readonly min?: number;
158
+ /** Largest value allowed. */
159
+ readonly max?: number;
160
+ /** Digits after the point. Defaults to `2`. */
161
+ readonly decimals?: number;
162
+ /** Between thousands. Defaults to a space. */
163
+ readonly separator?: string;
164
+ /** The decimal point shown. Defaults to `'.'`. Typing `.` or `,` both work. */
165
+ readonly point?: string;
77
166
  };
78
167
  /**
79
168
  * For `number` columns. Empty gives `null`, not `0`. The options work like
@@ -116,11 +205,17 @@ type Texts = {
116
205
  readonly keep: string;
117
206
  /** Under a row when `onDelete` throws something that isn't an `Error`. */
118
207
  readonly deleteFailed: string;
208
+ /** In the footer's first cell, when that column has no total of its own. */
209
+ readonly total: string;
210
+ /** The label of each row's checkbox, for screen readers. */
211
+ readonly selectRow: string;
212
+ /** The label of the header checkbox. */
213
+ readonly selectAll: string;
119
214
  /** Shown in a select while its options load. */
120
215
  readonly loading: string;
121
216
  /** Under a row when `onSave` throws something that isn't an `Error`. */
122
217
  readonly saveFailed: string;
123
- /** Under a new row when `onAdd` throws something that isn’t an `Error`. */
218
+ /** Under a new row when `onAdd` throws something that isn't an `Error`. */
124
219
  readonly addFailed: string;
125
220
  /** Under a select whose options failed to load. */
126
221
  readonly loadFailed: string;
@@ -216,18 +311,18 @@ type TextEditor = BaseEditor & {
216
311
  /**
217
312
  * Which editors a column can have, from its value type:
218
313
  *
219
- * | Column value | Built-in editors |
220
- * | ------------- | ------------------ |
221
- * | `string` | text, date, select |
222
- * | `number` | number, select |
223
- * | `boolean` | checkbox, select |
314
+ * | Column value | Built-in editors |
315
+ * | ------------- | --------------------------------------------- |
316
+ * | `string` | text, date, select, checkbox with `values` |
317
+ * | `number` | number, money, select, checkbox with `values` |
318
+ * | `boolean` | checkbox, select |
224
319
  *
225
320
  * `custom` works on anything. Nullable columns get the same editors as
226
321
  * non-nullable ones.
227
322
  */
228
323
  type EditorFor<Value, Row> = EditorForValue<NonNullable<Value>, Row> | CustomEditor<Value>;
229
324
  /** The built-in editors for a non-null value type. */
230
- type EditorForValue<Value, Row> = [Value] extends [string] ? TextEditor | DateEditor | SelectEditor<Value, Row> : [Value] extends [number] ? NumberEditor | SelectEditor<Value, Row> : [Value] extends [boolean] ? CheckboxEditor | SelectEditor<Value, Row> : never;
325
+ type EditorForValue<Value, Row> = [Value] extends [string] ? TextEditor | DateEditor | SelectEditor<Value, Row> | CheckboxEditor<Value> : [Value] extends [number] ? NumberEditor | MoneyEditor | SelectEditor<Value, Row> | CheckboxEditor<Value> : [Value] extends [boolean] ? CheckboxEditor | SelectEditor<Value, Row> : never;
231
326
  /**
232
327
  * Works on every editor. An empty value fails `required` and skips every
233
328
  * other check. Otherwise the editor's own rules (`min`, `maxLength`…) run,
@@ -254,6 +349,21 @@ type ValidationOptions<Row, Value> = {
254
349
  */
255
350
  readonly validate?: (value: Value | null, row: Row) => string | undefined;
256
351
  };
352
+ /** The totals that need numbers. `null` and empty cells are skipped. */
353
+ type NumberTotal = "sum" | "avg" | "min" | "max";
354
+ /**
355
+ * Gets the counted rows and returns the text to show. It's shown as text,
356
+ * not HTML.
357
+ *
358
+ * @example
359
+ * total: (rows) => `${rows.filter((row) => row.active).length} active`
360
+ */
361
+ type TotalFunction<Row extends object> = (rows: readonly Row[]) => string;
362
+ /**
363
+ * What a column's `total` can be: `sum`, `avg`, `min` and `max` only on
364
+ * number columns. `count` and a function work everywhere.
365
+ */
366
+ type TotalFor<Value, Row extends object> = [NonNullable<Value>] extends [number] ? NumberTotal | "count" | TotalFunction<Row> : "count" | TotalFunction<Row>;
257
367
  /**
258
368
  * A table column. The `editor` has to fit the type of `prop`, so a number
259
369
  * field can't get a text input. Columns without an editor are read-only.
@@ -275,12 +385,21 @@ type ColumnFor<Row extends object, Key extends keyof Row & string> = {
275
385
  readonly editor?: EditorFor<Row[Key], Row> & ValidationOptions<Row, Row[Key]>;
276
386
  /**
277
387
  * The cell's text when the row isn't open. It's shown as text, not HTML.
278
- * Search uses it; sorting still uses the raw value.
388
+ * Search uses it and the raw value; sorting uses the raw value. Totals go
389
+ * through it too.
279
390
  *
280
391
  * @example
281
392
  * render: (value) => `${value} MAD`
282
393
  */
283
394
  readonly render?: (value: Row[Key], row: Row) => string;
395
+ /**
396
+ * Adds a footer line with this column's total. It's formatted like the
397
+ * cells: a money column shows `12 500.00`, and a `render` is used too.
398
+ *
399
+ * @example
400
+ * total: 'sum'
401
+ */
402
+ readonly total?: TotalFor<Row[Key], Row>;
284
403
  /** Like `'60px'` or `'20%'`. */
285
404
  readonly width?: string;
286
405
  /** Hidden columns still keep their value in the row, but can't be edited. */
@@ -292,6 +411,11 @@ type ColumnFor<Row extends object, Key extends keyof Row & string> = {
292
411
  /** Classes for the cells (header too), not for the input. */
293
412
  readonly className?: string;
294
413
  };
414
+ /**
415
+ * Which rows something counts: every row matching the search (on every
416
+ * page), every row, or the rows on the page shown.
417
+ */
418
+ type RowScope = "searched" | "all" | "page";
295
419
  /** The second argument of `onSave`. */
296
420
  type SaveContext<Row extends object> = {
297
421
  /** The row as it was before editing. */
@@ -310,6 +434,11 @@ type EditingOptions = {
310
434
  * shows if `onDelete` is set.
311
435
  */
312
436
  readonly button?: boolean;
437
+ /**
438
+ * Double-clicking a row opens it for editing. Defaults to `false`.
439
+ * `onRowDoubleClick` still fires.
440
+ */
441
+ readonly doubleClick?: boolean;
313
442
  };
314
443
  /**
315
444
  * Extra CSS classes for the table and its buttons. They're added next to the
@@ -326,6 +455,7 @@ type EditingOptions = {
326
455
  * delete: 'btn-outline-danger',
327
456
  * confirm: 'btn-danger',
328
457
  * keep: 'btn-outline-secondary',
458
+ * select: 'form-check-input',
329
459
  * }
330
460
  */
331
461
  type TableClassNames = {
@@ -345,6 +475,8 @@ type TableClassNames = {
345
475
  readonly confirm?: string;
346
476
  /** The button that keeps the row instead. */
347
477
  readonly keep?: string;
478
+ /** The selection checkboxes, header included. */
479
+ readonly select?: string;
348
480
  };
349
481
  /**
350
482
  * Any {@link https://datatables.net/reference/option/ | DataTables option},
@@ -356,6 +488,14 @@ type TableClassNames = {
356
488
  * { order: [[1, 'asc']], pageLength: 20, language: { search: 'Rechercher :' } }
357
489
  */
358
490
  type DataTablesSettings = Omit<Config, "data" | "columns" | "columnDefs" | "ajax" | "serverSide">;
491
+ /** Options for `selection`. */
492
+ type SelectionOptions = {
493
+ /**
494
+ * Which rows the header checkbox selects. Defaults to `'searched'`: every
495
+ * row that matches the search, on every page.
496
+ */
497
+ readonly checkAll?: RowScope;
498
+ };
359
499
  /** Options for `new Tadabut()`. */
360
500
  type TadabutOptions<Row extends object> = {
361
501
  /** The rows. The array is copied, never changed. */
@@ -380,6 +520,27 @@ type TadabutOptions<Row extends object> = {
380
520
  * texts: { ...fr, save: 'Valider' }
381
521
  */
382
522
  readonly texts?: Partial<Texts>;
523
+ /**
524
+ * Which rows the totals count: `'searched'` (default) is every row that
525
+ * matches the search, on every page. `'all'` ignores the search. `'page'`
526
+ * is only the rows shown.
527
+ *
528
+ * Does nothing unless a column has `total`.
529
+ */
530
+ readonly totalRows?: RowScope;
531
+ /**
532
+ * Adds a first column with a checkbox on each row, and one in the header to
533
+ * check them all. `true` uses the defaults. Needs `rowId`.
534
+ *
535
+ * @example
536
+ * selection: { checkAll: 'page' }
537
+ */
538
+ readonly selection?: boolean | SelectionOptions;
539
+ /**
540
+ * Called with the selected rows each time the selection changes, by a
541
+ * click or from code.
542
+ */
543
+ readonly onSelect?: (rows: readonly Row[]) => void;
383
544
  /**
384
545
  * Called with the edited row once it passes validation. May be async.
385
546
  *
@@ -425,6 +586,37 @@ type TadabutOptions<Row extends object> = {
425
586
  * confirmDelete: (row) => window.confirm(`Delete ${row.name}?`)
426
587
  */
427
588
  readonly confirmDelete?: false | ((row: Row) => boolean | Promise<boolean>);
589
+ /**
590
+ * Called when a row opens for editing: by the Edit button, a double click,
591
+ * or `edit()`. Return `false` to keep it closed; `edit()` then returns
592
+ * `false`. Not called for the new row `add()` opens.
593
+ *
594
+ * @example
595
+ * onEditStart: (row) => row.status !== 'validated'
596
+ */
597
+ readonly onEditStart?: (row: Row) => boolean | void;
598
+ /**
599
+ * Called after a save worked and the row closed, with the row as it is now
600
+ * in the table (what `onSave` returned, if anything) and the row before
601
+ * editing.
602
+ */
603
+ readonly onSaved?: (row: Row, context: SaveContext<Row>) => void;
604
+ /** Called after a new row joined the table, with the row as saved. */
605
+ readonly onAdded?: (row: Row) => void;
606
+ /** Called after a row left the table, with that row. */
607
+ readonly onDeleted?: (row: Row) => void;
608
+ /**
609
+ * Called when a row is clicked. Clicks on buttons, inputs and checkboxes
610
+ * don't count, and neither do clicks in a row being edited, the new row,
611
+ * or an error line.
612
+ */
613
+ readonly onRowClick?: (row: Row, event: MouseEvent) => void;
614
+ /**
615
+ * Called when a row is double-clicked, with the same rules as
616
+ * `onRowClick`. The browser fires two clicks first, so `onRowClick` runs
617
+ * twice before it.
618
+ */
619
+ readonly onRowDoubleClick?: (row: Row, event: MouseEvent) => void;
428
620
  };
429
621
  /**
430
622
  * A DataTables table with row editing.
@@ -447,7 +639,7 @@ declare class Tadabut<Row extends object> {
447
639
  /** The `<table>` Tadabut created. */
448
640
  private readonly tableEl;
449
641
  /** The field that identifies a row. */
450
- private readonly rowId;
642
+ private readonly rowId?;
451
643
  /** The user's columns, without the actions column. */
452
644
  private readonly columns;
453
645
  /** `editing` with its defaults filled in. */
@@ -473,14 +665,37 @@ declare class Tadabut<Row extends object> {
473
665
  /** Rows waiting for `onDelete`, by row id. */
474
666
  private readonly deleting;
475
667
  /** The row `add()` opened, if any. */
476
- private newRow;
668
+ private newRow?;
669
+ /** Whether any column has a `total`, so the table has a footer. */
670
+ private readonly hasTotals;
671
+ /** Which rows the totals count, with its default filled in. */
672
+ private readonly totalRows;
673
+ /** How many columns Tadabut adds before yours: 1 with `selection`. */
674
+ private readonly offset;
675
+ /** The ticked rows. Only with `selection`. */
676
+ private readonly selection?;
677
+ /** Asked before a row opens. */
678
+ private readonly onEditStart;
679
+ /** The user's after-save callback. */
680
+ private readonly onSaved;
681
+ /** The user's after-add callback. */
682
+ private readonly onAdded;
683
+ /** The user's after-delete callback. */
684
+ private readonly onDeleted;
685
+ /** The user's row click callback. */
686
+ private readonly onRowClick;
687
+ /** The user's row double click callback. */
688
+ private readonly onRowDoubleClick;
477
689
  /**
478
690
  * Builds the table inside `container`, which should be empty.
479
691
  *
480
692
  * @throws If a column has an `editor` and there's no `rowId`.
481
693
  * @throws If `onDelete` is set and there's no `rowId`.
694
+ * @throws If `selection` is on and there's no `rowId`.
482
695
  * @throws If a hidden column (`visible: false`) has an `editor`.
483
696
  * @throws If a select's `labelProp` is its own column.
697
+ * @throws If a money editor's `separator` and `point` are the same.
698
+ * @throws If a date editor's `min` or `max` isn't a real date in its `format`.
484
699
  */
485
700
  constructor(container: HTMLElement, options: TadabutOptions<Row>);
486
701
  /**
@@ -496,7 +711,8 @@ declare class Tadabut<Row extends object> {
496
711
  * @param id The value of the row's `rowId` field.
497
712
  * @returns `false` if the row doesn't exist, is already open, another row
498
713
  * or the new row is open in `'single'` mode, nothing is editable, the row
499
- * is being deleted, or it's on a page that hasn't been displayed yet.
714
+ * is being deleted, it's on a page that hasn't been displayed yet, or
715
+ * `onEditStart` returned `false`.
500
716
  */
501
717
  edit(id: string | number): boolean;
502
718
  /**
@@ -512,6 +728,25 @@ declare class Tadabut<Row extends object> {
512
728
  add(defaults?: Partial<Row>): boolean;
513
729
  /** Whether a new row is open. */
514
730
  isAdding(): boolean;
731
+ /**
732
+ * The selected rows, in the order they were added. Rows the search hides,
733
+ * or on other pages, are in it too. Always `[]` without `selection`.
734
+ */
735
+ getSelected(): Row[];
736
+ /** Whether a row is selected. Always `false` without `selection`. */
737
+ isSelected(id: string | number): boolean;
738
+ /**
739
+ * Selects rows by id. Ids not in the table are skipped. Does nothing
740
+ * without `selection`.
741
+ *
742
+ * @example
743
+ * table.select([1, 2]);
744
+ */
745
+ select(ids: readonly (string | number)[]): void;
746
+ /** Unselects rows by id. Does nothing without `selection`. */
747
+ deselect(ids: readonly (string | number)[]): void;
748
+ /** Unselects every row. Does nothing without `selection`. */
749
+ clearSelection(): void;
515
750
  /**
516
751
  * Closes the new row and throws it away. Does nothing if there's none, or
517
752
  * while it's saving.
@@ -597,9 +832,27 @@ declare class Tadabut<Row extends object> {
597
832
  private readonly onClick;
598
833
  /** Enter saves, Escape cancels, in edited rows and the new row. */
599
834
  private readonly onKeyDown;
835
+ /**
836
+ * Writes every total. Runs after each redraw: search, paging, save, add,
837
+ * delete and `setData` all redraw.
838
+ */
839
+ private readonly updateTotals;
840
+ /** Your column's index as DataTables counts it. */
841
+ private dtIndex;
842
+ /**
843
+ * The row a click landed on, or `undefined` if it doesn't count: buttons,
844
+ * inputs, links, the checkbox and buttons cells, the new row, a row being
845
+ * edited, the header, the footer and error lines.
846
+ */
847
+ private clickedRow;
848
+ /**
849
+ * Row clicks and double clicks. A double click also opens the row with
850
+ * `editing.doubleClick`. An arrow function, so `destroy` can remove it.
851
+ */
852
+ private readonly onRowEvent;
600
853
  }
601
854
  /** English, the default. */
602
855
  declare const en: Texts;
603
856
  /** French. */
604
857
  declare const fr: Texts;
605
- export { EditorInstance as C, EditorContext as S, Texts as _, EditingOptions as a, CustomEditor as b, TadabutOptions as c, EditorFor as d, TextEditor as f, SelectOptionsLoader as g, SelectOption as h, DataTablesSettings as i, Column as l, SelectEditor as m, en as n, SaveContext as o, OptionsContext as p, Tadabut as r, TableClassNames as s, fr as t, ValidationOptions as u, NumberEditor as v, CheckboxEditor as x, DateEditor as y };
858
+ export { parseDate as A, MoneyFormat as C, DateFormat as D, DateEditor as E, CheckboxEditor as M, EditorContext as N, defaultDateFormat as O, EditorInstance as P, MoneyEditor as S, formatMoney as T, SelectEditor as _, EditingOptions as a, Texts as b, TableClassNames as c, Column as d, TotalFunction as f, OptionsContext as g, TextEditor as h, DataTablesSettings as i, CustomEditor as j, formatDate as k, TadabutOptions as l, EditorFor as m, en as n, SaveContext as o, ValidationOptions as p, Tadabut as r, SelectionOptions as s, fr as t, RowScope as u, SelectOption as v, defaultMoneyFormat as w, NumberEditor as x, SelectOptionsLoader as y };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,8 @@
1
- import { C as EditorInstance, S as EditorContext, _ as Texts, a as EditingOptions, b as CustomEditor, c as TadabutOptions, d as EditorFor, f as TextEditor, g as SelectOptionsLoader, h as SelectOption, i as DataTablesSettings, l as Column, m as SelectEditor, n as en, o as SaveContext, p as OptionsContext, r as Tadabut, s as TableClassNames, t as fr, u as ValidationOptions, v as NumberEditor, x as CheckboxEditor, y as DateEditor } from "./index-mZisu7MG.js";
2
- export { type CheckboxEditor, type Column, type CustomEditor, type DataTablesSettings, type DateEditor, type EditingOptions, type EditorContext, type EditorFor, type EditorInstance, type NumberEditor, type OptionsContext, type SaveContext, type SelectEditor, type SelectOption, type SelectOptionsLoader, type TableClassNames, Tadabut, type TadabutOptions, type TextEditor, type Texts, type ValidationOptions, en, fr };
1
+ /**
2
+ * Editable, typed DataTables. The React component lives in `tadabut/react`.
3
+ *
4
+ * @packageDocumentation
5
+ * @module tadabut
6
+ */
7
+ import { A as parseDate, C as MoneyFormat, D as DateFormat, E as DateEditor, M as CheckboxEditor, N as EditorContext, O as defaultDateFormat, P as EditorInstance, S as MoneyEditor, T as formatMoney, _ as SelectEditor, a as EditingOptions, b as Texts, c as TableClassNames, d as Column, f as TotalFunction, g as OptionsContext, h as TextEditor, i as DataTablesSettings, j as CustomEditor, k as formatDate, l as TadabutOptions, m as EditorFor, n as en, o as SaveContext, p as ValidationOptions, r as Tadabut, s as SelectionOptions, t as fr, u as RowScope, v as SelectOption, w as defaultMoneyFormat, x as NumberEditor, y as SelectOptionsLoader } from "./index-C_77p8Jv.js";
8
+ export { type CheckboxEditor, type Column, type CustomEditor, type DataTablesSettings, type DateEditor, type DateFormat, type EditingOptions, type EditorContext, type EditorFor, type EditorInstance, type MoneyEditor, type MoneyFormat, type NumberEditor, type OptionsContext, type RowScope, type SaveContext, type SelectEditor, type SelectOption, type SelectOptionsLoader, type SelectionOptions, type TableClassNames, Tadabut, type TadabutOptions, type TextEditor, type Texts, type TotalFunction, type ValidationOptions, defaultDateFormat, defaultMoneyFormat, en, formatDate, formatMoney, fr, parseDate };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { n as Tadabut, r as en, t as fr } from "./src-DWwW61Yo.js";
1
+ export { n as Tadabut, o as defaultDateFormat, i as defaultMoneyFormat, r as en, s as formatDate, a as formatMoney, t as fr, c as parseDate } from "./src-BxdYoViF.js";
package/dist/react.d.ts CHANGED
@@ -1,20 +1,35 @@
1
- import { c as TadabutOptions, r as Tadabut$1 } from "./index-mZisu7MG.js";
1
+ /**
2
+ * The React component.
3
+ *
4
+ * @packageDocumentation
5
+ * @module tadabut/react
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { Tadabut } from 'tadabut/react';
10
+ *
11
+ * <Tadabut data={employees} columns={columns} rowId='id' />
12
+ * ```
13
+ */
14
+ import { l as TadabutOptions, r as Tadabut$1 } from "./index-C_77p8Jv.js";
2
15
  import { ReactElement, Ref } from "react";
3
16
  /** What the `ref` gets. No `setData`: change the `data` prop instead. */
4
- export type TadabutHandle<Row extends object> = Pick<Tadabut$1<Row>, "edit" | "save" | "cancel" | "delete" | "isEditing" | "add" | "isAdding" | "saveNew" | "cancelNew">;
17
+ export type TadabutHandle<Row extends object> = Pick<Tadabut$1<Row>, "edit" | "save" | "cancel" | "delete" | "isEditing" | "add" | "isAdding" | "saveNew" | "cancelNew" | "getSelected" | "isSelected" | "select" | "deselect" | "clearSelection">;
5
18
  /** The core options, plus `ref`. */
6
19
  export type TadabutProps<Row extends object> = TadabutOptions<Row> & {
7
20
  /**
8
21
  * Gives you the same methods as the core table: `edit`, `save`, `cancel`,
9
- * `delete`, `add`, `saveNew`, `cancelNew`, `isEditing` and `isAdding`.
22
+ * `delete`, `add`, `saveNew`, `cancelNew`, `isEditing`, `isAdding`,
23
+ * `getSelected`, `isSelected`, `select`, `deselect` and `clearSelection`.
10
24
  */
11
25
  readonly ref?: Ref<TadabutHandle<Row>>;
12
26
  };
13
27
  /**
14
28
  * New `data` updates the table in place. Changing `columns`, `rowId`,
15
- * `dataTables` or `texts` rebuilds it (and closes open rows), so keep those
16
- * stable with a constant or `useMemo`. `editing`, `classNames`, `onSave`,
17
- * `onAdd`, `onDelete` and `confirmDelete` can be inline: they're compared by value or
29
+ * `dataTables`, `texts`, `totalRows` or `selection` rebuilds it (and closes
30
+ * open rows, and clears the selection), so keep the objects stable with a
31
+ * constant or `useMemo`. `editing`, `selection`, `classNames` and every `on…`
32
+ * callback (and `confirmDelete`) can be inline: they're compared by value or
18
33
  * read through a ref.
19
34
  */
20
- export declare function Tadabut<Row extends object>({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, onAdd, onDelete, confirmDelete, ref }: TadabutProps<Row>): ReactElement;
35
+ export declare function Tadabut<Row extends object>({ data, columns, rowId, editing, classNames, dataTables, texts, totalRows, selection, onSelect, onSave, onAdd, onDelete, confirmDelete, onEditStart, onSaved, onAdded, onDeleted, onRowClick, onRowDoubleClick, ref }: TadabutProps<Row>): ReactElement;
package/dist/react.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as Tadabut$1 } from "./src-DWwW61Yo.js";
1
+ import { n as Tadabut$1 } from "./src-BxdYoViF.js";
2
2
  import { useEffect, useImperativeHandle, useRef } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  // src/react/index.tsx
@@ -17,12 +17,13 @@ import { jsx } from "react/jsx-runtime";
17
17
  */
18
18
  /**
19
19
  * New `data` updates the table in place. Changing `columns`, `rowId`,
20
- * `dataTables` or `texts` rebuilds it (and closes open rows), so keep those
21
- * stable with a constant or `useMemo`. `editing`, `classNames`, `onSave`,
22
- * `onAdd`, `onDelete` and `confirmDelete` can be inline: they're compared by value or
20
+ * `dataTables`, `texts`, `totalRows` or `selection` rebuilds it (and closes
21
+ * open rows, and clears the selection), so keep the objects stable with a
22
+ * constant or `useMemo`. `editing`, `selection`, `classNames` and every `on…`
23
+ * callback (and `confirmDelete`) can be inline: they're compared by value or
23
24
  * read through a ref.
24
25
  */
25
- function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, onAdd, onDelete, confirmDelete, ref }) {
26
+ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, totalRows, selection, onSelect, onSave, onAdd, onDelete, confirmDelete, onEditStart, onSaved, onAdded, onDeleted, onRowClick, onRowDoubleClick, ref }) {
26
27
  /** The `<div>` the core builds the table in. */
27
28
  const containerRef = useRef(null);
28
29
  /** The core table. `null` before the first build and after unmount. */
@@ -37,8 +38,23 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
37
38
  const onDeleteRef = useRef(onDelete);
38
39
  /** The latest `confirmDelete`, so a new one doesn't rebuild the table. */
39
40
  const confirmDeleteRef = useRef(confirmDelete);
41
+ /** The latest `onSelect`, so a new one doesn't rebuild the table. */
42
+ const onSelectRef = useRef(onSelect);
43
+ /** The latest `onEditStart`, so a new one doesn't rebuild the table. */
44
+ const onEditStartRef = useRef(onEditStart);
45
+ /** The latest `onSaved`, so a new one doesn't rebuild the table. */
46
+ const onSavedRef = useRef(onSaved);
47
+ /** The latest `onAdded`, so a new one doesn't rebuild the table. */
48
+ const onAddedRef = useRef(onAdded);
49
+ /** The latest `onDeleted`, so a new one doesn't rebuild the table. */
50
+ const onDeletedRef = useRef(onDeleted);
51
+ /** The latest `onRowClick`, so a new one doesn't rebuild the table. */
52
+ const onRowClickRef = useRef(onRowClick);
53
+ /** The latest `onRowDoubleClick`, so a new one doesn't rebuild the table. */
54
+ const onRowDoubleClickRef = useRef(onRowDoubleClick);
40
55
  const mode = editing?.mode;
41
56
  const button = editing?.button;
57
+ const doubleClick = editing?.doubleClick;
42
58
  const tableClass = classNames?.table;
43
59
  const buttonClass = classNames?.button;
44
60
  const editClass = classNames?.edit;
@@ -47,6 +63,9 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
47
63
  const deleteClass = classNames?.delete;
48
64
  const confirmClass = classNames?.confirm;
49
65
  const keepClass = classNames?.keep;
66
+ const selectClass = classNames?.select;
67
+ const selectionOn = Boolean(selection);
68
+ const checkAll = typeof selection === "object" ? selection.checkAll : void 0;
50
69
  const hasDelete = onDelete !== void 0;
51
70
  const confirmWith = confirmDelete === false ? "never" : confirmDelete === void 0 ? "inline" : "function";
52
71
  useEffect(() => {
@@ -64,10 +83,13 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
64
83
  rowId,
65
84
  editing: {
66
85
  mode,
67
- button
86
+ button,
87
+ doubleClick
68
88
  },
69
89
  dataTables,
70
90
  texts,
91
+ totalRows,
92
+ selection: selectionOn && { checkAll },
71
93
  classNames: {
72
94
  table: tableClass,
73
95
  button: buttonClass,
@@ -76,12 +98,20 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
76
98
  cancel: cancelClass,
77
99
  delete: deleteClass,
78
100
  confirm: confirmClass,
79
- keep: keepClass
101
+ keep: keepClass,
102
+ select: selectClass
80
103
  },
81
104
  onSave: (row, context) => onSaveRef.current?.(row, context),
82
105
  onAdd: (row) => onAddRef.current?.(row),
83
106
  onDelete: hasDelete ? (row) => onDeleteRef.current?.(row) : void 0,
84
- confirmDelete: confirm
107
+ confirmDelete: confirm,
108
+ onSelect: (rows) => onSelectRef.current?.(rows),
109
+ onEditStart: (row) => onEditStartRef.current?.(row),
110
+ onSaved: (row, context) => onSavedRef.current?.(row, context),
111
+ onAdded: (row) => onAddedRef.current?.(row),
112
+ onDeleted: (row) => onDeletedRef.current?.(row),
113
+ onRowClick: (row, event) => onRowClickRef.current?.(row, event),
114
+ onRowDoubleClick: (row, event) => onRowDoubleClickRef.current?.(row, event)
85
115
  });
86
116
  tableRef.current = table;
87
117
  return () => {
@@ -93,8 +123,10 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
93
123
  rowId,
94
124
  dataTables,
95
125
  texts,
126
+ totalRows,
96
127
  mode,
97
128
  button,
129
+ doubleClick,
98
130
  tableClass,
99
131
  buttonClass,
100
132
  editClass,
@@ -103,6 +135,9 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
103
135
  deleteClass,
104
136
  confirmClass,
105
137
  keepClass,
138
+ selectClass,
139
+ selectionOn,
140
+ checkAll,
106
141
  hasDelete,
107
142
  confirmWith
108
143
  ]);
@@ -115,6 +150,13 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
115
150
  onAddRef.current = onAdd;
116
151
  onDeleteRef.current = onDelete;
117
152
  confirmDeleteRef.current = confirmDelete;
153
+ onSelectRef.current = onSelect;
154
+ onEditStartRef.current = onEditStart;
155
+ onSavedRef.current = onSaved;
156
+ onAddedRef.current = onAdded;
157
+ onDeletedRef.current = onDeleted;
158
+ onRowClickRef.current = onRowClick;
159
+ onRowDoubleClickRef.current = onRowDoubleClick;
118
160
  });
119
161
  useImperativeHandle(ref, () => ({
120
162
  edit: (id) => tableRef.current?.edit(id) ?? false,
@@ -125,7 +167,12 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
125
167
  isAdding: () => tableRef.current?.isAdding() ?? false,
126
168
  saveNew: () => tableRef.current?.saveNew() ?? Promise.resolve(false),
127
169
  cancelNew: () => tableRef.current?.cancelNew(),
128
- isEditing: (id) => tableRef.current?.isEditing(id) ?? false
170
+ isEditing: (id) => tableRef.current?.isEditing(id) ?? false,
171
+ getSelected: () => tableRef.current?.getSelected() ?? [],
172
+ isSelected: (id) => tableRef.current?.isSelected(id) ?? false,
173
+ select: (ids) => tableRef.current?.select(ids),
174
+ deselect: (ids) => tableRef.current?.deselect(ids),
175
+ clearSelection: () => tableRef.current?.clearSelection()
129
176
  }), []);
130
177
  return /* @__PURE__ */ jsx("div", { ref: containerRef });
131
178
  }