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