@7span/react-list 0.0.3 → 0.0.5

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