@chumsinc/sortable-tables 2.1.2 → 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 +87 -48
  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 -0
  6. package/dist/DataTableContext.d.ts +10 -0
  7. package/dist/DataTableHead.d.ts +2 -2
  8. package/dist/DataTableProvider.d.ts +12 -0
  9. package/dist/DataTableRow.d.ts +1 -1
  10. package/dist/DataTableTBody.d.ts +1 -1
  11. package/dist/DataTableTH.d.ts +6 -6
  12. package/dist/RowsPerPage.d.ts +0 -1
  13. package/dist/SortableTable.d.ts +6 -6
  14. package/dist/SortableTableHead.d.ts +6 -6
  15. package/dist/SortableTableHeadWrapper.d.ts +9 -0
  16. package/dist/SortableTableTH.d.ts +6 -6
  17. package/dist/StandaloneDataTable.d.ts +6 -0
  18. package/dist/StandaloneSortHelper.d.ts +5 -0
  19. package/dist/StandaloneSortableTable.d.ts +6 -0
  20. package/dist/Table.d.ts +6 -5
  21. package/dist/index.cjs.js +30 -28
  22. package/dist/index.cjs.js.map +1 -1
  23. package/dist/index.d.ts +20 -10
  24. package/dist/index.es.js +521 -388
  25. package/dist/index.es.js.map +1 -1
  26. package/dist/types.d.ts +104 -105
  27. package/dist/useField.d.ts +6 -0
  28. package/dist/useTableContext.d.ts +2 -0
  29. package/dist/useTableFields.d.ts +9 -0
  30. package/dist/useTableSort.d.ts +9 -0
  31. package/eslint.config.mjs +3 -3
  32. package/package.json +7 -5
  33. package/src/DataTable.tsx +23 -28
  34. package/src/DataTableCell.tsx +33 -28
  35. package/src/DataTableCols.tsx +18 -0
  36. package/src/DataTableContext.ts +13 -0
  37. package/src/DataTableHead.tsx +9 -9
  38. package/src/DataTableProvider.tsx +62 -0
  39. package/src/DataTableRow.tsx +10 -8
  40. package/src/DataTableTBody.tsx +1 -5
  41. package/src/DataTableTH.tsx +11 -9
  42. package/src/RowsPerPage.tsx +6 -9
  43. package/src/SortableTable.tsx +26 -28
  44. package/src/SortableTableHead.tsx +17 -16
  45. package/src/SortableTableHeadWrapper.tsx +20 -0
  46. package/src/SortableTableTH.tsx +11 -8
  47. package/src/StandaloneDataTable.tsx +16 -0
  48. package/src/StandaloneSortHelper.tsx +15 -0
  49. package/src/StandaloneSortableTable.tsx +21 -0
  50. package/src/Table.tsx +49 -44
  51. package/src/TablePagination.tsx +4 -6
  52. package/src/index.tsx +26 -12
  53. package/src/types.ts +127 -128
  54. package/src/useField.ts +19 -0
  55. package/src/useTableContext.ts +10 -0
  56. package/src/useTableFields.ts +20 -0
  57. package/src/useTableSort.ts +20 -0
  58. package/test/Main.tsx +37 -0
  59. package/test/TableColumnsHandler.tsx +28 -0
  60. package/test/TestTable.tsx +51 -68
  61. package/test/data.ts +232 -232
  62. package/test/index.tsx +11 -11
  63. package/test/tableFields.tsx +52 -0
  64. package/test/utils.ts +19 -0
  65. package/tsconfig.json +1 -0
  66. package/vite.config.ts +1 -2
