@7span/react-list 0.0.3 → 0.0.4

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 N } from "react/jsx-runtime";
2
- import { createContext as $, useContext as j, useState as T, useMemo as P, memo as R, useCallback as O, useEffect as E, useRef as q } from "react";
3
- const A = $(null), H = ({ children: n, config: o }) => {
4
- const { requestHandler: r, stateManager: e = {} } = o, [s, a] = T({
1
+ import { jsx as i, jsxs as z } from "react/jsx-runtime";
2
+ import { createContext as $, useContext as j, useState as T, useMemo as P, memo as w, useCallback as k, useEffect as E, useRef as q } from "react";
3
+ const A = $(null), H = ({ children: r, config: o }) => {
4
+ const { requestHandler: n, stateManager: e = {} } = o, [s, a] = T({
5
5
  data: [],
6
6
  response: null,
7
7
  error: null,
@@ -25,27 +25,27 @@ const A = $(null), H = ({ children: n, config: o }) => {
25
25
  isEmpty: !0,
26
26
  isInitializing: !0
27
27
  });
28
- if (!r)
28
+ if (!n)
29
29
  throw new Error("ListProvider: requestHandler is required.");
30
30
  const p = P(
31
31
  () => ({
32
- requestHandler: r,
32
+ requestHandler: n,
33
33
  stateManager: e,
34
34
  listState: s,
35
35
  setListState: a
36
36
  }),
37
- [r, e, s]
37
+ [n, e, s]
38
38
  );
39
- return /* @__PURE__ */ i(A.Provider, { value: p, children: n });
40
- }, C = () => {
41
- const n = j(A);
42
- if (!n)
39
+ return /* @__PURE__ */ i(A.Provider, { value: p, children: r });
40
+ }, R = () => {
41
+ const r = j(A);
42
+ if (!r)
43
43
  throw new Error("useListContext must be used within a ListProvider");
44
- return n;
45
- }, W = R(({ children: n, renderAttribute: o }) => {
46
- const { listState: r } = C(), { attrs: e, attrSettings: s, updateAttr: a } = r, p = O(
47
- (c) => (L) => {
48
- a(c, "visible", L.target.checked);
44
+ return r;
45
+ }, W = w(({ children: r, renderAttribute: o }) => {
46
+ const { listState: n } = R(), { attrs: e, attrSettings: s, updateAttr: a } = n, p = k(
47
+ (u) => (m) => {
48
+ a(u, "visible", m.target.checked);
49
49
  },
50
50
  [a]
51
51
  ), h = P(
@@ -56,70 +56,70 @@ const A = $(null), H = ({ children: n, config: o }) => {
56
56
  }),
57
57
  [e, s, a]
58
58
  );
59
- return n ? n(h) : /* @__PURE__ */ i("div", { className: "react-list-attributes", children: e.map((c, L) => {
59
+ return r ? r(h) : /* @__PURE__ */ i("div", { className: "react-list-attributes", children: e.map((u, m) => {
60
60
  var l;
61
61
  return o ? o({
62
- key: `attr-${L}`,
63
- attr: c,
62
+ key: `attr-${m}`,
63
+ attr: u,
64
64
  updateAttr: a,
65
65
  attrSettings: s
66
- }) : /* @__PURE__ */ N("label", { children: [
67
- /* @__PURE__ */ i("span", { children: c.label }),
66
+ }) : /* @__PURE__ */ z("label", { children: [
67
+ /* @__PURE__ */ i("span", { children: u.label }),
68
68
  /* @__PURE__ */ i(
69
69
  "input",
70
70
  {
71
71
  type: "checkbox",
72
- checked: ((l = s == null ? void 0 : s[c.name]) == null ? void 0 : l.visible) ?? !1,
73
- onChange: p(c.name)
72
+ checked: ((l = s == null ? void 0 : s[u.name]) == null ? void 0 : l.visible) ?? !1,
73
+ onChange: p(u.name)
74
74
  }
75
75
  )
76
- ] }, `attr-${L}`);
76
+ ] }, `attr-${m}`);
77
77
  }) });
78
- }), F = R(({ children: n }) => {
79
- const { listState: o } = C(), { data: r, loader: e, error: s } = o, { isLoading: a, initialLoading: p } = e;
80
- return (r == null ? void 0 : r.length) > 0 || p || a || s ? null : /* @__PURE__ */ i("div", { className: "react-list-empty", children: n || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "No data found!" }) }) });
81
- }), G = R(({ children: n }) => {
82
- const { listState: o } = C(), { error: r, loader: e } = o, { isLoading: s } = e;
83
- return !r || s ? null : /* @__PURE__ */ i("div", { className: "react-list-error", children: typeof n == "function" ? n({ error: r }) : n || /* @__PURE__ */ N("div", { children: [
78
+ }), F = w(({ children: r }) => {
79
+ const { listState: o } = R(), { data: n, loader: e, error: s } = o, { isLoading: a, initialLoading: p } = e;
80
+ return (n == null ? void 0 : n.length) > 0 || p || a || s ? null : /* @__PURE__ */ i("div", { className: "react-list-empty", children: r || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "No data found!" }) }) });
81
+ }), G = w(({ children: r }) => {
82
+ const { listState: o } = R(), { error: n, loader: e } = o, { isLoading: s } = e;
83
+ return !n || s ? null : /* @__PURE__ */ i("div", { className: "react-list-error", children: typeof r == "function" ? r({ error: n }) : r || /* @__PURE__ */ z("div", { children: [
84
84
  /* @__PURE__ */ i("h3", { children: "Error occurred" }),
85
- /* @__PURE__ */ N("pre", { children: [
86
- r.name,
85
+ /* @__PURE__ */ z("pre", { children: [
86
+ n.name,
87
87
  ": ",
88
- r.message
88
+ n.message
89
89
  ] })
90
90
  ] }) });
91
- }), J = R(({ children: n }) => {
92
- const { listState: o } = C(), { data: r, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: c, perPage: L } = s, { initialLoading: l, isLoading: S } = p, { pages: y, pagesCount: v } = P(() => {
93
- const f = Math.ceil(e / L);
94
- return { pages: Array.from({ length: f }, (k, b) => b + 1), pagesCount: f };
95
- }, [e, L]), x = O(
96
- (f) => {
97
- a(Number(f.target.value));
91
+ }), J = w(({ children: r }) => {
92
+ const { listState: o } = R(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: m } = s, { initialLoading: l, isLoading: L } = p, { pages: y, pagesCount: S } = P(() => {
93
+ const t = Math.ceil(e / m);
94
+ return { pages: Array.from({ length: t }, (M, b) => b + 1), pagesCount: t };
95
+ }, [e, m]), N = k(
96
+ (t) => {
97
+ a(Number(t.target.value));
98
98
  },
99
99
  [a]
100
- ), t = P(
100
+ ), v = P(
101
101
  () => ({
102
102
  setPage: a,
103
- page: c,
103
+ page: u,
104
104
  pages: y,
105
- pagesCount: v
105
+ pagesCount: S
106
106
  }),
107
- [a, c, y, v]
107
+ [a, u, y, S]
108
108
  );
109
- return l || !r || r.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-go-to", children: n ? n(t) : /* @__PURE__ */ i("select", { value: c, onChange: x, children: y.map((f) => /* @__PURE__ */ N("option", { value: f, children: [
109
+ return l || !n || n.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-go-to", children: r ? r(v) : /* @__PURE__ */ i("select", { value: u, onChange: N, children: y.map((t) => /* @__PURE__ */ z("option", { value: t, children: [
110
110
  "Page ",
111
- f
112
- ] }, `page-${f}`)) }) });
113
- }), K = R(({ children: n }) => {
114
- const { listState: o } = C(), { loader: r } = o, { initialLoading: e } = r;
111
+ t
112
+ ] }, `page-${t}`)) }) });
113
+ }), K = w(({ children: r }) => {
114
+ const { listState: o } = R(), { loader: n } = o, { initialLoading: e } = n;
115
115
  return P(
116
116
  () => ({
117
117
  loading: e
118
118
  }),
119
119
  [e]
120
- ), e ? /* @__PURE__ */ i("div", { className: "react-list-initial-loader", children: n || /* @__PURE__ */ i("p", { children: "Initial Loading..." }) }) : null;
121
- }), Q = R(({ children: n, renderItem: o }) => {
122
- const { listState: r } = C(), { data: e = [], loader: s, error: a, setSort: p, sort: h } = r, { initialLoading: c } = s, L = P(
120
+ ), e ? /* @__PURE__ */ i("div", { className: "react-list-initial-loader", children: r || /* @__PURE__ */ i("p", { children: "Initial Loading..." }) }) : null;
121
+ }), Q = w(({ children: r, renderItem: o }) => {
122
+ const { listState: n } = R(), { data: e = [], loader: s, error: a, setSort: p, sort: h } = n, { initialLoading: u } = s, m = P(
123
123
  () => ({
124
124
  items: e,
125
125
  setSort: p,
@@ -127,119 +127,120 @@ const A = $(null), H = ({ children: n, config: o }) => {
127
127
  }),
128
128
  [e, h, p]
129
129
  );
130
- return c || !e || e.length === 0 || a ? null : o ? /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, S) => /* @__PURE__ */ i("div", { children: o({ item: l, index: S }) }, l.id || S)) }) : typeof n == "function" ? /* @__PURE__ */ i("div", { className: "react-list-items", children: n(L) }) : /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, S) => /* @__PURE__ */ i("pre", { children: JSON.stringify(l, null, 2) }, l.id || S)) });
130
+ return u || !e || e.length === 0 || a ? null : o ? /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, L) => /* @__PURE__ */ i("div", { children: o({ item: l, index: L }) }, l.id || L)) }) : typeof r == "function" ? /* @__PURE__ */ i("div", { className: "react-list-items", children: r(m) }) : /* @__PURE__ */ i("div", { className: "react-list-items", children: e.map((l, L) => /* @__PURE__ */ i("pre", { children: JSON.stringify(l, null, 2) }, l.id || L)) });
131
131
  }), U = ({
132
- children: n,
133
- endpoint: o,
134
- page: r = 1,
135
- perPage: e = 25,
136
- sortBy: s,
137
- sortOrder: a = "desc",
138
- search: p = "",
139
- filters: h = {},
140
- attrs: c,
141
- version: L = 1,
142
- paginationMode: l = "pagination",
143
- meta: S = {}
132
+ initialItems: r = [],
133
+ children: o,
134
+ endpoint: n,
135
+ page: e = 1,
136
+ perPage: s = 25,
137
+ sortBy: a = "",
138
+ sortOrder: p = "desc",
139
+ search: h = "",
140
+ filters: u = {},
141
+ attrs: m,
142
+ version: l = 1,
143
+ paginationMode: L = "pagination",
144
+ meta: y = {}
144
145
  }) => {
145
- const { requestHandler: y, setListState: v } = C(), x = l === "loadMore", [t, f] = T({
146
- page: r,
147
- perPage: e,
148
- sortBy: s,
149
- sortOrder: a,
150
- search: p,
151
- filters: h,
146
+ const { requestHandler: S, setListState: N } = R(), v = L === "loadMore", [t, C] = T({
147
+ page: e,
148
+ perPage: s,
149
+ sortBy: a,
150
+ sortOrder: p,
151
+ search: h,
152
+ filters: u,
152
153
  attrSettings: {},
153
- items: [],
154
+ items: r,
154
155
  selection: [],
155
156
  error: null,
156
157
  response: null,
157
158
  count: 0,
158
159
  isLoading: !1,
159
160
  initializingState: !0
160
- }), w = O(
161
- async (m = {}, d = null) => {
162
- t.initializingState || f((g) => ({ ...g, error: null, isLoading: !0 }));
161
+ }), M = k(
162
+ async (d = {}, g = null) => {
163
+ t.initializingState || C((c) => ({ ...c, error: null, isLoading: !0 }));
163
164
  try {
164
- const g = d || t, u = await y({
165
- endpoint: o,
166
- version: L,
167
- meta: S,
168
- page: g.page,
169
- perPage: g.perPage,
170
- search: g.search,
171
- sortBy: g.sortBy,
172
- sortOrder: g.sortOrder,
173
- filters: g.filters,
174
- ...m
165
+ const c = g || t, f = await S({
166
+ endpoint: n,
167
+ version: l,
168
+ meta: y,
169
+ page: c.page,
170
+ perPage: c.perPage,
171
+ search: c.search,
172
+ sortBy: c.sortBy,
173
+ sortOrder: c.sortOrder,
174
+ filters: c.filters,
175
+ ...d
175
176
  });
176
- f((M) => ({
177
- ...M,
178
- response: u,
177
+ C((x) => ({
178
+ ...x,
179
+ response: f,
179
180
  selection: [],
180
181
  // Append items for loadMore, replace for pagination
181
- items: x && g.page > 1 ? [...M.items, ...u.items] : u.items,
182
- count: u.count,
182
+ items: v && c.page > 1 ? [...x.items, ...f.items] : f.items,
183
+ count: f.count,
183
184
  initializingState: !1,
184
185
  isLoading: !1
185
186
  }));
186
- } catch (g) {
187
- throw f((u) => ({
188
- ...u,
189
- error: g,
187
+ } catch (c) {
188
+ throw C((f) => ({
189
+ ...f,
190
+ error: c,
190
191
  items: [],
191
192
  count: 0,
192
193
  initializingState: !1,
193
194
  isLoading: !1
194
- })), g;
195
+ })), c;
195
196
  }
196
197
  },
197
- [o, L, x, S, y, t]
198
- ), k = P(
198
+ [n, l, v, y, S, t]
199
+ ), b = P(
199
200
  () => ({
200
- setPage: (m, d) => {
201
- let g = m;
202
- m === 0 && (g = "");
203
- const u = { ...t, page: g };
204
- f(u), g && w(d, u);
201
+ setPage: (d, g) => {
202
+ let c = d;
203
+ d === 0 && (c = "");
204
+ const f = { ...t, page: c };
205
+ C(f), c && M(g, f);
205
206
  },
206
- setPerPage: (m) => {
207
- const d = { ...t, perPage: m, page: 1 };
208
- f(d), w({}, d);
207
+ setPerPage: (d) => {
208
+ const g = { ...t, perPage: d, page: 1 };
209
+ C(g), M({}, g);
209
210
  },
210
- setSearch: (m) => {
211
- if (m !== t.search) {
212
- const d = { ...t, search: m, page: 1 };
213
- f(d), w({}, d);
211
+ setSearch: (d) => {
212
+ if (d !== t.search) {
213
+ const g = { ...t, search: d, page: 1 };
214
+ C(g), M({}, g);
214
215
  }
215
216
  },
216
- setSort: ({ by: m, order: d }) => {
217
- const g = { ...t, sortBy: m, sortOrder: d, page: 1 };
218
- f(g), w({}, g);
217
+ setSort: ({ by: d, order: g }) => {
218
+ const c = { ...t, sortBy: d, sortOrder: g, page: 1 };
219
+ C(c), M({}, c);
219
220
  },
220
221
  loadMore: () => {
221
- const m = { ...t, page: t.page + 1 };
222
- f(m), w({}, m);
222
+ const d = { ...t, page: t.page + 1 };
223
+ C(d), M({}, d);
223
224
  },
224
- refresh: (m = { isRefresh: !0 }) => {
225
- if (x) {
226
- const d = { ...t, page: 1, items: [] };
227
- f(d), w(m, d);
225
+ refresh: (d = { isRefresh: !0 }) => {
226
+ if (v) {
227
+ const g = { ...t, page: 1, items: [] };
228
+ C(g), M(d, g);
228
229
  } else
229
- w(m);
230
+ M(d);
230
231
  },
231
- setFilters: (m) => {
232
- const d = { ...t, filters: m, page: 1 };
233
- f(d), w({}, d);
232
+ setFilters: (d) => {
233
+ const g = { ...t, filters: d, page: 1 };
234
+ C(g), M({}, g);
234
235
  },
235
- updateItemById: (m, d) => {
236
- const g = t.items.map((u) => u.id === d ? { ...u, ...m } : u);
237
- f((u) => ({ ...u, items: g }));
236
+ updateItemById: (d, g) => {
237
+ const c = t.items.map((f) => f.id === g ? { ...f, ...d } : f);
238
+ C((f) => ({ ...f, items: c }));
238
239
  },
239
- setSelection: (m) => f((d) => ({ ...d, selection: m }))
240
+ setSelection: (d) => C((g) => ({ ...g, selection: d }))
240
241
  }),
241
- [w, x, t]
242
- ), b = P(
242
+ [M, v, t]
243
+ ), O = P(
243
244
  () => ({
244
245
  data: t.items,
245
246
  response: t.response,
@@ -258,9 +259,9 @@ const A = $(null), H = ({ children: n, config: o }) => {
258
259
  sort: { sortBy: t.sortBy, sortOrder: t.sortOrder },
259
260
  search: t.search,
260
261
  filters: t.filters,
261
- attrs: c || Object.keys(t.items[0] || {}),
262
+ attrs: m || Object.keys(t.items[0] || {}),
262
263
  isEmpty: t.items.length === 0,
263
- ...k
264
+ ...b
264
265
  }),
265
266
  [
266
267
  t.items,
@@ -276,16 +277,16 @@ const A = $(null), H = ({ children: n, config: o }) => {
276
277
  t.sortOrder,
277
278
  t.search,
278
279
  t.filters,
279
- k,
280
- c
280
+ b,
281
+ m
281
282
  ]
282
283
  );
283
284
  return E(() => {
284
- t.initializingState && k.setPage(t.page);
285
+ t.initializingState && b.setPage(t.page);
285
286
  }, []), E(() => {
286
- v(b);
287
+ N(O);
287
288
  }, [
288
- v,
289
+ N,
289
290
  t.items,
290
291
  t.count,
291
292
  t.error,
@@ -295,69 +296,69 @@ const A = $(null), H = ({ children: n, config: o }) => {
295
296
  t.perPage,
296
297
  t.sortBy,
297
298
  t.sortOrder
298
- ]), typeof n == "function" ? n(b) : n;
299
- }, V = R(({ children: n }) => {
300
- const { listState: o } = C(), { data: r, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: c, perPage: L } = s, { isLoading: l } = p, S = P(
301
- () => c * L < e,
302
- [c, L, e]
303
- ), y = O(() => {
304
- S && !l && a(c + 1);
305
- }, [S, l, a, c]), v = P(
299
+ ]), typeof o == "function" ? o(O) : o;
300
+ }, V = w(({ children: r }) => {
301
+ const { listState: o } = R(), { data: n, count: e, pagination: s, setPage: a, loader: p, error: h } = o, { page: u, perPage: m } = s, { isLoading: l } = p, L = P(
302
+ () => u * m < e,
303
+ [u, m, e]
304
+ ), y = k(() => {
305
+ L && !l && a(u + 1);
306
+ }, [L, l, a, u]), S = P(
306
307
  () => ({
307
308
  isLoading: l,
308
309
  loadMore: y,
309
- hasMoreItems: S
310
+ hasMoreItems: L
310
311
  }),
311
- [l, y, S]
312
+ [l, y, L]
312
313
  );
313
- return !r || r.length === 0 || h ? null : n(v);
314
- }), X = R(({ children: n, position: o = "overlay" }) => {
315
- const { listState: r } = C(), { loader: e } = r, { isLoading: s, initializingState: a } = e;
314
+ return !n || n.length === 0 || h ? null : r(S);
315
+ }), X = w(({ children: r, position: o = "overlay" }) => {
316
+ const { listState: n } = R(), { loader: e } = n, { isLoading: s, initializingState: a } = e;
316
317
  return P(
317
318
  () => ({
318
319
  isLoading: s
319
320
  }),
320
321
  [s]
321
- ), !a && !s ? null : /* @__PURE__ */ i("div", { children: n || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "Loading..." }) }) });
322
- }), Y = R(
322
+ ), !a && !s ? null : /* @__PURE__ */ i("div", { children: r || /* @__PURE__ */ i("div", { children: /* @__PURE__ */ i("p", { children: "Loading..." }) }) });
323
+ }), Y = w(
323
324
  ({
324
- children: n,
325
+ children: r,
325
326
  pageLinks: o = 5,
326
- renderFirst: r,
327
+ renderFirst: n,
327
328
  renderPrev: e,
328
329
  renderPages: s,
329
330
  renderPage: a,
330
331
  renderNext: p,
331
332
  renderLast: h
332
333
  }) => {
333
- const { listState: c } = C(), { data: L, count: l, pagination: S, setPage: y, loader: v, error: x } = c, { page: t, perPage: f } = S, { initialLoading: w, isLoading: k } = v, b = P(() => {
334
- const u = Math.ceil(l / f), M = Math.floor(o / 2), z = t * f < l;
335
- return { pagesCount: u, halfWay: M, hasNext: z, hasPrev: t !== 1 };
336
- }, [l, f, t, o]), m = P(() => {
337
- const { pagesCount: u, halfWay: M } = b, z = Array.from({ length: Math.min(o, u) });
338
- return t <= M ? z.map((I, B) => B + 1) : u - t < M ? z.map((I, B) => u - B).reverse() : z.map((I, B) => t - M + B);
339
- }, [t, o, b]), d = P(
334
+ const { listState: u } = R(), { data: m, count: l, pagination: L, setPage: y, loader: S, error: N } = u, { page: v, perPage: t } = L, { initialLoading: C, isLoading: M } = S, b = P(() => {
335
+ const c = Math.ceil(l / t), f = Math.floor(o / 2), x = v * t < l;
336
+ return { pagesCount: c, halfWay: f, hasNext: x, hasPrev: v !== 1 };
337
+ }, [l, t, v, o]), O = P(() => {
338
+ const { pagesCount: c, halfWay: f } = b, x = Array.from({ length: Math.min(o, c) });
339
+ return v <= f ? x.map((I, B) => B + 1) : c - v < f ? x.map((I, B) => c - B).reverse() : x.map((I, B) => v - f + B);
340
+ }, [v, o, b]), d = P(
340
341
  () => ({
341
- prev: () => y(t - 1),
342
- next: () => y(t + 1),
342
+ prev: () => y(v - 1),
343
+ next: () => y(v + 1),
343
344
  first: () => y(1),
344
345
  last: () => y(b.pagesCount),
345
- setPage: (u) => y(u)
346
+ setPage: (c) => y(c)
346
347
  }),
347
- [y, t, b.pagesCount]
348
+ [y, v, b.pagesCount]
348
349
  ), g = P(
349
350
  () => ({
350
- page: t,
351
- perPage: f,
351
+ page: v,
352
+ perPage: t,
352
353
  count: l,
353
354
  ...b,
354
- pagesToDisplay: m,
355
+ pagesToDisplay: O,
355
356
  ...d
356
357
  }),
357
- [t, f, l, b, m, d]
358
+ [v, t, l, b, O, d]
358
359
  );
359
- return w || !L || L.length === 0 || x ? null : n ? n(g) : /* @__PURE__ */ N("div", { className: "react-list-pagination", children: [
360
- r ? r(g) : /* @__PURE__ */ i(
360
+ return C || !m || m.length === 0 || N ? null : r ? r(g) : /* @__PURE__ */ z("div", { className: "react-list-pagination", children: [
361
+ n ? n(g) : /* @__PURE__ */ i(
361
362
  "button",
362
363
  {
363
364
  type: "button",
@@ -375,16 +376,16 @@ const A = $(null), H = ({ children: n, config: o }) => {
375
376
  children: "Prev"
376
377
  }
377
378
  ),
378
- s ? s(g) : /* @__PURE__ */ i("div", { children: m.map((u) => {
379
- const M = u === t, z = { ...g, page: u, isActive: M };
380
- return a ? a(z) : /* @__PURE__ */ i("div", { children: M ? /* @__PURE__ */ i("span", { children: u }) : /* @__PURE__ */ i(
379
+ s ? s(g) : /* @__PURE__ */ i("div", { children: O.map((c) => {
380
+ const f = c === v, x = { ...g, page: c, isActive: f };
381
+ return a ? a(x) : /* @__PURE__ */ i("div", { children: f ? /* @__PURE__ */ i("span", { children: c }) : /* @__PURE__ */ i(
381
382
  "button",
382
383
  {
383
384
  type: "button",
384
- onClick: () => d.setPage(u),
385
- children: u
385
+ onClick: () => d.setPage(c),
386
+ children: c
386
387
  }
387
- ) }, `page-${u}`);
388
+ ) }, `page-${c}`);
388
389
  }) }),
389
390
  p ? p(g) : /* @__PURE__ */ i(
390
391
  "button",
@@ -406,31 +407,31 @@ const A = $(null), H = ({ children: n, config: o }) => {
406
407
  )
407
408
  ] });
408
409
  }
409
- ), Z = R(
410
- ({ children: n, options: o = [10, 25, 50, 100] }) => {
411
- const { listState: r } = C(), { data: e, pagination: s, setPerPage: a, loader: p, error: h } = r, { perPage: c } = s, { initialLoading: L } = p, l = P(() => o.map((v) => typeof v != "object" ? {
412
- value: v,
413
- label: v
414
- } : v), [o]), S = O(
415
- (v) => {
416
- a(Number(v.target.value));
410
+ ), Z = w(
411
+ ({ children: r, options: o = [10, 25, 50, 100] }) => {
412
+ const { listState: n } = R(), { data: e, pagination: s, setPerPage: a, loader: p, error: h } = n, { perPage: u } = s, { initialLoading: m } = p, l = P(() => o.map((S) => typeof S != "object" ? {
413
+ value: S,
414
+ label: S
415
+ } : S), [o]), L = k(
416
+ (S) => {
417
+ a(Number(S.target.value));
417
418
  },
418
419
  [a]
419
420
  ), y = P(
420
421
  () => ({
421
- perPage: c,
422
+ perPage: u,
422
423
  setPerPage: a,
423
424
  options: l
424
425
  }),
425
- [c, a, l]
426
+ [u, a, l]
426
427
  );
427
- return L || !e || e.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-per-page", children: n ? n(y) : /* @__PURE__ */ i("select", { value: c, onChange: S, children: l.map((v) => /* @__PURE__ */ N("option", { value: v.value, children: [
428
- v.label,
428
+ return m || !e || e.length === 0 || h ? null : /* @__PURE__ */ i("div", { className: "react-list-per-page", children: r ? r(y) : /* @__PURE__ */ i("select", { value: u, onChange: L, children: l.map((S) => /* @__PURE__ */ z("option", { value: S.value, children: [
429
+ S.label,
429
430
  " items per page"
430
- ] }, `option-${v.value}`)) }) });
431
+ ] }, `option-${S.value}`)) }) });
431
432
  }
432
- ), tt = R(({ children: n }) => {
433
- const { listState: o } = C(), { loader: r, refresh: e } = o, { isLoading: s, initialLoading: a } = r, p = O(() => {
433
+ ), tt = w(({ children: r }) => {
434
+ const { listState: o } = R(), { loader: n, refresh: e } = o, { isLoading: s, initialLoading: a } = n, p = k(() => {
434
435
  e({ isRefresh: !0 });
435
436
  }, [e]), h = P(
436
437
  () => ({
@@ -439,50 +440,50 @@ const A = $(null), H = ({ children: n, config: o }) => {
439
440
  }),
440
441
  [s, p]
441
442
  );
442
- return a ? null : n ? n(h) : /* @__PURE__ */ i("div", { className: "react-list-refresh", children: /* @__PURE__ */ i("button", { onClick: p, disabled: s, children: s ? "Loading..." : "Refresh" }) });
443
- }), et = R(({ children: n, debounceTime: o = 500 }) => {
444
- const { listState: r } = C(), { search: e, setSearch: s } = r, [a, p] = T(e ?? ""), h = q(null);
443
+ return a ? null : r ? r(h) : /* @__PURE__ */ i("div", { className: "react-list-refresh", children: /* @__PURE__ */ i("button", { onClick: p, disabled: s, children: s ? "Loading..." : "Refresh" }) });
444
+ }), et = w(({ children: r, debounceTime: o = 500 }) => {
445
+ const { listState: n } = R(), { search: e, setSearch: s } = n, [a, p] = T(e ?? ""), h = q(null);
445
446
  E(() => {
446
447
  e !== a && p(e ?? "");
447
448
  }, [e]);
448
- const c = (l) => {
449
+ const u = (l) => {
449
450
  p(l), h.current && clearTimeout(h.current), h.current = setTimeout(() => {
450
451
  s(l);
451
452
  }, o);
452
453
  };
453
454
  return E(() => () => {
454
455
  h.current && clearTimeout(h.current);
455
- }, []), /* @__PURE__ */ i("div", { className: "react-list-search", children: n ? n({
456
+ }, []), /* @__PURE__ */ i("div", { className: "react-list-search", children: r ? r({
456
457
  search: a,
457
- setSearch: c
458
+ setSearch: u
458
459
  }) : /* @__PURE__ */ i(
459
460
  "input",
460
461
  {
461
462
  type: "text",
462
463
  value: a,
463
- onChange: (l) => c(l.target.value),
464
+ onChange: (l) => u(l.target.value),
464
465
  placeholder: "Search..."
465
466
  }
466
467
  ) });
467
- }), nt = R(({ children: n }) => {
468
- const { listState: o } = C(), { data: r, count: e, pagination: s, loader: a, error: p } = o, { page: h, perPage: c } = s, { initialLoading: L, isLoading: l } = a, S = P(() => {
469
- const v = h * c - c + 1, x = Math.min(h * c, e), t = (r == null ? void 0 : r.length) || 0;
470
- return { from: v, to: x, visibleCount: t };
471
- }, [h, c, e, r]), y = P(
468
+ }), nt = w(({ children: r }) => {
469
+ const { listState: o } = R(), { data: n, count: e, pagination: s, loader: a, error: p } = o, { page: h, perPage: u } = s, { initialLoading: m, isLoading: l } = a, L = P(() => {
470
+ const S = h * u - u + 1, N = Math.min(h * u, e), v = (n == null ? void 0 : n.length) || 0;
471
+ return { from: S, to: N, visibleCount: v };
472
+ }, [h, u, e, n]), y = P(
472
473
  () => ({
473
- ...S,
474
+ ...L,
474
475
  count: e
475
476
  }),
476
- [S, e]
477
+ [L, e]
477
478
  );
478
- return L || !r || r.length === 0 || p ? null : /* @__PURE__ */ i("div", { className: "react-list-summary", children: n ? n(y) : /* @__PURE__ */ N("span", { children: [
479
+ return m || !n || n.length === 0 || p ? null : /* @__PURE__ */ i("div", { className: "react-list-summary", children: r ? r(y) : /* @__PURE__ */ z("span", { children: [
479
480
  "Showing ",
480
- /* @__PURE__ */ i("span", { children: S.visibleCount }),
481
+ /* @__PURE__ */ i("span", { children: L.visibleCount }),
481
482
  " items (",
482
- /* @__PURE__ */ N("span", { children: [
483
- S.from,
483
+ /* @__PURE__ */ z("span", { children: [
484
+ L.from,
484
485
  " - ",
485
- S.to
486
+ L.to
486
487
  ] }),
487
488
  ") out of ",
488
489
  /* @__PURE__ */ i("span", { children: e })
@@ -1 +1 @@
1
- (function(y,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):(y=typeof globalThis<"u"?globalThis:y||self,s(y.ReactList={},y.jsxRuntime,y.react))})(this,function(y,s,n){"use strict";const B=n.createContext(null),I=({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})},C=()=>{const a=n.useContext(B);if(!a)throw new Error("useListContext must be used within a ListProvider");return a},j=n.memo(({children:a,renderAttribute:l})=>{const{listState:o}=C(),{attrs:t,attrSettings:r,updateAttr:i}=o,f=n.useCallback(c=>S=>{i(c,"visible",S.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((c,S)=>{var u;return l?l({key:`attr-${S}`,attr:c,updateAttr:i,attrSettings:r}):s.jsxs("label",{children:[s.jsx("span",{children:c.label}),s.jsx("input",{type:"checkbox",checked:((u=r==null?void 0:r[c.name])==null?void 0:u.visible)??!1,onChange:f(c.name)})]},`attr-${S}`)})})}),q=n.memo(({children:a})=>{const{listState:l}=C(),{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}=C(),{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}=C(),{data:o,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:c,perPage:S}=r,{initialLoading:u,isLoading:v}=f,{pages:b,pagesCount:P}=n.useMemo(()=>{const m=Math.ceil(t/S);return{pages:Array.from({length:m},(O,M)=>M+1),pagesCount:m}},[t,S]),E=n.useCallback(m=>{i(Number(m.target.value))},[i]),e=n.useMemo(()=>({setPage:i,page:c,pages:b,pagesCount:P}),[i,c,b,P]);return u||!o||o.length===0||p?null:s.jsx("div",{className:"react-list-go-to",children:a?a(e):s.jsx("select",{value:c,onChange:E,children:b.map(m=>s.jsxs("option",{value:m,children:["Page ",m]},`page-${m}`))})})}),$=n.memo(({children:a})=>{const{listState:l}=C(),{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}=C(),{data:t=[],loader:r,error:i,setSort:f,sort:p}=o,{initialLoading:c}=r,S=n.useMemo(()=>({items:t,setSort:f,sort:p}),[t,p,f]);return c||!t||t.length===0||i?null:l?s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("div",{children:l({item:u,index:v})},u.id||v))}):typeof a=="function"?s.jsx("div",{className:"react-list-items",children:a(S)}):s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("pre",{children:JSON.stringify(u,null,2)},u.id||v))})}),H=({children:a,endpoint:l,page:o=1,perPage:t=25,sortBy:r,sortOrder:i="desc",search:f="",filters:p={},attrs:c,version:S=1,paginationMode:u="pagination",meta:v={}})=>{const{requestHandler:b,setListState:P}=C(),E=u==="loadMore",[e,m]=n.useState({page:o,perPage:t,sortBy:r,sortOrder:i,search:f,filters:p,attrSettings:{},items:[],selection:[],error:null,response:null,count:0,isLoading:!1,initializingState:!0}),w=n.useCallback(async(L={},g=null)=>{e.initializingState||m(h=>({...h,error:null,isLoading:!0}));try{const h=g||e,d=await b({endpoint:l,version:S,meta:v,page:h.page,perPage:h.perPage,search:h.search,sortBy:h.sortBy,sortOrder:h.sortOrder,filters:h.filters,...L});m(k=>({...k,response:d,selection:[],items:E&&h.page>1?[...k.items,...d.items]:d.items,count:d.count,initializingState:!1,isLoading:!1}))}catch(h){throw m(d=>({...d,error:h,items:[],count:0,initializingState:!1,isLoading:!1})),h}},[l,S,E,v,b,e]),O=n.useMemo(()=>({setPage:(L,g)=>{let h=L;L===0&&(h="");const d={...e,page:h};m(d),h&&w(g,d)},setPerPage:L=>{const g={...e,perPage:L,page:1};m(g),w({},g)},setSearch:L=>{if(L!==e.search){const g={...e,search:L,page:1};m(g),w({},g)}},setSort:({by:L,order:g})=>{const h={...e,sortBy:L,sortOrder:g,page:1};m(h),w({},h)},loadMore:()=>{const L={...e,page:e.page+1};m(L),w({},L)},refresh:(L={isRefresh:!0})=>{if(E){const g={...e,page:1,items:[]};m(g),w(L,g)}else w(L)},setFilters:L=>{const g={...e,filters:L,page:1};m(g),w({},g)},updateItemById:(L,g)=>{const h=e.items.map(d=>d.id===g?{...d,...L}:d);m(d=>({...d,items:h}))},setSelection:L=>m(g=>({...g,selection:L}))}),[w,E,e]),M=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:c||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,c]);return n.useEffect(()=>{e.initializingState&&O.setPage(e.page)},[]),n.useEffect(()=>{P(M)},[P,e.items,e.count,e.error,e.isLoading,e.selection,e.page,e.perPage,e.sortBy,e.sortOrder]),typeof a=="function"?a(M):a},x=n.memo(({children:a})=>{const{listState:l}=C(),{data:o,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:c,perPage:S}=r,{isLoading:u}=f,v=n.useMemo(()=>c*S<t,[c,S,t]),b=n.useCallback(()=>{v&&!u&&i(c+1)},[v,u,i,c]),P=n.useMemo(()=>({isLoading:u,loadMore:b,hasMoreItems:v}),[u,b,v]);return!o||o.length===0||p?null:a(P)}),G=n.memo(({children:a,position:l="overlay"})=>{const{listState:o}=C(),{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:c}=C(),{data:S,count:u,pagination:v,setPage:b,loader:P,error:E}=c,{page:e,perPage:m}=v,{initialLoading:w,isLoading:O}=P,M=n.useMemo(()=>{const d=Math.ceil(u/m),k=Math.floor(l/2),N=e*m<u;return{pagesCount:d,halfWay:k,hasNext:N,hasPrev:e!==1}},[u,m,e,l]),L=n.useMemo(()=>{const{pagesCount:d,halfWay:k}=M,N=Array.from({length:Math.min(l,d)});return e<=k?N.map((T,z)=>z+1):d-e<k?N.map((T,z)=>d-z).reverse():N.map((T,z)=>e-k+z)},[e,l,M]),g=n.useMemo(()=>({prev:()=>b(e-1),next:()=>b(e+1),first:()=>b(1),last:()=>b(M.pagesCount),setPage:d=>b(d)}),[b,e,M.pagesCount]),h=n.useMemo(()=>({page:e,perPage:m,count:u,...M,pagesToDisplay:L,...g}),[e,m,u,M,L,g]);return w||!S||S.length===0||E?null:a?a(h):s.jsxs("div",{className:"react-list-pagination",children:[o?o(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.first,children:"First"}),t?t(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.prev,children:"Prev"}),r?r(h):s.jsx("div",{children:L.map(d=>{const k=d===e,N={...h,page:d,isActive:k};return i?i(N):s.jsx("div",{children:k?s.jsx("span",{children:d}):s.jsx("button",{type:"button",onClick:()=>g.setPage(d),children:d})},`page-${d}`)})}),f?f(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.next,children:"Next"}),p?p(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.last,children:"Last"})]})}),F=n.memo(({children:a,options:l=[10,25,50,100]})=>{const{listState:o}=C(),{data:t,pagination:r,setPerPage:i,loader:f,error:p}=o,{perPage:c}=r,{initialLoading:S}=f,u=n.useMemo(()=>l.map(P=>typeof P!="object"?{value:P,label:P}:P),[l]),v=n.useCallback(P=>{i(Number(P.target.value))},[i]),b=n.useMemo(()=>({perPage:c,setPerPage:i,options:u}),[c,i,u]);return S||!t||t.length===0||p?null:s.jsx("div",{className:"react-list-per-page",children:a?a(b):s.jsx("select",{value:c,onChange:v,children:u.map(P=>s.jsxs("option",{value:P.value,children:[P.label," items per page"]},`option-${P.value}`))})})}),J=n.memo(({children:a})=>{const{listState:l}=C(),{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}=C(),{search:t,setSearch:r}=o,[i,f]=n.useState(t??""),p=n.useRef(null);n.useEffect(()=>{t!==i&&f(t??"")},[t]);const c=u=>{f(u),p.current&&clearTimeout(p.current),p.current=setTimeout(()=>{r(u)},l)};n.useEffect(()=>()=>{p.current&&clearTimeout(p.current)},[]);const S={search:i,setSearch:c};return s.jsx("div",{className:"react-list-search",children:a?a(S):s.jsx("input",{type:"text",value:i,onChange:u=>c(u.target.value),placeholder:"Search..."})})}),Q=n.memo(({children:a})=>{const{listState:l}=C(),{data:o,count:t,pagination:r,loader:i,error:f}=l,{page:p,perPage:c}=r,{initialLoading:S,isLoading:u}=i,v=n.useMemo(()=>{const P=p*c-c+1,E=Math.min(p*c,t),e=(o==null?void 0:o.length)||0;return{from:P,to:E,visibleCount:e}},[p,c,t,o]),b=n.useMemo(()=>({...v,count:t}),[v,t]);return S||!o||o.length===0||f?null:s.jsx("div",{className:"react-list-summary",children:a?a(b):s.jsxs("span",{children:["Showing ",s.jsx("span",{children:v.visibleCount})," items (",s.jsxs("span",{children:[v.from," - ",v.to]}),") out of ",s.jsx("span",{children:t})]})})});y.ReactListAttributes=j,y.ReactListEmpty=q,y.ReactListError=A,y.ReactListGoTo=_,y.ReactListInitialLoader=$,y.ReactListItems=D,y.ReactListLoadMore=x,y.ReactListLoader=G,y.ReactListPagination=W,y.ReactListPerPage=F,y.ReactListProvider=I,y.ReactListRefresh=J,y.ReactListSearch=K,y.ReactListSummary=Q,y.default=H,Object.defineProperties(y,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ (function(S,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],s):(S=typeof globalThis<"u"?globalThis:S||self,s(S.ReactList={},S.jsxRuntime,S.react))})(this,function(S,s,n){"use strict";const B=n.createContext(null),I=({children:o,config:l})=>{const{requestHandler:a,stateManager:t={}}=l,[r,i]=n.useState({data:[],response:null,error:null,count:0,selection:[],pagination:{page:1,perPage:25},loader:{isLoading:!1,initialLoading:!0},sort:{sortBy:null,sortOrder:"desc"},search:"",filters:{},attrs:[],isEmpty:!0,isInitializing:!0});if(!a)throw new Error("ListProvider: requestHandler is required.");const f=n.useMemo(()=>({requestHandler:a,stateManager:t,listState:r,setListState:i}),[a,t,r]);return s.jsx(B.Provider,{value:f,children:o})},w=()=>{const o=n.useContext(B);if(!o)throw new Error("useListContext must be used within a ListProvider");return o},j=n.memo(({children:o,renderAttribute:l})=>{const{listState:a}=w(),{attrs:t,attrSettings:r,updateAttr:i}=a,f=n.useCallback(d=>m=>{i(d,"visible",m.target.checked)},[i]),p=n.useMemo(()=>({attrs:t,attrSettings:r,updateAttr:i}),[t,r,i]);return o?o(p):s.jsx("div",{className:"react-list-attributes",children:t.map((d,m)=>{var u;return l?l({key:`attr-${m}`,attr:d,updateAttr:i,attrSettings:r}):s.jsxs("label",{children:[s.jsx("span",{children:d.label}),s.jsx("input",{type:"checkbox",checked:((u=r==null?void 0:r[d.name])==null?void 0:u.visible)??!1,onChange:f(d.name)})]},`attr-${m}`)})})}),q=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,loader:t,error:r}=l,{isLoading:i,initialLoading:f}=t;return(a==null?void 0:a.length)>0||f||i||r?null:s.jsx("div",{className:"react-list-empty",children:o||s.jsx("div",{children:s.jsx("p",{children:"No data found!"})})})}),A=n.memo(({children:o})=>{const{listState:l}=w(),{error:a,loader:t}=l,{isLoading:r}=t;return!a||r?null:s.jsx("div",{className:"react-list-error",children:typeof o=="function"?o({error:a}):o||s.jsxs("div",{children:[s.jsx("h3",{children:"Error occurred"}),s.jsxs("pre",{children:[a.name,": ",a.message]})]})})}),_=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{initialLoading:u,isLoading:v}=f,{pages:b,pagesCount:y}=n.useMemo(()=>{const e=Math.ceil(t/m);return{pages:Array.from({length:e},(k,M)=>M+1),pagesCount:e}},[t,m]),N=n.useCallback(e=>{i(Number(e.target.value))},[i]),P=n.useMemo(()=>({setPage:i,page:d,pages:b,pagesCount:y}),[i,d,b,y]);return u||!a||a.length===0||p?null:s.jsx("div",{className:"react-list-go-to",children:o?o(P):s.jsx("select",{value:d,onChange:N,children:b.map(e=>s.jsxs("option",{value:e,children:["Page ",e]},`page-${e}`))})})}),$=n.memo(({children:o})=>{const{listState:l}=w(),{loader:a}=l,{initialLoading:t}=a;return n.useMemo(()=>({loading:t}),[t]),t?s.jsx("div",{className:"react-list-initial-loader",children:o||s.jsx("p",{children:"Initial Loading..."})}):null}),D=n.memo(({children:o,renderItem:l})=>{const{listState:a}=w(),{data:t=[],loader:r,error:i,setSort:f,sort:p}=a,{initialLoading:d}=r,m=n.useMemo(()=>({items:t,setSort:f,sort:p}),[t,p,f]);return d||!t||t.length===0||i?null:l?s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("div",{children:l({item:u,index:v})},u.id||v))}):typeof o=="function"?s.jsx("div",{className:"react-list-items",children:o(m)}):s.jsx("div",{className:"react-list-items",children:t.map((u,v)=>s.jsx("pre",{children:JSON.stringify(u,null,2)},u.id||v))})}),H=({initialItems:o=[],children:l,endpoint:a,page:t=1,perPage:r=25,sortBy:i="",sortOrder:f="desc",search:p="",filters:d={},attrs:m,version:u=1,paginationMode:v="pagination",meta:b={}})=>{const{requestHandler:y,setListState:N}=w(),P=v==="loadMore",[e,C]=n.useState({page:t,perPage:r,sortBy:i,sortOrder:f,search:p,filters:d,attrSettings:{},items:o,selection:[],error:null,response:null,count:0,isLoading:!1,initializingState:!0}),k=n.useCallback(async(g={},h=null)=>{e.initializingState||C(c=>({...c,error:null,isLoading:!0}));try{const c=h||e,L=await y({endpoint:a,version:u,meta:b,page:c.page,perPage:c.perPage,search:c.search,sortBy:c.sortBy,sortOrder:c.sortOrder,filters:c.filters,...g});C(E=>({...E,response:L,selection:[],items:P&&c.page>1?[...E.items,...L.items]:L.items,count:L.count,initializingState:!1,isLoading:!1}))}catch(c){throw C(L=>({...L,error:c,items:[],count:0,initializingState:!1,isLoading:!1})),c}},[a,u,P,b,y,e]),M=n.useMemo(()=>({setPage:(g,h)=>{let c=g;g===0&&(c="");const L={...e,page:c};C(L),c&&k(h,L)},setPerPage:g=>{const h={...e,perPage:g,page:1};C(h),k({},h)},setSearch:g=>{if(g!==e.search){const h={...e,search:g,page:1};C(h),k({},h)}},setSort:({by:g,order:h})=>{const c={...e,sortBy:g,sortOrder:h,page:1};C(c),k({},c)},loadMore:()=>{const g={...e,page:e.page+1};C(g),k({},g)},refresh:(g={isRefresh:!0})=>{if(P){const h={...e,page:1,items:[]};C(h),k(g,h)}else k(g)},setFilters:g=>{const h={...e,filters:g,page:1};C(h),k({},h)},updateItemById:(g,h)=>{const c=e.items.map(L=>L.id===h?{...L,...g}:L);C(L=>({...L,items:c}))},setSelection:g=>C(h=>({...h,selection:g}))}),[k,P,e]),O=n.useMemo(()=>({data:e.items,response:e.response,error:e.error,count:e.count,selection:e.selection,pagination:{page:e.page,perPage:e.perPage,hasMore:e.items.length<e.count},loader:{isLoading:e.isLoading,initialLoading:e.initializingState},sort:{sortBy:e.sortBy,sortOrder:e.sortOrder},search:e.search,filters:e.filters,attrs:m||Object.keys(e.items[0]||{}),isEmpty:e.items.length===0,...M}),[e.items,e.response,e.error,e.count,e.selection,e.page,e.perPage,e.isLoading,e.initializingState,e.sortBy,e.sortOrder,e.search,e.filters,M,m]);return n.useEffect(()=>{e.initializingState&&M.setPage(e.page)},[]),n.useEffect(()=>{N(O)},[N,e.items,e.count,e.error,e.isLoading,e.selection,e.page,e.perPage,e.sortBy,e.sortOrder]),typeof l=="function"?l(O):l},x=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,setPage:i,loader:f,error:p}=l,{page:d,perPage:m}=r,{isLoading:u}=f,v=n.useMemo(()=>d*m<t,[d,m,t]),b=n.useCallback(()=>{v&&!u&&i(d+1)},[v,u,i,d]),y=n.useMemo(()=>({isLoading:u,loadMore:b,hasMoreItems:v}),[u,b,v]);return!a||a.length===0||p?null:o(y)}),G=n.memo(({children:o,position:l="overlay"})=>{const{listState:a}=w(),{loader:t}=a,{isLoading:r,initializingState:i}=t;return n.useMemo(()=>({isLoading:r}),[r]),!i&&!r?null:s.jsx("div",{children:o||s.jsx("div",{children:s.jsx("p",{children:"Loading..."})})})}),W=n.memo(({children:o,pageLinks:l=5,renderFirst:a,renderPrev:t,renderPages:r,renderPage:i,renderNext:f,renderLast:p})=>{const{listState:d}=w(),{data:m,count:u,pagination:v,setPage:b,loader:y,error:N}=d,{page:P,perPage:e}=v,{initialLoading:C,isLoading:k}=y,M=n.useMemo(()=>{const c=Math.ceil(u/e),L=Math.floor(l/2),E=P*e<u;return{pagesCount:c,halfWay:L,hasNext:E,hasPrev:P!==1}},[u,e,P,l]),O=n.useMemo(()=>{const{pagesCount:c,halfWay:L}=M,E=Array.from({length:Math.min(l,c)});return P<=L?E.map((T,z)=>z+1):c-P<L?E.map((T,z)=>c-z).reverse():E.map((T,z)=>P-L+z)},[P,l,M]),g=n.useMemo(()=>({prev:()=>b(P-1),next:()=>b(P+1),first:()=>b(1),last:()=>b(M.pagesCount),setPage:c=>b(c)}),[b,P,M.pagesCount]),h=n.useMemo(()=>({page:P,perPage:e,count:u,...M,pagesToDisplay:O,...g}),[P,e,u,M,O,g]);return C||!m||m.length===0||N?null:o?o(h):s.jsxs("div",{className:"react-list-pagination",children:[a?a(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.first,children:"First"}),t?t(h):s.jsx("button",{type:"button",disabled:!M.hasPrev,onClick:g.prev,children:"Prev"}),r?r(h):s.jsx("div",{children:O.map(c=>{const L=c===P,E={...h,page:c,isActive:L};return i?i(E):s.jsx("div",{children:L?s.jsx("span",{children:c}):s.jsx("button",{type:"button",onClick:()=>g.setPage(c),children:c})},`page-${c}`)})}),f?f(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.next,children:"Next"}),p?p(h):s.jsx("button",{type:"button",disabled:!M.hasNext,onClick:g.last,children:"Last"})]})}),F=n.memo(({children:o,options:l=[10,25,50,100]})=>{const{listState:a}=w(),{data:t,pagination:r,setPerPage:i,loader:f,error:p}=a,{perPage:d}=r,{initialLoading:m}=f,u=n.useMemo(()=>l.map(y=>typeof y!="object"?{value:y,label:y}:y),[l]),v=n.useCallback(y=>{i(Number(y.target.value))},[i]),b=n.useMemo(()=>({perPage:d,setPerPage:i,options:u}),[d,i,u]);return m||!t||t.length===0||p?null:s.jsx("div",{className:"react-list-per-page",children:o?o(b):s.jsx("select",{value:d,onChange:v,children:u.map(y=>s.jsxs("option",{value:y.value,children:[y.label," items per page"]},`option-${y.value}`))})})}),J=n.memo(({children:o})=>{const{listState:l}=w(),{loader:a,refresh:t}=l,{isLoading:r,initialLoading:i}=a,f=n.useCallback(()=>{t({isRefresh:!0})},[t]),p=n.useMemo(()=>({isLoading:r,refresh:f}),[r,f]);return i?null:o?o(p):s.jsx("div",{className:"react-list-refresh",children:s.jsx("button",{onClick:f,disabled:r,children:r?"Loading...":"Refresh"})})}),K=n.memo(({children:o,debounceTime:l=500})=>{const{listState:a}=w(),{search:t,setSearch:r}=a,[i,f]=n.useState(t??""),p=n.useRef(null);n.useEffect(()=>{t!==i&&f(t??"")},[t]);const d=u=>{f(u),p.current&&clearTimeout(p.current),p.current=setTimeout(()=>{r(u)},l)};n.useEffect(()=>()=>{p.current&&clearTimeout(p.current)},[]);const m={search:i,setSearch:d};return s.jsx("div",{className:"react-list-search",children:o?o(m):s.jsx("input",{type:"text",value:i,onChange:u=>d(u.target.value),placeholder:"Search..."})})}),Q=n.memo(({children:o})=>{const{listState:l}=w(),{data:a,count:t,pagination:r,loader:i,error:f}=l,{page:p,perPage:d}=r,{initialLoading:m,isLoading:u}=i,v=n.useMemo(()=>{const y=p*d-d+1,N=Math.min(p*d,t),P=(a==null?void 0:a.length)||0;return{from:y,to:N,visibleCount:P}},[p,d,t,a]),b=n.useMemo(()=>({...v,count:t}),[v,t]);return m||!a||a.length===0||f?null:s.jsx("div",{className:"react-list-summary",children:o?o(b):s.jsxs("span",{children:["Showing ",s.jsx("span",{children:v.visibleCount})," items (",s.jsxs("span",{children:[v.from," - ",v.to]}),") out of ",s.jsx("span",{children:t})]})})});S.ReactListAttributes=j,S.ReactListEmpty=q,S.ReactListError=A,S.ReactListGoTo=_,S.ReactListInitialLoader=$,S.ReactListItems=D,S.ReactListLoadMore=x,S.ReactListLoader=G,S.ReactListPagination=W,S.ReactListPerPage=F,S.ReactListProvider=I,S.ReactListRefresh=J,S.ReactListSearch=K,S.ReactListSummary=Q,S.default=H,Object.defineProperties(S,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@7span/react-list",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A simple and reusable list component for React",
5
5
  "type": "module",
6
6
  "scripts": {