@7span/react-list 1.0.0 → 1.0.1

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