@chumsinc/sortable-tables 2.2.0 → 3.0.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +69 -52
  2. package/README.md +94 -4
  3. package/dist/DataTable.d.ts +6 -6
  4. package/dist/DataTableCell.d.ts +299 -295
  5. package/dist/DataTableCols.d.ts +5 -5
  6. package/dist/DataTableContext.d.ts +10 -0
  7. package/dist/DataTableProvider.d.ts +12 -0
  8. package/dist/DataTableTH.d.ts +6 -6
  9. package/dist/SortableTable.d.ts +6 -6
  10. package/dist/SortableTableHead.d.ts +6 -6
  11. package/dist/SortableTableHeadWrapper.d.ts +9 -0
  12. package/dist/SortableTableTH.d.ts +6 -6
  13. package/dist/StandaloneDataTable.d.ts +6 -0
  14. package/dist/StandaloneSortHelper.d.ts +5 -0
  15. package/dist/StandaloneSortableTable.d.ts +6 -0
  16. package/dist/Table.d.ts +6 -5
  17. package/dist/index.cjs.js +30 -34
  18. package/dist/index.cjs.js.map +1 -1
  19. package/dist/index.d.ts +20 -16
  20. package/dist/index.es.js +521 -471
  21. package/dist/index.es.js.map +1 -1
  22. package/dist/types.d.ts +104 -103
  23. package/dist/useField.d.ts +6 -0
  24. package/dist/useTableContext.d.ts +2 -0
  25. package/dist/useTableFields.d.ts +9 -0
  26. package/dist/useTableSort.d.ts +9 -0
  27. package/package.json +3 -3
  28. package/src/DataTable.tsx +34 -11
  29. package/src/DataTableCell.tsx +33 -28
  30. package/src/DataTableCols.tsx +11 -18
  31. package/src/DataTableContext.ts +13 -0
  32. package/src/DataTableHead.tsx +6 -7
  33. package/src/DataTableProvider.tsx +62 -0
  34. package/src/DataTableRow.tsx +8 -7
  35. package/src/DataTableTBody.tsx +1 -3
  36. package/src/DataTableTH.tsx +4 -3
  37. package/src/RowsPerPage.tsx +2 -4
  38. package/src/SortableTable.tsx +36 -12
  39. package/src/SortableTableHead.tsx +9 -9
  40. package/src/SortableTableHeadWrapper.tsx +20 -0
  41. package/src/SortableTableTH.tsx +4 -4
  42. package/src/StandaloneDataTable.tsx +16 -0
  43. package/src/StandaloneSortHelper.tsx +15 -0
  44. package/src/StandaloneSortableTable.tsx +21 -0
  45. package/src/Table.tsx +49 -44
  46. package/src/TablePagination.tsx +1 -3
  47. package/src/index.tsx +21 -15
  48. package/src/types.ts +127 -126
  49. package/src/useField.ts +19 -0
  50. package/src/useTableContext.ts +10 -0
  51. package/src/useTableFields.ts +20 -0
  52. package/src/useTableSort.ts +20 -0
  53. package/test/Main.tsx +37 -0
  54. package/test/TableColumnsHandler.tsx +9 -9
  55. package/test/TestTable.tsx +51 -46
  56. package/test/data.ts +232 -232
  57. package/test/index.tsx +11 -11
  58. package/test/tableFields.tsx +11 -3
  59. package/test/utils.ts +19 -0
  60. package/tsconfig.json +1 -0
  61. package/dist/DataTableWithContext.d.ts +0 -2
  62. package/dist/SortableTableWithContext.d.ts +0 -2
  63. package/dist/TableProvider.d.ts +0 -17
  64. package/src/DataTableWithContext.tsx +0 -41
  65. package/src/SortableTableWithContext.tsx +0 -44
  66. package/src/TableProvider.tsx +0 -77
