antd-crud-table 0.0.13 → 0.1.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.
@@ -1,373 +1,304 @@
1
- import { j as r } from "./jsx-runtime-Drh4NO5G.js";
2
- import { useState as g, useEffect as A } from "react";
3
- import { F as l, T as v, f as L, p as V, P as $, a as H, B as f, R as K, D as Y, b as J, M as b, S as Q, c as W, e as G, g as U, I as X, d as Z, h as ee } from "./CrudTable-W5dD5mDc.js";
4
- import { useCrudTable as te } from "./useCrudTable.js";
5
- import { s as R } from "./index-DNXRWKhU.js";
6
- const oe = (S) => {
7
- const { columns: m, rowKey: c, title: O, defaultPageSize: j = 10, hookConfig: D, enableBulkOperations: C = !1, customActions: x } = S, i = te(c, {
8
- defaultPageSize: j,
9
- ...D
10
- }), [E, y] = g(!1), [h, F] = g(null), [d, w] = g([]), [p] = l.useForm();
11
- A(() => {
12
- i.refresh();
13
- }, []);
14
- const I = m.map((e) => {
15
- const t = {
16
- ...e,
17
- dataIndex: e.dataIndex,
18
- title: e.title,
19
- search: e.searchable !== !1
20
- // Default to searchable
21
- };
22
- switch (e.fieldType) {
23
- case "date":
24
- return {
25
- ...t,
26
- valueType: "dateTime",
27
- render: (a, n) => {
28
- const s = n[e.dataIndex];
29
- if (!s) return "-";
30
- try {
31
- return /* @__PURE__ */ r.jsx("span", { children: L(V(s), "yyyy-MM-dd HH:mm") });
32
- } catch {
33
- return /* @__PURE__ */ r.jsx("span", { children: s });
34
- }
35
- }
36
- };
37
- case "enum":
38
- return {
39
- ...t,
40
- valueType: "select",
41
- valueEnum: e.enumOptions,
42
- render: (a, n) => {
43
- var u;
44
- const s = n[e.dataIndex], o = (u = e.enumOptions) == null ? void 0 : u[s];
45
- return o ? /* @__PURE__ */ r.jsx(v, { color: o.color, children: o.text }) : s;
46
- }
47
- };
48
- case "number":
49
- return {
50
- ...t,
51
- valueType: "digit",
52
- render: (a, n) => {
53
- const s = n[e.dataIndex];
54
- return typeof s == "number" ? s.toLocaleString() : s;
55
- }
56
- };
57
- case "boolean":
58
- return {
59
- ...t,
60
- valueType: "switch",
61
- render: (a, n) => /* @__PURE__ */ r.jsx(v, { color: n[e.dataIndex] ? "green" : "red", children: n[e.dataIndex] ? "Yes" : "No" })
62
- };
63
- case "custom":
64
- return {
65
- ...t,
66
- render: (a, n) => {
67
- var s;
68
- return (s = e.customRender) == null ? void 0 : s.call(e, n[e.dataIndex], n);
69
- }
70
- };
71
- default:
72
- return t;
73
- }
74
- });
75
- I.push({
76
- title: "Actions",
77
- valueType: "option",
78
- width: 200,
79
- render: (e, t) => {
80
- const a = [
81
- /* @__PURE__ */ r.jsx(
82
- f,
83
- {
84
- type: "link",
85
- size: "small",
86
- onClick: () => T(t),
87
- children: "Edit"
88
- },
89
- "edit"
90
- ),
91
- /* @__PURE__ */ r.jsx(
92
- f,
93
- {
94
- type: "link",
95
- size: "small",
96
- danger: !0,
97
- onClick: () => M(t[c]),
98
- children: "Delete"
99
- },
100
- "delete"
101
- )
102
- ], n = (x == null ? void 0 : x(t, i)) || [];
103
- return [...a, ...n];
104
- }
105
- });
106
- const P = async (e, t, a) => {
107
- try {
108
- const n = {
109
- current: e.current,
110
- pageSize: e.pageSize,
111
- sortBy: Object.keys(t)[0],
112
- sortOrder: Object.values(t)[0],
113
- ...a,
114
- ...e
115
- // Include search parameters
116
- }, s = i.operations;
117
- if (s != null && s.getList) {
118
- const o = await s.getList(n);
119
- return {
120
- data: o.data,
121
- success: !0,
122
- total: o.total
123
- };
124
- }
125
- return {
126
- data: i.state.data,
127
- success: !0,
128
- total: i.state.total
129
- };
130
- } catch {
131
- return R.error("Failed to fetch data"), { data: [], success: !1, total: 0 };
132
- }
133
- }, T = (e) => {
134
- if (F(e || null), e) {
135
- const t = { ...e };
136
- m.forEach((a) => {
137
- const n = a.dataIndex;
138
- if (a.fieldType === "date" && t[n])
139
- try {
140
- t[n] = Z(t[n]);
141
- } catch {
142
- }
143
- }), p.setFieldsValue(t);
144
- } else
145
- p.resetFields();
146
- y(!0);
147
- }, z = async () => {
148
- var e;
149
- try {
150
- const t = await p.validateFields(), a = { ...t };
151
- m.forEach((n) => {
152
- var o;
153
- const s = n.dataIndex;
154
- if (n.fieldType === "date" && t[s])
155
- try {
156
- a[s] = ee(t[s]);
157
- } catch {
158
- }
159
- (o = n.formConfig) != null && o.transform && (a[s] = n.formConfig.transform(t[s]));
160
- }), h && h[c] ? await i.update(h[c], a) : await i.create(a), y(!1), (e = i.actionRef.current) == null || e.reload();
161
- } catch (t) {
162
- console.error("Form validation failed:", t);
163
- }
164
- }, M = async (e) => {
165
- b.confirm({
166
- title: "Are you sure?",
167
- content: "This action cannot be undone.",
168
- okText: "Yes, Delete",
169
- okType: "danger",
170
- cancelText: "Cancel",
171
- onOk: async () => {
172
- var a;
173
- await i.delete(e) && ((a = i.actionRef.current) == null || a.reload());
174
- }
175
- });
176
- }, _ = async () => {
177
- if (d.length === 0) {
178
- R.warning("Please select items to delete");
179
- return;
180
- }
181
- b.confirm({
182
- title: `Delete ${d.length} items?`,
183
- content: "This action cannot be undone.",
184
- okText: "Yes, Delete All",
185
- okType: "danger",
186
- cancelText: "Cancel",
187
- onOk: async () => {
188
- var t;
189
- const e = d.map((a) => i.delete(a));
190
- await Promise.allSettled(e), w([]), (t = i.actionRef.current) == null || t.reload();
191
- }
192
- });
193
- }, q = C ? {
194
- selectedRowKeys: d,
195
- onChange: w
196
- } : void 0;
197
- return /* @__PURE__ */ r.jsxs($, { needDeps: !0, children: [
198
- /* @__PURE__ */ r.jsx(
199
- H,
200
- {
201
- headerTitle: O,
202
- rowKey: c,
203
- rowClassName: (e, t) => t % 2 === 0 ? "row-differentiator" : "",
204
- actionRef: i.actionRef,
205
- columns: I,
206
- request: P,
207
- search: { labelWidth: "auto" },
208
- pagination: {
209
- pageSize: j,
210
- showSizeChanger: !0,
211
- showQuickJumper: !0
212
- },
213
- loading: i.state.loading,
214
- rowSelection: q,
215
- toolBarRender: () => [
216
- /* @__PURE__ */ r.jsx(
217
- f,
218
- {
219
- type: "primary",
220
- icon: /* @__PURE__ */ r.jsx(K, {}),
221
- onClick: () => T(),
222
- children: "New"
223
- },
224
- "add"
225
- ),
226
- ...C && d.length > 0 ? [
227
- /* @__PURE__ */ r.jsxs(
228
- f,
229
- {
230
- danger: !0,
231
- onClick: _,
232
- children: [
233
- "Delete Selected (",
234
- d.length,
235
- ")"
236
- ]
237
- },
238
- "bulk-delete"
239
- )
240
- ] : [],
241
- /* @__PURE__ */ r.jsx(
242
- Y,
243
- {
244
- menu: {
245
- items: [
246
- {
247
- key: "export",
248
- label: "Export",
249
- disabled: !0
250
- // TODO: Implement export
251
- },
252
- {
253
- key: "refresh",
254
- label: "Refresh",
255
- onClick: () => i.refresh()
256
- }
257
- ]
258
- },
259
- children: /* @__PURE__ */ r.jsx(f, { children: /* @__PURE__ */ r.jsx(J, {}) })
260
- },
261
- "menu"
262
- )
263
- ],
264
- options: {
265
- setting: { listsHeight: 400 },
266
- reload: () => i.refresh()
267
- },
268
- dateFormatter: "string"
269
- }
270
- ),
271
- /* @__PURE__ */ r.jsx(
272
- b,
273
- {
274
- title: h ? "Edit Item" : "Create Item",
275
- open: E,
276
- onOk: z,
277
- onCancel: () => y(!1),
278
- destroyOnClose: !0,
279
- width: 600,
280
- children: /* @__PURE__ */ r.jsx(l, { form: p, layout: "vertical", children: m.map((e) => {
281
- var o, u, k;
282
- if (!e.dataIndex) return null;
283
- const t = e.dataIndex, a = e.title, n = !(e.fieldEditable ?? !0), s = ((o = e.formConfig) == null ? void 0 : o.rules) || ((u = e.formConfig) != null && u.required ? [
284
- { required: !0, message: `${a} is required` }
285
- ] : []);
286
- if ((k = e.formConfig) != null && k.component)
287
- return /* @__PURE__ */ r.jsx(
288
- l.Item,
289
- {
290
- name: t,
291
- label: a,
292
- rules: s,
293
- children: e.formConfig.component
294
- },
295
- t
296
- );
297
- switch (e.fieldType) {
298
- case "string":
299
- return /* @__PURE__ */ r.jsx(
300
- l.Item,
301
- {
302
- name: t,
303
- label: a,
304
- rules: s,
305
- children: /* @__PURE__ */ r.jsx(X, { disabled: n })
306
- },
307
- t
308
- );
309
- case "number":
310
- return /* @__PURE__ */ r.jsx(
311
- l.Item,
312
- {
313
- name: t,
314
- label: a,
315
- rules: s,
316
- children: /* @__PURE__ */ r.jsx(U, { style: { width: "100%" }, disabled: n })
317
- },
318
- t
319
- );
320
- case "date":
321
- return /* @__PURE__ */ r.jsx(
322
- l.Item,
323
- {
324
- name: t,
325
- label: a,
326
- rules: s,
327
- children: /* @__PURE__ */ r.jsx(G, { style: { width: "100%" }, showTime: !0, disabled: n })
328
- },
329
- t
330
- );
331
- case "boolean":
332
- return /* @__PURE__ */ r.jsx(
333
- l.Item,
334
- {
335
- name: t,
336
- label: a,
337
- valuePropName: "checked",
338
- children: /* @__PURE__ */ r.jsx(W, { disabled: n })
339
- },
340
- t
341
- );
342
- case "enum":
343
- return /* @__PURE__ */ r.jsx(
344
- l.Item,
345
- {
346
- name: t,
347
- label: a,
348
- rules: s,
349
- children: /* @__PURE__ */ r.jsx(
350
- Q,
351
- {
352
- disabled: n,
353
- placeholder: `Select ${a.toLowerCase()}`,
354
- options: Object.entries(e.enumOptions || {}).map(([B, N]) => ({
355
- label: N.text,
356
- value: B
357
- }))
358
- }
359
- )
360
- },
361
- t
362
- );
363
- default:
364
- return null;
365
- }
366
- }) })
367
- }
368
- )
369
- ] });
370
- };
371
- export {
372
- oe as default
1
+ import { a as e } from "./chunk-DrlAKpkd.js";
2
+ import { _ as t, a as n, c as r, d as i, f as a, g as o, h as s, i as c, l, m as u, n as d, o as f, p, r as m, s as h, t as g, u as _, v, y } from "./CrudTable-DbG9ZeYQ.js";
3
+ import { t as b } from "./message-C9NNC2sM.js";
4
+ import { t as x } from "./jsx-runtime-Blo4gjKb.js";
5
+ import { useCrudTable as S } from "./useCrudTable.js";
6
+ import { useEffect as C, useState as w } from "react";
7
+ //#region lib/CrudTableExperimental.tsx
8
+ var T = /* @__PURE__ */ e(s(), 1), E = x(), D = (e) => {
9
+ let { columns: s, rowKey: x, title: D, defaultPageSize: O = 10, hookConfig: k, enableBulkOperations: A = !1, customActions: j } = e, M = S(x, {
10
+ defaultPageSize: O,
11
+ ...k
12
+ }), [N, P] = w(!1), [F, I] = w(null), [L, R] = w([]), [z] = i.useForm();
13
+ C(() => {
14
+ M.refresh();
15
+ }, []);
16
+ let B = s.map((e) => {
17
+ let t = {
18
+ ...e,
19
+ dataIndex: e.dataIndex,
20
+ title: e.title,
21
+ search: e.searchable !== !1
22
+ };
23
+ switch (e.fieldType) {
24
+ case "date": return {
25
+ ...t,
26
+ valueType: "dateTime",
27
+ render: (t, n) => {
28
+ let r = n[e.dataIndex];
29
+ if (!r) return "-";
30
+ try {
31
+ return /* @__PURE__ */ (0, E.jsx)("span", { children: m(g(r), "yyyy-MM-dd HH:mm") });
32
+ } catch {
33
+ return /* @__PURE__ */ (0, E.jsx)("span", { children: r });
34
+ }
35
+ }
36
+ };
37
+ case "enum": return {
38
+ ...t,
39
+ valueType: "select",
40
+ valueEnum: e.enumOptions,
41
+ render: (t, n) => {
42
+ let r = n[e.dataIndex], i = e.enumOptions?.[r];
43
+ return i ? /* @__PURE__ */ (0, E.jsx)(h, {
44
+ color: i.color,
45
+ children: i.text
46
+ }) : r;
47
+ }
48
+ };
49
+ case "number": return {
50
+ ...t,
51
+ valueType: "digit",
52
+ render: (t, n) => {
53
+ let r = n[e.dataIndex];
54
+ return typeof r == "number" ? r.toLocaleString() : r;
55
+ }
56
+ };
57
+ case "boolean": return {
58
+ ...t,
59
+ valueType: "switch",
60
+ render: (t, n) => /* @__PURE__ */ (0, E.jsx)(h, {
61
+ color: n[e.dataIndex] ? "green" : "red",
62
+ children: n[e.dataIndex] ? "Yes" : "No"
63
+ })
64
+ };
65
+ case "custom": return {
66
+ ...t,
67
+ render: (t, n) => e.customRender?.(n[e.dataIndex], n)
68
+ };
69
+ default: return t;
70
+ }
71
+ });
72
+ B.push({
73
+ title: "Actions",
74
+ valueType: "option",
75
+ width: 200,
76
+ render: (e, t) => {
77
+ let n = [/* @__PURE__ */ (0, E.jsx)(o, {
78
+ type: "link",
79
+ size: "small",
80
+ onClick: () => H(t),
81
+ children: "Edit"
82
+ }, "edit"), /* @__PURE__ */ (0, E.jsx)(o, {
83
+ type: "link",
84
+ size: "small",
85
+ danger: !0,
86
+ onClick: () => W(t[x]),
87
+ children: "Delete"
88
+ }, "delete")], r = j?.(t, M) || [];
89
+ return [...n, ...r];
90
+ }
91
+ });
92
+ let V = async (e, t, n) => {
93
+ try {
94
+ let r = {
95
+ current: e.current,
96
+ pageSize: e.pageSize,
97
+ sortBy: Object.keys(t)[0],
98
+ sortOrder: Object.values(t)[0],
99
+ ...n,
100
+ ...e
101
+ }, i = M.operations;
102
+ if (i?.getList) {
103
+ let e = await i.getList(r);
104
+ return {
105
+ data: e.data,
106
+ success: !0,
107
+ total: e.total
108
+ };
109
+ }
110
+ return {
111
+ data: M.state.data,
112
+ success: !0,
113
+ total: M.state.total
114
+ };
115
+ } catch {
116
+ return b.error("Failed to fetch data"), {
117
+ data: [],
118
+ success: !1,
119
+ total: 0
120
+ };
121
+ }
122
+ }, H = (e) => {
123
+ if (I(e || null), e) {
124
+ let t = { ...e };
125
+ s.forEach((e) => {
126
+ let n = e.dataIndex;
127
+ if (e.fieldType === "date" && t[n]) try {
128
+ t[n] = (0, T.default)(t[n]);
129
+ } catch {}
130
+ }), z.setFieldsValue(t);
131
+ } else z.resetFields();
132
+ P(!0);
133
+ }, U = async () => {
134
+ try {
135
+ let e = await z.validateFields(), t = { ...e };
136
+ s.forEach((n) => {
137
+ let r = n.dataIndex;
138
+ if (n.fieldType === "date" && e[r]) try {
139
+ t[r] = d(e[r]);
140
+ } catch {}
141
+ n.formConfig?.transform && (t[r] = n.formConfig.transform(e[r]));
142
+ }), F && F[x] ? await M.update(F[x], t) : await M.create(t), P(!1), M.actionRef.current?.reload();
143
+ } catch (e) {
144
+ console.error("Form validation failed:", e);
145
+ }
146
+ }, W = async (e) => {
147
+ l.confirm({
148
+ title: "Are you sure?",
149
+ content: "This action cannot be undone.",
150
+ okText: "Yes, Delete",
151
+ okType: "danger",
152
+ cancelText: "Cancel",
153
+ onOk: async () => {
154
+ await M.delete(e) && M.actionRef.current?.reload();
155
+ }
156
+ });
157
+ }, G = async () => {
158
+ if (L.length === 0) {
159
+ b.warning("Please select items to delete");
160
+ return;
161
+ }
162
+ l.confirm({
163
+ title: `Delete ${L.length} items?`,
164
+ content: "This action cannot be undone.",
165
+ okText: "Yes, Delete All",
166
+ okType: "danger",
167
+ cancelText: "Cancel",
168
+ onOk: async () => {
169
+ let e = L.map((e) => M.delete(e));
170
+ await Promise.allSettled(e), R([]), M.actionRef.current?.reload();
171
+ }
172
+ });
173
+ }, K = A ? {
174
+ selectedRowKeys: L,
175
+ onChange: R
176
+ } : void 0;
177
+ return /* @__PURE__ */ (0, E.jsxs)(n, {
178
+ needDeps: !0,
179
+ intl: f,
180
+ children: [/* @__PURE__ */ (0, E.jsx)(c, {
181
+ headerTitle: D,
182
+ rowKey: x,
183
+ rowClassName: (e, t) => t % 2 == 0 ? "row-differentiator" : "",
184
+ actionRef: M.actionRef,
185
+ columns: B,
186
+ request: V,
187
+ search: { labelWidth: "auto" },
188
+ pagination: {
189
+ pageSize: O,
190
+ showSizeChanger: !0,
191
+ showQuickJumper: !0
192
+ },
193
+ loading: M.state.loading,
194
+ rowSelection: K,
195
+ toolBarRender: () => [
196
+ /* @__PURE__ */ (0, E.jsx)(o, {
197
+ type: "primary",
198
+ icon: /* @__PURE__ */ (0, E.jsx)(v, {}),
199
+ onClick: () => H(),
200
+ children: "New"
201
+ }, "add"),
202
+ ...A && L.length > 0 ? [/* @__PURE__ */ (0, E.jsxs)(o, {
203
+ danger: !0,
204
+ onClick: G,
205
+ children: [
206
+ "Delete Selected (",
207
+ L.length,
208
+ ")"
209
+ ]
210
+ }, "bulk-delete")] : [],
211
+ /* @__PURE__ */ (0, E.jsx)(a, {
212
+ menu: { items: [{
213
+ key: "export",
214
+ label: "Export",
215
+ disabled: !0
216
+ }, {
217
+ key: "refresh",
218
+ label: "Refresh",
219
+ onClick: () => M.refresh()
220
+ }] },
221
+ children: /* @__PURE__ */ (0, E.jsx)(o, { children: /* @__PURE__ */ (0, E.jsx)(y, {}) })
222
+ }, "menu")
223
+ ],
224
+ options: {
225
+ setting: { listsHeight: 400 },
226
+ reload: () => M.refresh()
227
+ },
228
+ dateFormatter: "string"
229
+ }), /* @__PURE__ */ (0, E.jsx)(l, {
230
+ title: F ? "Edit Item" : "Create Item",
231
+ open: N,
232
+ onOk: U,
233
+ onCancel: () => P(!1),
234
+ destroyOnClose: !0,
235
+ width: 600,
236
+ children: /* @__PURE__ */ (0, E.jsx)(i, {
237
+ form: z,
238
+ layout: "vertical",
239
+ children: s.map((e) => {
240
+ if (!e.dataIndex) return null;
241
+ let n = e.dataIndex, a = e.title, o = !(e.fieldEditable ?? !0), s = e.formConfig?.rules || (e.formConfig?.required ? [{
242
+ required: !0,
243
+ message: `${a} is required`
244
+ }] : []);
245
+ if (e.formConfig?.component) return /* @__PURE__ */ (0, E.jsx)(i.Item, {
246
+ name: n,
247
+ label: a,
248
+ rules: s,
249
+ children: e.formConfig.component
250
+ }, n);
251
+ switch (e.fieldType) {
252
+ case "string": return /* @__PURE__ */ (0, E.jsx)(i.Item, {
253
+ name: n,
254
+ label: a,
255
+ rules: s,
256
+ children: /* @__PURE__ */ (0, E.jsx)(_, { disabled: o })
257
+ }, n);
258
+ case "number": return /* @__PURE__ */ (0, E.jsx)(i.Item, {
259
+ name: n,
260
+ label: a,
261
+ rules: s,
262
+ children: /* @__PURE__ */ (0, E.jsx)(u, {
263
+ style: { width: "100%" },
264
+ disabled: o
265
+ })
266
+ }, n);
267
+ case "date": return /* @__PURE__ */ (0, E.jsx)(i.Item, {
268
+ name: n,
269
+ label: a,
270
+ rules: s,
271
+ children: /* @__PURE__ */ (0, E.jsx)(p, {
272
+ style: { width: "100%" },
273
+ showTime: !0,
274
+ disabled: o
275
+ })
276
+ }, n);
277
+ case "boolean": return /* @__PURE__ */ (0, E.jsx)(i.Item, {
278
+ name: n,
279
+ label: a,
280
+ valuePropName: "checked",
281
+ children: /* @__PURE__ */ (0, E.jsx)(r, { disabled: o })
282
+ }, n);
283
+ case "enum": return /* @__PURE__ */ (0, E.jsx)(i.Item, {
284
+ name: n,
285
+ label: a,
286
+ rules: s,
287
+ children: /* @__PURE__ */ (0, E.jsx)(t, {
288
+ disabled: o,
289
+ placeholder: `Select ${a.toLowerCase()}`,
290
+ options: Object.entries(e.enumOptions || {}).map(([e, t]) => ({
291
+ label: t.text,
292
+ value: e
293
+ }))
294
+ })
295
+ }, n);
296
+ default: return null;
297
+ }
298
+ })
299
+ })
300
+ })]
301
+ });
373
302
  };
303
+ //#endregion
304
+ export { D as default };