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