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