tadabut 0.5.0 → 0.7.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.d.ts CHANGED
@@ -1,20 +1,56 @@
1
- import { c as TadabutOptions, r as Tadabut$1 } from "./index-KmSXRJlI.js";
2
- import { ReactElement, Ref } from "react";
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" | "delete" | "isEditing" | "add" | "isAdding" | "saveNew" | "cancelNew">;
5
- /** The core options, plus `ref`. */
6
- export type TadabutProps<Row extends object> = TadabutOptions<Row> & {
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 { d as TadabutOptions, o as DetailContext, r as Tadabut$1 } from "./index-Cm0OsNb6.js";
15
+ import { Api } from "datatables.net";
16
+ import { ReactElement, ReactNode, Ref } from "react";
17
+ /**
18
+ * What the `ref` gets. No `setData`: change the `data` prop instead. Before
19
+ * the table is built, `getApi` gives `undefined`.
20
+ */
21
+ export type TadabutHandle<Row extends object> = Pick<Tadabut$1<Row>, "edit" | "save" | "cancel" | "delete" | "isEditing" | "add" | "isAdding" | "saveNew" | "cancelNew" | "getSelected" | "isSelected" | "select" | "deselect" | "clearSelection" | "openDetail" | "closeDetail" | "toggleDetail" | "isDetailOpen" | "getData" | "getRow" | "insertRow" | "updateRow" | "removeRow" | "getTotals" | "getOptions"> & {
22
+ /** The DataTables API, or `undefined` before the table is built. */
23
+ readonly getApi: () => Api<Row> | undefined;
24
+ };
25
+ /** The core options, with `detail` returning JSX, plus `ref`. */
26
+ export type TadabutProps<Row extends object> = Omit<TadabutOptions<Row>, "detail"> & {
27
+ /**
28
+ * Adds a ▸ button at the start of each row. It opens a line under the row
29
+ * with what this returns: any JSX, so hooks, state and your components
30
+ * work inside. May be async: "Loading…" shows while it waits, and the
31
+ * error message if it throws. It runs again after the row is saved. Needs
32
+ * `rowId`.
33
+ *
34
+ * @example
35
+ * detail={(row) => <OrderLines orderId={row.id} />}
36
+ */
37
+ readonly detail?: (row: Row, context: DetailContext) => ReactNode | Promise<ReactNode>;
7
38
  /**
8
39
  * Gives you the same methods as the core table: `edit`, `save`, `cancel`,
9
- * `delete`, `add`, `saveNew`, `cancelNew`, `isEditing` and `isAdding`.
40
+ * `delete`, `add`, `saveNew`, `cancelNew`, `isEditing`, `isAdding`,
41
+ * `getSelected`, `isSelected`, `select`, `deselect`, `clearSelection`,
42
+ * `openDetail`, `closeDetail`, `toggleDetail`, `isDetailOpen`, `getData`,
43
+ * `getRow`, `getApi`, `insertRow`, `updateRow`, `removeRow`, `getTotals`
44
+ * and `getOptions`.
10
45
  */
11
46
  readonly ref?: Ref<TadabutHandle<Row>>;
12
47
  };
13
48
  /**
14
49
  * New `data` updates the table in place. Changing `columns`, `rowId`,
15
- * `dataTables`, `texts` or `totalRows` rebuilds it (and closes open rows), so
16
- * keep the objects stable with a constant or `useMemo`. `editing`,
17
- * `classNames`, `onSave`, `onAdd`, `onDelete` and `confirmDelete` can be
18
- * inline: they're compared by value or read through a ref.
50
+ * `dataTables`, `texts`, `totalRows` or `selection` rebuilds it (and closes
51
+ * open rows and details, and clears the selection), so keep the objects
52
+ * stable with a constant or `useMemo`. `editing`, `selection`, `classNames`,
53
+ * `detail` and every `on…` callback (and `confirmDelete`) can be inline:
54
+ * they're compared by value or read through a ref.
19
55
  */
20
- export declare function Tadabut<Row extends object>({ data, columns, rowId, editing, classNames, dataTables, texts, totalRows, onSave, onAdd, onDelete, confirmDelete, ref }: TadabutProps<Row>): ReactElement;
56
+ 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, detail, ref }: TadabutProps<Row>): ReactElement;
package/dist/react.js CHANGED
@@ -1,28 +1,17 @@
1
- import { n as Tadabut$1 } from "./src-CBK3xu9-.js";
2
- import { useEffect, useImperativeHandle, useRef } from "react";
3
- import { jsx } from "react/jsx-runtime";
1
+ import { n as Tadabut$1 } from "./src-D6zL6H0B.js";
2
+ import { useEffect, useImperativeHandle, useRef, useState } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
4
5
  // src/react/index.tsx