package/dist/index.es.js CHANGED
@@ -1,471 +1,521 @@
1
- import { jsx as l, jsxs as p } from "react/jsx-runtime";
2
- import A, { createContext as B, useState as V, useCallback as v, useMemo as W, useContext as g, createElement as G, useId as I } from "react";
3
- import x from "@emotion/styled";
4
- const N = B(null);
5
- function S({ children: t, initialFields: a = [] }) {
6
- const [e, n] = V(a), s = v((o) => {
7
- n(o);
8
- }, []), c = v((o, u) => {
9
- const b = e.map((d) => d.id === o ? { ...d, ...u } : d);
10
- n(b);
11
- }, [e]), i = v((o) => e.find((u) => u.id === o), [e]), r = W(
12
- () => ({
13
- fields: e,
14
- setFields: s,
15
- updateField: c,
16
- getField: i
17
- }),
18
- [e, s, c, i]
19
- );
20
- return /* @__PURE__ */ l(N.Provider, { value: r, children: t });
21
- }
22
- function J() {
23
- const t = g(N);
24
- if (!t)
25
- throw new Error("useTableContext must be used within a FieldsProvider");
26
- return t;
27
- }
28
- function y() {
29
- return J().fields;
30
- }
31
- function nt(t) {
32
- const a = g(N);
33
- if (!a)
34
- throw new Error("useTableContext must be used within a FieldsProvider");
35
- return a.fields.find((n) => n.id === t) ?? null;
36
- }
37
- function lt() {
38
- return g(N) !== null;
39
- }
40
- function H(t) {
41
- var a, e, n = "";
42
- if (typeof t == "string" || typeof t == "number") n += t;
43
- else if (typeof t == "object") if (Array.isArray(t)) {
44
- var s = t.length;
45
- for (a = 0; a < s; a++) t[a] && (e = H(t[a])) && (n && (n += " "), n += e);
46
- } else for (e in t) t[e] && (n && (n += " "), n += e);
47
- return n;
48
- }
49
- function m() {
50
- for (var t, a, e = 0, n = "", s = arguments.length; e < s; e++) (t = arguments[e]) && (a = H(t)) && (n && (n += " "), n += a);
51
- return n;
52
- }
53
- const D = x.table`
54
- --table-sticky-top: ${(t) => t.sticky ? "0" : void 0};
55
-
56
- thead {
57
- tr:nth-of-type(1) td,
58
- tr:nth-of-type(1) th {
59
- top: var(--table-sticky-top, unset);
60
- position: ${(t) => t.sticky ? "sticky" : "unset"};
61
- z-index: ${(t) => t.sticky ? 10 : "unset"};
62
- background: ${(t) => t.sticky ? "linear-gradient(var(--bs-table-bg) 75%, rgba(var(--bs-secondary-bg-rgb), 0.9))" : "unset"};
63
- }
64
- }
65
- `, F = A.forwardRef(
66
- function({
67
- sticky: a,
68
- responsive: e,
69
- children: n,
70
- className: s,
71
- ...c
72
- }, i) {
73
- if (e) {
74
- const r = m(s, {
75
- "table-responsive": e === !0,
76
- [`table-responsive-${e}`]: e !== !0
77
- });
78
- return /* @__PURE__ */ l("div", { className: r, children: /* @__PURE__ */ l(D, { ref: i, ...c, children: n }) });
79
- }
80
- return /* @__PURE__ */ l(D, { className: s, sticky: a, ref: i, ...c, children: n });
81
- }
82
- );
83
- function C({
84
- field: t,
85
- className: a,
86
- children: e,
87
- ...n
88
- }) {
89
- const s = m({ [`text-${t.align}`]: !!t.align }, a);
90
- return /* @__PURE__ */ l("th", { className: s, scope: "col", ...n, children: e ?? t.title });
91
- }
92
- C.displayName = "DataTableTH";
93
- function R({ ...t }) {
94
- const a = y();
95
- return /* @__PURE__ */ l("thead", { ...t, children: /* @__PURE__ */ l("tr", { children: a.map((e, n) => /* @__PURE__ */ l(
96
- C,
97
- {
98
- ...e.thProps,
99
- field: e,
100
- className: m(
101
- typeof e.className == "function" ? { [`text-${e.align}`]: !!e.align } : e.className
102
- )
103
- },
104
- e.id ?? n
105
- )) }) });
106
- }
107
- R.displayName = "DataTableHead";
108
- function L({
109
- className: t,
110
- size: a,
111
- responsive: e,
112
- sticky: n,
113
- data: s,
114
- keyField: c,
115
- rowClassName: i,
116
- renderRow: r,
117
- onSelectRow: o,
118
- selected: u,
119
- tableHeadProps: b,
120
- children: d,
121
- tfoot: f,
122
- ...h
123
- }) {
124
- const T = m("table", t, {
125
- [`table-${a}`]: !!a
126
- });
127
- return /* @__PURE__ */ p(F, { sticky: n, responsive: e, className: T, ...h, children: [
128
- /* @__PURE__ */ l(k, {}),
129
- /* @__PURE__ */ l(R, { ...b }),
130
- !!s.length && /* @__PURE__ */ l(
131
- $,
132
- {
133
- data: s,
134
- keyField: c,
135
- rowClassName: i,
136
- renderRow: r,
137
- onSelectRow: o,
138
- selected: u
139
- }
140
- ),
141
- d,
142
- f
143
- ] });
144
- }
145
- function q({
146
- fields: t,
147
- ...a
148
- }) {
149
- return /* @__PURE__ */ l(S, { initialFields: t, children: /* @__PURE__ */ l(L, { ...a }) });
150
- }
151
- q.displayName = "DataTable";
152
- function K({ field: t, row: a, className: e, as: n, ...s }) {
153
- const c = m(
154
- { [`text-${t.align}`]: !!t.align },
155
- e,
156
- typeof t.className == "function" ? t.className(a) : t.className
157
- );
158
- return G(
159
- n ?? t.as ?? "td",
160
- {
161
- className: c,
162
- scope: (n ?? t.as) === "th" ? "row" : void 0,
163
- colSpan: t.colSpan,
164
- ...t.cellProps,
165
- ...s
166
- },
167
- a[t.field] === void 0 && !t.render ? null : typeof t.render == "function" ? t.render(a) : a[t.field]
168
- );
169
- }
170
- function w({
171
- className: t,
172
- rowClassName: a,
173
- selected: e,
174
- row: n,
175
- trRef: s,
176
- onClick: c,
177
- ...i
178
- }) {
179
- const r = y(), o = (b) => {
180
- c?.(n, b);
181
- }, u = typeof a == "function" ? a(n) : a;
182
- return n ? /* @__PURE__ */ l(
183
- "tr",
184
- {
185
- ref: s,
186
- className: m({ "table-active": e }, t, u),
187
- onClick: o,
188
- ...i,
189
- children: r.map((b, d) => /* @__PURE__ */ l(K, { field: b, row: n }, b?.id ?? d))
190
- }
191
- ) : null;
192
- }
193
- w.displayName = "DataTableRow";
194
- function $({
195
- data: t,
196
- keyField: a,
197
- rowClassName: e,
198
- renderRow: n,
199
- onSelectRow: s,
200
- selected: c = "",
201
- children: i,
202
- ...r
203
- }) {
204
- return /* @__PURE__ */ p("tbody", { ...r, children: [
205
- t.map((o) => {
206
- const u = String(typeof a == "function" ? a(o) : o[a]), b = typeof c == "function" ? c(o) : u === c;
207
- return n ? n(o) : /* @__PURE__ */ l(
208
- w,
209
- {
210
- onClick: s,
211
- rowClassName: e,
212
- row: o,
213
- selected: b
214
- },
215
- u
216
- );
217
- }),
218
- i
219
- ] });
220
- }
221
- $.displayName = "DataTableTBody";
222
- const O = (t) => {
223
- if (!t)
224
- return "flex-start";
225
- switch (t) {
226
- case "end":
227
- return "flex-end";
228
- default:
229
- return "center";
230
- }
231
- }, Q = x.div`
232
- display: flex;
233
- width: 100%;
234
- flex-direction: ${(t) => t.align === "end" ? "row-reverse" : "row"};
235
- justify-content: ${(t) => O(t.align)};
236
-
237
- .sort-icon {
238
- flex-grow: ${(t) => t.align === "end" ? "1" : "0"};
239
- opacity: ${(t) => t.sorted ? 1 : 0};
240
- }
241
-
242
- &:hover .sort-icon {
243
- color: ${(t) => t.sorted ? "unset" : "var(--bs-primary)"};
244
- opacity: 0.75;
245
- transition: opacity 0.2s;
246
- }
247
- `;
248
- function j({
249
- field: t,
250
- sorted: a,
251
- ascending: e,
252
- className: n,
253
- onClick: s
254
- }) {
255
- if (!t.sortable)
256
- return /* @__PURE__ */ l(C, { field: t, className: n });
257
- const { className: c, ...i } = t.thProps ?? {}, r = m(
258
- n,
259
- c,
260
- { [`text-${t.align}`]: !!t.align }
261
- ), o = () => {
262
- s({ field: t.field, ascending: a ? !e : !0 });
263
- }, u = {
264
- "bi-arrow-down": e,
265
- "bi-arrow-up": !e
266
- };
267
- return /* @__PURE__ */ l("th", { ...i, className: m("sortable", r), scope: "col", onClick: o, children: /* @__PURE__ */ p(Q, { sorted: a, align: t.align, children: [
268
- /* @__PURE__ */ l("div", { className: "field-title", children: t.title }),
269
- /* @__PURE__ */ l("div", { className: m("me-1 sort-icon", u) })
270
- ] }) });
271
- }
272
- j.displayName = "SortableTableTH";
273
- function E({
274
- currentSort: t,
275
- onChangeSort: a
276
- }) {
277
- const e = y(), { field: n, ascending: s } = t;
278
- return /* @__PURE__ */ l("thead", { children: /* @__PURE__ */ l("tr", { children: e.map((c, i) => /* @__PURE__ */ l(
279
- j,
280
- {
281
- field: c,
282
- sorted: n === c.field,
283
- ascending: s,
284
- className: m(
285
- typeof c.className == "function" ? { [`text-${c.align}`]: !!c.align } : c.className
286
- ),
287
- onClick: a
288
- },
289
- i
290
- )) }) });
291
- }
292
- E.displayName = "SortableTableHead";
293
- function U({
294
- className: t,
295
- size: a,
296
- responsive: e,
297
- sticky: n,
298
- data: s,
299
- keyField: c,
300
- rowClassName: i,
301
- renderRow: r,
302
- onSelectRow: o,
303
- selected: u,
304
- tableHeadProps: b,
305
- children: d,
306
- tfoot: f,
307
- currentSort: h,
308
- onChangeSort: T,
309
- ...P
310
- }) {
311
- const _ = m("table", t, {
312
- [`table-${a}`]: !!a
313
- });
314
- return /* @__PURE__ */ p(F, { className: _, responsive: e, sticky: n, ...P, children: [
315
- /* @__PURE__ */ l(k, {}),
316
- /* @__PURE__ */ l(E, { currentSort: h, onChangeSort: T, ...b }),
317
- !!s.length && /* @__PURE__ */ l(
318
- $,
319
- {
320
- data: s,
321
- keyField: c,
322
- rowClassName: i,
323
- renderRow: r,
324
- onSelectRow: o,
325
- selected: u
326
- }
327
- ),
328
- d,
329
- f
330
- ] });
331
- }
332
- function X({
333
- fields: t,
334
- ...a
335
- }) {
336
- return /* @__PURE__ */ l(S, { initialFields: t, children: /* @__PURE__ */ l(U, { ...a }) });
337
- }
338
- X.displayName = "SortableTable";
339
- const Y = [10, 25, 50, 100, 250, 500, 1e3];
340
- function M({
341
- value: t,
342
- pageValues: a = Y,
343
- size: e,
344
- label: n,
345
- className: s,
346
- onChange: c,
347
- ...i
348
- }) {
349
- const r = I(), o = (d) => c(Number(d.target.value)), u = s ?? m("form-select", { [`form-select-${e}`]: !!e }), b = m("input-group", {
350
- [`input-group-${e}`]: !!e
351
- });
352
- return /* @__PURE__ */ p("div", { className: b, children: [
353
- /* @__PURE__ */ l("label", { className: "input-group-text", htmlFor: r, children: n ?? "Rows" }),
354
- /* @__PURE__ */ l(
355
- "select",
356
- {
357
- className: u,
358
- id: r,
359
- value: t,
360
- onChange: o,
361
- ...i,
362
- children: a.map((d) => /* @__PURE__ */ l("option", { value: d, children: d }, d))
363
- }
364
- )
365
- ] }, t);
366
- }
367
- M.displayName = "RowsPerPage";
368
- function Z({
369
- page: t,
370
- rowsPerPage: a,
371
- onChangePage: e,
372
- count: n,
373
- size: s,
374
- showFirst: c,
375
- showLast: i,
376
- className: r,
377
- rowsPerPageProps: o,
378
- ...u
379
- }) {
380
- const b = n === 0 ? 0 : t * a + 1, d = Math.min(t * a + a, n), f = a === 0 ? 0 : Math.floor((n - 1) / a), h = m("btn btn-link", { [`btn-${s}`]: !!s });
381
- return /* @__PURE__ */ p("div", { className: m("row g-3 justify-content-end", r), ...u, children: [
382
- !!o && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(M, { ...o, value: a, size: s }) }),
383
- /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ p("div", { className: "row g-3 flex-nowrap align-items-baseline", children: [
384
- /* @__PURE__ */ p("div", { className: "col-auto", children: [
385
- b,
386
- "-",
387
- d,
388
- " of ",
389
- n
390
- ] }),
391
- c && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
392
- "button",
393
- {
394
- className: h,
395
- disabled: t === 0,
396
- onClick: () => e(0),
397
- "aria-label": "First page",
398
- children: /* @__PURE__ */ l("span", { className: "bi-chevron-bar-left", "aria-hidden": "true" })
399
- }
400
- ) }),
401
- /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
402
- "button",
403
- {
404
- className: h,
405
- disabled: t === 0,
406
- onClick: () => e(t - 1),
407
- "aria-label": "Previous page",
408
- children: /* @__PURE__ */ l("span", { className: "bi-chevron-left", "aria-hidden": "true" })
409
- }
410
- ) }),
411
- /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
412
- "button",
413
- {
414
- className: h,
415
- disabled: t >= f,
416
- onClick: () => e(t + 1),
417
- "aria-label": "Next page",
418
- children: /* @__PURE__ */ l("span", { className: "bi-chevron-right", "aria-hidden": "true" })
419
- }
420
- ) }),
421
- i && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
422
- "button",
423
- {
424
- className: h,
425
- disabled: t >= f,
426
- onClick: () => e(f),
427
- "aria-label": "Last page",
428
- children: /* @__PURE__ */ l("span", { className: "bi-chevron-bar-right", "aria-hidden": "true" })
429
- }
430
- ) })
431
- ] }) })
432
- ] });
433
- }
434
- Z.displayname = "TablePagination";
435
- const z = x.col`
436
- &.col-collapsed {
437
- visibility: collapse;
438
- }
439
- `;
440
- function k() {
441
- const t = y();
442
- return /* @__PURE__ */ l("colgroup", { children: t.map((a, e) => /* @__PURE__ */ l(
443
- z,
444
- {
445
- className: m(a.colClassName, { "col-collapsed": a.collapse }),
446
- span: a.colSpan ?? 1
447
- },
448
- e
449
- )) });
450
- }
451
- k.displayName = "DataTableCols";
452
- export {
453
- q as DataTable,
454
- k as DataTableCols,
455
- S as DataTableProvider,
456
- w as DataTableRow,
457
- $ as DataTableTBody,
458
- C as DataTableTH,
459
- L as DataTableWithContext,
460
- M as RowsPerPage,
461
- X as SortableTable,
462
- E as SortableTableHead,
463
- j as SortableTableTH,
464
- U as SortableTableWithContext,
465
- Z as TablePagination,
466
- nt as useField,
467
- lt as useHasTableFieldsContext,
468
- J as useTableContext,
469
- y as useTableFields
470
- };
471
- //# sourceMappingURL=index.es.js.map
1
+ import { jsx as l, jsxs as f } from "react/jsx-runtime";
2
+ import { createContext as I, useState as H, useCallback as g, useMemo as J, useContext as S, createElement as L, useEffect as q, useId as K } from "react";
3
+ import F from "@emotion/styled";
4
+ const y = I(null);
5
+ function x({
6
+ children: t,
7
+ initialFields: a = [],
8
+ initialSort: e = null
9
+ }) {
10
+ const [n, s] = H(a), [r, i] = H(e), c = g(
11
+ (m) => {
12
+ s(m);
13
+ },
14
+ []
15
+ ), o = g(
16
+ (m) => {
17
+ i(m);
18
+ },
19
+ []
20
+ ), u = g((m, h) => {
21
+ const N = n.map((T) => T.id === m ? { ...T, ...h } : T);
22
+ s(N);
23
+ }, [n]), d = g((m) => n.find((h) => h.id === m), [n]), b = J(
24
+ () => ({
25
+ fields: n,
26
+ setFields: c,
27
+ sort: r,
28
+ setSort: o,
29
+ getField: d,
30
+ updateField: u
31
+ }),
32
+ [n, c, r, o, u, d]
33
+ );
34
+ return /* @__PURE__ */ l(y.Provider, { value: b, children: t });
35
+ }
36
+ x.displayName = "DataTableProvider";
37
+ function E(t) {
38
+ var a, e, n = "";
39
+ if (typeof t == "string" || typeof t == "number") n += t;
40
+ else if (typeof t == "object") if (Array.isArray(t)) {
41
+ var s = t.length;
42
+ for (a = 0; a < s; a++) t[a] && (e = E(t[a])) && (n && (n += " "), n += e);
43
+ } else for (e in t) t[e] && (n && (n += " "), n += e);
44
+ return n;
45
+ }
46
+ function p() {
47
+ for (var t, a, e = 0, n = "", s = arguments.length; e < s; e++) (t = arguments[e]) && (a = E(t)) && (n && (n += " "), n += a);
48
+ return n;
49
+ }
50
+ const w = F.table`
51
+ --table-sticky-top: ${(t) => t.sticky ? "0" : void 0};
52
+
53
+ thead {
54
+ tr:nth-of-type(1) td,
55
+ tr:nth-of-type(1) th {
56
+ top: var(--table-sticky-top, unset);
57
+ position: ${(t) => t.sticky ? "sticky" : "unset"};
58
+ z-index: ${(t) => t.sticky ? 10 : "unset"};
59
+ background: ${(t) => t.sticky ? "linear-gradient(var(--bs-table-bg) 75%, rgba(var(--bs-secondary-bg-rgb), 0.9))" : "unset"};
60
+ }
61
+ }
62
+ `;
63
+ function P({
64
+ sticky: t,
65
+ responsive: a,
66
+ children: e,
67
+ className: n,
68
+ ref: s,
69
+ ...r
70
+ }) {
71
+ if (a) {
72
+ const i = p(n, {
73
+ "table-responsive": a === !0,
74
+ [`table-responsive-${a}`]: a !== !0
75
+ });
76
+ return /* @__PURE__ */ l("div", { className: i, children: /* @__PURE__ */ l(w, { ref: s, ...r, children: e }) });
77
+ }
78
+ return /* @__PURE__ */ l(w, { className: n, sticky: t, ref: s, ...r, children: e });
79
+ }
80
+ function C({
81
+ field: t,
82
+ className: a,
83
+ children: e,
84
+ ...n
85
+ }) {
86
+ if (t.visible === !1)
87
+ return null;
88
+ const s = p({ [`text-${t.align}`]: !!t.align }, a);
89
+ return /* @__PURE__ */ l("th", { className: s, scope: "col", ...n, children: e ?? t.title });
90
+ }
91
+ C.displayName = "DataTableTH";
92
+ function v() {
93
+ const t = S(y);
94
+ if (!t)
95
+ throw new Error("useTableContext must be used within a DataTableProvider");
96
+ return [
97
+ t.fields,
98
+ t.setFields
99
+ ];
100
+ }
101
+ function j({ ...t }) {
102
+ const [a] = v();
103
+ return /* @__PURE__ */ l("thead", { ...t, children: /* @__PURE__ */ l("tr", { children: a.map((e, n) => /* @__PURE__ */ l(
104
+ C,
105
+ {
106
+ ...e.thProps,
107
+ field: e,
108
+ className: p(
109
+ typeof e.className == "function" ? { [`text-${e.align}`]: !!e.align } : e.className
110
+ )
111
+ },
112
+ String(e.id ?? n)
113
+ )) }) });
114
+ }
115
+ j.displayName = "DataTableHead";
116
+ function R({
117
+ className: t,
118
+ size: a,
119
+ responsive: e,
120
+ sticky: n,
121
+ data: s,
122
+ keyField: r,
123
+ rowClassName: i,
124
+ renderRow: c,
125
+ onSelectRow: o,
126
+ selected: u,
127
+ tableHeadProps: d,
128
+ children: b,
129
+ tfoot: m,
130
+ ...h
131
+ }) {
132
+ const N = p("table", t, {
133
+ [`table-${a}`]: !!a
134
+ });
135
+ return /* @__PURE__ */ f(P, { sticky: n, responsive: e, className: N, ...h, children: [
136
+ /* @__PURE__ */ l(k, {}),
137
+ /* @__PURE__ */ l(j, { ...d }),
138
+ !!s.length && /* @__PURE__ */ l(
139
+ D,
140
+ {
141
+ data: s,
142
+ keyField: r,
143
+ rowClassName: i,
144
+ renderRow: c,
145
+ onSelectRow: o,
146
+ selected: u
147
+ }
148
+ ),
149
+ b,
150
+ m
151
+ ] });
152
+ }
153
+ R.displayName = "DataTable";
154
+ function O({
155
+ fields: t,
156
+ ...a
157
+ }) {
158
+ return /* @__PURE__ */ l(x, { initialFields: t, children: /* @__PURE__ */ l(R, { ...a }) });
159
+ }
160
+ O.displayName = "StandaloneDataTable";
161
+ function M({ field: t, row: a, className: e, as: n, ...s }) {
162
+ if (t.visible === !1)
163
+ return null;
164
+ const r = p(
165
+ { [`text-${t.align}`]: !!t.align },
166
+ e,
167
+ typeof t.className == "function" ? t.className(a) : t.className
168
+ );
169
+ return L(
170
+ n ?? t.as ?? "td",
171
+ {
172
+ className: r,
173
+ scope: (n ?? t.as) === "th" ? "row" : void 0,
174
+ colSpan: t.colSpan,
175
+ ...t.cellProps,
176
+ ...s
177
+ },
178
+ a[t.field] === void 0 && !t.render ? null : typeof t.render == "function" ? t.render(a) : a[t.field]
179
+ );
180
+ }
181
+ M.displayName = "DataTableCell";
182
+ function W({
183
+ className: t,
184
+ rowClassName: a,
185
+ selected: e,
186
+ row: n,
187
+ trRef: s,
188
+ onClick: r,
189
+ ...i
190
+ }) {
191
+ const [c] = v(), o = (d) => {
192
+ r?.(n, d);
193
+ }, u = typeof a == "function" ? a(n) : a;
194
+ return n ? /* @__PURE__ */ l(
195
+ "tr",
196
+ {
197
+ ref: s,
198
+ className: p({ "table-active": e }, t, u),
199
+ onClick: o,
200
+ ...i,
201
+ children: c.map((d, b) => /* @__PURE__ */ l(M, { field: d, row: n }, String(d?.id ?? b)))
202
+ }
203
+ ) : null;
204
+ }
205
+ W.displayName = "DataTableRow";
206
+ function D({
207
+ data: t,
208
+ keyField: a,
209
+ rowClassName: e,
210
+ renderRow: n,
211
+ onSelectRow: s,
212
+ selected: r = "",
213
+ children: i,
214
+ ...c
215
+ }) {
216
+ return /* @__PURE__ */ f("tbody", { ...c, children: [
217
+ t.map((o) => {
218
+ const u = String(typeof a == "function" ? a(o) : o[a]), d = typeof r == "function" ? r(o) : u === r;
219
+ return n ? n(o) : /* @__PURE__ */ l(
220
+ W,
221
+ {
222
+ onClick: s,
223
+ rowClassName: e,
224
+ row: o,
225
+ selected: d
226
+ },
227
+ u
228
+ );
229
+ }),
230
+ i
231
+ ] });
232
+ }
233
+ D.displayName = "DataTableTBody";
234
+ function $() {
235
+ const t = S(y);
236
+ if (!t)
237
+ throw new Error("useTableSort must be used within a DataTableProvider");
238
+ return [
239
+ t.sort,
240
+ t.setSort
241
+ ];
242
+ }
243
+ const Q = (t) => {
244
+ if (!t)
245
+ return "flex-start";
246
+ switch (t) {
247
+ case "end":
248
+ return "flex-end";
249
+ default:
250
+ return "center";
251
+ }
252
+ }, U = F.div`
253
+ display: flex;
254
+ width: 100%;
255
+ flex-direction: ${(t) => t.align === "end" ? "row-reverse" : "row"};
256
+ justify-content: ${(t) => Q(t.align)};
257
+
258
+ .sort-icon {
259
+ flex-grow: ${(t) => t.align === "end" ? "1" : "0"};
260
+ opacity: ${(t) => t.sorted ? 1 : 0};
261
+ }
262
+
263
+ &:hover .sort-icon {
264
+ color: ${(t) => t.sorted ? "unset" : "var(--bs-primary)"};
265
+ opacity: 0.75;
266
+ transition: opacity 0.2s;
267
+ }
268
+ `;
269
+ function _({
270
+ field: t,
271
+ sorted: a,
272
+ ascending: e,
273
+ className: n,
274
+ onClick: s
275
+ }) {
276
+ if (t.visible === !1)
277
+ return null;
278
+ if (!t.sortable)
279
+ return /* @__PURE__ */ l(C, { field: t, className: n });
280
+ const { className: r, ...i } = t.thProps ?? {}, c = p(
281
+ n,
282
+ r,
283
+ { [`text-${t.align}`]: !!t.align }
284
+ ), o = () => {
285
+ s({ field: t.field, ascending: a ? !e : !0 });
286
+ }, u = {
287
+ "bi-arrow-down": e,
288
+ "bi-arrow-up": !e
289
+ };
290
+ return /* @__PURE__ */ l("th", { ...i, className: p("sortable", c), scope: "col", onClick: o, children: /* @__PURE__ */ f(U, { sorted: a, align: t.align, children: [
291
+ /* @__PURE__ */ l("div", { className: "field-title", children: t.title }),
292
+ /* @__PURE__ */ l("div", { className: p("me-1 sort-icon", u) })
293
+ ] }) });
294
+ }
295
+ _.displayName = "SortableTableTH";
296
+ function A({
297
+ onChangeSort: t
298
+ }) {
299
+ const [a] = v(), [e] = $();
300
+ return /* @__PURE__ */ l("thead", { children: /* @__PURE__ */ l("tr", { children: a.map((n, s) => /* @__PURE__ */ l(
301
+ _,
302
+ {
303
+ field: n,
304
+ sorted: e?.field === n.field,
305
+ ascending: e?.ascending,
306
+ className: p(
307
+ typeof n.className == "function" ? { [`text-${n.align}`]: !!n.align } : n.className
308
+ ),
309
+ onClick: t
310
+ },
311
+ s
312
+ )) }) });
313
+ }
314
+ A.displayName = "SortableTableHead";
315
+ function B({
316
+ onChangeSort: t
317
+ }) {
318
+ const [a] = v(), [e] = $();
319
+ return /* @__PURE__ */ l(A, { fields: a, currentSort: e, onChangeSort: t });
320
+ }
321
+ B.displayName = "SortableTableHeadWrapper";
322
+ function V({
323
+ className: t,
324
+ size: a,
325
+ responsive: e,
326
+ sticky: n,
327
+ data: s,
328
+ keyField: r,
329
+ rowClassName: i,
330
+ renderRow: c,
331
+ onSelectRow: o,
332
+ selected: u,
333
+ tableHeadProps: d,
334
+ children: b,
335
+ tfoot: m,
336
+ onChangeSort: h,
337
+ ...N
338
+ }) {
339
+ const T = p("table", t, {
340
+ [`table-${a}`]: !!a
341
+ });
342
+ return /* @__PURE__ */ f(P, { className: T, responsive: e, sticky: n, ...N, children: [
343
+ /* @__PURE__ */ l(k, {}),
344
+ /* @__PURE__ */ l(B, { onChangeSort: h, ...d }),
345
+ !!s.length && /* @__PURE__ */ l(
346
+ D,
347
+ {
348
+ data: s,
349
+ keyField: r,
350
+ rowClassName: i,
351
+ renderRow: c,
352
+ onSelectRow: o,
353
+ selected: u
354
+ }
355
+ ),
356
+ b,
357
+ m
358
+ ] });
359
+ }
360
+ V.displayName = "SortableTable";
361
+ function X({ nextSort: t }) {
362
+ const [, a] = $();
363
+ return q(() => {
364
+ console.log("setNextSort", t), a(t);
365
+ }, [t, a]), null;
366
+ }
367
+ function Y({
368
+ fields: t,
369
+ currentSort: a,
370
+ ...e
371
+ }) {
372
+ return /* @__PURE__ */ f(x, { initialFields: t, initialSort: a, children: [
373
+ /* @__PURE__ */ l(X, { nextSort: a }),
374
+ /* @__PURE__ */ l(V, { ...e })
375
+ ] });
376
+ }
377
+ Y.displayName = "StandaloneSortableTable";
378
+ const Z = [10, 25, 50, 100, 250, 500, 1e3];
379
+ function G({
380
+ value: t,
381
+ pageValues: a = Z,
382
+ size: e,
383
+ label: n,
384
+ className: s,
385
+ onChange: r,
386
+ ...i
387
+ }) {
388
+ const c = K(), o = (b) => r(Number(b.target.value)), u = s ?? p("form-select", { [`form-select-${e}`]: !!e }), d = p("input-group", {
389
+ [`input-group-${e}`]: !!e
390
+ });
391
+ return /* @__PURE__ */ f("div", { className: d, children: [
392
+ /* @__PURE__ */ l("label", { className: "input-group-text", htmlFor: c, children: n ?? "Rows" }),
393
+ /* @__PURE__ */ l(
394
+ "select",
395
+ {
396
+ className: u,
397
+ id: c,
398
+ value: t,
399
+ onChange: o,
400
+ ...i,
401
+ children: a.map((b) => /* @__PURE__ */ l("option", { value: b, children: b }, b))
402
+ }
403
+ )
404
+ ] }, t);
405
+ }
406
+ G.displayName = "RowsPerPage";
407
+ function z({
408
+ page: t,
409
+ rowsPerPage: a,
410
+ onChangePage: e,
411
+ count: n,
412
+ size: s,
413
+ showFirst: r,
414
+ showLast: i,
415
+ className: c,
416
+ rowsPerPageProps: o,
417
+ ...u
418
+ }) {
419
+ const d = n === 0 ? 0 : t * a + 1, b = Math.min(t * a + a, n), m = a === 0 ? 0 : Math.floor((n - 1) / a), h = p("btn btn-link", { [`btn-${s}`]: !!s });
420
+ return /* @__PURE__ */ f("div", { className: p("row g-3 justify-content-end", c), ...u, children: [
421
+ !!o && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(G, { ...o, value: a, size: s }) }),
422
+ /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ f("div", { className: "row g-3 flex-nowrap align-items-baseline", children: [
423
+ /* @__PURE__ */ f("div", { className: "col-auto", children: [
424
+ d,
425
+ "-",
426
+ b,
427
+ " of ",
428
+ n
429
+ ] }),
430
+ r && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
431
+ "button",
432
+ {
433
+ className: h,
434
+ disabled: t === 0,
435
+ onClick: () => e(0),
436
+ "aria-label": "First page",
437
+ children: /* @__PURE__ */ l("span", { className: "bi-chevron-bar-left", "aria-hidden": "true" })
438
+ }
439
+ ) }),
440
+ /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
441
+ "button",
442
+ {
443
+ className: h,
444
+ disabled: t === 0,
445
+ onClick: () => e(t - 1),
446
+ "aria-label": "Previous page",
447
+ children: /* @__PURE__ */ l("span", { className: "bi-chevron-left", "aria-hidden": "true" })
448
+ }
449
+ ) }),
450
+ /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
451
+ "button",
452
+ {
453
+ className: h,
454
+ disabled: t >= m,
455
+ onClick: () => e(t + 1),
456
+ "aria-label": "Next page",
457
+ children: /* @__PURE__ */ l("span", { className: "bi-chevron-right", "aria-hidden": "true" })
458
+ }
459
+ ) }),
460
+ i && /* @__PURE__ */ l("div", { className: "col-auto", children: /* @__PURE__ */ l(
461
+ "button",
462
+ {
463
+ className: h,
464
+ disabled: t >= m,
465
+ onClick: () => e(m),
466
+ "aria-label": "Last page",
467
+ children: /* @__PURE__ */ l("span", { className: "bi-chevron-bar-right", "aria-hidden": "true" })
468
+ }
469
+ ) })
470
+ ] }) })
471
+ ] });
472
+ }
473
+ z.displayname = "TablePagination";
474
+ function k() {
475
+ const [t] = v();
476
+ return /* @__PURE__ */ l("colgroup", { children: t.filter((a) => a.visible !== !1).map((a, e) => /* @__PURE__ */ l(
477
+ "col",
478
+ {
479
+ className: a.colClassName,
480
+ span: a.colSpan ?? 1
481
+ },
482
+ e
483
+ )) });
484
+ }
485
+ k.displayName = "DataTableCols";
486
+ function nt(t) {
487
+ const a = S(y);
488
+ if (!a)
489
+ throw new Error("useField must be used within a DataTableProvider");
490
+ return [
491
+ a.fields.find((e) => e.id === t) ?? null,
492
+ a.updateField
493
+ ];
494
+ }
495
+ function lt() {
496
+ const t = S(y);
497
+ if (!t)
498
+ throw new Error("useTableContext must be used within a DataTableProvider");
499
+ return t;
500
+ }
501
+ export {
502
+ O as DataTable,
503
+ k as DataTableCols,
504
+ y as DataTableContext,
505
+ x as DataTableProvider,
506
+ W as DataTableRow,
507
+ D as DataTableTBody,
508
+ C as DataTableTH,
509
+ R as DataTableWithContext,
510
+ G as RowsPerPage,
511
+ V as SortableTable,
512
+ A as SortableTableHead,
513
+ _ as SortableTableTH,
514
+ Y as StandaloneSortableTable,
515
+ z as TablePagination,
516
+ nt as useField,
517
+ lt as useTableContext,
518
+ v as useTableFields,
519
+ $ as useTableSort
520
+ };
521
+ //# sourceMappingURL=index.es.js.map