@hairy/react-lib 1.36.0 → 1.38.0
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/index.cjs +73 -16
- package/dist/index.d.ts +26 -3
- package/dist/index.global.js +72 -16
- package/dist/index.js +69 -13
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -59,6 +59,7 @@ __export(index_exports, {
|
|
|
59
59
|
useFetchRequestIntercept: () => useFetchRequestIntercept,
|
|
60
60
|
useFetchResponseIntercept: () => useFetchResponseIntercept,
|
|
61
61
|
useMounted: () => useMounted,
|
|
62
|
+
useOffsetPagination: () => useOffsetPagination,
|
|
62
63
|
useStatus: () => useStatus,
|
|
63
64
|
useStore: () => useStore,
|
|
64
65
|
useUpdate: () => useUpdate,
|
|
@@ -423,7 +424,7 @@ function tryUseUpdate() {
|
|
|
423
424
|
var import_react13 = require("react");
|
|
424
425
|
function useAsyncCallback(fun) {
|
|
425
426
|
const [state, set] = (0, import_react13.useState)({ loading: false });
|
|
426
|
-
async function
|
|
427
|
+
async function refetch(...args) {
|
|
427
428
|
set({ loading: true });
|
|
428
429
|
return fun(...args).then((value) => {
|
|
429
430
|
set({ loading: false });
|
|
@@ -433,21 +434,21 @@ function useAsyncCallback(fun) {
|
|
|
433
434
|
return Promise.reject(err);
|
|
434
435
|
});
|
|
435
436
|
}
|
|
436
|
-
return [state.loading,
|
|
437
|
+
return [state.loading, refetch, state.error];
|
|
437
438
|
}
|
|
438
439
|
|
|
439
440
|
// src/hooks/useAsyncState.ts
|
|
440
441
|
var import_react14 = require("react");
|
|
441
442
|
function useAsyncState(fun, deps = [], options) {
|
|
442
443
|
const [value, set] = (0, import_react14.useState)(options?.initial);
|
|
443
|
-
const [loading,
|
|
444
|
+
const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
|
|
444
445
|
(0, import_react14.useEffect)(
|
|
445
446
|
() => {
|
|
446
|
-
|
|
447
|
+
refetch();
|
|
447
448
|
},
|
|
448
449
|
deps
|
|
449
450
|
);
|
|
450
|
-
return [{ value, loading, error },
|
|
451
|
+
return [{ value, loading, error }, refetch];
|
|
451
452
|
}
|
|
452
453
|
|
|
453
454
|
// src/hooks/useDebounce.ts
|
|
@@ -538,20 +539,36 @@ function useMounted() {
|
|
|
538
539
|
return mounted;
|
|
539
540
|
}
|
|
540
541
|
|
|
542
|
+
// src/hooks/useOffsetPagination.ts
|
|
543
|
+
var import_react21 = require("react");
|
|
544
|
+
|
|
541
545
|
// src/hooks/useWatch.ts
|
|
546
|
+
var import_react20 = require("react");
|
|
547
|
+
|
|
548
|
+
// src/hooks/usePrevious.ts
|
|
542
549
|
var import_react19 = require("react");
|
|
550
|
+
function usePrevious(value) {
|
|
551
|
+
const ref2 = (0, import_react19.useRef)(void 0);
|
|
552
|
+
(0, import_react19.useEffect)(() => {
|
|
553
|
+
ref2.current = value;
|
|
554
|
+
});
|
|
555
|
+
return ref2.current;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// src/hooks/useWatch.ts
|
|
543
559
|
function useWatch(source, callback, options = {}) {
|
|
544
|
-
const firstUpdate = (0,
|
|
545
|
-
const then = (0,
|
|
546
|
-
const
|
|
560
|
+
const firstUpdate = (0, import_react20.useRef)(false);
|
|
561
|
+
const then = (0, import_react20.useRef)(void 0);
|
|
562
|
+
const oldValue = usePrevious(source);
|
|
563
|
+
const deps = (0, import_react20.useMemo)(
|
|
547
564
|
() => Array.isArray(source) ? source : [source],
|
|
548
565
|
[source]
|
|
549
566
|
);
|
|
550
|
-
(0,
|
|
567
|
+
(0, import_react20.useEffect)(() => {
|
|
551
568
|
if (!firstUpdate.current)
|
|
552
569
|
recordFirst();
|
|
553
570
|
else
|
|
554
|
-
callback(source);
|
|
571
|
+
callback(source, oldValue);
|
|
555
572
|
}, deps);
|
|
556
573
|
async function recordFirst() {
|
|
557
574
|
if (then.current)
|
|
@@ -559,17 +576,56 @@ function useWatch(source, callback, options = {}) {
|
|
|
559
576
|
then.current = Promise.resolve(source);
|
|
560
577
|
then.current.then(() => firstUpdate.current = true);
|
|
561
578
|
if (options.immediate)
|
|
562
|
-
then.current.then((value) => callback(value));
|
|
579
|
+
then.current.then((value) => callback(value, oldValue));
|
|
563
580
|
}
|
|
564
581
|
}
|
|
565
582
|
|
|
583
|
+
// src/hooks/useOffsetPagination.ts
|
|
584
|
+
function useOffsetPagination(options) {
|
|
585
|
+
const [page, setPage] = (0, import_react21.useState)(options.page || 1);
|
|
586
|
+
const [pageSize, setPageSize] = (0, import_react21.useState)(options.pageSize || 10);
|
|
587
|
+
const total = options.total || 0;
|
|
588
|
+
const pageCount = (0, import_react21.useMemo)(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
|
|
589
|
+
const isFirstPage = (0, import_react21.useMemo)(() => page === 1, [page]);
|
|
590
|
+
const isLastPage = (0, import_react21.useMemo)(() => page === pageCount, [page, pageCount]);
|
|
591
|
+
function next() {
|
|
592
|
+
setPage((page2) => Math.min(pageCount, page2 + 1));
|
|
593
|
+
}
|
|
594
|
+
function prev() {
|
|
595
|
+
setPage((page2) => Math.max(1, page2 - 1));
|
|
596
|
+
}
|
|
597
|
+
function pageChange(page2) {
|
|
598
|
+
setPage(() => Math.max(1, Math.min(page2, pageCount)));
|
|
599
|
+
}
|
|
600
|
+
function pageSizeChange(limit) {
|
|
601
|
+
setPageSize(limit);
|
|
602
|
+
pageChange(1);
|
|
603
|
+
}
|
|
604
|
+
const pagination = {
|
|
605
|
+
next,
|
|
606
|
+
prev,
|
|
607
|
+
page,
|
|
608
|
+
pageSize,
|
|
609
|
+
isFirstPage,
|
|
610
|
+
pageSizeChange,
|
|
611
|
+
pageChange,
|
|
612
|
+
isLastPage,
|
|
613
|
+
pageCount,
|
|
614
|
+
total
|
|
615
|
+
};
|
|
616
|
+
useWatch(page, () => options.onChange?.(pagination));
|
|
617
|
+
useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
|
|
618
|
+
useWatch(pageCount, () => options.onPageCountChange?.(pagination));
|
|
619
|
+
return pagination;
|
|
620
|
+
}
|
|
621
|
+
|
|
566
622
|
// src/hooks/useWhenever.ts
|
|
567
623
|
function useWhenever(source, cb, options) {
|
|
568
|
-
useWatch(source, () =>
|
|
624
|
+
useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
|
|
569
625
|
}
|
|
570
626
|
|
|
571
627
|
// src/storage/defineStore.ts
|
|
572
|
-
var
|
|
628
|
+
var import_react22 = require("react");
|
|
573
629
|
var import_valtio4 = require("valtio");
|
|
574
630
|
|
|
575
631
|
// src/storage/persistant.ts
|
|
@@ -649,10 +705,10 @@ function defineStore(store, options = {}) {
|
|
|
649
705
|
Object.assign($state, patch);
|
|
650
706
|
}
|
|
651
707
|
function $signal(fn) {
|
|
652
|
-
return (0,
|
|
708
|
+
return (0, import_react22.createElement)(() => fn((0, import_valtio4.useSnapshot)($state)));
|
|
653
709
|
}
|
|
654
710
|
$signal.status = function(fn) {
|
|
655
|
-
return (0,
|
|
711
|
+
return (0, import_react22.createElement)(() => fn((0, import_valtio4.useSnapshot)($status)));
|
|
656
712
|
};
|
|
657
713
|
return {
|
|
658
714
|
$subscribe,
|
|
@@ -736,7 +792,7 @@ function defineStoreAsync(fetch, options = {}) {
|
|
|
736
792
|
store.$state.loading = status.loading;
|
|
737
793
|
store.$state.finished = status.finished;
|
|
738
794
|
});
|
|
739
|
-
options.immediate && store.
|
|
795
|
+
options.immediate && store.refetch();
|
|
740
796
|
return store;
|
|
741
797
|
}
|
|
742
798
|
var defienAsyncStore = defineStoreAsync;
|
|
@@ -783,6 +839,7 @@ function useStore(store) {
|
|
|
783
839
|
useFetchRequestIntercept,
|
|
784
840
|
useFetchResponseIntercept,
|
|
785
841
|
useMounted,
|
|
842
|
+
useOffsetPagination,
|
|
786
843
|
useStatus,
|
|
787
844
|
useStore,
|
|
788
845
|
useUpdate,
|
package/dist/index.d.ts
CHANGED
|
@@ -191,14 +191,37 @@ declare function useFetchRequestIntercept(intercept: FetchRequestInterceptCallba
|
|
|
191
191
|
|
|
192
192
|
declare function useMounted(): boolean;
|
|
193
193
|
|
|
194
|
+
interface UseOffsetPaginationOptions {
|
|
195
|
+
total?: number;
|
|
196
|
+
page?: number;
|
|
197
|
+
pageSize?: number;
|
|
198
|
+
onChange?: (pagination: Pagination) => void;
|
|
199
|
+
onPageSizeChange?: (pagination: Pagination) => void;
|
|
200
|
+
onPageCountChange?: (pagination: Pagination) => void;
|
|
201
|
+
}
|
|
202
|
+
interface Pagination {
|
|
203
|
+
pageSizeChange: (limit: number) => void;
|
|
204
|
+
pageChange: (page: number) => void;
|
|
205
|
+
next: () => void;
|
|
206
|
+
prev: () => void;
|
|
207
|
+
page: number;
|
|
208
|
+
pageSize: number;
|
|
209
|
+
isFirstPage: boolean;
|
|
210
|
+
isLastPage: boolean;
|
|
211
|
+
pageCount: number;
|
|
212
|
+
total: number;
|
|
213
|
+
}
|
|
214
|
+
declare function useOffsetPagination(options: UseOffsetPaginationOptions): Pagination;
|
|
215
|
+
|
|
194
216
|
declare function useUpdate(): () => void;
|
|
195
217
|
|
|
196
|
-
interface UseWatchCallback<T> {
|
|
197
|
-
(value: T): void;
|
|
218
|
+
interface UseWatchCallback<T = any> {
|
|
219
|
+
(value: T, oldValue: T): void;
|
|
198
220
|
}
|
|
199
221
|
interface UseWatchOptions {
|
|
200
222
|
immediate?: boolean;
|
|
201
223
|
}
|
|
224
|
+
declare function useWatch<T extends any[]>(source: readonly [...T], callback: UseWatchCallback<[...T]>, options?: UseWatchOptions): void;
|
|
202
225
|
declare function useWatch<T>(source: T, callback: UseWatchCallback<T>, options?: UseWatchOptions): void;
|
|
203
226
|
|
|
204
227
|
declare function useWhenever<T>(source: T, cb: UseWatchCallback<Exclude<T, null | undefined>>, options?: UseWatchOptions): void;
|
|
@@ -332,4 +355,4 @@ declare function useStore<S extends object, A extends Actions<S>, G extends Gett
|
|
|
332
355
|
|
|
333
356
|
type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
|
|
334
357
|
|
|
335
|
-
export { type Argument, type ArgumentArray, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type Exposer, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StoreAsync, type StoreAsyncInitial, type StoreAsyncInitialOptions, type StoreAsyncOptions, Switch, type SwitchProps, Then, type ThenProps, Trigger, Unless, type UnlessProps, type UseAsyncStateOptions, type UseWatchCallback, type UseWatchOptions, type Value, type WrapperProps, type WrapperTag, cls, defienAsyncStore, defineStore, defineStoreAsync, proxyWithPersistant, track, tryUseCallback, tryUseEffect, tryUseInsertionEffect, tryUseReducer, tryUseRef, tryUseState, tryUseUpdate, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStatus, useStore, useUpdate, useWatch, useWhenever, wrapper };
|
|
358
|
+
export { type Argument, type ArgumentArray, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type Exposer, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type Pagination, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StoreAsync, type StoreAsyncInitial, type StoreAsyncInitialOptions, type StoreAsyncOptions, Switch, type SwitchProps, Then, type ThenProps, Trigger, Unless, type UnlessProps, type UseAsyncStateOptions, type UseOffsetPaginationOptions, type UseWatchCallback, type UseWatchOptions, type Value, type WrapperProps, type WrapperTag, cls, defienAsyncStore, defineStore, defineStoreAsync, proxyWithPersistant, track, tryUseCallback, tryUseEffect, tryUseInsertionEffect, tryUseReducer, tryUseRef, tryUseState, tryUseUpdate, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useOffsetPagination, useStatus, useStore, useUpdate, useWatch, useWhenever, wrapper };
|
package/dist/index.global.js
CHANGED
|
@@ -71,6 +71,7 @@ var LibReact = (() => {
|
|
|
71
71
|
useFetchRequestIntercept: () => useFetchRequestIntercept,
|
|
72
72
|
useFetchResponseIntercept: () => useFetchResponseIntercept,
|
|
73
73
|
useMounted: () => useMounted,
|
|
74
|
+
useOffsetPagination: () => useOffsetPagination,
|
|
74
75
|
useStatus: () => useStatus,
|
|
75
76
|
useStore: () => useStore,
|
|
76
77
|
useUpdate: () => useUpdate,
|
|
@@ -1136,7 +1137,7 @@ var LibReact = (() => {
|
|
|
1136
1137
|
var import_react14 = __toESM(require_react(), 1);
|
|
1137
1138
|
function useAsyncCallback(fun) {
|
|
1138
1139
|
const [state, set] = (0, import_react14.useState)({ loading: false });
|
|
1139
|
-
async function
|
|
1140
|
+
async function refetch(...args) {
|
|
1140
1141
|
set({ loading: true });
|
|
1141
1142
|
return fun(...args).then((value) => {
|
|
1142
1143
|
set({ loading: false });
|
|
@@ -1146,21 +1147,21 @@ var LibReact = (() => {
|
|
|
1146
1147
|
return Promise.reject(err);
|
|
1147
1148
|
});
|
|
1148
1149
|
}
|
|
1149
|
-
return [state.loading,
|
|
1150
|
+
return [state.loading, refetch, state.error];
|
|
1150
1151
|
}
|
|
1151
1152
|
|
|
1152
1153
|
// src/hooks/useAsyncState.ts
|
|
1153
1154
|
var import_react15 = __toESM(require_react(), 1);
|
|
1154
1155
|
function useAsyncState(fun, deps = [], options) {
|
|
1155
1156
|
const [value, set] = (0, import_react15.useState)(options?.initial);
|
|
1156
|
-
const [loading,
|
|
1157
|
+
const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
|
|
1157
1158
|
(0, import_react15.useEffect)(
|
|
1158
1159
|
() => {
|
|
1159
|
-
|
|
1160
|
+
refetch();
|
|
1160
1161
|
},
|
|
1161
1162
|
deps
|
|
1162
1163
|
);
|
|
1163
|
-
return [{ value, loading, error },
|
|
1164
|
+
return [{ value, loading, error }, refetch];
|
|
1164
1165
|
}
|
|
1165
1166
|
|
|
1166
1167
|
// src/hooks/useDebounce.ts
|
|
@@ -1268,20 +1269,36 @@ var LibReact = (() => {
|
|
|
1268
1269
|
return mounted;
|
|
1269
1270
|
}
|
|
1270
1271
|
|
|
1272
|
+
// src/hooks/useOffsetPagination.ts
|
|
1273
|
+
var import_react22 = __toESM(require_react(), 1);
|
|
1274
|
+
|
|
1271
1275
|
// src/hooks/useWatch.ts
|
|
1276
|
+
var import_react21 = __toESM(require_react(), 1);
|
|
1277
|
+
|
|
1278
|
+
// src/hooks/usePrevious.ts
|
|
1272
1279
|
var import_react20 = __toESM(require_react(), 1);
|
|
1280
|
+
function usePrevious(value) {
|
|
1281
|
+
const ref2 = (0, import_react20.useRef)(void 0);
|
|
1282
|
+
(0, import_react20.useEffect)(() => {
|
|
1283
|
+
ref2.current = value;
|
|
1284
|
+
});
|
|
1285
|
+
return ref2.current;
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
// src/hooks/useWatch.ts
|
|
1273
1289
|
function useWatch(source, callback, options = {}) {
|
|
1274
|
-
const firstUpdate = (0,
|
|
1275
|
-
const then = (0,
|
|
1276
|
-
const
|
|
1290
|
+
const firstUpdate = (0, import_react21.useRef)(false);
|
|
1291
|
+
const then = (0, import_react21.useRef)(void 0);
|
|
1292
|
+
const oldValue = usePrevious(source);
|
|
1293
|
+
const deps = (0, import_react21.useMemo)(
|
|
1277
1294
|
() => Array.isArray(source) ? source : [source],
|
|
1278
1295
|
[source]
|
|
1279
1296
|
);
|
|
1280
|
-
(0,
|
|
1297
|
+
(0, import_react21.useEffect)(() => {
|
|
1281
1298
|
if (!firstUpdate.current)
|
|
1282
1299
|
recordFirst();
|
|
1283
1300
|
else
|
|
1284
|
-
callback(source);
|
|
1301
|
+
callback(source, oldValue);
|
|
1285
1302
|
}, deps);
|
|
1286
1303
|
async function recordFirst() {
|
|
1287
1304
|
if (then.current)
|
|
@@ -1289,17 +1306,56 @@ var LibReact = (() => {
|
|
|
1289
1306
|
then.current = Promise.resolve(source);
|
|
1290
1307
|
then.current.then(() => firstUpdate.current = true);
|
|
1291
1308
|
if (options.immediate)
|
|
1292
|
-
then.current.then((value) => callback(value));
|
|
1309
|
+
then.current.then((value) => callback(value, oldValue));
|
|
1293
1310
|
}
|
|
1294
1311
|
}
|
|
1295
1312
|
|
|
1313
|
+
// src/hooks/useOffsetPagination.ts
|
|
1314
|
+
function useOffsetPagination(options) {
|
|
1315
|
+
const [page, setPage] = (0, import_react22.useState)(options.page || 1);
|
|
1316
|
+
const [pageSize, setPageSize] = (0, import_react22.useState)(options.pageSize || 10);
|
|
1317
|
+
const total = options.total || 0;
|
|
1318
|
+
const pageCount = (0, import_react22.useMemo)(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
|
|
1319
|
+
const isFirstPage = (0, import_react22.useMemo)(() => page === 1, [page]);
|
|
1320
|
+
const isLastPage = (0, import_react22.useMemo)(() => page === pageCount, [page, pageCount]);
|
|
1321
|
+
function next() {
|
|
1322
|
+
setPage((page2) => Math.min(pageCount, page2 + 1));
|
|
1323
|
+
}
|
|
1324
|
+
function prev() {
|
|
1325
|
+
setPage((page2) => Math.max(1, page2 - 1));
|
|
1326
|
+
}
|
|
1327
|
+
function pageChange(page2) {
|
|
1328
|
+
setPage(() => Math.max(1, Math.min(page2, pageCount)));
|
|
1329
|
+
}
|
|
1330
|
+
function pageSizeChange(limit) {
|
|
1331
|
+
setPageSize(limit);
|
|
1332
|
+
pageChange(1);
|
|
1333
|
+
}
|
|
1334
|
+
const pagination = {
|
|
1335
|
+
next,
|
|
1336
|
+
prev,
|
|
1337
|
+
page,
|
|
1338
|
+
pageSize,
|
|
1339
|
+
isFirstPage,
|
|
1340
|
+
pageSizeChange,
|
|
1341
|
+
pageChange,
|
|
1342
|
+
isLastPage,
|
|
1343
|
+
pageCount,
|
|
1344
|
+
total
|
|
1345
|
+
};
|
|
1346
|
+
useWatch(page, () => options.onChange?.(pagination));
|
|
1347
|
+
useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
|
|
1348
|
+
useWatch(pageCount, () => options.onPageCountChange?.(pagination));
|
|
1349
|
+
return pagination;
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1296
1352
|
// src/hooks/useWhenever.ts
|
|
1297
1353
|
function useWhenever(source, cb, options) {
|
|
1298
|
-
useWatch(source, () =>
|
|
1354
|
+
useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
|
|
1299
1355
|
}
|
|
1300
1356
|
|
|
1301
1357
|
// src/storage/defineStore.ts
|
|
1302
|
-
var
|
|
1358
|
+
var import_react23 = __toESM(require_react(), 1);
|
|
1303
1359
|
|
|
1304
1360
|
// src/storage/persistant.ts
|
|
1305
1361
|
function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
@@ -1377,10 +1433,10 @@ var LibReact = (() => {
|
|
|
1377
1433
|
Object.assign($state, patch);
|
|
1378
1434
|
}
|
|
1379
1435
|
function $signal(fn) {
|
|
1380
|
-
return (0,
|
|
1436
|
+
return (0, import_react23.createElement)(() => fn(useSnapshot($state)));
|
|
1381
1437
|
}
|
|
1382
1438
|
$signal.status = function(fn) {
|
|
1383
|
-
return (0,
|
|
1439
|
+
return (0, import_react23.createElement)(() => fn(useSnapshot($status)));
|
|
1384
1440
|
};
|
|
1385
1441
|
return {
|
|
1386
1442
|
$subscribe,
|
|
@@ -1463,7 +1519,7 @@ var LibReact = (() => {
|
|
|
1463
1519
|
store.$state.loading = status.loading;
|
|
1464
1520
|
store.$state.finished = status.finished;
|
|
1465
1521
|
});
|
|
1466
|
-
options.immediate && store.
|
|
1522
|
+
options.immediate && store.refetch();
|
|
1467
1523
|
return store;
|
|
1468
1524
|
}
|
|
1469
1525
|
var defienAsyncStore = defineStoreAsync;
|
package/dist/index.js
CHANGED
|
@@ -353,7 +353,7 @@ function tryUseUpdate() {
|
|
|
353
353
|
import { useState as useState2 } from "react";
|
|
354
354
|
function useAsyncCallback(fun) {
|
|
355
355
|
const [state, set] = useState2({ loading: false });
|
|
356
|
-
async function
|
|
356
|
+
async function refetch(...args) {
|
|
357
357
|
set({ loading: true });
|
|
358
358
|
return fun(...args).then((value) => {
|
|
359
359
|
set({ loading: false });
|
|
@@ -363,21 +363,21 @@ function useAsyncCallback(fun) {
|
|
|
363
363
|
return Promise.reject(err);
|
|
364
364
|
});
|
|
365
365
|
}
|
|
366
|
-
return [state.loading,
|
|
366
|
+
return [state.loading, refetch, state.error];
|
|
367
367
|
}
|
|
368
368
|
|
|
369
369
|
// src/hooks/useAsyncState.ts
|
|
370
370
|
import { useEffect as useEffect2, useState as useState3 } from "react";
|
|
371
371
|
function useAsyncState(fun, deps = [], options) {
|
|
372
372
|
const [value, set] = useState3(options?.initial);
|
|
373
|
-
const [loading,
|
|
373
|
+
const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
|
|
374
374
|
useEffect2(
|
|
375
375
|
() => {
|
|
376
|
-
|
|
376
|
+
refetch();
|
|
377
377
|
},
|
|
378
378
|
deps
|
|
379
379
|
);
|
|
380
|
-
return [{ value, loading, error },
|
|
380
|
+
return [{ value, loading, error }, refetch];
|
|
381
381
|
}
|
|
382
382
|
|
|
383
383
|
// src/hooks/useDebounce.ts
|
|
@@ -468,20 +468,36 @@ function useMounted() {
|
|
|
468
468
|
return mounted;
|
|
469
469
|
}
|
|
470
470
|
|
|
471
|
+
// src/hooks/useOffsetPagination.ts
|
|
472
|
+
import { useMemo as useMemo3, useState as useState6 } from "react";
|
|
473
|
+
|
|
474
|
+
// src/hooks/useWatch.ts
|
|
475
|
+
import { useEffect as useEffect8, useMemo as useMemo2, useRef as useRef4 } from "react";
|
|
476
|
+
|
|
477
|
+
// src/hooks/usePrevious.ts
|
|
478
|
+
import { useEffect as useEffect7, useRef as useRef3 } from "react";
|
|
479
|
+
function usePrevious(value) {
|
|
480
|
+
const ref2 = useRef3(void 0);
|
|
481
|
+
useEffect7(() => {
|
|
482
|
+
ref2.current = value;
|
|
483
|
+
});
|
|
484
|
+
return ref2.current;
|
|
485
|
+
}
|
|
486
|
+
|
|
471
487
|
// src/hooks/useWatch.ts
|
|
472
|
-
import { useEffect as useEffect7, useMemo as useMemo2, useRef as useRef3 } from "react";
|
|
473
488
|
function useWatch(source, callback, options = {}) {
|
|
474
|
-
const firstUpdate =
|
|
475
|
-
const then =
|
|
489
|
+
const firstUpdate = useRef4(false);
|
|
490
|
+
const then = useRef4(void 0);
|
|
491
|
+
const oldValue = usePrevious(source);
|
|
476
492
|
const deps = useMemo2(
|
|
477
493
|
() => Array.isArray(source) ? source : [source],
|
|
478
494
|
[source]
|
|
479
495
|
);
|
|
480
|
-
|
|
496
|
+
useEffect8(() => {
|
|
481
497
|
if (!firstUpdate.current)
|
|
482
498
|
recordFirst();
|
|
483
499
|
else
|
|
484
|
-
callback(source);
|
|
500
|
+
callback(source, oldValue);
|
|
485
501
|
}, deps);
|
|
486
502
|
async function recordFirst() {
|
|
487
503
|
if (then.current)
|
|
@@ -489,13 +505,52 @@ function useWatch(source, callback, options = {}) {
|
|
|
489
505
|
then.current = Promise.resolve(source);
|
|
490
506
|
then.current.then(() => firstUpdate.current = true);
|
|
491
507
|
if (options.immediate)
|
|
492
|
-
then.current.then((value) => callback(value));
|
|
508
|
+
then.current.then((value) => callback(value, oldValue));
|
|
493
509
|
}
|
|
494
510
|
}
|
|
495
511
|
|
|
512
|
+
// src/hooks/useOffsetPagination.ts
|
|
513
|
+
function useOffsetPagination(options) {
|
|
514
|
+
const [page, setPage] = useState6(options.page || 1);
|
|
515
|
+
const [pageSize, setPageSize] = useState6(options.pageSize || 10);
|
|
516
|
+
const total = options.total || 0;
|
|
517
|
+
const pageCount = useMemo3(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
|
|
518
|
+
const isFirstPage = useMemo3(() => page === 1, [page]);
|
|
519
|
+
const isLastPage = useMemo3(() => page === pageCount, [page, pageCount]);
|
|
520
|
+
function next() {
|
|
521
|
+
setPage((page2) => Math.min(pageCount, page2 + 1));
|
|
522
|
+
}
|
|
523
|
+
function prev() {
|
|
524
|
+
setPage((page2) => Math.max(1, page2 - 1));
|
|
525
|
+
}
|
|
526
|
+
function pageChange(page2) {
|
|
527
|
+
setPage(() => Math.max(1, Math.min(page2, pageCount)));
|
|
528
|
+
}
|
|
529
|
+
function pageSizeChange(limit) {
|
|
530
|
+
setPageSize(limit);
|
|
531
|
+
pageChange(1);
|
|
532
|
+
}
|
|
533
|
+
const pagination = {
|
|
534
|
+
next,
|
|
535
|
+
prev,
|
|
536
|
+
page,
|
|
537
|
+
pageSize,
|
|
538
|
+
isFirstPage,
|
|
539
|
+
pageSizeChange,
|
|
540
|
+
pageChange,
|
|
541
|
+
isLastPage,
|
|
542
|
+
pageCount,
|
|
543
|
+
total
|
|
544
|
+
};
|
|
545
|
+
useWatch(page, () => options.onChange?.(pagination));
|
|
546
|
+
useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
|
|
547
|
+
useWatch(pageCount, () => options.onPageCountChange?.(pagination));
|
|
548
|
+
return pagination;
|
|
549
|
+
}
|
|
550
|
+
|
|
496
551
|
// src/hooks/useWhenever.ts
|
|
497
552
|
function useWhenever(source, cb, options) {
|
|
498
|
-
useWatch(source, () =>
|
|
553
|
+
useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
|
|
499
554
|
}
|
|
500
555
|
|
|
501
556
|
// src/storage/defineStore.ts
|
|
@@ -666,7 +721,7 @@ function defineStoreAsync(fetch, options = {}) {
|
|
|
666
721
|
store.$state.loading = status.loading;
|
|
667
722
|
store.$state.finished = status.finished;
|
|
668
723
|
});
|
|
669
|
-
options.immediate && store.
|
|
724
|
+
options.immediate && store.refetch();
|
|
670
725
|
return store;
|
|
671
726
|
}
|
|
672
727
|
var defienAsyncStore = defineStoreAsync;
|
|
@@ -712,6 +767,7 @@ export {
|
|
|
712
767
|
useFetchRequestIntercept,
|
|
713
768
|
useFetchResponseIntercept,
|
|
714
769
|
useMounted,
|
|
770
|
+
useOffsetPagination,
|
|
715
771
|
useStatus,
|
|
716
772
|
useStore,
|
|
717
773
|
useUpdate,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/react-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.38.0",
|
|
5
5
|
"description": "Library for react",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"react-dom": "^19.1.0",
|
|
39
39
|
"react-i18next": "^14.1.2",
|
|
40
40
|
"react-use": "^17.6.0",
|
|
41
|
-
"@hairy/utils": "1.
|
|
41
|
+
"@hairy/utils": "1.38.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup",
|