@7span/react-list 0.0.5 → 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 A, useMemo as P, memo as M, useCallback as B, useEffect as T, useRef as q } from "react";
3
- const I = $(null), H = ({ children: r, config: o }) => {
4
- const { requestHandler: n, stateManager: e = {} } = o, [s, a] = A({
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,223 +25,305 @@ const I = $(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(I.Provider, { value: p, children: r });
40
- }, w = () => {
41
- const r = j(I);
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 = M(({ children: r, renderAttribute: o }) => {
46
- const { listState: n } = w(), { attrs: e, attrSettings: s, updateAttr: a } = n, p = B(
47
- (u) => (L) => {
48
- a(u, "visible", L.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, L) => {
60
- var l;
61
- return o ? o({
62
- key: `attr-${L}`,
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-${L}`);
76
+ ] }, `attr-${h}`);
77
77
  }) });
78
- }), F = M(({ children: r }) => {
79
- const { listState: o } = w(), { 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 = M(({ children: r }) => {
82
- const { listState: o } = w(), { 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 = M(({ children: r }) => {
92
- const { listState: o } = w(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: L } = s, { initialLoading: l, isLoading: f } = p, { pages: b, pagesCount: S } = P(() => {
93
- const C = Math.ceil(e / L);
94
- return { pages: Array.from({ length: C }, (R, v) => v + 1), pagesCount: C };
95
- }, [e, L]), N = B(
96
- (C) => {
97
- a(Number(C.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
- ), y = P(
99
+ [l]
100
+ ), P = C(
101
101
  () => ({
102
- setPage: a,
102
+ setPage: l,
103
103
  page: u,
104
- pages: b,
105
- pagesCount: S
104
+ pages: R,
105
+ pagesCount: L
106
106
  }),
107
- [a, u, b, 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(y) : /* @__PURE__ */ i("select", { value: u, onChange: N, children: b.map((C) => /* @__PURE__ */ z("option", { value: C, 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
- C
112
- ] }, `page-${C}`)) }) });
113
- }), K = M(({ children: r }) => {
114
- const { listState: o } = w(), { 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 = M(({ children: r, renderItem: o }) => {
122
- const { listState: n } = w(), { data: e = [], loader: s, error: a, setSort: p, sort: h } = n, { initialLoading: u } = s, L = 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, f) => /* @__PURE__ */ i("div", { children: o({ item: l, index: f }) }, l.id || f)) }) : typeof r == "function" ? /* @__PURE__ */ i("div", { className: "react-list-items", children: r(L) }) : /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, f) => /* @__PURE__ */ i("pre", { children: JSON.stringify(l, null, 2) }, l.id || f)) });
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
- count: h = 0,
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,
140
156
  search: u = "",
141
- filters: L = {},
142
- attrs: l,
143
- version: f = 1,
144
- paginationMode: b = "pagination",
145
- meta: S = {}
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
146
165
  }) => {
147
- const { requestHandler: N, setListState: y } = w(), C = b === "loadMore", [t, R] = A({
148
- page: e,
149
- perPage: s,
150
- sortBy: a,
151
- sortOrder: p,
152
- search: u,
153
- filters: L,
154
- attrSettings: {},
155
- items: r,
156
- selection: [],
157
- error: null,
158
- response: null,
159
- count: h,
160
- isLoading: !1,
161
- initializingState: !r.length
162
- }), v = B(
163
- async (d = {}, c = null) => {
164
- t.initializingState || R((g) => ({ ...g, 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 }));
165
239
  try {
166
- const g = c || t, m = await N({
167
- endpoint: n,
168
- version: f,
169
- meta: S,
170
- page: g.page,
171
- perPage: g.perPage,
172
- search: g.search,
173
- sortBy: g.sortBy,
174
- sortOrder: g.sortOrder,
175
- filters: g.filters,
176
- ...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
177
251
  });
178
- R((k) => ({
179
- ...k,
180
- response: m,
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,
181
258
  selection: [],
182
259
  // Append items for loadMore, replace for pagination
183
- items: C && g.page > 1 ? [...k.items, ...m.items] : m.items,
184
- count: m.count,
260
+ items: k && o.page > 1 ? [...prev.items, ...v.items] : v.items,
261
+ count: v.count,
185
262
  initializingState: !1,
186
263
  isLoading: !1
187
- }));
188
- } catch (g) {
189
- throw R((m) => ({
190
- ...m,
191
- error: g,
264
+ };
265
+ T(K), b(K);
266
+ } catch (o) {
267
+ throw b((v) => ({
268
+ ...v,
269
+ error: o,
192
270
  items: [],
193
271
  count: 0,
194
272
  initializingState: !1,
195
273
  isLoading: !1
196
- })), g;
274
+ })), o;
197
275
  }
198
276
  },
199
- [n, f, C, S, N, t]
200
- ), O = P(
277
+ [a, y, k, L, M, t]
278
+ ), D = C(
201
279
  () => ({
202
- setPage: (d, c) => {
203
- let g = d;
204
- d === 0 && (g = "");
205
- const m = { ...t, page: g };
206
- R(m), g && v(c, m);
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);
207
285
  },
208
- setPerPage: (d) => {
209
- const c = { ...t, perPage: d, page: 1 };
210
- R(c), v({}, c);
286
+ setPerPage: (e) => {
287
+ const d = { ...t, perPage: e, page: 1 };
288
+ b(d), N({}, d);
211
289
  },
212
- setSearch: (d) => {
213
- if (d !== t.search) {
214
- const c = { ...t, search: d, page: 1 };
215
- R(c), v({}, c);
290
+ setSearch: (e) => {
291
+ if (e !== t.search) {
292
+ const d = { ...t, search: e, page: 1 };
293
+ b(d), N({}, d);
216
294
  }
217
295
  },
218
- setSort: ({ by: d, order: c }) => {
219
- const g = { ...t, sortBy: d, sortOrder: c, page: 1 };
220
- R(g), v({}, g);
296
+ setSort: ({ by: e, order: d }) => {
297
+ const o = { ...t, sortBy: e, sortOrder: d, page: 1 };
298
+ b(o), N({}, o);
221
299
  },
222
300
  loadMore: () => {
223
- const d = { ...t, page: t.page + 1 };
224
- R(d), v({}, d);
301
+ const e = { ...t, page: t.page + 1 };
302
+ b(e), N({}, e);
225
303
  },
226
- refresh: (d = { isRefresh: !0 }) => {
227
- if (C) {
228
- const c = { ...t, page: 1, items: [] };
229
- R(c), v(d, c);
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);
230
312
  } else
231
- v(d);
313
+ N(e);
232
314
  },
233
- setFilters: (d) => {
234
- const c = { ...t, filters: d, page: 1 };
235
- R(c), v({}, c);
315
+ setFilters: (e) => {
316
+ const d = { ...t, filters: e, page: 1 };
317
+ b(d), N({}, d);
236
318
  },
237
- updateItemById: (d, c) => {
238
- const g = t.items.map((m) => m.id === c ? { ...m, ...d } : m);
239
- R((m) => ({ ...m, items: g }));
319
+ updateItemById: (e, d) => {
320
+ const o = t.items.map((v) => v.id === d ? { ...v, ...e } : v);
321
+ b((v) => ({ ...v, items: o }));
240
322
  },
241
- setSelection: (d) => R((c) => ({ ...c, selection: d }))
323
+ setSelection: (e) => b((d) => ({ ...d, selection: e }))
242
324
  }),
243
- [v, C, t]
244
- ), x = P(
325
+ [N, k, t]
326
+ ), J = C(
245
327
  () => ({
246
328
  data: t.items,
247
329
  response: t.response,
@@ -258,11 +340,12 @@ const I = $(null), H = ({ children: r, config: o }) => {
258
340
  initialLoading: t.initializingState
259
341
  },
260
342
  sort: { sortBy: t.sortBy, sortOrder: t.sortOrder },
343
+ hasActiveFilters: Y(t.filters, h),
261
344
  search: t.search,
262
345
  filters: t.filters,
263
- attrs: l || Object.keys(t.items[0] || {}),
346
+ attrs: g || Object.keys(t.items[0] || {}),
264
347
  isEmpty: t.items.length === 0,
265
- ...O
348
+ ...D
266
349
  }),
267
350
  [
268
351
  t.items,
@@ -278,16 +361,22 @@ const I = $(null), H = ({ children: r, config: o }) => {
278
361
  t.sortOrder,
279
362
  t.search,
280
363
  t.filters,
281
- O,
282
- l
364
+ D,
365
+ g
283
366
  ]
284
367
  );
285
- return T(() => {
286
- t.initializingState && (r.length || O.setPage(t.page));
287
- }, []), T(() => {
288
- y(x);
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);
289
378
  }, [
290
- y,
379
+ $,
291
380
  t.items,
292
381
  t.count,
293
382
  t.error,
@@ -297,213 +386,213 @@ const I = $(null), H = ({ children: r, config: o }) => {
297
386
  t.perPage,
298
387
  t.sortBy,
299
388
  t.sortOrder
300
- ]), typeof o == "function" ? o(x) : o;
301
- }, V = M(({ children: r }) => {
302
- const { listState: o } = w(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: L } = s, { isLoading: l } = p, f = P(
303
- () => u * L < e,
304
- [u, L, e]
305
- ), b = B(() => {
306
- f && !l && a(u + 1);
307
- }, [f, 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(
308
397
  () => ({
309
- isLoading: l,
310
- loadMore: b,
311
- hasMoreItems: f
398
+ isLoading: g,
399
+ loadMore: R,
400
+ hasMoreItems: y
312
401
  }),
313
- [l, b, f]
402
+ [g, R, y]
314
403
  );
315
- return !n || n.length === 0 || h ? null : r(S);
316
- }), X = M(({ children: r, position: o = "overlay" }) => {
317
- const { listState: n } = w(), { loader: e } = n, { isLoading: s, initializingState: a } = e;
318
- 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(
319
408
  () => ({
320
- isLoading: s
409
+ isLoading: r
321
410
  }),
322
- [s]
323
- ), !a && !s ? null : /* @__PURE__ */ i("div", { children: r || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "Loading..." }) }) });
324
- }), Y = M(
411
+ [r]
412
+ ), !l && !r ? null : /* @__PURE__ */ c("div", { children: s || /* @__PURE__ */ c("div", { children: /* @__PURE__ */ c("p", { children: "Loading..." }) }) });
413
+ }), ce = S(
325
414
  ({
326
- children: r,
327
- pageLinks: o = 5,
328
- renderFirst: n,
329
- renderPrev: e,
330
- renderPages: s,
331
- renderPage: a,
332
- renderNext: p,
333
- 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
334
423
  }) => {
335
- const { listState: u } = w(), { data: L, count: l, pagination: f, setPage: b, loader: S, error: N } = u, { page: y, perPage: C } = f, { initialLoading: t, isLoading: R } = S, v = P(() => {
336
- const c = Math.ceil(l / C), g = Math.floor(o / 2), m = y * C < l;
337
- return { pagesCount: c, halfWay: g, hasNext: m, hasPrev: y !== 1 };
338
- }, [l, C, y, o]), O = P(() => {
339
- const { pagesCount: c, halfWay: g } = v, m = Array.from({ length: Math.min(o, c) });
340
- return y <= g ? m.map((k, E) => E + 1) : c - y < g ? m.map((k, E) => c - E).reverse() : m.map((k, E) => y - g + E);
341
- }, [y, o, v]), x = 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(
342
431
  () => ({
343
- prev: () => b(y - 1),
344
- next: () => b(y + 1),
345
- first: () => b(1),
346
- last: () => b(v.pagesCount),
347
- setPage: (c) => b(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)
348
437
  }),
349
- [b, y, v.pagesCount]
350
- ), d = P(
438
+ [R, P, p.pagesCount]
439
+ ), A = C(
351
440
  () => ({
352
- page: y,
353
- perPage: C,
354
- count: l,
355
- ...v,
356
- pagesToDisplay: O,
357
- ...x
441
+ page: P,
442
+ perPage: O,
443
+ count: g,
444
+ ...p,
445
+ pagesToDisplay: q,
446
+ ...k
358
447
  }),
359
- [y, C, l, v, O, x]
448
+ [P, O, g, p, q, k]
360
449
  );
361
- return t || !L || L.length === 0 || N ? null : r ? r(d) : /* @__PURE__ */ z("div", { className: "react-list-pagination", children: [
362
- n ? n(d) : /* @__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(
363
452
  "button",
364
453
  {
365
454
  type: "button",
366
- disabled: !v.hasPrev,
367
- onClick: x.first,
455
+ disabled: !p.hasPrev,
456
+ onClick: k.first,
368
457
  children: "First"
369
458
  }
370
459
  ),
371
- e ? e(d) : /* @__PURE__ */ i(
460
+ n ? n(A) : /* @__PURE__ */ c(
372
461
  "button",
373
462
  {
374
463
  type: "button",
375
- disabled: !v.hasPrev,
376
- onClick: x.prev,
464
+ disabled: !p.hasPrev,
465
+ onClick: k.prev,
377
466
  children: "Prev"
378
467
  }
379
468
  ),
380
- s ? s(d) : /* @__PURE__ */ i("div", { children: O.map((c) => {
381
- const g = c === y, m = { ...d, page: c, isActive: g };
382
- return a ? a(m) : /* @__PURE__ */ i("div", { children: g ? /* @__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(
383
472
  "button",
384
473
  {
385
474
  type: "button",
386
- onClick: () => x.setPage(c),
387
- children: c
475
+ onClick: () => k.setPage(w),
476
+ children: w
388
477
  }
389
- ) }, `page-${c}`);
478
+ ) }, `page-${w}`);
390
479
  }) }),
391
- p ? p(d) : /* @__PURE__ */ i(
480
+ f ? f(A) : /* @__PURE__ */ c(
392
481
  "button",
393
482
  {
394
483
  type: "button",
395
- disabled: !v.hasNext,
396
- onClick: x.next,
484
+ disabled: !p.hasNext,
485
+ onClick: k.next,
397
486
  children: "Next"
398
487
  }
399
488
  ),
400
- h ? h(d) : /* @__PURE__ */ i(
489
+ m ? m(A) : /* @__PURE__ */ c(
401
490
  "button",
402
491
  {
403
492
  type: "button",
404
- disabled: !v.hasNext,
405
- onClick: x.last,
493
+ disabled: !p.hasNext,
494
+ onClick: k.last,
406
495
  children: "Last"
407
496
  }
408
497
  )
409
498
  ] });
410
499
  }
411
- ), Z = M(
412
- ({ children: r, options: o = [10, 25, 50, 100] }) => {
413
- const { listState: n } = w(), { data: e, pagination: s, setPerPage: a, loader: p, error: h } = n, { perPage: u } = s, { initialLoading: L } = p, l = P(() => o.map((S) => typeof S != "object" ? {
414
- value: S,
415
- label: S
416
- } : S), [o]), f = B(
417
- (S) => {
418
- 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));
419
508
  },
420
- [a]
421
- ), b = P(
509
+ [l]
510
+ ), R = C(
422
511
  () => ({
423
512
  perPage: u,
424
- setPerPage: a,
425
- options: l
513
+ setPerPage: l,
514
+ options: g
426
515
  }),
427
- [u, a, l]
516
+ [u, l, g]
428
517
  );
429
- return L || !e || e.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-per-page", children: r ? r(b) : /* @__PURE__ */ i("select", { value: u, onChange: f, children: l.map((S) => /* @__PURE__ */ z("option", { value: S.value, children: [
430
- 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,
431
520
  " items per page"
432
- ] }, `option-${S.value}`)) }) });
521
+ ] }, `option-${L.value}`)) }) });
433
522
  }
434
- ), tt = M(({ children: r }) => {
435
- const { listState: o } = w(), { loader: n, refresh: e } = o, { isLoading: s, initialLoading: a } = n, p = B(() => {
436
- e({ isRefresh: !0 });
437
- }, [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(
438
527
  () => ({
439
- isLoading: s,
440
- refresh: p
528
+ isLoading: r,
529
+ refresh: f
441
530
  }),
442
- [s, p]
531
+ [r, f]
443
532
  );
444
- 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" }) });
445
- }), et = M(({ children: r, debounceTime: o = 500 }) => {
446
- const { listState: n } = w(), { search: e, setSearch: s } = n, [a, p] = A(e ?? ""), h = q(null);
447
- T(() => {
448
- e !== a && p(e ?? "");
449
- }, [e]);
450
- const u = (l) => {
451
- p(l), h.current && clearTimeout(h.current), h.current = setTimeout(() => {
452
- s(l);
453
- }, 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);
454
543
  };
455
- return T(() => () => {
456
- h.current && clearTimeout(h.current);
457
- }, []), /* @__PURE__ */ i("div", { className: "react-list-search", children: r ? r({
458
- search: a,
544
+ return _(() => () => {
545
+ m.current && clearTimeout(m.current);
546
+ }, []), /* @__PURE__ */ c("div", { className: "react-list-search", children: s ? s({
547
+ search: l,
459
548
  setSearch: u
460
- }) : /* @__PURE__ */ i(
549
+ }) : /* @__PURE__ */ c(
461
550
  "input",
462
551
  {
463
552
  type: "text",
464
- value: a,
465
- onChange: (l) => u(l.target.value),
553
+ value: l,
554
+ onChange: (g) => u(g.target.value),
466
555
  placeholder: "Search..."
467
556
  }
468
557
  ) });
469
- }), nt = M(({ children: r }) => {
470
- const { listState: o } = w(), { data: n, count: e, pagination: s, loader: a, error: p } = o, { page: h, perPage: u } = s, { initialLoading: L, isLoading: l } = a, f = P(() => {
471
- const S = h * u - u + 1, N = Math.min(h * u, e), y = (n == null ? void 0 : n.length) || 0;
472
- return { from: S, to: N, visibleCount: y };
473
- }, [h, u, e, n]), b = 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(
474
563
  () => ({
475
- ...f,
476
- count: e
564
+ ...y,
565
+ count: n
477
566
  }),
478
- [f, e]
567
+ [y, n]
479
568
  );
480
- return L || !n || n.length === 0 || p ? null : /* @__PURE__ */ i("div", { className: "react-list-summary", children: r ? r(b) : /* @__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: [
481
570
  "Showing ",
482
- /* @__PURE__ */ i("span", { children: f.visibleCount }),
571
+ /* @__PURE__ */ c("span", { children: y.visibleCount }),
483
572
  " items (",
484
- /* @__PURE__ */ z("span", { children: [
485
- f.from,
573
+ /* @__PURE__ */ I("span", { children: [
574
+ y.from,
486
575
  " - ",
487
- f.to
576
+ y.to
488
577
  ] }),
489
578
  ") out of ",
490
- /* @__PURE__ */ i("span", { children: e })
579
+ /* @__PURE__ */ c("span", { children: n })
491
580
  ] }) });
492
581
  });
493
582
  export {
494
- W as ReactListAttributes,
495
- F as ReactListEmpty,
496
- G as ReactListError,
497
- J as ReactListGoTo,
498
- K as ReactListInitialLoader,
499
- Q as ReactListItems,
500
- V as ReactListLoadMore,
501
- X as ReactListLoader,
502
- Y as ReactListPagination,
503
- Z as ReactListPerPage,
504
- H as ReactListProvider,
505
- tt as ReactListRefresh,
506
- et as ReactListSearch,
507
- nt as ReactListSummary,
508
- 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
509
598
  };
@@ -1 +1 @@
1
- (function(v,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):(v=typeof globalThis<"u"?globalThis:v||self,s(v.ReactList={},v.jsxRuntime,v.react))})(this,function(v,s,n){"use strict";const B=n.createContext(null),j=({children:a,config:l})=>{const{requestHandler:o,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(!o)throw new Error("ListProvider: requestHandler is required.");const f=n.useMemo(()=>({requestHandler:o,stateManager:t,listState:r,setListState:i}),[o,t,r]);return s.jsx(B.Provider,{value:f,children:a})},w=()=>{const a=n.useContext(B);if(!a)throw new Error("useListContext must be used within a ListProvider");return a},I=n.memo(({children:a,renderAttribute:l})=>{const{listState:o}=w(),{attrs:t,attrSettings:r,updateAttr:i}=o,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 a?a(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:a})=>{const{listState:l}=w(),{data:o,loader:t,error:r}=l,{isLoading:i,initialLoading:f}=t;return(o==null?void 0:o.length)>0||f||i||r?null:s.jsx("div",{className:"react-list-empty",children:a||s.jsx("div",{children:s.jsx("p",{children:"No data found!"})})})}),A=n.memo(({children:a})=>{const{listState:l}=w(),{error:o,loader:t}=l,{isLoading:r}=t;return!o||r?null:s.jsx("div",{className:"react-list-error",children:typeof a=="function"?a({error:o}):a||s.jsxs("div",{children:[s.jsx("h3",{children:"Error occurred"}),s.jsxs("pre",{children:[o.name,": ",o.message]})]})})}),_=n.memo(({children:a})=>{const{listState:l}=w(),{data:o,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{initialLoading:u,isLoading:L}=f,{pages:M,pagesCount:y}=n.useMemo(()=>{const C=Math.ceil(t/m);return{pages:Array.from({length:C},(k,P)=>P+1),pagesCount:C}},[t,m]),N=n.useCallback(C=>{i(Number(C.target.value))},[i]),b=n.useMemo(()=>({setPage:i,page:d,pages:M,pagesCount:y}),[i,d,M,y]);return u||!o||o.length===0||p?null:s.jsx("div",{className:"react-list-go-to",children:a?a(b):s.jsx("select",{value:d,onChange:N,children:M.map(C=>s.jsxs("option",{value:C,children:["Page ",C]},`page-${C}`))})})}),$=n.memo(({children:a})=>{const{listState:l}=w(),{loader:o}=l,{initialLoading:t}=o;return n.useMemo(()=>({loading:t}),[t]),t?s.jsx("div",{className:"react-list-initial-loader",children:a||s.jsx("p",{children:"Initial Loading..."})}):null}),D=n.memo(({children:a,renderItem:l})=>{const{listState:o}=w(),{data:t=[],loader:r,error:i,setSort:f,sort:p}=o,{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,L)=>s.jsx("div",{children:l({item:u,index:L})},u.id||L))}):typeof a=="function"?s.jsx("div",{className:"react-list-items",children:a(m)}):s.jsx("div",{className:"react-list-items",children:t.map((u,L)=>s.jsx("pre",{children:JSON.stringify(u,null,2)},u.id||L))})}),H=({initialItems:a=[],children:l,endpoint:o,page:t=1,perPage:r=25,sortBy:i="",sortOrder:f="desc",count:p=0,search:d="",filters:m={},attrs:u,version:L=1,paginationMode:M="pagination",meta:y={}})=>{const{requestHandler:N,setListState:b}=w(),C=M==="loadMore",[e,k]=n.useState({page:t,perPage:r,sortBy:i,sortOrder:f,search:d,filters:m,attrSettings:{},items:a,selection:[],error:null,response:null,count:p,isLoading:!1,initializingState:!a.length}),P=n.useCallback(async(g={},c=null)=>{e.initializingState||k(h=>({...h,error:null,isLoading:!0}));try{const h=c||e,S=await N({endpoint:o,version:L,meta:y,page:h.page,perPage:h.perPage,search:h.search,sortBy:h.sortBy,sortOrder:h.sortOrder,filters:h.filters,...g});k(z=>({...z,response:S,selection:[],items:C&&h.page>1?[...z.items,...S.items]:S.items,count:S.count,initializingState:!1,isLoading:!1}))}catch(h){throw k(S=>({...S,error:h,items:[],count:0,initializingState:!1,isLoading:!1})),h}},[o,L,C,y,N,e]),O=n.useMemo(()=>({setPage:(g,c)=>{let h=g;g===0&&(h="");const S={...e,page:h};k(S),h&&P(c,S)},setPerPage:g=>{const c={...e,perPage:g,page:1};k(c),P({},c)},setSearch:g=>{if(g!==e.search){const c={...e,search:g,page:1};k(c),P({},c)}},setSort:({by:g,order:c})=>{const h={...e,sortBy:g,sortOrder:c,page:1};k(h),P({},h)},loadMore:()=>{const g={...e,page:e.page+1};k(g),P({},g)},refresh:(g={isRefresh:!0})=>{if(C){const c={...e,page:1,items:[]};k(c),P(g,c)}else P(g)},setFilters:g=>{const c={...e,filters:g,page:1};k(c),P({},c)},updateItemById:(g,c)=>{const h=e.items.map(S=>S.id===c?{...S,...g}:S);k(S=>({...S,items:h}))},setSelection:g=>k(c=>({...c,selection:g}))}),[P,C,e]),E=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:u||Object.keys(e.items[0]||{}),isEmpty:e.items.length===0,...O}),[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,O,u]);return n.useEffect(()=>{e.initializingState&&(a.length||O.setPage(e.page))},[]),n.useEffect(()=>{b(E)},[b,e.items,e.count,e.error,e.isLoading,e.selection,e.page,e.perPage,e.sortBy,e.sortOrder]),typeof l=="function"?l(E):l},x=n.memo(({children:a})=>{const{listState:l}=w(),{data:o,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{isLoading:u}=f,L=n.useMemo(()=>d*m<t,[d,m,t]),M=n.useCallback(()=>{L&&!u&&i(d+1)},[L,u,i,d]),y=n.useMemo(()=>({isLoading:u,loadMore:M,hasMoreItems:L}),[u,M,L]);return!o||o.length===0||p?null:a(y)}),G=n.memo(({children:a,position:l="overlay"})=>{const{listState:o}=w(),{loader:t}=o,{isLoading:r,initializingState:i}=t;return n.useMemo(()=>({isLoading:r}),[r]),!i&&!r?null:s.jsx("div",{children:a||s.jsx("div",{children:s.jsx("p",{children:"Loading..."})})})}),W=n.memo(({children:a,pageLinks:l=5,renderFirst:o,renderPrev:t,renderPages:r,renderPage:i,renderNext:f,renderLast:p})=>{const{listState:d}=w(),{data:m,count:u,pagination:L,setPage:M,loader:y,error:N}=d,{page:b,perPage:C}=L,{initialLoading:e,isLoading:k}=y,P=n.useMemo(()=>{const c=Math.ceil(u/C),h=Math.floor(l/2),S=b*C<u;return{pagesCount:c,halfWay:h,hasNext:S,hasPrev:b!==1}},[u,C,b,l]),O=n.useMemo(()=>{const{pagesCount:c,halfWay:h}=P,S=Array.from({length:Math.min(l,c)});return b<=h?S.map((z,T)=>T+1):c-b<h?S.map((z,T)=>c-T).reverse():S.map((z,T)=>b-h+T)},[b,l,P]),E=n.useMemo(()=>({prev:()=>M(b-1),next:()=>M(b+1),first:()=>M(1),last:()=>M(P.pagesCount),setPage:c=>M(c)}),[M,b,P.pagesCount]),g=n.useMemo(()=>({page:b,perPage:C,count:u,...P,pagesToDisplay:O,...E}),[b,C,u,P,O,E]);return e||!m||m.length===0||N?null:a?a(g):s.jsxs("div",{className:"react-list-pagination",children:[o?o(g):s.jsx("button",{type:"button",disabled:!P.hasPrev,onClick:E.first,children:"First"}),t?t(g):s.jsx("button",{type:"button",disabled:!P.hasPrev,onClick:E.prev,children:"Prev"}),r?r(g):s.jsx("div",{children:O.map(c=>{const h=c===b,S={...g,page:c,isActive:h};return i?i(S):s.jsx("div",{children:h?s.jsx("span",{children:c}):s.jsx("button",{type:"button",onClick:()=>E.setPage(c),children:c})},`page-${c}`)})}),f?f(g):s.jsx("button",{type:"button",disabled:!P.hasNext,onClick:E.next,children:"Next"}),p?p(g):s.jsx("button",{type:"button",disabled:!P.hasNext,onClick:E.last,children:"Last"})]})}),F=n.memo(({children:a,options:l=[10,25,50,100]})=>{const{listState:o}=w(),{data:t,pagination:r,setPerPage:i,loader:f,error:p}=o,{perPage:d}=r,{initialLoading:m}=f,u=n.useMemo(()=>l.map(y=>typeof y!="object"?{value:y,label:y}:y),[l]),L=n.useCallback(y=>{i(Number(y.target.value))},[i]),M=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:a?a(M):s.jsx("select",{value:d,onChange:L,children:u.map(y=>s.jsxs("option",{value:y.value,children:[y.label," items per page"]},`option-${y.value}`))})})}),J=n.memo(({children:a})=>{const{listState:l}=w(),{loader:o,refresh:t}=l,{isLoading:r,initialLoading:i}=o,f=n.useCallback(()=>{t({isRefresh:!0})},[t]),p=n.useMemo(()=>({isLoading:r,refresh:f}),[r,f]);return i?null:a?a(p):s.jsx("div",{className:"react-list-refresh",children:s.jsx("button",{onClick:f,disabled:r,children:r?"Loading...":"Refresh"})})}),K=n.memo(({children:a,debounceTime:l=500})=>{const{listState:o}=w(),{search:t,setSearch:r}=o,[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:a?a(m):s.jsx("input",{type:"text",value:i,onChange:u=>d(u.target.value),placeholder:"Search..."})})}),Q=n.memo(({children:a})=>{const{listState:l}=w(),{data:o,count:t,pagination:r,loader:i,error:f}=l,{page:p,perPage:d}=r,{initialLoading:m,isLoading:u}=i,L=n.useMemo(()=>{const y=p*d-d+1,N=Math.min(p*d,t),b=(o==null?void 0:o.length)||0;return{from:y,to:N,visibleCount:b}},[p,d,t,o]),M=n.useMemo(()=>({...L,count:t}),[L,t]);return m||!o||o.length===0||f?null:s.jsx("div",{className:"react-list-summary",children:a?a(M):s.jsxs("span",{children:["Showing ",s.jsx("span",{children:L.visibleCount})," items (",s.jsxs("span",{children:[L.from," - ",L.to]}),") out of ",s.jsx("span",{children:t})]})})});v.ReactListAttributes=I,v.ReactListEmpty=q,v.ReactListError=A,v.ReactListGoTo=_,v.ReactListInitialLoader=$,v.ReactListItems=D,v.ReactListLoadMore=x,v.ReactListLoader=G,v.ReactListPagination=W,v.ReactListPerPage=F,v.ReactListProvider=j,v.ReactListRefresh=J,v.ReactListSearch=K,v.ReactListSummary=Q,v.default=H,Object.defineProperties(v,{__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.5",
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
+ }