5
6
  /**
6
- * The React component.
7
- *
8
- * @packageDocumentation
9
- * @module tadabut/react
10
- *
11
- * @example
12
- * ```tsx
13
- * import { Tadabut } from 'tadabut/react';
14
- *
15
- * <Tadabut data={employees} columns={columns} rowId='id' />
16
- * ```
17
- */
18
- /**
19
7
  * New `data` updates the table in place. Changing `columns`, `rowId`,
20
- * `dataTables`, `texts` or `totalRows` rebuilds it (and closes open rows), so
21
- * keep the objects stable with a constant or `useMemo`. `editing`,
22
- * `classNames`, `onSave`, `onAdd`, `onDelete` and `confirmDelete` can be
23
- * inline: they're compared by value or read through a ref.
8
+ * `dataTables`, `texts`, `totalRows` or `selection` rebuilds it (and closes
9
+ * open rows and details, and clears the selection), so keep the objects
10
+ * stable with a constant or `useMemo`. `editing`, `selection`, `classNames`,
11
+ * `detail` and every `on…` callback (and `confirmDelete`) can be inline:
12
+ * they're compared by value or read through a ref.
24
13
  */
25
- function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, totalRows, onSave, onAdd, onDelete, confirmDelete, ref }) {
14
+ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts, totalRows, selection, onSelect, onSave, onAdd, onDelete, confirmDelete, onEditStart, onSaved, onAdded, onDeleted, onRowClick, onRowDoubleClick, detail, ref }) {
26
15
  /** The `<div>` the core builds the table in. */
27
16
  const containerRef = useRef(null);
28
17
  /** The core table. `null` before the first build and after unmount. */
@@ -37,8 +26,29 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
37
26
  const onDeleteRef = useRef(onDelete);
38
27
  /** The latest `confirmDelete`, so a new one doesn't rebuild the table. */
39
28
  const confirmDeleteRef = useRef(confirmDelete);
29
+ /** The latest `onSelect`, so a new one doesn't rebuild the table. */
30
+ const onSelectRef = useRef(onSelect);
31
+ /** The latest `onEditStart`, so a new one doesn't rebuild the table. */
32
+ const onEditStartRef = useRef(onEditStart);
33
+ /** The latest `onSaved`, so a new one doesn't rebuild the table. */
34
+ const onSavedRef = useRef(onSaved);
35
+ /** The latest `onAdded`, so a new one doesn't rebuild the table. */
36
+ const onAddedRef = useRef(onAdded);
37
+ /** The latest `onDeleted`, so a new one doesn't rebuild the table. */
38
+ const onDeletedRef = useRef(onDeleted);
39
+ /** The latest `onRowClick`, so a new one doesn't rebuild the table. */
40
+ const onRowClickRef = useRef(onRowClick);
41
+ /** The latest `onRowDoubleClick`, so a new one doesn't rebuild the table. */
42
+ const onRowDoubleClickRef = useRef(onRowDoubleClick);
43
+ /** The latest `detail`, so a new one doesn't rebuild the table. */
44
+ const detailRef = useRef(detail);
45
+ /** The next key for an open detail. Never reused, so a reload gets a fresh one. */
46
+ const nextKeyRef = useRef(0);
47
+ /** The open details React draws into, by key. */
48
+ const [details, setDetails] = useState(() => /* @__PURE__ */ new Map());
40
49
  const mode = editing?.mode;
41
50
  const button = editing?.button;
51
+ const doubleClick = editing?.doubleClick;
42
52
  const tableClass = classNames?.table;
43
53
  const buttonClass = classNames?.button;
44
54
  const editClass = classNames?.edit;
@@ -47,8 +57,12 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
47
57
  const deleteClass = classNames?.delete;
48
58
  const confirmClass = classNames?.confirm;
49
59
  const keepClass = classNames?.keep;
60
+ const selectClass = classNames?.select;
61
+ const selectionOn = Boolean(selection);
62
+ const checkAll = typeof selection === "object" ? selection.checkAll : void 0;
50
63
  const hasDelete = onDelete !== void 0;
51
64
  const confirmWith = confirmDelete === false ? "never" : confirmDelete === void 0 ? "inline" : "function";
65
+ const hasDetail = detail !== void 0;
52
66
  useEffect(() => {
53
67
  const container = containerRef.current;
54
68
  if (!container) return;
@@ -64,11 +78,13 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
64
78
  rowId,
65
79
  editing: {
66
80
  mode,
67
- button
81
+ button,
82
+ doubleClick
68
83
  },
69
84
  dataTables,
70
85
  texts,
71
86
  totalRows,
87
+ selection: selectionOn && { checkAll },
72
88
  classNames: {
73
89
  table: tableClass,
74
90
  button: buttonClass,
@@ -77,12 +93,36 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
77
93
  cancel: cancelClass,
78
94
  delete: deleteClass,
79
95
  confirm: confirmClass,
80
- keep: keepClass
96
+ keep: keepClass,
97
+ select: selectClass
81
98
  },
82
99
  onSave: (row, context) => onSaveRef.current?.(row, context),
83
100
  onAdd: (row) => onAddRef.current?.(row),
84
101
  onDelete: hasDelete ? (row) => onDeleteRef.current?.(row) : void 0,
85
- confirmDelete: confirm
102
+ confirmDelete: confirm,
103
+ onSelect: (rows) => onSelectRef.current?.(rows),
104
+ onEditStart: (row) => onEditStartRef.current?.(row),
105
+ onSaved: (row, context) => onSavedRef.current?.(row, context),
106
+ onAdded: (row) => onAddedRef.current?.(row),
107
+ onDeleted: (row) => onDeletedRef.current?.(row),
108
+ onRowClick: (row, event) => onRowClickRef.current?.(row, event),
109
+ onRowDoubleClick: (row, event) => onRowDoubleClickRef.current?.(row, event),
110
+ detail: hasDetail ? async (row, context) => {
111
+ const content = await detailRef.current?.(row, context);
112
+ if (context.signal.aborted) return "";
113
+ const key = nextKeyRef.current++;
114
+ const element = document.createElement("div");
115
+ setDetails((previous) => new Map(previous).set(key, {
116
+ element,
117
+ content
118
+ }));
119
+ context.signal.addEventListener("abort", () => setDetails((previous) => {
120
+ const next = new Map(previous);
121
+ next.delete(key);
122
+ return next;
123
+ }), { once: true });
124
+ return element;
125
+ } : void 0
86
126
  });
87
127
  tableRef.current = table;
88
128
  return () => {
@@ -97,6 +137,7 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
97
137
  totalRows,
98
138
  mode,
99
139
  button,
140
+ doubleClick,
100
141
  tableClass,
101
142
  buttonClass,
102
143
  editClass,
@@ -105,8 +146,12 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
105
146
  deleteClass,
106
147
  confirmClass,
107
148
  keepClass,
149
+ selectClass,
150
+ selectionOn,
151
+ checkAll,
108
152
  hasDelete,
109
- confirmWith
153
+ confirmWith,
154
+ hasDetail
110
155
  ]);
