@7span/react-list 0.0.4 → 0.0.6

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,7 +1,7 @@
1
- import { jsx as i, jsxs as z } from "react/jsx-runtime";
2
- import { createContext as $, useContext as j, useState as T, useMemo as P, memo as w, useCallback as k, useEffect as E, useRef as q } from "react";
3
- const A = $(null), H = ({ children: r, config: o }) => {
4
- const { requestHandler: n, stateManager: e = {} } = o, [s, a] = T({
1
+ import { jsx as c, jsxs as I } from "react/jsx-runtime";
2
+ import { createContext as V, useContext as X, useState as G, useMemo as C, memo as S, useCallback as z, useRef as Q, useEffect as _ } from "react";
3
+ const U = V(null), j = ({ children: s, config: i }) => {
4
+ const { requestHandler: a, stateManager: n = {} } = i, [r, l] = G({
5
5
  data: [],
6
6
  response: null,
7
7
  error: null,
@@ -25,222 +25,305 @@ const A = $(null), H = ({ children: r, config: o }) => {
25
25
  isEmpty: !0,
26
26
  isInitializing: !0
27
27
  });
28
- if (!n)
28
+ if (!a)
29
29
  throw new Error("ListProvider: requestHandler is required.");
30
- const p = P(
30
+ const f = C(
31
31
  () => ({
32
- requestHandler: n,
33
- stateManager: e,
34
- listState: s,
35
- setListState: a
32
+ requestHandler: a,
33
+ stateManager: n,
34
+ listState: r,
35
+ setListState: l
36
36
  }),
37
- [n, e, s]
37
+ [a, n, r]
38
38
  );
39
- return /* @__PURE__ */ i(A.Provider, { value: p, children: r });
40
- }, R = () => {
41
- const r = j(A);
42
- if (!r)
39
+ return /* @__PURE__ */ c(U.Provider, { value: f, children: s });
40
+ }, x = () => {
41
+ const s = X(U);
42
+ if (!s)
43
43
  throw new Error("useListContext must be used within a ListProvider");
44
- return r;
45
- }, W = w(({ children: r, renderAttribute: o }) => {
46
- const { listState: n } = R(), { attrs: e, attrSettings: s, updateAttr: a } = n, p = k(
47
- (u) => (m) => {
48
- a(u, "visible", m.target.checked);
44
+ return s;
45
+ }, ee = S(({ children: s, renderAttribute: i }) => {
46
+ const { listState: a } = x(), { attrs: n, attrSettings: r, updateAttr: l } = a, f = z(
47
+ (u) => (h) => {
48
+ l(u, "visible", h.target.checked);
49
49
  },
50
- [a]
51
- ), h = P(
50
+ [l]
51
+ ), m = C(
52
52
  () => ({
53
- attrs: e,
54
- attrSettings: s,
55
- updateAttr: a
53
+ attrs: n,
54
+ attrSettings: r,
55
+ updateAttr: l
56
56
  }),
57
- [e, s, a]
57
+ [n, r, l]
58
58
  );
59
- return r ? r(h) : /* @__PURE__ */ i("div", { className: "react-list-attributes", children: e.map((u, m) => {
60
- var l;
61
- return o ? o({
62
- key: `attr-${m}`,
59
+ return s ? s(m) : /* @__PURE__ */ c("div", { className: "react-list-attributes", children: n.map((u, h) => {
60
+ var g;
61
+ return i ? i({
62
+ key: `attr-${h}`,
63
63
  attr: u,
64
- updateAttr: a,
65
- attrSettings: s
66
- }) : /* @__PURE__ */ z("label", { children: [
67
- /* @__PURE__ */ i("span", { children: u.label }),
68
- /* @__PURE__ */ i(
64
+ updateAttr: l,
65
+ attrSettings: r
66
+ }) : /* @__PURE__ */ I("label", { children: [
67
+ /* @__PURE__ */ c("span", { children: u.label }),
68
+ /* @__PURE__ */ c(
69
69
  "input",
70
70
  {
71
71
  type: "checkbox",
72
- checked: ((l = s == null ? void 0 : s[u.name]) == null ? void 0 : l.visible) ?? !1,
73
- onChange: p(u.name)
72
+ checked: ((g = r == null ? void 0 : r[u.name]) == null ? void 0 : g.visible) ?? !1,
73
+ onChange: f(u.name)
74
74
  }
75
75
  )
76
- ] }, `attr-${m}`);
76
+ ] }, `attr-${h}`);
77
77
  }) });
78
- }), F = w(({ children: r }) => {
79
- const { listState: o } = R(), { data: n, loader: e, error: s } = o, { isLoading: a, initialLoading: p } = e;
80
- return (n == null ? void 0 : n.length) > 0 || p || a || s ? null : /* @__PURE__ */ i("div", { className: "react-list-empty", children: r || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "No data found!" }) }) });
81
- }), G = w(({ children: r }) => {
82
- const { listState: o } = R(), { error: n, loader: e } = o, { isLoading: s } = e;
83
- return !n || s ? null : /* @__PURE__ */ i("div", { className: "react-list-error", children: typeof r == "function" ? r({ error: n }) : r || /* @__PURE__ */ z("div", { children: [
84
- /* @__PURE__ */ i("h3", { children: "Error occurred" }),
85
- /* @__PURE__ */ z("pre", { children: [
86
- n.name,
78
+ }), te = S(({ children: s }) => {
79
+ const { listState: i } = x(), { data: a, loader: n, error: r } = i, { isLoading: l, initialLoading: f } = n;
80
+ return (a == null ? void 0 : a.length) > 0 || f || l || r ? null : /* @__PURE__ */ c("div", { className: "react-list-empty", children: s || /* @__PURE__ */ c("div", { children: /* @__PURE__ */ c("p", { children: "No data found!" }) }) });
81
+ }), se = S(({ children: s }) => {
82
+ const { listState: i } = x(), { error: a, loader: n } = i, { isLoading: r } = n;
83
+ return !a || r ? null : /* @__PURE__ */ c("div", { className: "react-list-error", children: typeof s == "function" ? s({ error: a }) : s || /* @__PURE__ */ I("div", { children: [
84
+ /* @__PURE__ */ c("h3", { children: "Error occurred" }),
85
+ /* @__PURE__ */ I("pre", { children: [
86
+ a.name,
87
87
  ": ",
88
- n.message
88
+ a.message
89
89
  ] })
90
90
  ] }) });
91
- }), J = w(({ children: r }) => {
92
- const { listState: o } = R(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: m } = s, { initialLoading: l, isLoading: L } = p, { pages: y, pagesCount: S } = P(() => {
93
- const t = Math.ceil(e / m);
94
- return { pages: Array.from({ length: t }, (M, b) => b + 1), pagesCount: t };
95
- }, [e, m]), N = k(
96
- (t) => {
97
- a(Number(t.target.value));
91
+ }), ne = S(({ children: s }) => {
92
+ const { listState: i } = x(), { data: a, count: n, pagination: r, setPage: l, loader: f, error: m } = i, { page: u, perPage: h } = r, { initialLoading: g, isLoading: y } = f, { pages: R, pagesCount: L } = C(() => {
93
+ const O = Math.ceil(n / h);
94
+ return { pages: Array.from({ length: O }, ($, p) => p + 1), pagesCount: O };
95
+ }, [n, h]), E = z(
96
+ (O) => {
97
+ l(Number(O.target.value));
98
98
  },
99
- [a]
100
- ), v = P(
99
+ [l]
100
+ ), P = C(
101
101
  () => ({
102
- setPage: a,
102
+ setPage: l,
103
103
  page: u,
104
- pages: y,
105
- pagesCount: S
104
+ pages: R,
105
+ pagesCount: L
106
106
  }),
107
- [a, u, y, S]
107
+ [l, u, R, L]
108
108
  );
109
- return l || !n || n.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-go-to", children: r ? r(v) : /* @__PURE__ */ i("select", { value: u, onChange: N, children: y.map((t) => /* @__PURE__ */ z("option", { value: t, children: [
109
+ return g || !a || a.length === 0 || m ? null : /* @__PURE__ */ c("div", { className: "react-list-go-to", children: s ? s(P) : /* @__PURE__ */ c("select", { value: u, onChange: E, children: R.map((O) => /* @__PURE__ */ I("option", { value: O, children: [
110
110
  "Page ",
111
- t
112
- ] }, `page-${t}`)) }) });
113
- }), K = w(({ children: r }) => {
114
- const { listState: o } = R(), { loader: n } = o, { initialLoading: e } = n;
115
- return P(
111
+ O
112
+ ] }, `page-${O}`)) }) });
113
+ }), re = S(({ children: s }) => {
114
+ const { listState: i } = x(), { loader: a } = i, { initialLoading: n } = a;
115
+ return C(
116
116
  () => ({
117
- loading: e
117
+ loading: n
118
118
  }),
119
- [e]
120
- ), e ? /* @__PURE__ */ i("div", { className: "react-list-initial-loader", children: r || /* @__PURE__ */ i("p", { children: "Initial Loading..." }) }) : null;
121
- }), Q = w(({ children: r, renderItem: o }) => {
122
- const { listState: n } = R(), { data: e = [], loader: s, error: a, setSort: p, sort: h } = n, { initialLoading: u } = s, m = P(
119
+ [n]
120
+ ), n ? /* @__PURE__ */ c("div", { className: "react-list-initial-loader", children: s || /* @__PURE__ */ c("p", { children: "Initial Loading..." }) }) : null;
121
+ }), ie = S(({ children: s, renderItem: i }) => {
122
+ const { listState: a } = x(), { data: n = [], loader: r, error: l, setSort: f, sort: m } = a, { initialLoading: u } = r, h = C(
123
123
  () => ({
124
- items: e,
125
- setSort: p,
126
- sort: h
124
+ items: n,
125
+ setSort: f,
126
+ sort: m
127
127
  }),
128
- [e, h, p]
128
+ [n, m, f]
129
129
  );
130
- return u || !e || e.length === 0 || a ? null : o ? /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, L) => /* @__PURE__ */ i("div", { children: o({ item: l, index: L }) }, l.id || L)) }) : typeof r == "function" ? /* @__PURE__ */ i("div", { className: "react-list-items", children: r(m) }) : /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, L) => /* @__PURE__ */ i("pre", { children: JSON.stringify(l, null, 2) }, l.id || L)) });
131
- }), U = ({
132
- initialItems: r = [],
133
- children: o,
134
- endpoint: n,
135
- page: e = 1,
136
- perPage: s = 25,
137
- sortBy: a = "",
138
- sortOrder: p = "desc",
139
- search: h = "",
140
- filters: u = {},
141
- attrs: m,
142
- version: l = 1,
143
- paginationMode: L = "pagination",
144
- meta: y = {}
130
+ return u || !n || n.length === 0 || l ? null : i ? /* @__PURE__ */ c("div", { className: "react-list-items", children: n.map((g, y) => /* @__PURE__ */ c("div", { children: i({ item: g, index: y }) }, g.id || y)) }) : typeof s == "function" ? /* @__PURE__ */ c("div", { className: "react-list-items", children: s(h) }) : /* @__PURE__ */ c("div", { className: "react-list-items", children: n.map((g, y) => /* @__PURE__ */ c("pre", { children: JSON.stringify(g, null, 2) }, g.id || y)) });
131
+ }), W = (s, i) => {
132
+ if (s === i) return !0;
133
+ if (s == null || i == null || typeof s != "object" || typeof i != "object")
134
+ return s === i;
135
+ if (Array.isArray(s) && Array.isArray(i)) {
136
+ if (s.length !== i.length) return !1;
137
+ for (let r = 0; r < s.length; r++)
138
+ if (!W(s[r], i[r])) return !1;
139
+ return !0;
140
+ }
141
+ if (Array.isArray(s) || Array.isArray(i)) return !1;
142
+ const a = Object.keys(s).filter((r) => s[r] !== void 0), n = Object.keys(i).filter((r) => i[r] !== void 0);
143
+ if (a.length !== n.length) return !1;
144
+ for (let r of a)
145
+ if (!n.includes(r) || !W(s[r], i[r])) return !1;
146
+ return !0;
147
+ }, Y = (s, i) => !i || Object.keys(i).length === 0 ? s && Object.keys(s).length > 0 : !s || Object.keys(s).length === 0 ? !1 : !W(s, i), ae = ({
148
+ initialItems: s = [],
149
+ children: i,
150
+ endpoint: a,
151
+ page: n = 1,
152
+ perPage: r = 25,
153
+ sortBy: l = "",
154
+ sortOrder: f = "desc",
155
+ count: m = 0,
156
+ search: u = "",
157
+ filters: h = {},
158
+ attrs: g,
159
+ version: y = 1,
160
+ paginationMode: R = "pagination",
161
+ meta: L = {},
162
+ onResponse: E,
163
+ afterPageChange: P,
164
+ afterLoadMore: O
145
165
  }) => {
146
- const { requestHandler: S, setListState: N } = R(), v = L === "loadMore", [t, C] = T({
147
- page: e,
148
- perPage: s,
149
- sortBy: a,
150
- sortOrder: p,
151
- search: h,
152
- filters: u,
153
- attrSettings: {},
154
- items: r,
155
- selection: [],
156
- error: null,
157
- response: null,
158
- count: 0,
159
- isLoading: !1,
160
- initializingState: !0
161
- }), M = k(
162
- async (d = {}, g = null) => {
163
- t.initializingState || C((c) => ({ ...c, error: null, isLoading: !0 }));
166
+ const { requestHandler: M, setListState: $, stateManager: p } = x(), q = Q(!1), k = R === "loadMore", A = z(
167
+ (e) => ({
168
+ endpoint: a,
169
+ version: y,
170
+ meta: L,
171
+ search: (e == null ? void 0 : e.search) || u,
172
+ page: (e == null ? void 0 : e.page) || n,
173
+ perPage: (e == null ? void 0 : e.perPage) || r,
174
+ sortBy: (e == null ? void 0 : e.sortBy) || l,
175
+ sortOrder: (e == null ? void 0 : e.sortOrder) || f,
176
+ filters: (e == null ? void 0 : e.filters) || h,
177
+ attrSettings: (e == null ? void 0 : e.attrSettings) || {},
178
+ isRefresh: !1
179
+ }),
180
+ [a, y, L, u, n, r, l, f, h]
181
+ ), w = z(() => {
182
+ var e;
183
+ try {
184
+ const d = A(), o = (e = p == null ? void 0 : p.get) == null ? void 0 : e.call(p, d);
185
+ return {
186
+ page: o == null ? void 0 : o.page,
187
+ perPage: o == null ? void 0 : o.perPage,
188
+ sortBy: o == null ? void 0 : o.sortBy,
189
+ sortOrder: o == null ? void 0 : o.sortOrder,
190
+ search: o == null ? void 0 : o.search,
191
+ attrSettings: o == null ? void 0 : o.attrSettings,
192
+ filters: o == null ? void 0 : o.filters
193
+ };
194
+ } catch (d) {
195
+ return console.error(d), {};
196
+ }
197
+ }, [A, p]), B = z(() => {
198
+ const e = w();
199
+ let d = n;
200
+ return k ? d = 1 : e.page != null && (d = e.page), {
201
+ page: d,
202
+ perPage: e.perPage != null ? e.perPage : r,
203
+ sortBy: e.sortBy != null ? e.sortBy : l,
204
+ sortOrder: e.sortOrder != null ? e.sortOrder : f,
205
+ search: e.search != null ? e.search : u,
206
+ filters: e.filters != null ? e.filters : h,
207
+ attrSettings: e.attrSettings || {},
208
+ items: s,
209
+ selection: [],
210
+ error: null,
211
+ response: null,
212
+ count: 0,
213
+ isLoading: !1,
214
+ initializingState: !s.length,
215
+ confirmedPage: null
216
+ };
217
+ }, [
218
+ w,
219
+ u,
220
+ n,
221
+ r,
222
+ l,
223
+ f,
224
+ u,
225
+ h,
226
+ k
227
+ ]), [t, b] = G(B), T = z(
228
+ (e) => {
229
+ var d;
230
+ if (p) {
231
+ const o = A(e);
232
+ (d = p == null ? void 0 : p.set) == null || d.call(p, o);
233
+ }
234
+ },
235
+ [p, A]
236
+ ), N = z(
237
+ async (e = {}, d = null) => {
238
+ t.initializingState || b((o) => ({ ...o, error: null, isLoading: !0 }));
164
239
  try {
165
- const c = g || t, f = await S({
166
- endpoint: n,
167
- version: l,
168
- meta: y,
169
- page: c.page,
170
- perPage: c.perPage,
171
- search: c.search,
172
- sortBy: c.sortBy,
173
- sortOrder: c.sortOrder,
174
- filters: c.filters,
175
- ...d
240
+ const o = d || t, v = await M({
241
+ endpoint: a,
242
+ version: y,
243
+ meta: L,
244
+ page: o.page,
245
+ perPage: o.perPage,
246
+ search: o.search,
247
+ sortBy: o.sortBy,
248
+ sortOrder: o.sortOrder,
249
+ filters: o.filters,
250
+ ...e
176
251
  });
177
- C((x) => ({
178
- ...x,
179
- response: f,
252
+ E && E(v);
253
+ let H;
254
+ k ? (prev.page === 1 ? H = v.items : H = [...prev.items, ...v.items], O && O(v)) : (H = v.items, P && P(v));
255
+ const K = {
256
+ ...o,
257
+ response: v,
180
258
  selection: [],
181
259
  // Append items for loadMore, replace for pagination
182
- items: v && c.page > 1 ? [...x.items, ...f.items] : f.items,
183
- count: f.count,
260
+ items: k && o.page > 1 ? [...prev.items, ...v.items] : v.items,
261
+ count: v.count,
184
262
  initializingState: !1,
185
263
  isLoading: !1
186
- }));
187
- } catch (c) {
188
- throw C((f) => ({
189
- ...f,
190
- error: c,
264
+ };
265
+ T(K), b(K);
266
+ } catch (o) {
267
+ throw b((v) => ({
268
+ ...v,
269
+ error: o,
191
270
  items: [],
192
271
  count: 0,
193
272
  initializingState: !1,
194
273
  isLoading: !1
195
- })), c;
274
+ })), o;
196
275
  }
197
276
  },
198
- [n, l, v, y, S, t]
199
- ), b = P(
277
+ [a, y, k, L, M, t]
278
+ ), D = C(
200
279
  () => ({
201
- setPage: (d, g) => {
202
- let c = d;
203
- d === 0 && (c = "");
204
- const f = { ...t, page: c };
205
- C(f), c && M(g, f);
280
+ setPage: (e, d) => {
281
+ let o = e;
282
+ e === 0 && (o = "");
283
+ const v = { ...t, page: o };
284
+ b(v), o && N(d, v);
206
285
  },
207
- setPerPage: (d) => {
208
- const g = { ...t, perPage: d, page: 1 };
209
- C(g), M({}, g);
286
+ setPerPage: (e) => {
287
+ const d = { ...t, perPage: e, page: 1 };
288
+ b(d), N({}, d);
210
289
  },
211
- setSearch: (d) => {
212
- if (d !== t.search) {
213
- const g = { ...t, search: d, page: 1 };
214
- C(g), M({}, g);
290
+ setSearch: (e) => {
291
+ if (e !== t.search) {
292
+ const d = { ...t, search: e, page: 1 };
293
+ b(d), N({}, d);
215
294
  }
216
295
  },
217
- setSort: ({ by: d, order: g }) => {
218
- const c = { ...t, sortBy: d, sortOrder: g, page: 1 };
219
- C(c), M({}, c);
296
+ setSort: ({ by: e, order: d }) => {
297
+ const o = { ...t, sortBy: e, sortOrder: d, page: 1 };
298
+ b(o), N({}, o);
220
299
  },
221
300
  loadMore: () => {
222
- const d = { ...t, page: t.page + 1 };
223
- C(d), M({}, d);
301
+ const e = { ...t, page: t.page + 1 };
302
+ b(e), N({}, e);
224
303
  },
225
- refresh: (d = { isRefresh: !0 }) => {
226
- if (v) {
227
- const g = { ...t, page: 1, items: [] };
228
- C(g), M(d, g);
304
+ clearFilters: () => {
305
+ const e = { ...t, filters: h, page: 1 };
306
+ b(e), N({}, e);
307
+ },
308
+ refresh: (e = { isRefresh: !0 }) => {
309
+ if (k) {
310
+ const d = { ...t, page: 1, items: [] };
311
+ b(d), N(e, d);
229
312
  } else
230
- M(d);
313
+ N(e);
231
314
  },
232
- setFilters: (d) => {
233
- const g = { ...t, filters: d, page: 1 };
234
- C(g), M({}, g);
315
+ setFilters: (e) => {
316
+ const d = { ...t, filters: e, page: 1 };
317
+ b(d), N({}, d);
235
318
  },
236
- updateItemById: (d, g) => {
237
- const c = t.items.map((f) => f.id === g ? { ...f, ...d } : f);
238
- C((f) => ({ ...f, items: c }));
319
+ updateItemById: (e, d) => {
320
+ const o = t.items.map((v) => v.id === d ? { ...v, ...e } : v);
321
+ b((v) => ({ ...v, items: o }));
239
322
  },
240
- setSelection: (d) => C((g) => ({ ...g, selection: d }))
323
+ setSelection: (e) => b((d) => ({ ...d, selection: e }))
241
324
  }),
242
- [M, v, t]
243
- ), O = P(
325
+ [N, k, t]
326
+ ), J = C(
244
327
  () => ({
245
328
  data: t.items,
246
329
  response: t.response,
@@ -257,11 +340,12 @@ const A = $(null), H = ({ children: r, config: o }) => {
257
340
  initialLoading: t.initializingState
258
341
  },
259
342
  sort: { sortBy: t.sortBy, sortOrder: t.sortOrder },
343
+ hasActiveFilters: Y(t.filters, h),
260
344
  search: t.search,
261
345
  filters: t.filters,
262
- attrs: m || Object.keys(t.items[0] || {}),
346
+ attrs: g || Object.keys(t.items[0] || {}),
263
347
  isEmpty: t.items.length === 0,
264
- ...b
348
+ ...D
265
349
  }),
266
350
  [
267
351
  t.items,
@@ -277,16 +361,22 @@ const A = $(null), H = ({ children: r, config: o }) => {
277
361
  t.sortOrder,
278
362
  t.search,
279
363
  t.filters,
280
- b,
281
- m
364
+ D,
365
+ g
282
366
  ]
283
367
  );
284
- return E(() => {
285
- t.initializingState && b.setPage(t.page);
286
- }, []), E(() => {
287
- N(O);
368
+ return _(() => {
369
+ if (t.initializingState && !q.current) {
370
+ if (q.current = !0, p != null && p.init) {
371
+ const e = A(t);
372
+ p.init(e);
373
+ }
374
+ s.length || D.setPage(t.page);
375
+ }
376
+ }, []), _(() => {
377
+ $(J);
288
378
  }, [
289
- N,
379
+ $,
290
380
  t.items,
291
381
  t.count,
292
382
  t.error,
@@ -296,213 +386,213 @@ const A = $(null), H = ({ children: r, config: o }) => {
296
386
  t.perPage,
297
387
  t.sortBy,
298
388
  t.sortOrder
299
- ]), typeof o == "function" ? o(O) : o;
300
- }, V = w(({ children: r }) => {
301
- const { listState: o } = R(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: m } = s, { isLoading: l } = p, L = P(
302
- () => u * m < e,
303
- [u, m, e]
304
- ), y = k(() => {
305
- L && !l && a(u + 1);
306
- }, [L, l, a, u]), S = P(
389
+ ]), typeof i == "function" ? i(J) : i;
390
+ }, oe = S(({ children: s }) => {
391
+ const { listState: i } = x(), { data: a, count: n, pagination: r, setPage: l, loader: f, error: m } = i, { page: u, perPage: h } = r, { isLoading: g } = f, y = C(
392
+ () => u * h < n,
393
+ [u, h, n]
394
+ ), R = z(() => {
395
+ y && !g && l(u + 1);
396
+ }, [y, g, l, u]), L = C(
307
397
  () => ({
308
- isLoading: l,
309
- loadMore: y,
310
- hasMoreItems: L
398
+ isLoading: g,
399
+ loadMore: R,
400
+ hasMoreItems: y
311
401
  }),
312
- [l, y, L]
402
+ [g, R, y]
313
403
  );
314
- return !n || n.length === 0 || h ? null : r(S);
315
- }), X = w(({ children: r, position: o = "overlay" }) => {
316
- const { listState: n } = R(), { loader: e } = n, { isLoading: s, initializingState: a } = e;
317
- return P(
404
+ return !a || a.length === 0 || m ? null : s(L);
405
+ }), le = S(({ children: s, position: i = "overlay" }) => {
406
+ const { listState: a } = x(), { loader: n } = a, { isLoading: r, initializingState: l } = n;
407
+ return C(
318
408
  () => ({
319
- isLoading: s
409
+ isLoading: r
320
410
  }),
321
- [s]
322
- ), !a && !s ? null : /* @__PURE__ */ i("div", { children: r || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "Loading..." }) }) });
323
- }), Y = w(
411
+ [r]
412
+ ), !l && !r ? null : /* @__PURE__ */ c("div", { children: s || /* @__PURE__ */ c("div", { children: /* @__PURE__ */ c("p", { children: "Loading..." }) }) });
413
+ }), ce = S(
324
414
  ({
325
- children: r,
326
- pageLinks: o = 5,
327
- renderFirst: n,
328
- renderPrev: e,
329
- renderPages: s,
330
- renderPage: a,
331
- renderNext: p,
332
- renderLast: h
415
+ children: s,
416
+ pageLinks: i = 5,
417
+ renderFirst: a,
418
+ renderPrev: n,
419
+ renderPages: r,
420
+ renderPage: l,
421
+ renderNext: f,
422
+ renderLast: m
333
423
  }) => {
334
- const { listState: u } = R(), { data: m, count: l, pagination: L, setPage: y, loader: S, error: N } = u, { page: v, perPage: t } = L, { initialLoading: C, isLoading: M } = S, b = P(() => {
335
- const c = Math.ceil(l / t), f = Math.floor(o / 2), x = v * t < l;
336
- return { pagesCount: c, halfWay: f, hasNext: x, hasPrev: v !== 1 };
337
- }, [l, t, v, o]), O = P(() => {
338
- const { pagesCount: c, halfWay: f } = b, x = Array.from({ length: Math.min(o, c) });
339
- return v <= f ? x.map((I, B) => B + 1) : c - v < f ? x.map((I, B) => c - B).reverse() : x.map((I, B) => v - f + B);
340
- }, [v, o, b]), d = P(
424
+ const { listState: u } = x(), { data: h, count: g, pagination: y, setPage: R, loader: L, error: E } = u, { page: P, perPage: O } = y, { initialLoading: M, isLoading: $ } = L, p = C(() => {
425
+ const w = Math.ceil(g / O), B = Math.floor(i / 2), t = P * O < g;
426
+ return { pagesCount: w, halfWay: B, hasNext: t, hasPrev: P !== 1 };
427
+ }, [g, O, P, i]), q = C(() => {
428
+ const { pagesCount: w, halfWay: B } = p, t = Array.from({ length: Math.min(i, w) });
429
+ return P <= B ? t.map((b, T) => T + 1) : w - P < B ? t.map((b, T) => w - T).reverse() : t.map((b, T) => P - B + T);
430
+ }, [P, i, p]), k = C(
341
431
  () => ({
342
- prev: () => y(v - 1),
343
- next: () => y(v + 1),
344
- first: () => y(1),
345
- last: () => y(b.pagesCount),
346
- setPage: (c) => y(c)
432
+ prev: () => R(P - 1),
433
+ next: () => R(P + 1),
434
+ first: () => R(1),
435
+ last: () => R(p.pagesCount),
436
+ setPage: (w) => R(w)
347
437
  }),
348
- [y, v, b.pagesCount]
349
- ), g = P(
438
+ [R, P, p.pagesCount]
439
+ ), A = C(
350
440
  () => ({
351
- page: v,
352
- perPage: t,
353
- count: l,
354
- ...b,
355
- pagesToDisplay: O,
356
- ...d
441
+ page: P,
442
+ perPage: O,
443
+ count: g,
444
+ ...p,
445
+ pagesToDisplay: q,
446
+ ...k
357
447
  }),
358
- [v, t, l, b, O, d]
448
+ [P, O, g, p, q, k]
359
449
  );
360
- return C || !m || m.length === 0 || N ? null : r ? r(g) : /* @__PURE__ */ z("div", { className: "react-list-pagination", children: [
361
- n ? n(g) : /* @__PURE__ */ i(
450
+ return M || !h || h.length === 0 || E ? null : s ? s(A) : /* @__PURE__ */ I("div", { className: "react-list-pagination", children: [
451
+ a ? a(A) : /* @__PURE__ */ c(
362
452
  "button",
363
453
  {
364
454
  type: "button",
365
- disabled: !b.hasPrev,
366
- onClick: d.first,
455
+ disabled: !p.hasPrev,
456
+ onClick: k.first,
367
457
  children: "First"
368
458
  }
369
459
  ),
370
- e ? e(g) : /* @__PURE__ */ i(
460
+ n ? n(A) : /* @__PURE__ */ c(
371
461
  "button",
372
462
  {
373
463
  type: "button",
374
- disabled: !b.hasPrev,
375
- onClick: d.prev,
464
+ disabled: !p.hasPrev,
465
+ onClick: k.prev,
376
466
  children: "Prev"
377
467
  }
378
468
  ),
379
- s ? s(g) : /* @__PURE__ */ i("div", { children: O.map((c) => {
380
- const f = c === v, x = { ...g, page: c, isActive: f };
381
- return a ? a(x) : /* @__PURE__ */ i("div", { children: f ? /* @__PURE__ */ i("span", { children: c }) : /* @__PURE__ */ i(
469
+ r ? r(A) : /* @__PURE__ */ c("div", { children: q.map((w) => {
470
+ const B = w === P, t = { ...A, page: w, isActive: B };
471
+ return l ? l(t) : /* @__PURE__ */ c("div", { children: B ? /* @__PURE__ */ c("span", { children: w }) : /* @__PURE__ */ c(
382
472
  "button",
383
473
  {
384
474
  type: "button",
385
- onClick: () => d.setPage(c),
386
- children: c
475
+ onClick: () => k.setPage(w),
476
+ children: w
387
477
  }
388
- ) }, `page-${c}`);
478
+ ) }, `page-${w}`);
389
479
  }) }),
390
- p ? p(g) : /* @__PURE__ */ i(
480
+ f ? f(A) : /* @__PURE__ */ c(
391
481
  "button",
392
482
  {
393
483
  type: "button",
394
- disabled: !b.hasNext,
395
- onClick: d.next,
484
+ disabled: !p.hasNext,
485
+ onClick: k.next,
396
486
  children: "Next"
397
487
  }
398
488
  ),
399
- h ? h(g) : /* @__PURE__ */ i(
489
+ m ? m(A) : /* @__PURE__ */ c(
400
490
  "button",
401
491
  {
402
492
  type: "button",
403
- disabled: !b.hasNext,
404
- onClick: d.last,
493
+ disabled: !p.hasNext,
494
+ onClick: k.last,
405
495
  children: "Last"
406
496
  }
407
497
  )
408
498
  ] });
409
499
  }
410
- ), Z = w(
411
- ({ children: r, options: o = [10, 25, 50, 100] }) => {
412
- const { listState: n } = R(), { data: e, pagination: s, setPerPage: a, loader: p, error: h } = n, { perPage: u } = s, { initialLoading: m } = p, l = P(() => o.map((S) => typeof S != "object" ? {
413
- value: S,
414
- label: S
415
- } : S), [o]), L = k(
416
- (S) => {
417
- a(Number(S.target.value));
500
+ ), ue = S(
501
+ ({ children: s, options: i = [10, 25, 50, 100] }) => {
502
+ const { listState: a } = x(), { data: n, pagination: r, setPerPage: l, loader: f, error: m } = a, { perPage: u } = r, { initialLoading: h } = f, g = C(() => i.map((L) => typeof L != "object" ? {
503
+ value: L,
504
+ label: L
505
+ } : L), [i]), y = z(
506
+ (L) => {
507
+ l(Number(L.target.value));
418
508
  },
419
- [a]
420
- ), y = P(
509
+ [l]
510
+ ), R = C(
421
511
  () => ({
422
512
  perPage: u,
423
- setPerPage: a,
424
- options: l
513
+ setPerPage: l,
514
+ options: g
425
515
  }),
426
- [u, a, l]
516
+ [u, l, g]
427
517
  );
428
- return m || !e || e.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-per-page", children: r ? r(y) : /* @__PURE__ */ i("select", { value: u, onChange: L, children: l.map((S) => /* @__PURE__ */ z("option", { value: S.value, children: [
429
- S.label,
518
+ return h || !n || n.length === 0 || m ? null : /* @__PURE__ */ c("div", { className: "react-list-per-page", children: s ? s(R) : /* @__PURE__ */ c("select", { value: u, onChange: y, children: g.map((L) => /* @__PURE__ */ I("option", { value: L.value, children: [
519
+ L.label,
430
520
  " items per page"
431
- ] }, `option-${S.value}`)) }) });
521
+ ] }, `option-${L.value}`)) }) });
432
522
  }
433
- ), tt = w(({ children: r }) => {
434
- const { listState: o } = R(), { loader: n, refresh: e } = o, { isLoading: s, initialLoading: a } = n, p = k(() => {
435
- e({ isRefresh: !0 });
436
- }, [e]), h = P(
523
+ ), ge = S(({ children: s }) => {
524
+ const { listState: i } = x(), { loader: a, refresh: n } = i, { isLoading: r, initialLoading: l } = a, f = z(() => {
525
+ n({ isRefresh: !0 });
526
+ }, [n]), m = C(
437
527
  () => ({
438
- isLoading: s,
439
- refresh: p
528
+ isLoading: r,
529
+ refresh: f
440
530
  }),
441
- [s, p]
531
+ [r, f]
442
532
  );
443
- return a ? null : r ? r(h) : /* @__PURE__ */ i("div", { className: "react-list-refresh", children: /* @__PURE__ */ i("button", { onClick: p, disabled: s, children: s ? "Loading..." : "Refresh" }) });
444
- }), et = w(({ children: r, debounceTime: o = 500 }) => {
445
- const { listState: n } = R(), { search: e, setSearch: s } = n, [a, p] = T(e ?? ""), h = q(null);
446
- E(() => {
447
- e !== a && p(e ?? "");
448
- }, [e]);
449
- const u = (l) => {
450
- p(l), h.current && clearTimeout(h.current), h.current = setTimeout(() => {
451
- s(l);
452
- }, o);
533
+ return l ? null : s ? s(m) : /* @__PURE__ */ c("div", { className: "react-list-refresh", children: /* @__PURE__ */ c("button", { onClick: f, disabled: r, children: r ? "Loading..." : "Refresh" }) });
534
+ }), de = S(({ children: s, debounceTime: i = 500 }) => {
535
+ const { listState: a } = x(), { search: n, setSearch: r } = a, [l, f] = G(n ?? ""), m = Q(null);
536
+ _(() => {
537
+ n !== l && f(n ?? "");
538
+ }, [n]);
539
+ const u = (g) => {
540
+ f(g), m.current && clearTimeout(m.current), m.current = setTimeout(() => {
541
+ r(g);
542
+ }, i);
453
543
  };
454
- return E(() => () => {
455
- h.current && clearTimeout(h.current);
456
- }, []), /* @__PURE__ */ i("div", { className: "react-list-search", children: r ? r({
457
- search: a,
544
+ return _(() => () => {
545
+ m.current && clearTimeout(m.current);
546
+ }, []), /* @__PURE__ */ c("div", { className: "react-list-search", children: s ? s({
547
+ search: l,
458
548
  setSearch: u
459
- }) : /* @__PURE__ */ i(
549
+ }) : /* @__PURE__ */ c(
460
550
  "input",
461
551
  {
462
552
  type: "text",
463
- value: a,
464
- onChange: (l) => u(l.target.value),
553
+ value: l,
554
+ onChange: (g) => u(g.target.value),
465
555
  placeholder: "Search..."
466
556
  }
467
557
  ) });
468
- }), nt = w(({ children: r }) => {
469
- const { listState: o } = R(), { data: n, count: e, pagination: s, loader: a, error: p } = o, { page: h, perPage: u } = s, { initialLoading: m, isLoading: l } = a, L = P(() => {
470
- const S = h * u - u + 1, N = Math.min(h * u, e), v = (n == null ? void 0 : n.length) || 0;
471
- return { from: S, to: N, visibleCount: v };
472
- }, [h, u, e, n]), y = P(
558
+ }), fe = S(({ children: s }) => {
559
+ const { listState: i } = x(), { data: a, count: n, pagination: r, loader: l, error: f } = i, { page: m, perPage: u } = r, { initialLoading: h, isLoading: g } = l, y = C(() => {
560
+ const L = m * u - u + 1, E = Math.min(m * u, n), P = (a == null ? void 0 : a.length) || 0;
561
+ return { from: L, to: E, visibleCount: P };
562
+ }, [m, u, n, a]), R = C(
473
563
  () => ({
474
- ...L,
475
- count: e
564
+ ...y,
565
+ count: n
476
566
  }),
477
- [L, e]
567
+ [y, n]
478
568
  );
479
- return m || !n || n.length === 0 || p ? null : /* @__PURE__ */ i("div", { className: "react-list-summary", children: r ? r(y) : /* @__PURE__ */ z("span", { children: [
569
+ return h || !a || a.length === 0 || f ? null : /* @__PURE__ */ c("div", { className: "react-list-summary", children: s ? s(R) : /* @__PURE__ */ I("span", { children: [
480
570
  "Showing ",
481
- /* @__PURE__ */ i("span", { children: L.visibleCount }),
571
+ /* @__PURE__ */ c("span", { children: y.visibleCount }),
482
572
  " items (",
483
- /* @__PURE__ */ z("span", { children: [
484
- L.from,
573
+ /* @__PURE__ */ I("span", { children: [
574
+ y.from,
485
575
  " - ",
486
- L.to
576
+ y.to
487
577
  ] }),
488
578
  ") out of ",
489
- /* @__PURE__ */ i("span", { children: e })
579
+ /* @__PURE__ */ c("span", { children: n })
490
580
  ] }) });
491
581
  });
492
582
  export {
493
- W as ReactListAttributes,
494
- F as ReactListEmpty,
495
- G as ReactListError,
496
- J as ReactListGoTo,
497
- K as ReactListInitialLoader,
498
- Q as ReactListItems,
499
- V as ReactListLoadMore,
500
- X as ReactListLoader,
501
- Y as ReactListPagination,
502
- Z as ReactListPerPage,
503
- H as ReactListProvider,
504
- tt as ReactListRefresh,
505
- et as ReactListSearch,
506
- nt as ReactListSummary,
507
- U as default
583
+ ee as ReactListAttributes,
584
+ te as ReactListEmpty,
585
+ se as ReactListError,
586
+ ne as ReactListGoTo,
587
+ re as ReactListInitialLoader,
588
+ ie as ReactListItems,
589
+ oe as ReactListLoadMore,
590
+ le as ReactListLoader,
591
+ ce as ReactListPagination,
592
+ ue as ReactListPerPage,
593
+ j as ReactListProvider,
594
+ ge as ReactListRefresh,
595
+ de as ReactListSearch,
596
+ fe as ReactListSummary,
597
+ ae as default
508
598
  };
@@ -1 +1 @@
1
- (function(S,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],s):(S=typeof globalThis<"u"?globalThis:S||self,s(S.ReactList={},S.jsxRuntime,S.react))})(this,function(S,s,n){"use strict";const B=n.createContext(null),I=({children:o,config:l})=>{const{requestHandler:a,stateManager:t={}}=l,[r,i]=n.useState({data:[],response:null,error:null,count:0,selection:[],pagination:{page:1,perPage:25},loader:{isLoading:!1,initialLoading:!0},sort:{sortBy:null,sortOrder:"desc"},search:"",filters:{},attrs:[],isEmpty:!0,isInitializing:!0});if(!a)throw new Error("ListProvider: requestHandler is required.");const f=n.useMemo(()=>({requestHandler:a,stateManager:t,listState:r,setListState:i}),[a,t,r]);return s.jsx(B.Provider,{value:f,children:o})},w=()=>{const o=n.useContext(B);if(!o)throw new Error("useListContext must be used within a ListProvider");return o},j=n.memo(({children:o,renderAttribute:l})=>{const{listState:a}=w(),{attrs:t,attrSettings:r,updateAttr:i}=a,f=n.useCallback(d=>m=>{i(d,"visible",m.target.checked)},[i]),p=n.useMemo(()=>({attrs:t,attrSettings:r,updateAttr:i}),[t,r,i]);return o?o(p):s.jsx("div",{className:"react-list-attributes",children:t.map((d,m)=>{var u;return l?l({key:`attr-${m}`,attr:d,updateAttr:i,attrSettings:r}):s.jsxs("label",{children:[s.jsx("span",{children:d.label}),s.jsx("input",{type:"checkbox",checked:((u=r==null?void 0:r[d.name])==null?void 0:u.visible)??!1,onChange:f(d.name)})]},`attr-${m}`)})})}),q=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,loader:t,error:r}=l,{isLoading:i,initialLoading:f}=t;return(a==null?void 0:a.length)>0||f||i||r?null:s.jsx("div",{className:"react-list-empty",children:o||s.jsx("div",{children:s.jsx("p",{children:"No data found!"})})})}),A=n.memo(({children:o})=>{const{listState:l}=w(),{error:a,loader:t}=l,{isLoading:r}=t;return!a||r?null:s.jsx("div",{className:"react-list-error",children:typeof o=="function"?o({error:a}):o||s.jsxs("div",{children:[s.jsx("h3",{children:"Error occurred"}),s.jsxs("pre",{children:[a.name,": ",a.message]})]})})}),_=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{initialLoading:u,isLoading:v}=f,{pages:b,pagesCount:y}=n.useMemo(()=>{const e=Math.ceil(t/m);return{pages:Array.from({length:e},(k,M)=>M+1),pagesCount:e}},[t,m]),N=n.useCallback(e=>{i(Number(e.target.value))},[i]),P=n.useMemo(()=>({setPage:i,page:d,pages:b,pagesCount:y}),[i,d,b,y]);return u||!a||a.length===0||p?null:s.jsx("div",{className:"react-list-go-to",children:o?o(P):s.jsx("select",{value:d,onChange:N,children:b.map(e=>s.jsxs("option",{value:e,children:["Page ",e]},`page-${e}`))})})}),$=n.memo(({children:o})=>{const{listState:l}=w(),{loader:a}=l,{initialLoading:t}=a;return n.useMemo(()=>({loading:t}),[t]),t?s.jsx("div",{className:"react-list-initial-loader",children:o||s.jsx("p",{children:"Initial Loading..."})}):null}),D=n.memo(({children:o,renderItem:l})=>{const{listState:a}=w(),{data:t=[],loader:r,error:i,setSort:f,sort:p}=a,{initialLoading:d}=r,m=n.useMemo(()=>({items:t,setSort:f,sort:p}),[t,p,f]);return d||!t||t.length===0||i?null:l?s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("div",{children:l({item:u,index:v})},u.id||v))}):typeof o=="function"?s.jsx("div",{className:"react-list-items",children:o(m)}):s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("pre",{children:JSON.stringify(u,null,2)},u.id||v))})}),H=({initialItems:o=[],children:l,endpoint:a,page:t=1,perPage:r=25,sortBy:i="",sortOrder:f="desc",search:p="",filters:d={},attrs:m,version:u=1,paginationMode:v="pagination",meta:b={}})=>{const{requestHandler:y,setListState:N}=w(),P=v==="loadMore",[e,C]=n.useState({page:t,perPage:r,sortBy:i,sortOrder:f,search:p,filters:d,attrSettings:{},items:o,selection:[],error:null,response:null,count:0,isLoading:!1,initializingState:!0}),k=n.useCallback(async(g={},h=null)=>{e.initializingState||C(c=>({...c,error:null,isLoading:!0}));try{const c=h||e,L=await y({endpoint:a,version:u,meta:b,page:c.page,perPage:c.perPage,search:c.search,sortBy:c.sortBy,sortOrder:c.sortOrder,filters:c.filters,...g});C(E=>({...E,response:L,selection:[],items:P&&c.page>1?[...E.items,...L.items]:L.items,count:L.count,initializingState:!1,isLoading:!1}))}catch(c){throw C(L=>({...L,error:c,items:[],count:0,initializingState:!1,isLoading:!1})),c}},[a,u,P,b,y,e]),M=n.useMemo(()=>({setPage:(g,h)=>{let c=g;g===0&&(c="");const L={...e,page:c};C(L),c&&k(h,L)},setPerPage:g=>{const h={...e,perPage:g,page:1};C(h),k({},h)},setSearch:g=>{if(g!==e.search){const h={...e,search:g,page:1};C(h),k({},h)}},setSort:({by:g,order:h})=>{const c={...e,sortBy:g,sortOrder:h,page:1};C(c),k({},c)},loadMore:()=>{const g={...e,page:e.page+1};C(g),k({},g)},refresh:(g={isRefresh:!0})=>{if(P){const h={...e,page:1,items:[]};C(h),k(g,h)}else k(g)},setFilters:g=>{const h={...e,filters:g,page:1};C(h),k({},h)},updateItemById:(g,h)=>{const c=e.items.map(L=>L.id===h?{...L,...g}:L);C(L=>({...L,items:c}))},setSelection:g=>C(h=>({...h,selection:g}))}),[k,P,e]),O=n.useMemo(()=>({data:e.items,response:e.response,error:e.error,count:e.count,selection:e.selection,pagination:{page:e.page,perPage:e.perPage,hasMore:e.items.length<e.count},loader:{isLoading:e.isLoading,initialLoading:e.initializingState},sort:{sortBy:e.sortBy,sortOrder:e.sortOrder},search:e.search,filters:e.filters,attrs:m||Object.keys(e.items[0]||{}),isEmpty:e.items.length===0,...M}),[e.items,e.response,e.error,e.count,e.selection,e.page,e.perPage,e.isLoading,e.initializingState,e.sortBy,e.sortOrder,e.search,e.filters,M,m]);return n.useEffect(()=>{e.initializingState&&M.setPage(e.page)},[]),n.useEffect(()=>{N(O)},[N,e.items,e.count,e.error,e.isLoading,e.selection,e.page,e.perPage,e.sortBy,e.sortOrder]),typeof l=="function"?l(O):l},x=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{isLoading:u}=f,v=n.useMemo(()=>d*m<t,[d,m,t]),b=n.useCallback(()=>{v&&!u&&i(d+1)},[v,u,i,d]),y=n.useMemo(()=>({isLoading:u,loadMore:b,hasMoreItems:v}),[u,b,v]);return!a||a.length===0||p?null:o(y)}),G=n.memo(({children:o,position:l="overlay"})=>{const{listState:a}=w(),{loader:t}=a,{isLoading:r,initializingState:i}=t;return n.useMemo(()=>({isLoading:r}),[r]),!i&&!r?null:s.jsx("div",{children:o||s.jsx("div",{children:s.jsx("p",{children:"Loading..."})})})}),W=n.memo(({children:o,pageLinks:l=5,renderFirst:a,renderPrev:t,renderPages:r,renderPage:i,renderNext:f,renderLast:p})=>{const{listState:d}=w(),{data:m,count:u,pagination:v,setPage:b,loader:y,error:N}=d,{page:P,perPage:e}=v,{initialLoading:C,isLoading:k}=y,M=n.useMemo(()=>{const c=Math.ceil(u/e),L=Math.floor(l/2),E=P*e<u;return{pagesCount:c,halfWay:L,hasNext:E,hasPrev:P!==1}},[u,e,P,l]),O=n.useMemo(()=>{const{pagesCount:c,halfWay:L}=M,E=Array.from({length:Math.min(l,c)});return P<=L?E.map((T,z)=>z+1):c-P<L?E.map((T,z)=>c-z).reverse():E.map((T,z)=>P-L+z)},[P,l,M]),g=n.useMemo(()=>({prev:()=>b(P-1),next:()=>b(P+1),first:()=>b(1),last:()=>b(M.pagesCount),setPage:c=>b(c)}),[b,P,M.pagesCount]),h=n.useMemo(()=>({page:P,perPage:e,count:u,...M,pagesToDisplay:O,...g}),[P,e,u,M,O,g]);return C||!m||m.length===0||N?null:o?o(h):s.jsxs("div",{className:"react-list-pagination",children:[a?a(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.first,children:"First"}),t?t(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.prev,children:"Prev"}),r?r(h):s.jsx("div",{children:O.map(c=>{const L=c===P,E={...h,page:c,isActive:L};return i?i(E):s.jsx("div",{children:L?s.jsx("span",{children:c}):s.jsx("button",{type:"button",onClick:()=>g.setPage(c),children:c})},`page-${c}`)})}),f?f(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.next,children:"Next"}),p?p(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.last,children:"Last"})]})}),F=n.memo(({children:o,options:l=[10,25,50,100]})=>{const{listState:a}=w(),{data:t,pagination:r,setPerPage:i,loader:f,error:p}=a,{perPage:d}=r,{initialLoading:m}=f,u=n.useMemo(()=>l.map(y=>typeof y!="object"?{value:y,label:y}:y),[l]),v=n.useCallback(y=>{i(Number(y.target.value))},[i]),b=n.useMemo(()=>({perPage:d,setPerPage:i,options:u}),[d,i,u]);return m||!t||t.length===0||p?null:s.jsx("div",{className:"react-list-per-page",children:o?o(b):s.jsx("select",{value:d,onChange:v,children:u.map(y=>s.jsxs("option",{value:y.value,children:[y.label," items per page"]},`option-${y.value}`))})})}),J=n.memo(({children:o})=>{const{listState:l}=w(),{loader:a,refresh:t}=l,{isLoading:r,initialLoading:i}=a,f=n.useCallback(()=>{t({isRefresh:!0})},[t]),p=n.useMemo(()=>({isLoading:r,refresh:f}),[r,f]);return i?null:o?o(p):s.jsx("div",{className:"react-list-refresh",children:s.jsx("button",{onClick:f,disabled:r,children:r?"Loading...":"Refresh"})})}),K=n.memo(({children:o,debounceTime:l=500})=>{const{listState:a}=w(),{search:t,setSearch:r}=a,[i,f]=n.useState(t??""),p=n.useRef(null);n.useEffect(()=>{t!==i&&f(t??"")},[t]);const d=u=>{f(u),p.current&&clearTimeout(p.current),p.current=setTimeout(()=>{r(u)},l)};n.useEffect(()=>()=>{p.current&&clearTimeout(p.current)},[]);const m={search:i,setSearch:d};return s.jsx("div",{className:"react-list-search",children:o?o(m):s.jsx("input",{type:"text",value:i,onChange:u=>d(u.target.value),placeholder:"Search..."})})}),Q=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,loader:i,error:f}=l,{page:p,perPage:d}=r,{initialLoading:m,isLoading:u}=i,v=n.useMemo(()=>{const y=p*d-d+1,N=Math.min(p*d,t),P=(a==null?void 0:a.length)||0;return{from:y,to:N,visibleCount:P}},[p,d,t,a]),b=n.useMemo(()=>({...v,count:t}),[v,t]);return m||!a||a.length===0||f?null:s.jsx("div",{className:"react-list-summary",children:o?o(b):s.jsxs("span",{children:["Showing ",s.jsx("span",{children:v.visibleCount})," items (",s.jsxs("span",{children:[v.from," - ",v.to]}),") out of ",s.jsx("span",{children:t})]})})});S.ReactListAttributes=j,S.ReactListEmpty=q,S.ReactListError=A,S.ReactListGoTo=_,S.ReactListInitialLoader=$,S.ReactListItems=D,S.ReactListLoadMore=x,S.ReactListLoader=G,S.ReactListPagination=W,S.ReactListPerPage=F,S.ReactListProvider=I,S.ReactListRefresh=J,S.ReactListSearch=K,S.ReactListSummary=Q,S.default=H,Object.defineProperties(S,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ (function(P,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],a):(P=typeof globalThis<"u"?globalThis:P||self,a(P.ReactList={},P.jsxRuntime,P.react))})(this,function(P,a,r){"use strict";const H=r.createContext(null),J=({children:s,config:o})=>{const{requestHandler:l,stateManager:n={}}=o,[i,u]=r.useState({data:[],response:null,error:null,count:0,selection:[],pagination:{page:1,perPage:25},loader:{isLoading:!1,initialLoading:!0},sort:{sortBy:null,sortOrder:"desc"},search:"",filters:{},attrs:[],isEmpty:!0,isInitializing:!0});if(!l)throw new Error("ListProvider: requestHandler is required.");const h=r.useMemo(()=>({requestHandler:l,stateManager:n,listState:i,setListState:u}),[l,n,i]);return a.jsx(H.Provider,{value:h,children:s})},A=()=>{const s=r.useContext(H);if(!s)throw new Error("useListContext must be used within a ListProvider");return s},K=r.memo(({children:s,renderAttribute:o})=>{const{listState:l}=A(),{attrs:n,attrSettings:i,updateAttr:u}=l,h=r.useCallback(d=>m=>{u(d,"visible",m.target.checked)},[u]),L=r.useMemo(()=>({attrs:n,attrSettings:i,updateAttr:u}),[n,i,u]);return s?s(L):a.jsx("div",{className:"react-list-attributes",children:n.map((d,m)=>{var g;return o?o({key:`attr-${m}`,attr:d,updateAttr:u,attrSettings:i}):a.jsxs("label",{children:[a.jsx("span",{children:d.label}),a.jsx("input",{type:"checkbox",checked:((g=i==null?void 0:i[d.name])==null?void 0:g.visible)??!1,onChange:h(d.name)})]},`attr-${m}`)})})}),Q=r.memo(({children:s})=>{const{listState:o}=A(),{data:l,loader:n,error:i}=o,{isLoading:u,initialLoading:h}=n;return(l==null?void 0:l.length)>0||h||u||i?null:a.jsx("div",{className:"react-list-empty",children:s||a.jsx("div",{children:a.jsx("p",{children:"No data found!"})})})}),U=r.memo(({children:s})=>{const{listState:o}=A(),{error:l,loader:n}=o,{isLoading:i}=n;return!l||i?null:a.jsx("div",{className:"react-list-error",children:typeof s=="function"?s({error:l}):s||a.jsxs("div",{children:[a.jsx("h3",{children:"Error occurred"}),a.jsxs("pre",{children:[l.name,": ",l.message]})]})})}),V=r.memo(({children:s})=>{const{listState:o}=A(),{data:l,count:n,pagination:i,setPage:u,loader:h,error:L}=o,{page:d,perPage:m}=i,{initialLoading:g,isLoading:y}=h,{pages:M,pagesCount:v}=r.useMemo(()=>{const k=Math.ceil(n/m);return{pages:Array.from({length:k},(x,p)=>p+1),pagesCount:k}},[n,m]),z=r.useCallback(k=>{u(Number(k.target.value))},[u]),b=r.useMemo(()=>({setPage:u,page:d,pages:M,pagesCount:v}),[u,d,M,v]);return g||!l||l.length===0||L?null:a.jsx("div",{className:"react-list-go-to",children:s?s(b):a.jsx("select",{value:d,onChange:z,children:M.map(k=>a.jsxs("option",{value:k,children:["Page ",k]},`page-${k}`))})})}),X=r.memo(({children:s})=>{const{listState:o}=A(),{loader:l}=o,{initialLoading:n}=l;return r.useMemo(()=>({loading:n}),[n]),n?a.jsx("div",{className:"react-list-initial-loader",children:s||a.jsx("p",{children:"Initial Loading..."})}):null}),Y=r.memo(({children:s,renderItem:o})=>{const{listState:l}=A(),{data:n=[],loader:i,error:u,setSort:h,sort:L}=l,{initialLoading:d}=i,m=r.useMemo(()=>({items:n,setSort:h,sort:L}),[n,L,h]);return d||!n||n.length===0||u?null:o?a.jsx("div",{className:"react-list-items",children:n.map((g,y)=>a.jsx("div",{children:o({item:g,index:y})},g.id||y))}):typeof s=="function"?a.jsx("div",{className:"react-list-items",children:s(m)}):a.jsx("div",{className:"react-list-items",children:n.map((g,y)=>a.jsx("pre",{children:JSON.stringify(g,null,2)},g.id||y))})}),_=(s,o)=>{if(s===o)return!0;if(s==null||o==null||typeof s!="object"||typeof o!="object")return s===o;if(Array.isArray(s)&&Array.isArray(o)){if(s.length!==o.length)return!1;for(let i=0;i<s.length;i++)if(!_(s[i],o[i]))return!1;return!0}if(Array.isArray(s)||Array.isArray(o))return!1;const l=Object.keys(s).filter(i=>s[i]!==void 0),n=Object.keys(o).filter(i=>o[i]!==void 0);if(l.length!==n.length)return!1;for(let i of l)if(!n.includes(i)||!_(s[i],o[i]))return!1;return!0},Z=(s,o)=>!o||Object.keys(o).length===0?s&&Object.keys(s).length>0:!s||Object.keys(s).length===0?!1:!_(s,o),F=({initialItems:s=[],children:o,endpoint:l,page:n=1,perPage:i=25,sortBy:u="",sortOrder:h="desc",count:L=0,search:d="",filters:m={},attrs:g,version:y=1,paginationMode:M="pagination",meta:v={},onResponse:z,afterPageChange:b,afterLoadMore:k})=>{const{requestHandler:q,setListState:x,stateManager:p}=A(),I=r.useRef(!1),w=M==="loadMore",E=r.useCallback(e=>({endpoint:l,version:y,meta:v,search:(e==null?void 0:e.search)||d,page:(e==null?void 0:e.page)||n,perPage:(e==null?void 0:e.perPage)||i,sortBy:(e==null?void 0:e.sortBy)||u,sortOrder:(e==null?void 0:e.sortOrder)||h,filters:(e==null?void 0:e.filters)||m,attrSettings:(e==null?void 0:e.attrSettings)||{},isRefresh:!1}),[l,y,v,d,n,i,u,h,m]),O=r.useCallback(()=>{var e;try{const f=E(),c=(e=p==null?void 0:p.get)==null?void 0:e.call(p,f);return{page:c==null?void 0:c.page,perPage:c==null?void 0:c.perPage,sortBy:c==null?void 0:c.sortBy,sortOrder:c==null?void 0:c.sortOrder,search:c==null?void 0:c.search,attrSettings:c==null?void 0:c.attrSettings,filters:c==null?void 0:c.filters}}catch(f){return console.error(f),{}}},[E,p]),B=r.useCallback(()=>{const e=O();let f=n;return w?f=1:e.page!=null&&(f=e.page),{page:f,perPage:e.perPage!=null?e.perPage:i,sortBy:e.sortBy!=null?e.sortBy:u,sortOrder:e.sortOrder!=null?e.sortOrder:h,search:e.search!=null?e.search:d,filters:e.filters!=null?e.filters:m,attrSettings:e.attrSettings||{},items:s,selection:[],error:null,response:null,count:0,isLoading:!1,initializingState:!s.length,confirmedPage:null}},[O,d,n,i,u,h,d,m,w]),[t,S]=r.useState(B),T=r.useCallback(e=>{var f;if(p){const c=E(e);(f=p==null?void 0:p.set)==null||f.call(p,c)}},[p,E]),N=r.useCallback(async(e={},f=null)=>{t.initializingState||S(c=>({...c,error:null,isLoading:!0}));try{const c=f||t,C=await q({endpoint:l,version:y,meta:v,page:c.page,perPage:c.perPage,search:c.search,sortBy:c.sortBy,sortOrder:c.sortOrder,filters:c.filters,...e});z&&z(C);let D;w?(prev.page===1?D=C.items:D=[...prev.items,...C.items],k&&k(C)):(D=C.items,b&&b(C));const W={...c,response:C,selection:[],items:w&&c.page>1?[...prev.items,...C.items]:C.items,count:C.count,initializingState:!1,isLoading:!1};T(W),S(W)}catch(c){throw S(C=>({...C,error:c,items:[],count:0,initializingState:!1,isLoading:!1})),c}},[l,y,w,v,q,t]),$=r.useMemo(()=>({setPage:(e,f)=>{let c=e;e===0&&(c="");const C={...t,page:c};S(C),c&&N(f,C)},setPerPage:e=>{const f={...t,perPage:e,page:1};S(f),N({},f)},setSearch:e=>{if(e!==t.search){const f={...t,search:e,page:1};S(f),N({},f)}},setSort:({by:e,order:f})=>{const c={...t,sortBy:e,sortOrder:f,page:1};S(c),N({},c)},loadMore:()=>{const e={...t,page:t.page+1};S(e),N({},e)},clearFilters:()=>{const e={...t,filters:m,page:1};S(e),N({},e)},refresh:(e={isRefresh:!0})=>{if(w){const f={...t,page:1,items:[]};S(f),N(e,f)}else N(e)},setFilters:e=>{const f={...t,filters:e,page:1};S(f),N({},f)},updateItemById:(e,f)=>{const c=t.items.map(C=>C.id===f?{...C,...e}:C);S(C=>({...C,items:c}))},setSelection:e=>S(f=>({...f,selection:e}))}),[N,w,t]),G=r.useMemo(()=>({data:t.items,response:t.response,error:t.error,count:t.count,selection:t.selection,pagination:{page:t.page,perPage:t.perPage,hasMore:t.items.length<t.count},loader:{isLoading:t.isLoading,initialLoading:t.initializingState},sort:{sortBy:t.sortBy,sortOrder:t.sortOrder},hasActiveFilters:Z(t.filters,m),search:t.search,filters:t.filters,attrs:g||Object.keys(t.items[0]||{}),isEmpty:t.items.length===0,...$}),[t.items,t.response,t.error,t.count,t.selection,t.page,t.perPage,t.isLoading,t.initializingState,t.sortBy,t.sortOrder,t.search,t.filters,$,g]);return r.useEffect(()=>{if(t.initializingState&&!I.current){if(I.current=!0,p!=null&&p.init){const e=E(t);p.init(e)}s.length||$.setPage(t.page)}},[]),r.useEffect(()=>{x(G)},[x,t.items,t.count,t.error,t.isLoading,t.selection,t.page,t.perPage,t.sortBy,t.sortOrder]),typeof o=="function"?o(G):o},R=r.memo(({children:s})=>{const{listState:o}=A(),{data:l,count:n,pagination:i,setPage:u,loader:h,error:L}=o,{page:d,perPage:m}=i,{isLoading:g}=h,y=r.useMemo(()=>d*m<n,[d,m,n]),M=r.useCallback(()=>{y&&!g&&u(d+1)},[y,g,u,d]),v=r.useMemo(()=>({isLoading:g,loadMore:M,hasMoreItems:y}),[g,M,y]);return!l||l.length===0||L?null:s(v)}),j=r.memo(({children:s,position:o="overlay"})=>{const{listState:l}=A(),{loader:n}=l,{isLoading:i,initializingState:u}=n;return r.useMemo(()=>({isLoading:i}),[i]),!u&&!i?null:a.jsx("div",{children:s||a.jsx("div",{children:a.jsx("p",{children:"Loading..."})})})}),ee=r.memo(({children:s,pageLinks:o=5,renderFirst:l,renderPrev:n,renderPages:i,renderPage:u,renderNext:h,renderLast:L})=>{const{listState:d}=A(),{data:m,count:g,pagination:y,setPage:M,loader:v,error:z}=d,{page:b,perPage:k}=y,{initialLoading:q,isLoading:x}=v,p=r.useMemo(()=>{const O=Math.ceil(g/k),B=Math.floor(o/2),t=b*k<g;return{pagesCount:O,halfWay:B,hasNext:t,hasPrev:b!==1}},[g,k,b,o]),I=r.useMemo(()=>{const{pagesCount:O,halfWay:B}=p,t=Array.from({length:Math.min(o,O)});return b<=B?t.map((S,T)=>T+1):O-b<B?t.map((S,T)=>O-T).reverse():t.map((S,T)=>b-B+T)},[b,o,p]),w=r.useMemo(()=>({prev:()=>M(b-1),next:()=>M(b+1),first:()=>M(1),last:()=>M(p.pagesCount),setPage:O=>M(O)}),[M,b,p.pagesCount]),E=r.useMemo(()=>({page:b,perPage:k,count:g,...p,pagesToDisplay:I,...w}),[b,k,g,p,I,w]);return q||!m||m.length===0||z?null:s?s(E):a.jsxs("div",{className:"react-list-pagination",children:[l?l(E):a.jsx("button",{type:"button",disabled:!p.hasPrev,onClick:w.first,children:"First"}),n?n(E):a.jsx("button",{type:"button",disabled:!p.hasPrev,onClick:w.prev,children:"Prev"}),i?i(E):a.jsx("div",{children:I.map(O=>{const B=O===b,t={...E,page:O,isActive:B};return u?u(t):a.jsx("div",{children:B?a.jsx("span",{children:O}):a.jsx("button",{type:"button",onClick:()=>w.setPage(O),children:O})},`page-${O}`)})}),h?h(E):a.jsx("button",{type:"button",disabled:!p.hasNext,onClick:w.next,children:"Next"}),L?L(E):a.jsx("button",{type:"button",disabled:!p.hasNext,onClick:w.last,children:"Last"})]})}),te=r.memo(({children:s,options:o=[10,25,50,100]})=>{const{listState:l}=A(),{data:n,pagination:i,setPerPage:u,loader:h,error:L}=l,{perPage:d}=i,{initialLoading:m}=h,g=r.useMemo(()=>o.map(v=>typeof v!="object"?{value:v,label:v}:v),[o]),y=r.useCallback(v=>{u(Number(v.target.value))},[u]),M=r.useMemo(()=>({perPage:d,setPerPage:u,options:g}),[d,u,g]);return m||!n||n.length===0||L?null:a.jsx("div",{className:"react-list-per-page",children:s?s(M):a.jsx("select",{value:d,onChange:y,children:g.map(v=>a.jsxs("option",{value:v.value,children:[v.label," items per page"]},`option-${v.value}`))})})}),se=r.memo(({children:s})=>{const{listState:o}=A(),{loader:l,refresh:n}=o,{isLoading:i,initialLoading:u}=l,h=r.useCallback(()=>{n({isRefresh:!0})},[n]),L=r.useMemo(()=>({isLoading:i,refresh:h}),[i,h]);return u?null:s?s(L):a.jsx("div",{className:"react-list-refresh",children:a.jsx("button",{onClick:h,disabled:i,children:i?"Loading...":"Refresh"})})}),ne=r.memo(({children:s,debounceTime:o=500})=>{const{listState:l}=A(),{search:n,setSearch:i}=l,[u,h]=r.useState(n??""),L=r.useRef(null);r.useEffect(()=>{n!==u&&h(n??"")},[n]);const d=g=>{h(g),L.current&&clearTimeout(L.current),L.current=setTimeout(()=>{i(g)},o)};r.useEffect(()=>()=>{L.current&&clearTimeout(L.current)},[]);const m={search:u,setSearch:d};return a.jsx("div",{className:"react-list-search",children:s?s(m):a.jsx("input",{type:"text",value:u,onChange:g=>d(g.target.value),placeholder:"Search..."})})}),ie=r.memo(({children:s})=>{const{listState:o}=A(),{data:l,count:n,pagination:i,loader:u,error:h}=o,{page:L,perPage:d}=i,{initialLoading:m,isLoading:g}=u,y=r.useMemo(()=>{const v=L*d-d+1,z=Math.min(L*d,n),b=(l==null?void 0:l.length)||0;return{from:v,to:z,visibleCount:b}},[L,d,n,l]),M=r.useMemo(()=>({...y,count:n}),[y,n]);return m||!l||l.length===0||h?null:a.jsx("div",{className:"react-list-summary",children:s?s(M):a.jsxs("span",{children:["Showing ",a.jsx("span",{children:y.visibleCount})," items (",a.jsxs("span",{children:[y.from," - ",y.to]}),") out of ",a.jsx("span",{children:n})]})})});P.ReactListAttributes=K,P.ReactListEmpty=Q,P.ReactListError=U,P.ReactListGoTo=V,P.ReactListInitialLoader=X,P.ReactListItems=Y,P.ReactListLoadMore=R,P.ReactListLoader=j,P.ReactListPagination=ee,P.ReactListPerPage=te,P.ReactListProvider=J,P.ReactListRefresh=se,P.ReactListSearch=ne,P.ReactListSummary=ie,P.default=F,Object.defineProperties(P,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,15 +1,8 @@
1
1
  {
2
2
  "name": "@7span/react-list",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "A simple and reusable list component for React",
5
5
  "type": "module",
6
- "scripts": {
7
- "dev": "vite",
8
- "build": "vite build",
9
- "lint": "eslint .",
10
- "preview": "vite preview",
11
- "prepublishOnly": "npm run build"
12
- },
13
6
  "main": "dist/react-list.js",
14
7
  "files": [
15
8
  "dist"
@@ -20,5 +13,11 @@
20
13
  "devDependencies": {
21
14
  "@vitejs/plugin-react": "^4.3.4",
22
15
  "vite": "^6.3.1"
16
+ },
17
+ "scripts": {
18
+ "dev": "vite",
19
+ "build": "vite build",
20
+ "lint": "eslint .",
21
+ "preview": "vite preview"
23
22
  }
24
- }
23
+ }