tadabut 0.2.0 → 0.4.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/dist/react.js CHANGED
@@ -1,54 +1,42 @@
1
- import { t as Tadabut$1 } from "./src-C4vgw-1w.js";
1
+ import { n as Tadabut$1 } from "./src-DWwW61Yo.js";
2
2
  import { useEffect, useImperativeHandle, useRef } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  // src/react/index.tsx
5
5
  /**
6
- * React adapter for Tadabut.
6
+ * The React component.
7
+ *
8
+ * @packageDocumentation
9
+ * @module tadabut/react
7
10
  *
8
11
  * @example
12
+ * ```tsx
9
13
  * import { Tadabut } from 'tadabut/react';
10
14
  *
11
15
  * <Tadabut data={employees} columns={columns} rowId='id' />
12
- *
13
- * @packageDocumentation
16
+ * ```
14
17
  */
15
18
  /**
16
- * A Tadabut table as a React component.
17
- *
18
- * React only renders an empty `<div>`. The table inside it is created and
19
- * owned by the core, so React never touches it.
20
- *
21
- * - New `data` updates the table in place: sorting, search and the current
22
- * page are kept.
23
- * - New `columns`, `rowId`, `editing.mode`, `editing.button` or `classNames`
24
- * values rebuild the table, and any row being edited is lost. Define
25
- * `columns` outside the component, or with `useMemo`.
26
- * - `editing` and `classNames` can be new objects on every render: only a
27
- * change in their values rebuilds the table.
28
- * - `onSave` can be a new function on every render. The table always calls
29
- * the latest one, without being rebuilt.
30
- *
31
- * @typeParam Row - The shape of one row. Usually worked out from `data`.
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
23
+ * read through a ref.
32
24
  */
33
- function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
34
- /** The empty `<div>` React renders. The core builds the table inside it. */
25
+ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, onAdd, onDelete, confirmDelete, ref }) {
26
+ /** The `<div>` the core builds the table in. */
35
27
  const containerRef = useRef(null);
36
- /**
37
- * The live core table, for `setData` and the ref's methods.
38
- * `null` before the first build and after unmount.
39
- */
28
+ /** The core table. `null` before the first build and after unmount. */
40
29
  const tableRef = useRef(null);
41
- /**
42
- * The latest `data`. When the table is rebuilt, it starts from the current
43
- * rows instead of the ones from the first render. Kept in a ref so `data`
44
- * stays out of the build effect's dependencies.
45
- */
30
+ /** The latest rows, so a rebuild doesn't go back to the first ones. */
46
31
  const dataRef = useRef(data);
47
- /**
48
- * The latest `onSave`. The core gets a function that never changes and
49
- * calls this one, so a new `onSave` doesn't rebuild the table.
50
- */
32
+ /** The latest `onSave`, so a new one doesn't rebuild the table. */
51
33
  const onSaveRef = useRef(onSave);
34
+ /** The latest `onAdd`, so a new one doesn't rebuild the table. */
35
+ const onAddRef = useRef(onAdd);
36
+ /** The latest `onDelete`, so a new one doesn't rebuild the table. */
37
+ const onDeleteRef = useRef(onDelete);
38
+ /** The latest `confirmDelete`, so a new one doesn't rebuild the table. */
39
+ const confirmDeleteRef = useRef(confirmDelete);
52
40
  const mode = editing?.mode;
53
41
  const button = editing?.button;
54
42
  const tableClass = classNames?.table;
@@ -56,9 +44,20 @@ function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
56
44
  const editClass = classNames?.edit;
57
45
  const saveClass = classNames?.save;
58
46
  const cancelClass = classNames?.cancel;
47
+ const deleteClass = classNames?.delete;
48
+ const confirmClass = classNames?.confirm;
49
+ const keepClass = classNames?.keep;
50
+ const hasDelete = onDelete !== void 0;
51
+ const confirmWith = confirmDelete === false ? "never" : confirmDelete === void 0 ? "inline" : "function";
59
52
  useEffect(() => {
60
53
  const container = containerRef.current;
61
54
  if (!container) return;
55
+ let confirm;
56
+ if (confirmWith === "never") confirm = false;
57
+ else if (confirmWith === "function") confirm = (row) => {
58
+ const ask = confirmDeleteRef.current;
59
+ return typeof ask === "function" ? ask(row) : true;
60
+ };
62
61
  const table = new Tadabut$1(container, {
63
62
  data: dataRef.current,
64
63
  columns,
@@ -67,14 +66,22 @@ function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
67
66
  mode,
68
67
  button
69
68
  },
69
+ dataTables,
70
+ texts,
70
71
  classNames: {
71
72
  table: tableClass,
72
73
  button: buttonClass,
73
74
  edit: editClass,
74
75
  save: saveClass,
75
- cancel: cancelClass
76
+ cancel: cancelClass,
77
+ delete: deleteClass,
78
+ confirm: confirmClass,
79
+ keep: keepClass
76
80
  },
77
- onSave: (row, context) => onSaveRef.current?.(row, context)
81
+ onSave: (row, context) => onSaveRef.current?.(row, context),
82
+ onAdd: (row) => onAddRef.current?.(row),
83
+ onDelete: hasDelete ? (row) => onDeleteRef.current?.(row) : void 0,
84
+ confirmDelete: confirm
78
85
  });
79
86
  tableRef.current = table;
80
87
  return () => {
@@ -84,13 +91,20 @@ function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
84
91
  }, [
85
92
  columns,
86
93
  rowId,
94
+ dataTables,
95
+ texts,
87
96
  mode,
88
97
  button,
89
98
  tableClass,
90
99
  buttonClass,
91
100
  editClass,
92
101
  saveClass,
93
- cancelClass
102
+ cancelClass,
103
+ deleteClass,
104
+ confirmClass,
105
+ keepClass,
106
+ hasDelete,
107
+ confirmWith
94
108
  ]);
95
109
  useEffect(() => {
96
110
  dataRef.current = data;
@@ -98,11 +112,19 @@ function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
98
112
  }, [data]);
99
113
  useEffect(() => {
100
114
  onSaveRef.current = onSave;
115
+ onAddRef.current = onAdd;
116
+ onDeleteRef.current = onDelete;
117
+ confirmDeleteRef.current = confirmDelete;
101
118
  });
102
119
  useImperativeHandle(ref, () => ({
103
120
  edit: (id) => tableRef.current?.edit(id) ?? false,
104
121
  save: (id) => tableRef.current?.save(id) ?? Promise.resolve(false),
105
122
  cancel: (id) => tableRef.current?.cancel(id),
123
+ delete: (id) => tableRef.current?.delete(id) ?? Promise.resolve(false),
124
+ add: (defaults) => tableRef.current?.add(defaults) ?? false,
125
+ isAdding: () => tableRef.current?.isAdding() ?? false,
126
+ saveNew: () => tableRef.current?.saveNew() ?? Promise.resolve(false),
127
+ cancelNew: () => tableRef.current?.cancelNew(),
106
128
  isEditing: (id) => tableRef.current?.isEditing(id) ?? false
107
129
  }), []);
108
130
  return /* @__PURE__ */ jsx("div", { ref: containerRef });