111
156
  useEffect(() => {
112
157
  dataRef.current = data;
@@ -117,6 +162,14 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
117
162
  onAddRef.current = onAdd;
118
163
  onDeleteRef.current = onDelete;
119
164
  confirmDeleteRef.current = confirmDelete;
165
+ onSelectRef.current = onSelect;
166
+ onEditStartRef.current = onEditStart;
167
+ onSavedRef.current = onSaved;
168
+ onAddedRef.current = onAdded;
169
+ onDeletedRef.current = onDeleted;
170
+ onRowClickRef.current = onRowClick;
171
+ onRowDoubleClickRef.current = onRowDoubleClick;
172
+ detailRef.current = detail;
120
173
  });
121
174
  useImperativeHandle(ref, () => ({
122
175
  edit: (id) => tableRef.current?.edit(id) ?? false,
@@ -127,8 +180,25 @@ function Tadabut({ data, columns, rowId, editing, classNames, dataTables, texts,
127
180
  isAdding: () => tableRef.current?.isAdding() ?? false,
128
181
  saveNew: () => tableRef.current?.saveNew() ?? Promise.resolve(false),
129
182
  cancelNew: () => tableRef.current?.cancelNew(),
130
- isEditing: (id) => tableRef.current?.isEditing(id) ?? false
183
+ isEditing: (id) => tableRef.current?.isEditing(id) ?? false,
184
+ getSelected: () => tableRef.current?.getSelected() ?? [],
185
+ isSelected: (id) => tableRef.current?.isSelected(id) ?? false,
186
+ select: (ids) => tableRef.current?.select(ids),
187
+ deselect: (ids) => tableRef.current?.deselect(ids),
188
+ clearSelection: () => tableRef.current?.clearSelection(),
189
+ openDetail: (id) => tableRef.current?.openDetail(id),
190
+ closeDetail: (id) => tableRef.current?.closeDetail(id),
191
+ toggleDetail: (id) => tableRef.current?.toggleDetail(id),
192
+ isDetailOpen: (id) => tableRef.current?.isDetailOpen(id) ?? false,
193
+ getData: (scope) => tableRef.current?.getData(scope) ?? [],
194
+ getRow: (id) => tableRef.current?.getRow(id),
195
+ getApi: () => tableRef.current?.getApi(),
196
+ insertRow: (rows) => tableRef.current?.insertRow(rows),
197
+ updateRow: (row) => tableRef.current?.updateRow(row) ?? false,
198
+ removeRow: (id) => tableRef.current?.removeRow(id) ?? false,
199
+ getTotals: () => tableRef.current?.getTotals() ?? {},
200
+ getOptions: (prop) => tableRef.current?.getOptions(prop)
131
201
  }), []);
132
- return /* @__PURE__ */ jsx("div", { ref: containerRef });
202
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", { ref: containerRef }), [...details].map(([key, { element, content }]) => createPortal(content, element, key))] });
133
203
  }
134
204
  export { Tadabut };