package/dist/index.es.js CHANGED
@@ -1,388 +1,521 @@
1
- import { jsx as e, jsxs as N } from "react/jsx-runtime";
2
- import u from "classnames";
3
- import $, { useId as j } from "react";
4
- import k from "@emotion/styled";
5
- function T({
6
- field: a,
7
- className: n,
8
- children: t,
9
- ...l
10
- }) {
11
- const s = u({ [`text-${a.align}`]: !!a.align }, n);
12
- return /* @__PURE__ */ e("th", { className: s, scope: "col", ...l, children: t ?? a.title });
13
- }
14
- T.displayName = "DataTableTH";
15
- function x({ fields: a, ...n }) {
16
- return /* @__PURE__ */ e("thead", { ...n, children: /* @__PURE__ */ e("tr", { children: a.map((t, l) => /* @__PURE__ */ e(
17
- T,
18
- {
19
- ...t.thProps,
20
- field: t,
21
- className: u(
22
- typeof t.className == "function" ? { [`text-${t.align}`]: !!t.align } : t.className
23
- )
24
- },
25
- t.id ?? l
26
- )) }) });
27
- }
28
- x.displayName = "DataTableHead";
29
- function _({ field: a, row: n, className: t, as: l, ...s }) {
30
- const c = u(
31
- { [`text-${a.align}`]: !!a.align },
32
- t,
33
- typeof a.className == "function" ? a.className(n) : a.className
34
- );
35
- return $.createElement(
36
- l ?? a.as ?? "td",
37
- {
38
- className: c,
39
- scope: (l ?? a.as) === "th" ? "row" : void 0,
40
- colSpan: a.colSpan,
41
- ...a.cellProps,
42
- ...s
43
- },
44
- n[a.field] === void 0 && !a.render ? null : typeof a.render == "function" ? a.render(n) : n[a.field]
45
- );
46
- }
47
- function C({
48
- className: a,
49
- rowClassName: n,
50
- selected: t,
51
- fields: l,
52
- row: s,
53
- trRef: c,
54
- onClick: r,
55
- ...i
56
- }) {
57
- const m = (b) => {
58
- r?.(s, b);
59
- }, o = typeof n == "function" ? n(s) : n;
60
- return s ? /* @__PURE__ */ e(
61
- "tr",
62
- {
63
- ref: c,
64
- className: u({ "table-active": t }, a, o),
65
- onClick: m,
66
- ...i,
67
- children: l.map((b, d) => /* @__PURE__ */ e(_, { field: b, row: s }, d))
68
- }
69
- ) : null;
70
- }
71
- C.displayName = "DataTableRow";
72
- function v({
73
- fields: a,
74
- data: n,
75
- keyField: t,
76
- rowClassName: l,
77
- renderRow: s,
78
- onSelectRow: c,
79
- selected: r = "",
80
- children: i,
81
- ...m
82
- }) {
83
- return /* @__PURE__ */ N("tbody", { ...m, children: [
84
- n.map((o) => {
85
- const b = String(typeof t == "function" ? t(o) : o[t]), d = typeof r == "function" ? r(o) : b === r;
86
- return s ? s(o) : /* @__PURE__ */ e(
87
- C,
88
- {
89
- onClick: c,
90
- rowClassName: l,
91
- fields: a,
92
- row: o,
93
- selected: d
94
- },
95
- b
96
- );
97
- }),
98
- i
99
- ] });
100
- }
101
- v.displayName = "DataTableTBody";
102
- const g = k.table`
103
- --table-sticky-top: ${(a) => a.sticky ? "0" : void 0};
104
-
105
- thead {
106
- tr:nth-of-type(1) td,
107
- tr:nth-of-type(1) th {
108
- top: var(--table-sticky-top, unset);
109
- position: ${(a) => a.sticky ? "sticky" : "unset"};
110
- z-index: ${(a) => a.sticky ? 10 : "unset"};
111
- background: ${(a) => a.sticky ? "linear-gradient(var(--bs-table-bg) 75%, rgba(var(--bs-secondary-bg-rgb), 0.9))" : "unset"};
112
- }
113
- }
114
- `, D = $.forwardRef(
115
- function({
116
- sticky: n,
117
- responsive: t,
118
- children: l,
119
- className: s,
120
- ...c
121
- }, r) {
122
- if (t) {
123
- const i = u(s, {
124
- "table-responsive": t === !0,
125
- [`table-responsive-${t}`]: t !== !0
126
- });
127
- return /* @__PURE__ */ e("div", { className: i, children: /* @__PURE__ */ e(g, { ref: r, ...c, children: l }) });
128
- }
129
- return /* @__PURE__ */ e(g, { className: s, sticky: n, ref: r, ...c, children: l });
130
- }
131
- );
132
- function B({
133
- fields: a,
134
- data: n,
135
- keyField: t,
136
- size: l = "",
137
- sticky: s,
138
- responsive: c,
139
- rowClassName: r,
140
- renderRow: i,
141
- onSelectRow: m,
142
- selected: o = "",
143
- className: b = "",
144
- tfoot: d,
145
- children: p,
146
- tableHeadProps: h,
147
- ...y
148
- }) {
149
- const f = u("table", b, {
150
- [`table-${l}`]: !!l
151
- });
152
- return /* @__PURE__ */ N(D, { sticky: s, responsive: c, className: f, ...y, children: [
153
- /* @__PURE__ */ e(x, { ...h, fields: a }),
154
- !!n.length && /* @__PURE__ */ e(
155
- v,
156
- {
157
- fields: a,
158
- data: n,
159
- keyField: t,
160
- rowClassName: r,
161
- renderRow: i,
162
- onSelectRow: m,
163
- selected: o
164
- }
165
- ),
166
- p,
167
- d
168
- ] });
169
- }
170
- B.displayName = "DataTable";
171
- const M = (a) => {
172
- if (!a)
173
- return "flex-start";
174
- switch (a) {
175
- case "end":
176
- return "flex-end";
177
- default:
178
- return "center";
179
- }
180
- }, V = k.div`
181
- display: flex;
182
- width: 100%;
183
- flex-direction: ${(a) => a.align === "end" ? "row-reverse" : "row"};
184
- justify-content: ${(a) => M(a.align)};
185
- .sort-icon {
186
- flex-grow: ${(a) => a.align === "end" ? "1" : "0"};
187
- opacity: ${(a) => a.sorted ? 1 : 0};
188
- }
189
- &:hover .sort-icon {
190
- color: ${(a) => a.sorted ? "unset" : "var(--bs-primary)"} ;
191
- opacity: 0.75;
192
- transition: opacity 0.2s;
193
- }
194
- `;
195
- function H({
196
- field: a,
197
- sorted: n,
198
- ascending: t,
199
- className: l,
200
- onClick: s
201
- }) {
202
- if (!a.sortable)
203
- return /* @__PURE__ */ e(T, { field: a, className: l });
204
- const { className: c, ...r } = a.thProps ?? {}, i = u(
205
- l,
206
- c,
207
- { [`text-${a.align}`]: !!a.align }
208
- ), m = () => {
209
- s({ field: a.field, ascending: n ? !t : !0 });
210
- }, o = {
211
- "bi-arrow-down": t,
212
- "bi-arrow-up": !t
213
- };
214
- return /* @__PURE__ */ e("th", { ...r, className: u("sortable", i), scope: "col", onClick: m, children: /* @__PURE__ */ N(V, { sorted: n, align: a.align, children: [
215
- /* @__PURE__ */ e("div", { className: "field-title", children: a.title }),
216
- /* @__PURE__ */ e("div", { className: u("me-1 sort-icon", o) })
217
- ] }) });
218
- }
219
- H.displayName = "SortableTableTH";
220
- function S({
221
- currentSort: a,
222
- fields: n,
223
- onChangeSort: t
224
- }) {
225
- const { field: l, ascending: s } = a;
226
- return /* @__PURE__ */ e("thead", { children: /* @__PURE__ */ e("tr", { children: n.map((c, r) => /* @__PURE__ */ e(
227
- H,
228
- {
229
- field: c,
230
- sorted: l === c.field,
231
- ascending: s,
232
- className: u(
233
- typeof c.className == "function" ? { [`text-${c.align}`]: !!c.align } : c.className
234
- ),
235
- onClick: t
236
- },
237
- r
238
- )) }) });
239
- }
240
- S.displayName = "SortableTableHead";
241
- function E({
242
- fields: a,
243
- data: n,
244
- currentSort: t,
245
- onChangeSort: l,
246
- keyField: s,
247
- size: c = "",
248
- sticky: r,
249
- rowClassName: i,
250
- renderRow: m,
251
- onSelectRow: o,
252
- selected: b = "",
253
- className: d = "",
254
- tfoot: p,
255
- children: h,
256
- ...y
257
- }) {
258
- const f = u("table", d, {
259
- [`table-${c}`]: !!c
260
- });
261
- return /* @__PURE__ */ N(D, { className: f, sticky: r, ...y, children: [
262
- /* @__PURE__ */ e(S, { currentSort: t, fields: a, onChangeSort: l }),
263
- !!n.length && /* @__PURE__ */ e(
264
- v,
265
- {
266
- fields: a,
267
- data: n,
268
- keyField: s,
269
- rowClassName: i,
270
- renderRow: m,
271
- onSelectRow: o,
272
- selected: b
273
- }
274
- ),
275
- h,
276
- p
277
- ] });
278
- }
279
- E.displayName = "SortableTable";
280
- const G = [10, 25, 50, 100, 250, 500, 1e3];
281
- function R({
282
- value: a,
283
- pageValues: n = G,
284
- size: t,
285
- label: l,
286
- className: s,
287
- onChange: c,
288
- ...r
289
- }) {
290
- const i = j(), m = (d) => c(Number(d.target.value)), o = s ?? u("form-select", { [`form-select-${t}`]: !!t }), b = u("input-group", {
291
- [`input-group-${t}`]: !!t
292
- });
293
- return /* @__PURE__ */ N("div", { className: b, children: [
294
- /* @__PURE__ */ e("label", { className: "input-group-text", htmlFor: i, children: l ?? "Rows" }),
295
- /* @__PURE__ */ e(
296
- "select",
297
- {
298
- className: o,
299
- id: i,
300
- value: a,
301
- onChange: m,
302
- ...r,
303
- children: n.map((d) => /* @__PURE__ */ e("option", { value: d, children: d }, d))
304
- }
305
- )
306
- ] }, a);
307
- }
308
- R.displayName = "RowsPerPage";
309
- function I({
310
- page: a,
311
- rowsPerPage: n,
312
- onChangePage: t,
313
- count: l,
314
- size: s,
315
- showFirst: c,
316
- showLast: r,
317
- className: i,
318
- rowsPerPageProps: m,
319
- ...o
320
- }) {
321
- const b = l === 0 ? 0 : a * n + 1, d = Math.min(a * n + n, l), p = n === 0 ? 0 : Math.floor((l - 1) / n), h = u("btn btn-link", { [`btn-${s}`]: !!s });
322
- return /* @__PURE__ */ N("div", { className: u("row g-3 justify-content-end", i), ...o, children: [
323
- !!m && /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ e(R, { ...m, value: n, size: s }) }),
324
- /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ N("div", { className: "row g-3 flex-nowrap align-items-baseline", children: [
325
- /* @__PURE__ */ N("div", { className: "col-auto", children: [
326
- b,
327
- "-",
328
- d,
329
- " of ",
330
- l
331
- ] }),
332
- c && /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ e(
333
- "button",
334
- {
335
- className: h,
336
- disabled: a === 0,
337
- onClick: () => t(0),
338
- "aria-label": "First page",
339
- children: /* @__PURE__ */ e("span", { className: "bi-chevron-bar-left", "aria-hidden": "true" })
340
- }
341
- ) }),
342
- /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ e(
343
- "button",
344
- {
345
- className: h,
346
- disabled: a === 0,
347
- onClick: () => t(a - 1),
348
- "aria-label": "Previous page",
349
- children: /* @__PURE__ */ e("span", { className: "bi-chevron-left", "aria-hidden": "true" })
350
- }
351
- ) }),
352
- /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ e(
353
- "button",
354
- {
355
- className: h,
356
- disabled: a >= p,
357
- onClick: () => t(a + 1),
358
- "aria-label": "Next page",
359
- children: /* @__PURE__ */ e("span", { className: "bi-chevron-right", "aria-hidden": "true" })
360
- }
361
- ) }),
362
- r && /* @__PURE__ */ e("div", { className: "col-auto", children: /* @__PURE__ */ e(
363
- "button",
364
- {
365
- className: h,
366
- disabled: a >= p,
367
- onClick: () => t(p),
368
- "aria-label": "Last page",
369
- children: /* @__PURE__ */ e("span", { className: "bi-chevron-bar-right", "aria-hidden": "true" })
370
- }
371
- ) })
372
- ] }) })
373
- ] });
374
- }
375
- I.displayname = "TablePagination";
376
- export {
377
- B as DataTable,
378
- C as DataTableRow,
379
- v as DataTableTBody,
380
- T as DataTableTH,
381
- R as RowsPerPage,
382
- E as SortableTable,
383
- S as SortableTableHead,
384
- H as SortableTableTH,
385
- I as TablePagination,
386
- G as defaultRowsPerPageValues
387
- };
388
- //# 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