tadabut 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +671 -2
- package/dist/index-D2g9_Fd9.d.ts +603 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/react.d.ts +39 -0
- package/dist/react.js +110 -0
- package/dist/src-C4vgw-1w.js +982 -0
- package/package.json +77 -3
package/dist/react.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { t as Tadabut$1 } from "./src-C4vgw-1w.js";
|
|
2
|
+
import { useEffect, useImperativeHandle, useRef } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/react/index.tsx
|
|
5
|
+
/**
|
|
6
|
+
* React adapter for Tadabut.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { Tadabut } from 'tadabut/react';
|
|
10
|
+
*
|
|
11
|
+
* <Tadabut data={employees} columns={columns} rowId='id' />
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
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`.
|
|
32
|
+
*/
|
|
33
|
+
function Tadabut({ data, columns, rowId, editing, classNames, onSave, ref }) {
|
|
34
|
+
/** The empty `<div>` React renders. The core builds the table inside it. */
|
|
35
|
+
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
|
+
*/
|
|
40
|
+
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
|
+
*/
|
|
46
|
+
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
|
+
*/
|
|
51
|
+
const onSaveRef = useRef(onSave);
|
|
52
|
+
const mode = editing?.mode;
|
|
53
|
+
const button = editing?.button;
|
|
54
|
+
const tableClass = classNames?.table;
|
|
55
|
+
const buttonClass = classNames?.button;
|
|
56
|
+
const editClass = classNames?.edit;
|
|
57
|
+
const saveClass = classNames?.save;
|
|
58
|
+
const cancelClass = classNames?.cancel;
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const container = containerRef.current;
|
|
61
|
+
if (!container) return;
|
|
62
|
+
const table = new Tadabut$1(container, {
|
|
63
|
+
data: dataRef.current,
|
|
64
|
+
columns,
|
|
65
|
+
rowId,
|
|
66
|
+
editing: {
|
|
67
|
+
mode,
|
|
68
|
+
button
|
|
69
|
+
},
|
|
70
|
+
classNames: {
|
|
71
|
+
table: tableClass,
|
|
72
|
+
button: buttonClass,
|
|
73
|
+
edit: editClass,
|
|
74
|
+
save: saveClass,
|
|
75
|
+
cancel: cancelClass
|
|
76
|
+
},
|
|
77
|
+
onSave: (row, context) => onSaveRef.current?.(row, context)
|
|
78
|
+
});
|
|
79
|
+
tableRef.current = table;
|
|
80
|
+
return () => {
|
|
81
|
+
table.destroy();
|
|
82
|
+
tableRef.current = null;
|
|
83
|
+
};
|
|
84
|
+
}, [
|
|
85
|
+
columns,
|
|
86
|
+
rowId,
|
|
87
|
+
mode,
|
|
88
|
+
button,
|
|
89
|
+
tableClass,
|
|
90
|
+
buttonClass,
|
|
91
|
+
editClass,
|
|
92
|
+
saveClass,
|
|
93
|
+
cancelClass
|
|
94
|
+
]);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
dataRef.current = data;
|
|
97
|
+
tableRef.current?.setData(data);
|
|
98
|
+
}, [data]);
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
onSaveRef.current = onSave;
|
|
101
|
+
});
|
|
102
|
+
useImperativeHandle(ref, () => ({
|
|
103
|
+
edit: (id) => tableRef.current?.edit(id) ?? false,
|
|
104
|
+
save: (id) => tableRef.current?.save(id) ?? Promise.resolve(false),
|
|
105
|
+
cancel: (id) => tableRef.current?.cancel(id),
|
|
106
|
+
isEditing: (id) => tableRef.current?.isEditing(id) ?? false
|
|
107
|
+
}), []);
|
|
108
|
+
return /* @__PURE__ */ jsx("div", { ref: containerRef });
|
|
109
|
+
}
|
|
110
|
+
export { Tadabut };
|