tadabut 0.3.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/README.md +203 -35
- package/dist/{index-DYUdG69c.d.ts → index-mZisu7MG.d.ts} +141 -18
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react.d.ts +10 -6
- package/dist/react.js +44 -7
- package/dist/{src-cI0CYV46.js → src-DWwW61Yo.js} +434 -56
- package/package.json +2 -2
package/dist/react.d.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import { c as TadabutOptions, r as Tadabut$1 } from "./index-
|
|
1
|
+
import { c as TadabutOptions, r as Tadabut$1 } from "./index-mZisu7MG.js";
|
|
2
2
|
import { ReactElement, Ref } from "react";
|
|
3
3
|
/** 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" | "isEditing">;
|
|
4
|
+
export type TadabutHandle<Row extends object> = Pick<Tadabut$1<Row>, "edit" | "save" | "cancel" | "delete" | "isEditing" | "add" | "isAdding" | "saveNew" | "cancelNew">;
|
|
5
5
|
/** The core options, plus `ref`. */
|
|
6
6
|
export type TadabutProps<Row extends object> = TadabutOptions<Row> & {
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Gives you the same methods as the core table: `edit`, `save`, `cancel`,
|
|
9
|
+
* `delete`, `add`, `saveNew`, `cancelNew`, `isEditing` and `isAdding`.
|
|
10
|
+
*/
|
|
8
11
|
readonly ref?: Ref<TadabutHandle<Row>>;
|
|
9
12
|
};
|
|
10
13
|
/**
|
|
11
14
|
* New `data` updates the table in place. Changing `columns`, `rowId`,
|
|
12
15
|
* `dataTables` or `texts` rebuilds it (and closes open rows), so keep those
|
|
13
|
-
* stable with a constant or `useMemo`. `editing`, `classNames
|
|
14
|
-
* inline: they're compared by value or
|
|
16
|
+
* stable with a constant or `useMemo`. `editing`, `classNames`, `onSave`,
|
|
17
|
+
* `onAdd`, `onDelete` and `confirmDelete` can be inline: they're compared by value or
|
|
18
|
+
* read through a ref.
|
|
15
19
|
*/
|
|
16
|
-
export declare function Tadabut<Row extends object>({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, ref }: TadabutProps<Row>): ReactElement;
|
|
20
|
+
export declare function Tadabut<Row extends object>({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, onAdd, onDelete, confirmDelete, ref }: TadabutProps<Row>): ReactElement;
|
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as Tadabut$1 } from "./src-
|
|
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
|
|
@@ -18,10 +18,11 @@ import { jsx } from "react/jsx-runtime";
|
|
|
18
18
|
/**
|
|
19
19
|
* New `data` updates the table in place. Changing `columns`, `rowId`,
|
|
20
20
|
* `dataTables` or `texts` rebuilds it (and closes open rows), so keep those
|
|
21
|
-
* stable with a constant or `useMemo`. `editing`, `classNames
|
|
22
|
-
* inline: they're compared by value or
|
|
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.
|
|
23
24
|
*/
|
|
24
|
-
function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, ref }) {
|
|
25
|
+
function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, onSave, onAdd, onDelete, confirmDelete, ref }) {
|
|
25
26
|
/** The `<div>` the core builds the table in. */
|
|
26
27
|
const containerRef = useRef(null);
|
|
27
28
|
/** The core table. `null` before the first build and after unmount. */
|
|
@@ -30,6 +31,12 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
|
|
|
30
31
|
const dataRef = useRef(data);
|
|
31
32
|
/** The latest `onSave`, so a new one doesn't rebuild the table. */
|
|
32
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);
|
|
33
40
|
const mode = editing?.mode;
|
|
34
41
|
const button = editing?.button;
|
|
35
42
|
const tableClass = classNames?.table;
|
|
@@ -37,9 +44,20 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
|
|
|
37
44
|
const editClass = classNames?.edit;
|
|
38
45
|
const saveClass = classNames?.save;
|
|
39
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";
|
|
40
52
|
useEffect(() => {
|
|
41
53
|
const container = containerRef.current;
|
|
42
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
|
+
};
|
|
43
61
|
const table = new Tadabut$1(container, {
|
|
44
62
|
data: dataRef.current,
|
|
45
63
|
columns,
|
|
@@ -55,9 +73,15 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
|
|
|
55
73
|
button: buttonClass,
|
|
56
74
|
edit: editClass,
|
|
57
75
|
save: saveClass,
|
|
58
|
-
cancel: cancelClass
|
|
76
|
+
cancel: cancelClass,
|
|
77
|
+
delete: deleteClass,
|
|
78
|
+
confirm: confirmClass,
|
|
79
|
+
keep: keepClass
|
|
59
80
|
},
|
|
60
|
-
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
|
|
61
85
|
});
|
|
62
86
|
tableRef.current = table;
|
|
63
87
|
return () => {
|
|
@@ -75,7 +99,12 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
|
|
|
75
99
|
buttonClass,
|
|
76
100
|
editClass,
|
|
77
101
|
saveClass,
|
|
78
|
-
cancelClass
|
|
102
|
+
cancelClass,
|
|
103
|
+
deleteClass,
|
|
104
|
+
confirmClass,
|
|
105
|
+
keepClass,
|
|
106
|
+
hasDelete,
|
|
107
|
+
confirmWith
|
|
79
108
|
]);
|
|
80
109
|
useEffect(() => {
|
|
81
110
|
dataRef.current = data;
|
|
@@ -83,11 +112,19 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
|
|
|
83
112
|
}, [data]);
|
|
84
113
|
useEffect(() => {
|
|
85
114
|
onSaveRef.current = onSave;
|
|
115
|
+
onAddRef.current = onAdd;
|
|
116
|
+
onDeleteRef.current = onDelete;
|
|
117
|
+
confirmDeleteRef.current = confirmDelete;
|
|
86
118
|
});
|
|
87
119
|
useImperativeHandle(ref, () => ({
|
|
88
120
|
edit: (id) => tableRef.current?.edit(id) ?? false,
|
|
89
121
|
save: (id) => tableRef.current?.save(id) ?? Promise.resolve(false),
|
|
90
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(),
|
|
91
128
|
isEditing: (id) => tableRef.current?.isEditing(id) ?? false
|
|
92
129
|
}), []);
|
|
93
130
|
return /* @__PURE__ */ jsx("div", { ref: containerRef });
|