@websolutespa/bom-mixer-hooks 1.8.1 → 1.8.2
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +20 -12
- package/dist/index.js +115 -43
- package/dist/index.mjs +112 -43
- package/package.json +1 -1
- package/src/useSearchParams/useSearchParams.ts +65 -30
- package/src/useSearchParamsEncoded/useSearchParamsEncoded.ts +42 -52
- package/src/useSearchParamsQs/useSearchParamsQs.ts +41 -16
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -461,21 +461,23 @@ declare function updateSearchParams(currentPath: string, params: IParams): {
|
|
|
461
461
|
[key: string]: string;
|
|
462
462
|
};
|
|
463
463
|
};
|
|
464
|
-
declare function replaceSearchParams(router: NextRouter, params: IParams): void;
|
|
465
464
|
declare function pushSearchParams(router: NextRouter, params: IParams): void;
|
|
465
|
+
declare function pushSearchParamsSilently(params: IParams): void;
|
|
466
|
+
declare function replaceSearchParams(router: NextRouter, params: IParams): void;
|
|
466
467
|
declare function replaceSearchParamsSilently(params: IParams): void;
|
|
467
|
-
declare function useSearchParams(): IUseSearchParamsResult;
|
|
468
468
|
type IParams = {
|
|
469
469
|
[key: string]: string;
|
|
470
470
|
};
|
|
471
|
-
type IUseSearchParamsValueResult = {
|
|
472
|
-
params: string | null;
|
|
473
|
-
replaceParamsSilently: (params: IParams) => void;
|
|
474
|
-
};
|
|
475
471
|
type IUseSearchParamsResult = {
|
|
476
472
|
params: IParams;
|
|
477
|
-
|
|
473
|
+
pushParams: (params: IParams) => void;
|
|
474
|
+
pushParamsSilently: (params: IParams) => void;
|
|
478
475
|
replaceParams: (params: IParams) => void;
|
|
476
|
+
replaceParamsSilently: (params: IParams) => void;
|
|
477
|
+
};
|
|
478
|
+
declare function useSearchParams(): IUseSearchParamsResult;
|
|
479
|
+
type IUseSearchParamsValueResult = Omit<IUseSearchParamsResult, 'params'> & {
|
|
480
|
+
params: string | null;
|
|
479
481
|
};
|
|
480
482
|
declare function useSearchParamsValue(key: string): IUseSearchParamsValueResult;
|
|
481
483
|
|
|
@@ -486,32 +488,38 @@ declare function updateSearchParamsEncoded(currentPath: string, value: IParamsEn
|
|
|
486
488
|
[key: string]: string;
|
|
487
489
|
};
|
|
488
490
|
};
|
|
489
|
-
declare function replaceSearchParamsEncoded(router: NextRouter, value: IParamsEncoded, key?: string): void;
|
|
490
491
|
declare function pushSearchParamsEncoded(router: NextRouter, value: IParamsEncoded, key?: string): void;
|
|
492
|
+
declare function pushSearchParamsEncodedSilently(value: IParamsEncoded, key?: string): void;
|
|
493
|
+
declare function replaceSearchParamsEncoded(router: NextRouter, value: IParamsEncoded, key?: string): void;
|
|
491
494
|
declare function replaceSearchParamsEncodedSilently(value: IParamsEncoded, key?: string): void;
|
|
492
495
|
type IParamsEncoded = {
|
|
493
496
|
[key: string]: any;
|
|
494
497
|
} | null;
|
|
495
498
|
type IUseSearchParamsEncodedResult = {
|
|
496
499
|
params: IParamsEncoded;
|
|
497
|
-
|
|
500
|
+
pushParams: (params: IParamsEncoded) => void;
|
|
501
|
+
pushParamsSilently: (params: IParamsEncoded) => void;
|
|
498
502
|
replaceParams: (params: IParamsEncoded) => void;
|
|
503
|
+
replaceParamsSilently: (params: IParamsEncoded) => void;
|
|
499
504
|
};
|
|
500
505
|
declare function useSearchParamsEncoded(key?: string): IUseSearchParamsEncodedResult;
|
|
501
506
|
|
|
502
507
|
type IParamsQs = Record<string, any>;
|
|
503
508
|
type IUseSearchParamsQsResult = {
|
|
504
509
|
params: IParamsQs;
|
|
505
|
-
|
|
510
|
+
pushParams: (params: IParamsQs) => void;
|
|
511
|
+
pushParamsSilently: (params: IParamsQs) => void;
|
|
506
512
|
replaceParams: (params: IParamsQs) => void;
|
|
513
|
+
replaceParamsSilently: (params: IParamsQs) => void;
|
|
507
514
|
};
|
|
508
515
|
declare function getSearchParamsQs(url: string, key?: string): IParamsQs;
|
|
509
516
|
declare function updateSearchParamsQs(url: string, value: IParamsQs, key?: string): {
|
|
510
517
|
pathname: string;
|
|
511
518
|
query: string;
|
|
512
519
|
};
|
|
513
|
-
declare function replaceSearchParamsQs(router: NextRouter, value: IParamsQs, key?: string): void;
|
|
514
520
|
declare function pushSearchParamsQs(router: NextRouter, value: IParamsQs, key?: string): void;
|
|
521
|
+
declare function pushSearchParamsQsSilently(value: IParamsQs, key?: string): void;
|
|
522
|
+
declare function replaceSearchParamsQs(router: NextRouter, value: IParamsQs, key?: string): void;
|
|
515
523
|
declare function replaceSearchParamsQsSilently(value: IParamsQs, key?: string): void;
|
|
516
524
|
declare function useSearchParamsQs(key?: string): IUseSearchParamsQsResult;
|
|
517
525
|
|
|
@@ -549,4 +557,4 @@ type WindowSize = {
|
|
|
549
557
|
};
|
|
550
558
|
declare function useWindowSize(): WindowSize;
|
|
551
559
|
|
|
552
|
-
export { BodyScrollOptions, ClickOutGetContainer, CurrentStateType, ElementStackItem, ExtraProvider, Filter, FilterMode, FilterParams, FilterValues, GeoLocationSensorState, IExtra, IFilter, IFilterOption, IGeolocationPositionError, ILabelContext, ILayoutContext, ILazyModulesContext, IPageContext, IPaginationInfo, IParams, IParamsEncoded, IParamsQs, IUseSearchParamsEncodedResult, IUseSearchParamsQsResult, IUseSearchParamsResult, IUseSearchParamsValueResult, KeyCode, KeyMod, KeyboardOptions, KeyboardResult, LabelProvider, LabelService, LayoutProvider, LazyModulesProvider, PageProvider, SSRState, SizeResult, SizeType, Sorting, UIReducer, UIValue, UseFiltersResultType, UseKeyboard, UseKeyboardHandler, createElementWithId, createGenericContext, detectIsBrowser, filtersToParams, getCurrentPosition, getFilteredItems, getFilters, getOwnerPaddingRight, getOwnerScrollbarWidth, getPagedItems, getPaginationInfo, getRealSize, getSearchParams, getSearchParamsEncoded, getSearchParamsQs, getUID, hasGeolocation, isElement, isPaginationInfo, localCollectionDelete, localCollectionFetch, localCollectionGet, localCollectionPatch, localCollectionPost, localCollectionPut, parseLabel, parseParams, pushSearchParams, pushSearchParamsEncoded, pushSearchParamsQs, replaceSearchParams, replaceSearchParamsEncoded, replaceSearchParamsEncodedSilently, replaceSearchParamsQs, replaceSearchParamsQsSilently, replaceSearchParamsSilently, scrollToRelativeY, scrollToSelector, scrollToY, setFilters, updateSearchParams, updateSearchParamsEncoded, updateSearchParamsQs, useApi, useApiDelete, useApiFetch, useApiGet, useApiPatch, useApiPost, useApiPut, useBodyScroll, useBreakpoint, useClickAnyWhere, useClickOut, useCollectionApi, useCollectionApiDelete, useCollectionApiGet, useCollectionApiPatch, useCollectionApiPost, useCollectionApiPut, useCurrency, useCurrentState, useDateTimeFormat, useDebounce, useDebounceCallback, useDebugChangedProps, useDomObserver, useDrawer, useElementSize, useEventListener, useExtra, useFilters, useGeolocation, useIdle, useInfiniteLoader, useIntersectionObserver, useIsomorphicLayoutEffect, useKeyboard, useLabel, useLayout, useLazyModules, useModal, useMounted, useNumber, usePage, usePagination, usePortal, usePrevious, useRealSize, useResize, useSSR, useScroll, useScrollTo, useScrolled, useSearchParams, useSearchParamsEncoded, useSearchParamsQs, useSearchParamsValue, useSorting, useSyncedRef, useUI, useUnit, useUnmountEffect, useWindowSize };
|
|
560
|
+
export { BodyScrollOptions, ClickOutGetContainer, CurrentStateType, ElementStackItem, ExtraProvider, Filter, FilterMode, FilterParams, FilterValues, GeoLocationSensorState, IExtra, IFilter, IFilterOption, IGeolocationPositionError, ILabelContext, ILayoutContext, ILazyModulesContext, IPageContext, IPaginationInfo, IParams, IParamsEncoded, IParamsQs, IUseSearchParamsEncodedResult, IUseSearchParamsQsResult, IUseSearchParamsResult, IUseSearchParamsValueResult, KeyCode, KeyMod, KeyboardOptions, KeyboardResult, LabelProvider, LabelService, LayoutProvider, LazyModulesProvider, PageProvider, SSRState, SizeResult, SizeType, Sorting, UIReducer, UIValue, UseFiltersResultType, UseKeyboard, UseKeyboardHandler, createElementWithId, createGenericContext, detectIsBrowser, filtersToParams, getCurrentPosition, getFilteredItems, getFilters, getOwnerPaddingRight, getOwnerScrollbarWidth, getPagedItems, getPaginationInfo, getRealSize, getSearchParams, getSearchParamsEncoded, getSearchParamsQs, getUID, hasGeolocation, isElement, isPaginationInfo, localCollectionDelete, localCollectionFetch, localCollectionGet, localCollectionPatch, localCollectionPost, localCollectionPut, parseLabel, parseParams, pushSearchParams, pushSearchParamsEncoded, pushSearchParamsEncodedSilently, pushSearchParamsQs, pushSearchParamsQsSilently, pushSearchParamsSilently, replaceSearchParams, replaceSearchParamsEncoded, replaceSearchParamsEncodedSilently, replaceSearchParamsQs, replaceSearchParamsQsSilently, replaceSearchParamsSilently, scrollToRelativeY, scrollToSelector, scrollToY, setFilters, updateSearchParams, updateSearchParamsEncoded, updateSearchParamsQs, useApi, useApiDelete, useApiFetch, useApiGet, useApiPatch, useApiPost, useApiPut, useBodyScroll, useBreakpoint, useClickAnyWhere, useClickOut, useCollectionApi, useCollectionApiDelete, useCollectionApiGet, useCollectionApiPatch, useCollectionApiPost, useCollectionApiPut, useCurrency, useCurrentState, useDateTimeFormat, useDebounce, useDebounceCallback, useDebugChangedProps, useDomObserver, useDrawer, useElementSize, useEventListener, useExtra, useFilters, useGeolocation, useIdle, useInfiniteLoader, useIntersectionObserver, useIsomorphicLayoutEffect, useKeyboard, useLabel, useLayout, useLazyModules, useModal, useMounted, useNumber, usePage, usePagination, usePortal, usePrevious, useRealSize, useResize, useSSR, useScroll, useScrollTo, useScrolled, useSearchParams, useSearchParamsEncoded, useSearchParamsQs, useSearchParamsValue, useSorting, useSyncedRef, useUI, useUnit, useUnmountEffect, useWindowSize };
|
package/dist/index.js
CHANGED
|
@@ -96,7 +96,10 @@ __export(src_exports, {
|
|
|
96
96
|
parseParams: () => parseParams,
|
|
97
97
|
pushSearchParams: () => pushSearchParams,
|
|
98
98
|
pushSearchParamsEncoded: () => pushSearchParamsEncoded,
|
|
99
|
+
pushSearchParamsEncodedSilently: () => pushSearchParamsEncodedSilently,
|
|
99
100
|
pushSearchParamsQs: () => pushSearchParamsQs,
|
|
101
|
+
pushSearchParamsQsSilently: () => pushSearchParamsQsSilently,
|
|
102
|
+
pushSearchParamsSilently: () => pushSearchParamsSilently,
|
|
100
103
|
replaceSearchParams: () => replaceSearchParams,
|
|
101
104
|
replaceSearchParamsEncoded: () => replaceSearchParamsEncoded,
|
|
102
105
|
replaceSearchParamsEncodedSilently: () => replaceSearchParamsEncodedSilently,
|
|
@@ -1601,13 +1604,6 @@ function useScrolled(callback = DEFAULT_CALLBACK) {
|
|
|
1601
1604
|
var import_bom_core7 = require("@websolutespa/bom-core");
|
|
1602
1605
|
var import_router = require("next/router");
|
|
1603
1606
|
var import_react31 = require("react");
|
|
1604
|
-
function replace_(params) {
|
|
1605
|
-
if (window.history) {
|
|
1606
|
-
const title = document.title;
|
|
1607
|
-
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1608
|
-
window.history.replaceState(params.toString(), title, url);
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
1607
|
function deserialize_(url) {
|
|
1612
1608
|
const components = url.split("?");
|
|
1613
1609
|
const search = components[1] || "";
|
|
@@ -1642,18 +1638,38 @@ function updateSearchParams(currentPath, params) {
|
|
|
1642
1638
|
const pathname = components[0] || "";
|
|
1643
1639
|
return { pathname, query };
|
|
1644
1640
|
}
|
|
1641
|
+
function pushSearchParams(router, params) {
|
|
1642
|
+
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
1643
|
+
router.push({ pathname, query });
|
|
1644
|
+
}
|
|
1645
|
+
function historyPushState(params) {
|
|
1646
|
+
if (window.history) {
|
|
1647
|
+
const title = document.title;
|
|
1648
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1649
|
+
window.history.pushState(params.toString(), title, url);
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
function pushSearchParamsSilently(params) {
|
|
1653
|
+
if (import_bom_core7.isBrowser) {
|
|
1654
|
+
const searchParams = serialize_(window.location.search, params);
|
|
1655
|
+
historyPushState(searchParams);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1645
1658
|
function replaceSearchParams(router, params) {
|
|
1646
1659
|
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
1647
1660
|
router.replace({ pathname, query });
|
|
1648
1661
|
}
|
|
1649
|
-
function
|
|
1650
|
-
|
|
1651
|
-
|
|
1662
|
+
function historyReplaceState(params) {
|
|
1663
|
+
if (window.history) {
|
|
1664
|
+
const title = document.title;
|
|
1665
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1666
|
+
window.history.replaceState(params.toString(), title, url);
|
|
1667
|
+
}
|
|
1652
1668
|
}
|
|
1653
1669
|
function replaceSearchParamsSilently(params) {
|
|
1654
1670
|
if (import_bom_core7.isBrowser) {
|
|
1655
1671
|
const searchParams = serialize_(window.location.search, params);
|
|
1656
|
-
|
|
1672
|
+
historyReplaceState(searchParams);
|
|
1657
1673
|
}
|
|
1658
1674
|
}
|
|
1659
1675
|
function useSearchParams() {
|
|
@@ -1664,13 +1680,19 @@ function useSearchParams() {
|
|
|
1664
1680
|
return value;
|
|
1665
1681
|
}, [router.asPath]);
|
|
1666
1682
|
const params = initialValue;
|
|
1667
|
-
const
|
|
1668
|
-
|
|
1683
|
+
const pushParams = (0, import_react31.useCallback)((params2) => {
|
|
1684
|
+
pushSearchParams(router, params2);
|
|
1685
|
+
}, [router]);
|
|
1686
|
+
const pushParamsSilently = (0, import_react31.useCallback)((params2) => {
|
|
1687
|
+
pushSearchParamsSilently(params2);
|
|
1669
1688
|
}, []);
|
|
1670
1689
|
const replaceParams = (0, import_react31.useCallback)((params2) => {
|
|
1671
1690
|
replaceSearchParams(router, params2);
|
|
1672
1691
|
}, [router]);
|
|
1673
|
-
|
|
1692
|
+
const replaceParamsSilently = (0, import_react31.useCallback)((params2) => {
|
|
1693
|
+
replaceSearchParamsSilently(params2);
|
|
1694
|
+
}, []);
|
|
1695
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1674
1696
|
}
|
|
1675
1697
|
function useSearchParamsValue(key) {
|
|
1676
1698
|
const router = (0, import_router.useRouter)();
|
|
@@ -1680,10 +1702,19 @@ function useSearchParamsValue(key) {
|
|
|
1680
1702
|
return value;
|
|
1681
1703
|
}, [key, router.asPath]);
|
|
1682
1704
|
const params = initialValue;
|
|
1705
|
+
const pushParams = (0, import_react31.useCallback)((params2) => {
|
|
1706
|
+
pushSearchParams(router, params2);
|
|
1707
|
+
}, [router]);
|
|
1708
|
+
const pushParamsSilently = (0, import_react31.useCallback)((params2) => {
|
|
1709
|
+
pushSearchParamsSilently(params2);
|
|
1710
|
+
}, []);
|
|
1711
|
+
const replaceParams = (0, import_react31.useCallback)((params2) => {
|
|
1712
|
+
replaceSearchParams(router, params2);
|
|
1713
|
+
}, [router]);
|
|
1683
1714
|
const replaceParamsSilently = (0, import_react31.useCallback)((params2) => {
|
|
1684
1715
|
replaceSearchParamsSilently(params2);
|
|
1685
1716
|
}, []);
|
|
1686
|
-
return { params, replaceParamsSilently };
|
|
1717
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1687
1718
|
}
|
|
1688
1719
|
|
|
1689
1720
|
// src/useSearchParamsEncoded/useSearchParamsEncoded.ts
|
|
@@ -1707,13 +1738,6 @@ function setURLSearchParams(url, keyOrValue, value) {
|
|
|
1707
1738
|
}
|
|
1708
1739
|
return params;
|
|
1709
1740
|
}
|
|
1710
|
-
function replace_2(params) {
|
|
1711
|
-
if (window.history) {
|
|
1712
|
-
const title = document.title;
|
|
1713
|
-
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1714
|
-
window.history.replaceState(params.toString(), title, url);
|
|
1715
|
-
}
|
|
1716
|
-
}
|
|
1717
1741
|
function deserialize_2(url, key) {
|
|
1718
1742
|
const encoded = getURLSearchParams(url, "params");
|
|
1719
1743
|
return decode(encoded, key);
|
|
@@ -1769,18 +1793,38 @@ function updateSearchParamsEncoded(currentPath, value, key) {
|
|
|
1769
1793
|
const pathname = components[0] || "";
|
|
1770
1794
|
return { pathname, query };
|
|
1771
1795
|
}
|
|
1796
|
+
function pushSearchParamsEncoded(router, value, key) {
|
|
1797
|
+
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
1798
|
+
router.push({ pathname, query });
|
|
1799
|
+
}
|
|
1800
|
+
function historyPushState2(params) {
|
|
1801
|
+
if (window.history) {
|
|
1802
|
+
const title = document.title;
|
|
1803
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1804
|
+
window.history.pushState(params.toString(), title, url);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
function pushSearchParamsEncodedSilently(value, key) {
|
|
1808
|
+
if (import_bom_core8.isBrowser) {
|
|
1809
|
+
const searchParams = serialize_2(window.location.search, value, key);
|
|
1810
|
+
historyPushState2(searchParams);
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1772
1813
|
function replaceSearchParamsEncoded(router, value, key) {
|
|
1773
1814
|
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
1774
1815
|
router.replace({ pathname, query });
|
|
1775
1816
|
}
|
|
1776
|
-
function
|
|
1777
|
-
|
|
1778
|
-
|
|
1817
|
+
function historyReplaceState2(params) {
|
|
1818
|
+
if (window.history) {
|
|
1819
|
+
const title = document.title;
|
|
1820
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1821
|
+
window.history.replaceState(params.toString(), title, url);
|
|
1822
|
+
}
|
|
1779
1823
|
}
|
|
1780
1824
|
function replaceSearchParamsEncodedSilently(value, key) {
|
|
1781
1825
|
if (import_bom_core8.isBrowser) {
|
|
1782
1826
|
const searchParams = serialize_2(window.location.search, value, key);
|
|
1783
|
-
|
|
1827
|
+
historyReplaceState2(searchParams);
|
|
1784
1828
|
}
|
|
1785
1829
|
}
|
|
1786
1830
|
function useSearchParamsEncoded(key) {
|
|
@@ -1790,26 +1834,25 @@ function useSearchParamsEncoded(key) {
|
|
|
1790
1834
|
return value;
|
|
1791
1835
|
}, [key, router.asPath]);
|
|
1792
1836
|
const params = initialValue;
|
|
1793
|
-
const
|
|
1794
|
-
|
|
1837
|
+
const pushParams = (0, import_react32.useCallback)((params2) => {
|
|
1838
|
+
pushSearchParamsEncoded(router, params2, key);
|
|
1839
|
+
}, [key, router]);
|
|
1840
|
+
const pushParamsSilently = (0, import_react32.useCallback)((params2) => {
|
|
1841
|
+
pushSearchParamsEncodedSilently(params2, key);
|
|
1795
1842
|
}, [key]);
|
|
1796
1843
|
const replaceParams = (0, import_react32.useCallback)((params2) => {
|
|
1797
1844
|
replaceSearchParamsEncoded(router, params2, key);
|
|
1798
1845
|
}, [key, router]);
|
|
1799
|
-
|
|
1846
|
+
const replaceParamsSilently = (0, import_react32.useCallback)((params2) => {
|
|
1847
|
+
replaceSearchParamsEncodedSilently(params2, key);
|
|
1848
|
+
}, [key]);
|
|
1849
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1800
1850
|
}
|
|
1801
1851
|
|
|
1802
1852
|
// src/useSearchParamsQs/useSearchParamsQs.ts
|
|
1803
1853
|
var import_bom_core9 = require("@websolutespa/bom-core");
|
|
1804
1854
|
var import_router3 = require("next/router");
|
|
1805
1855
|
var import_react33 = require("react");
|
|
1806
|
-
function replace(search) {
|
|
1807
|
-
if (window.history) {
|
|
1808
|
-
const title = document.title;
|
|
1809
|
-
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1810
|
-
window.history.replaceState(search, title, url);
|
|
1811
|
-
}
|
|
1812
|
-
}
|
|
1813
1856
|
function deserialize(encoded, key) {
|
|
1814
1857
|
let decoded = {};
|
|
1815
1858
|
if (encoded) {
|
|
@@ -1852,18 +1895,38 @@ function updateSearchParamsQs(url, value, key) {
|
|
|
1852
1895
|
const { search, params } = serialize(components.search, value, key);
|
|
1853
1896
|
return { pathname: components.pathname, query: search };
|
|
1854
1897
|
}
|
|
1898
|
+
function pushSearchParamsQs(router, value, key) {
|
|
1899
|
+
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
1900
|
+
router.push({ pathname, query });
|
|
1901
|
+
}
|
|
1902
|
+
function historyPushState3(search) {
|
|
1903
|
+
if (window.history) {
|
|
1904
|
+
const title = document.title;
|
|
1905
|
+
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1906
|
+
window.history.pushState(search, title, url);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
function pushSearchParamsQsSilently(value, key) {
|
|
1910
|
+
if (import_bom_core9.isBrowser) {
|
|
1911
|
+
const { search } = serialize(window.location.search, value, key);
|
|
1912
|
+
historyPushState3(search);
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1855
1915
|
function replaceSearchParamsQs(router, value, key) {
|
|
1856
1916
|
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
1857
1917
|
router.replace({ pathname, query });
|
|
1858
1918
|
}
|
|
1859
|
-
function
|
|
1860
|
-
|
|
1861
|
-
|
|
1919
|
+
function historyReplaceState3(search) {
|
|
1920
|
+
if (window.history) {
|
|
1921
|
+
const title = document.title;
|
|
1922
|
+
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1923
|
+
window.history.replaceState(search, title, url);
|
|
1924
|
+
}
|
|
1862
1925
|
}
|
|
1863
1926
|
function replaceSearchParamsQsSilently(value, key) {
|
|
1864
1927
|
if (import_bom_core9.isBrowser) {
|
|
1865
1928
|
const { search } = serialize(window.location.search, value, key);
|
|
1866
|
-
|
|
1929
|
+
historyReplaceState3(search);
|
|
1867
1930
|
}
|
|
1868
1931
|
}
|
|
1869
1932
|
function useSearchParamsQs(key) {
|
|
@@ -1873,13 +1936,19 @@ function useSearchParamsQs(key) {
|
|
|
1873
1936
|
return value;
|
|
1874
1937
|
}, [key, router.asPath]);
|
|
1875
1938
|
const params = initialValue;
|
|
1876
|
-
const
|
|
1877
|
-
|
|
1939
|
+
const pushParams = (0, import_react33.useCallback)((params2) => {
|
|
1940
|
+
pushSearchParamsQs(router, params2, key);
|
|
1941
|
+
}, [key, router]);
|
|
1942
|
+
const pushParamsSilently = (0, import_react33.useCallback)((params2) => {
|
|
1943
|
+
pushSearchParamsQsSilently(params2, key);
|
|
1878
1944
|
}, [key]);
|
|
1879
1945
|
const replaceParams = (0, import_react33.useCallback)((params2) => {
|
|
1880
1946
|
replaceSearchParamsQs(router, params2, key);
|
|
1881
1947
|
}, [key, router]);
|
|
1882
|
-
|
|
1948
|
+
const replaceParamsSilently = (0, import_react33.useCallback)((params2) => {
|
|
1949
|
+
replaceSearchParamsQsSilently(params2, key);
|
|
1950
|
+
}, [key]);
|
|
1951
|
+
return { params, pushParamsSilently, pushParams, replaceParamsSilently, replaceParams };
|
|
1883
1952
|
}
|
|
1884
1953
|
|
|
1885
1954
|
// src/useSorting/useSorting.ts
|
|
@@ -1969,7 +2038,10 @@ function useUnmountEffect(effect) {
|
|
|
1969
2038
|
parseParams,
|
|
1970
2039
|
pushSearchParams,
|
|
1971
2040
|
pushSearchParamsEncoded,
|
|
2041
|
+
pushSearchParamsEncodedSilently,
|
|
1972
2042
|
pushSearchParamsQs,
|
|
2043
|
+
pushSearchParamsQsSilently,
|
|
2044
|
+
pushSearchParamsSilently,
|
|
1973
2045
|
replaceSearchParams,
|
|
1974
2046
|
replaceSearchParamsEncoded,
|
|
1975
2047
|
replaceSearchParamsEncodedSilently,
|
package/dist/index.mjs
CHANGED
|
@@ -1465,13 +1465,6 @@ function useScrolled(callback = DEFAULT_CALLBACK) {
|
|
|
1465
1465
|
import { isBrowser as isBrowser3 } from "@websolutespa/bom-core";
|
|
1466
1466
|
import { useRouter } from "next/router";
|
|
1467
1467
|
import { useCallback as useCallback5, useMemo as useMemo5 } from "react";
|
|
1468
|
-
function replace_(params) {
|
|
1469
|
-
if (window.history) {
|
|
1470
|
-
const title = document.title;
|
|
1471
|
-
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1472
|
-
window.history.replaceState(params.toString(), title, url);
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
1468
|
function deserialize_(url) {
|
|
1476
1469
|
const components = url.split("?");
|
|
1477
1470
|
const search = components[1] || "";
|
|
@@ -1506,18 +1499,38 @@ function updateSearchParams(currentPath, params) {
|
|
|
1506
1499
|
const pathname = components[0] || "";
|
|
1507
1500
|
return { pathname, query };
|
|
1508
1501
|
}
|
|
1502
|
+
function pushSearchParams(router, params) {
|
|
1503
|
+
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
1504
|
+
router.push({ pathname, query });
|
|
1505
|
+
}
|
|
1506
|
+
function historyPushState(params) {
|
|
1507
|
+
if (window.history) {
|
|
1508
|
+
const title = document.title;
|
|
1509
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1510
|
+
window.history.pushState(params.toString(), title, url);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
function pushSearchParamsSilently(params) {
|
|
1514
|
+
if (isBrowser3) {
|
|
1515
|
+
const searchParams = serialize_(window.location.search, params);
|
|
1516
|
+
historyPushState(searchParams);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1509
1519
|
function replaceSearchParams(router, params) {
|
|
1510
1520
|
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
1511
1521
|
router.replace({ pathname, query });
|
|
1512
1522
|
}
|
|
1513
|
-
function
|
|
1514
|
-
|
|
1515
|
-
|
|
1523
|
+
function historyReplaceState(params) {
|
|
1524
|
+
if (window.history) {
|
|
1525
|
+
const title = document.title;
|
|
1526
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1527
|
+
window.history.replaceState(params.toString(), title, url);
|
|
1528
|
+
}
|
|
1516
1529
|
}
|
|
1517
1530
|
function replaceSearchParamsSilently(params) {
|
|
1518
1531
|
if (isBrowser3) {
|
|
1519
1532
|
const searchParams = serialize_(window.location.search, params);
|
|
1520
|
-
|
|
1533
|
+
historyReplaceState(searchParams);
|
|
1521
1534
|
}
|
|
1522
1535
|
}
|
|
1523
1536
|
function useSearchParams() {
|
|
@@ -1528,13 +1541,19 @@ function useSearchParams() {
|
|
|
1528
1541
|
return value;
|
|
1529
1542
|
}, [router.asPath]);
|
|
1530
1543
|
const params = initialValue;
|
|
1531
|
-
const
|
|
1532
|
-
|
|
1544
|
+
const pushParams = useCallback5((params2) => {
|
|
1545
|
+
pushSearchParams(router, params2);
|
|
1546
|
+
}, [router]);
|
|
1547
|
+
const pushParamsSilently = useCallback5((params2) => {
|
|
1548
|
+
pushSearchParamsSilently(params2);
|
|
1533
1549
|
}, []);
|
|
1534
1550
|
const replaceParams = useCallback5((params2) => {
|
|
1535
1551
|
replaceSearchParams(router, params2);
|
|
1536
1552
|
}, [router]);
|
|
1537
|
-
|
|
1553
|
+
const replaceParamsSilently = useCallback5((params2) => {
|
|
1554
|
+
replaceSearchParamsSilently(params2);
|
|
1555
|
+
}, []);
|
|
1556
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1538
1557
|
}
|
|
1539
1558
|
function useSearchParamsValue(key) {
|
|
1540
1559
|
const router = useRouter();
|
|
@@ -1544,10 +1563,19 @@ function useSearchParamsValue(key) {
|
|
|
1544
1563
|
return value;
|
|
1545
1564
|
}, [key, router.asPath]);
|
|
1546
1565
|
const params = initialValue;
|
|
1566
|
+
const pushParams = useCallback5((params2) => {
|
|
1567
|
+
pushSearchParams(router, params2);
|
|
1568
|
+
}, [router]);
|
|
1569
|
+
const pushParamsSilently = useCallback5((params2) => {
|
|
1570
|
+
pushSearchParamsSilently(params2);
|
|
1571
|
+
}, []);
|
|
1572
|
+
const replaceParams = useCallback5((params2) => {
|
|
1573
|
+
replaceSearchParams(router, params2);
|
|
1574
|
+
}, [router]);
|
|
1547
1575
|
const replaceParamsSilently = useCallback5((params2) => {
|
|
1548
1576
|
replaceSearchParamsSilently(params2);
|
|
1549
1577
|
}, []);
|
|
1550
|
-
return { params, replaceParamsSilently };
|
|
1578
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1551
1579
|
}
|
|
1552
1580
|
|
|
1553
1581
|
// src/useSearchParamsEncoded/useSearchParamsEncoded.ts
|
|
@@ -1571,13 +1599,6 @@ function setURLSearchParams(url, keyOrValue, value) {
|
|
|
1571
1599
|
}
|
|
1572
1600
|
return params;
|
|
1573
1601
|
}
|
|
1574
|
-
function replace_2(params) {
|
|
1575
|
-
if (window.history) {
|
|
1576
|
-
const title = document.title;
|
|
1577
|
-
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1578
|
-
window.history.replaceState(params.toString(), title, url);
|
|
1579
|
-
}
|
|
1580
|
-
}
|
|
1581
1602
|
function deserialize_2(url, key) {
|
|
1582
1603
|
const encoded = getURLSearchParams(url, "params");
|
|
1583
1604
|
return decode(encoded, key);
|
|
@@ -1633,18 +1654,38 @@ function updateSearchParamsEncoded(currentPath, value, key) {
|
|
|
1633
1654
|
const pathname = components[0] || "";
|
|
1634
1655
|
return { pathname, query };
|
|
1635
1656
|
}
|
|
1657
|
+
function pushSearchParamsEncoded(router, value, key) {
|
|
1658
|
+
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
1659
|
+
router.push({ pathname, query });
|
|
1660
|
+
}
|
|
1661
|
+
function historyPushState2(params) {
|
|
1662
|
+
if (window.history) {
|
|
1663
|
+
const title = document.title;
|
|
1664
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1665
|
+
window.history.pushState(params.toString(), title, url);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
function pushSearchParamsEncodedSilently(value, key) {
|
|
1669
|
+
if (isBrowser4) {
|
|
1670
|
+
const searchParams = serialize_2(window.location.search, value, key);
|
|
1671
|
+
historyPushState2(searchParams);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1636
1674
|
function replaceSearchParamsEncoded(router, value, key) {
|
|
1637
1675
|
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
1638
1676
|
router.replace({ pathname, query });
|
|
1639
1677
|
}
|
|
1640
|
-
function
|
|
1641
|
-
|
|
1642
|
-
|
|
1678
|
+
function historyReplaceState2(params) {
|
|
1679
|
+
if (window.history) {
|
|
1680
|
+
const title = document.title;
|
|
1681
|
+
const url = `${window.location.href.split("?")[0]}?${params.toString()}`;
|
|
1682
|
+
window.history.replaceState(params.toString(), title, url);
|
|
1683
|
+
}
|
|
1643
1684
|
}
|
|
1644
1685
|
function replaceSearchParamsEncodedSilently(value, key) {
|
|
1645
1686
|
if (isBrowser4) {
|
|
1646
1687
|
const searchParams = serialize_2(window.location.search, value, key);
|
|
1647
|
-
|
|
1688
|
+
historyReplaceState2(searchParams);
|
|
1648
1689
|
}
|
|
1649
1690
|
}
|
|
1650
1691
|
function useSearchParamsEncoded(key) {
|
|
@@ -1654,26 +1695,25 @@ function useSearchParamsEncoded(key) {
|
|
|
1654
1695
|
return value;
|
|
1655
1696
|
}, [key, router.asPath]);
|
|
1656
1697
|
const params = initialValue;
|
|
1657
|
-
const
|
|
1658
|
-
|
|
1698
|
+
const pushParams = useCallback6((params2) => {
|
|
1699
|
+
pushSearchParamsEncoded(router, params2, key);
|
|
1700
|
+
}, [key, router]);
|
|
1701
|
+
const pushParamsSilently = useCallback6((params2) => {
|
|
1702
|
+
pushSearchParamsEncodedSilently(params2, key);
|
|
1659
1703
|
}, [key]);
|
|
1660
1704
|
const replaceParams = useCallback6((params2) => {
|
|
1661
1705
|
replaceSearchParamsEncoded(router, params2, key);
|
|
1662
1706
|
}, [key, router]);
|
|
1663
|
-
|
|
1707
|
+
const replaceParamsSilently = useCallback6((params2) => {
|
|
1708
|
+
replaceSearchParamsEncodedSilently(params2, key);
|
|
1709
|
+
}, [key]);
|
|
1710
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
1664
1711
|
}
|
|
1665
1712
|
|
|
1666
1713
|
// src/useSearchParamsQs/useSearchParamsQs.ts
|
|
1667
1714
|
import { isBrowser as isBrowser5, qsDeserializeRaw, qsSerializeRaw } from "@websolutespa/bom-core";
|
|
1668
1715
|
import { useRouter as useRouter3 } from "next/router";
|
|
1669
1716
|
import { useCallback as useCallback7, useMemo as useMemo7 } from "react";
|
|
1670
|
-
function replace(search) {
|
|
1671
|
-
if (window.history) {
|
|
1672
|
-
const title = document.title;
|
|
1673
|
-
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1674
|
-
window.history.replaceState(search, title, url);
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
1717
|
function deserialize(encoded, key) {
|
|
1678
1718
|
let decoded = {};
|
|
1679
1719
|
if (encoded) {
|
|
@@ -1716,18 +1756,38 @@ function updateSearchParamsQs(url, value, key) {
|
|
|
1716
1756
|
const { search, params } = serialize(components.search, value, key);
|
|
1717
1757
|
return { pathname: components.pathname, query: search };
|
|
1718
1758
|
}
|
|
1759
|
+
function pushSearchParamsQs(router, value, key) {
|
|
1760
|
+
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
1761
|
+
router.push({ pathname, query });
|
|
1762
|
+
}
|
|
1763
|
+
function historyPushState3(search) {
|
|
1764
|
+
if (window.history) {
|
|
1765
|
+
const title = document.title;
|
|
1766
|
+
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1767
|
+
window.history.pushState(search, title, url);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
function pushSearchParamsQsSilently(value, key) {
|
|
1771
|
+
if (isBrowser5) {
|
|
1772
|
+
const { search } = serialize(window.location.search, value, key);
|
|
1773
|
+
historyPushState3(search);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1719
1776
|
function replaceSearchParamsQs(router, value, key) {
|
|
1720
1777
|
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
1721
1778
|
router.replace({ pathname, query });
|
|
1722
1779
|
}
|
|
1723
|
-
function
|
|
1724
|
-
|
|
1725
|
-
|
|
1780
|
+
function historyReplaceState3(search) {
|
|
1781
|
+
if (window.history) {
|
|
1782
|
+
const title = document.title;
|
|
1783
|
+
const url = `${window.location.href.split("?")[0]}?${search}`;
|
|
1784
|
+
window.history.replaceState(search, title, url);
|
|
1785
|
+
}
|
|
1726
1786
|
}
|
|
1727
1787
|
function replaceSearchParamsQsSilently(value, key) {
|
|
1728
1788
|
if (isBrowser5) {
|
|
1729
1789
|
const { search } = serialize(window.location.search, value, key);
|
|
1730
|
-
|
|
1790
|
+
historyReplaceState3(search);
|
|
1731
1791
|
}
|
|
1732
1792
|
}
|
|
1733
1793
|
function useSearchParamsQs(key) {
|
|
@@ -1737,13 +1797,19 @@ function useSearchParamsQs(key) {
|
|
|
1737
1797
|
return value;
|
|
1738
1798
|
}, [key, router.asPath]);
|
|
1739
1799
|
const params = initialValue;
|
|
1740
|
-
const
|
|
1741
|
-
|
|
1800
|
+
const pushParams = useCallback7((params2) => {
|
|
1801
|
+
pushSearchParamsQs(router, params2, key);
|
|
1802
|
+
}, [key, router]);
|
|
1803
|
+
const pushParamsSilently = useCallback7((params2) => {
|
|
1804
|
+
pushSearchParamsQsSilently(params2, key);
|
|
1742
1805
|
}, [key]);
|
|
1743
1806
|
const replaceParams = useCallback7((params2) => {
|
|
1744
1807
|
replaceSearchParamsQs(router, params2, key);
|
|
1745
1808
|
}, [key, router]);
|
|
1746
|
-
|
|
1809
|
+
const replaceParamsSilently = useCallback7((params2) => {
|
|
1810
|
+
replaceSearchParamsQsSilently(params2, key);
|
|
1811
|
+
}, [key]);
|
|
1812
|
+
return { params, pushParamsSilently, pushParams, replaceParamsSilently, replaceParams };
|
|
1747
1813
|
}
|
|
1748
1814
|
|
|
1749
1815
|
// src/useSorting/useSorting.ts
|
|
@@ -1832,7 +1898,10 @@ export {
|
|
|
1832
1898
|
parseParams,
|
|
1833
1899
|
pushSearchParams,
|
|
1834
1900
|
pushSearchParamsEncoded,
|
|
1901
|
+
pushSearchParamsEncodedSilently,
|
|
1835
1902
|
pushSearchParamsQs,
|
|
1903
|
+
pushSearchParamsQsSilently,
|
|
1904
|
+
pushSearchParamsSilently,
|
|
1836
1905
|
replaceSearchParams,
|
|
1837
1906
|
replaceSearchParamsEncoded,
|
|
1838
1907
|
replaceSearchParamsEncodedSilently,
|
package/package.json
CHANGED
|
@@ -2,14 +2,6 @@ import { isBrowser } from '@websolutespa/bom-core';
|
|
|
2
2
|
import { NextRouter, useRouter } from 'next/router';
|
|
3
3
|
import { useCallback, useMemo } from 'react';
|
|
4
4
|
|
|
5
|
-
function replace_(params: URLSearchParams) {
|
|
6
|
-
if (window.history) {
|
|
7
|
-
const title = document.title;
|
|
8
|
-
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
9
|
-
window.history.replaceState(params.toString(), title, url);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
5
|
function deserialize_(url: string): URLSearchParams {
|
|
14
6
|
const components = url.split('?');
|
|
15
7
|
const search = components[1] || '';
|
|
@@ -51,23 +43,58 @@ export function updateSearchParams(currentPath: string, params: IParams): { path
|
|
|
51
43
|
return { pathname, query };
|
|
52
44
|
}
|
|
53
45
|
|
|
46
|
+
export function pushSearchParams(router: NextRouter, params: IParams): void {
|
|
47
|
+
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
48
|
+
router.push({ pathname, query });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function historyPushState(params: URLSearchParams) {
|
|
52
|
+
if (window.history) {
|
|
53
|
+
const title = document.title;
|
|
54
|
+
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
55
|
+
window.history.pushState(params.toString(), title, url);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function pushSearchParamsSilently(params: IParams): void {
|
|
60
|
+
if (isBrowser) {
|
|
61
|
+
const searchParams = serialize_(window.location.search, params);
|
|
62
|
+
historyPushState(searchParams);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
export function replaceSearchParams(router: NextRouter, params: IParams): void {
|
|
55
67
|
const { pathname, query } = updateSearchParams(router.asPath, params);
|
|
56
68
|
router.replace({ pathname, query });
|
|
57
69
|
}
|
|
58
70
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
function historyReplaceState(params: URLSearchParams) {
|
|
72
|
+
if (window.history) {
|
|
73
|
+
const title = document.title;
|
|
74
|
+
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
75
|
+
window.history.replaceState(params.toString(), title, url);
|
|
76
|
+
}
|
|
62
77
|
}
|
|
63
78
|
|
|
64
79
|
export function replaceSearchParamsSilently(params: IParams): void {
|
|
65
80
|
if (isBrowser) {
|
|
66
81
|
const searchParams = serialize_(window.location.search, params);
|
|
67
|
-
|
|
82
|
+
historyReplaceState(searchParams);
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
|
|
86
|
+
export type IParams = {
|
|
87
|
+
[key: string]: string;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type IUseSearchParamsResult = {
|
|
91
|
+
params: IParams;
|
|
92
|
+
pushParams: (params: IParams) => void;
|
|
93
|
+
pushParamsSilently: (params: IParams) => void;
|
|
94
|
+
replaceParams: (params: IParams) => void;
|
|
95
|
+
replaceParamsSilently: (params: IParams) => void;
|
|
96
|
+
};
|
|
97
|
+
|
|
71
98
|
export function useSearchParams(): IUseSearchParamsResult {
|
|
72
99
|
const router = useRouter();
|
|
73
100
|
|
|
@@ -79,31 +106,27 @@ export function useSearchParams(): IUseSearchParamsResult {
|
|
|
79
106
|
|
|
80
107
|
const params = initialValue;
|
|
81
108
|
|
|
82
|
-
const
|
|
83
|
-
|
|
109
|
+
const pushParams = useCallback((params: IParams) => {
|
|
110
|
+
pushSearchParams(router, params);
|
|
111
|
+
}, [router]);
|
|
112
|
+
|
|
113
|
+
const pushParamsSilently = useCallback((params: IParams) => {
|
|
114
|
+
pushSearchParamsSilently(params);
|
|
84
115
|
}, []);
|
|
85
116
|
|
|
86
117
|
const replaceParams = useCallback((params: IParams) => {
|
|
87
118
|
replaceSearchParams(router, params);
|
|
88
119
|
}, [router]);
|
|
89
120
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
export type IParams = {
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
-
[key: string]: string;
|
|
96
|
-
};
|
|
121
|
+
const replaceParamsSilently = useCallback((params: IParams) => {
|
|
122
|
+
replaceSearchParamsSilently(params);
|
|
123
|
+
}, []);
|
|
97
124
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
replaceParamsSilently: (params: IParams) => void
|
|
101
|
-
};
|
|
125
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
126
|
+
}
|
|
102
127
|
|
|
103
|
-
export type
|
|
104
|
-
params:
|
|
105
|
-
replaceParamsSilently: (params: IParams) => void,
|
|
106
|
-
replaceParams: (params: IParams) => void
|
|
128
|
+
export type IUseSearchParamsValueResult = Omit<IUseSearchParamsResult, 'params'> & {
|
|
129
|
+
params: string | null;
|
|
107
130
|
};
|
|
108
131
|
|
|
109
132
|
export function useSearchParamsValue(key: string): IUseSearchParamsValueResult {
|
|
@@ -117,9 +140,21 @@ export function useSearchParamsValue(key: string): IUseSearchParamsValueResult {
|
|
|
117
140
|
|
|
118
141
|
const params = initialValue;
|
|
119
142
|
|
|
143
|
+
const pushParams = useCallback((params: IParams) => {
|
|
144
|
+
pushSearchParams(router, params);
|
|
145
|
+
}, [router]);
|
|
146
|
+
|
|
147
|
+
const pushParamsSilently = useCallback((params: IParams) => {
|
|
148
|
+
pushSearchParamsSilently(params);
|
|
149
|
+
}, []);
|
|
150
|
+
|
|
151
|
+
const replaceParams = useCallback((params: IParams) => {
|
|
152
|
+
replaceSearchParams(router, params);
|
|
153
|
+
}, [router]);
|
|
154
|
+
|
|
120
155
|
const replaceParamsSilently = useCallback((params: IParams) => {
|
|
121
156
|
replaceSearchParamsSilently(params);
|
|
122
157
|
}, []);
|
|
123
158
|
|
|
124
|
-
return { params, replaceParamsSilently };
|
|
159
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
125
160
|
}
|
|
@@ -30,44 +30,9 @@ function setURLSearchParams(url: string, keyOrValue: string, value: string | nul
|
|
|
30
30
|
}
|
|
31
31
|
return params;
|
|
32
32
|
// push_(params);
|
|
33
|
-
//
|
|
33
|
+
// historyReplaceState(params);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/*
|
|
37
|
-
function push_(params: URLSearchParams) {
|
|
38
|
-
if (window.history) {
|
|
39
|
-
const title = document.title;
|
|
40
|
-
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
41
|
-
window.history.pushState(params.toString(), title, url);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
function replace_(params: URLSearchParams) {
|
|
47
|
-
if (window.history) {
|
|
48
|
-
const title = document.title;
|
|
49
|
-
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
50
|
-
window.history.replaceState(params.toString(), title, url);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/*
|
|
55
|
-
function replace__(from: string, to: string) {
|
|
56
|
-
const history = window.history;
|
|
57
|
-
if (history && history.replaceState) {
|
|
58
|
-
const location = window.location;
|
|
59
|
-
const title = document.title;
|
|
60
|
-
if (location.pathname === '/') {
|
|
61
|
-
const url = location.origin + to + location.search;
|
|
62
|
-
history.replaceState(history.state, title, url);
|
|
63
|
-
} else if (location.href.indexOf(from) !== -1) {
|
|
64
|
-
const url = location.href.replace(from, to);
|
|
65
|
-
history.replaceState(history.state, title, url);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
36
|
function deserialize_(url: string, key?: string) {
|
|
72
37
|
const encoded = getURLSearchParams(url, 'params');
|
|
73
38
|
return decode(encoded, key);
|
|
@@ -134,20 +99,43 @@ export function updateSearchParamsEncoded(currentPath: string, value: IParamsEnc
|
|
|
134
99
|
return { pathname, query };
|
|
135
100
|
}
|
|
136
101
|
|
|
102
|
+
export function pushSearchParamsEncoded(router: NextRouter, value: IParamsEncoded, key?: string): void {
|
|
103
|
+
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
104
|
+
router.push({ pathname, query });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function historyPushState(params: URLSearchParams) {
|
|
108
|
+
if (window.history) {
|
|
109
|
+
const title = document.title;
|
|
110
|
+
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
111
|
+
window.history.pushState(params.toString(), title, url);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function pushSearchParamsEncodedSilently(value: IParamsEncoded, key?: string): void {
|
|
116
|
+
if (isBrowser) {
|
|
117
|
+
const searchParams = serialize_(window.location.search, value, key);
|
|
118
|
+
historyPushState(searchParams);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
137
122
|
export function replaceSearchParamsEncoded(router: NextRouter, value: IParamsEncoded, key?: string): void {
|
|
138
123
|
const { pathname, query } = updateSearchParamsEncoded(router.asPath, value, key);
|
|
139
124
|
router.replace({ pathname, query });
|
|
140
125
|
}
|
|
141
126
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
127
|
+
function historyReplaceState(params: URLSearchParams) {
|
|
128
|
+
if (window.history) {
|
|
129
|
+
const title = document.title;
|
|
130
|
+
const url = `${window.location.href.split('?')[0]}?${params.toString()}`;
|
|
131
|
+
window.history.replaceState(params.toString(), title, url);
|
|
132
|
+
}
|
|
145
133
|
}
|
|
146
134
|
|
|
147
135
|
export function replaceSearchParamsEncodedSilently(value: IParamsEncoded, key?: string): void {
|
|
148
136
|
if (isBrowser) {
|
|
149
137
|
const searchParams = serialize_(window.location.search, value, key);
|
|
150
|
-
|
|
138
|
+
historyReplaceState(searchParams);
|
|
151
139
|
}
|
|
152
140
|
}
|
|
153
141
|
|
|
@@ -157,9 +145,11 @@ export type IParamsEncoded = {
|
|
|
157
145
|
} | null;
|
|
158
146
|
|
|
159
147
|
export type IUseSearchParamsEncodedResult = {
|
|
160
|
-
params: IParamsEncoded
|
|
161
|
-
|
|
162
|
-
|
|
148
|
+
params: IParamsEncoded;
|
|
149
|
+
pushParams: (params: IParamsEncoded) => void;
|
|
150
|
+
pushParamsSilently: (params: IParamsEncoded) => void;
|
|
151
|
+
replaceParams: (params: IParamsEncoded) => void;
|
|
152
|
+
replaceParamsSilently: (params: IParamsEncoded) => void;
|
|
163
153
|
};
|
|
164
154
|
|
|
165
155
|
export function useSearchParamsEncoded(key?: string): IUseSearchParamsEncodedResult {
|
|
@@ -171,22 +161,22 @@ export function useSearchParamsEncoded(key?: string): IUseSearchParamsEncodedRes
|
|
|
171
161
|
}, [key, router.asPath]);
|
|
172
162
|
|
|
173
163
|
const params = initialValue;
|
|
174
|
-
// console.log('useSearchParamsEncoded', params);
|
|
175
|
-
// const [params, setParams_] = useState(initialValue);
|
|
176
164
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
replaceSearchParamsEncoded(router, params, key);
|
|
165
|
+
const pushParams = useCallback((params: IParamsEncoded) => {
|
|
166
|
+
pushSearchParamsEncoded(router, params, key);
|
|
180
167
|
}, [key, router]);
|
|
181
|
-
*/
|
|
182
168
|
|
|
183
|
-
const
|
|
184
|
-
|
|
169
|
+
const pushParamsSilently = useCallback((params: IParamsEncoded) => {
|
|
170
|
+
pushSearchParamsEncodedSilently(params, key);
|
|
185
171
|
}, [key]);
|
|
186
172
|
|
|
187
173
|
const replaceParams = useCallback((params: IParamsEncoded) => {
|
|
188
174
|
replaceSearchParamsEncoded(router, params, key);
|
|
189
175
|
}, [key, router]);
|
|
190
176
|
|
|
191
|
-
|
|
177
|
+
const replaceParamsSilently = useCallback((params: IParamsEncoded) => {
|
|
178
|
+
replaceSearchParamsEncodedSilently(params, key);
|
|
179
|
+
}, [key]);
|
|
180
|
+
|
|
181
|
+
return { params, pushParams, pushParamsSilently, replaceParams, replaceParamsSilently };
|
|
192
182
|
}
|
|
@@ -6,18 +6,12 @@ export type IParamsQs = Record<string, any>;
|
|
|
6
6
|
|
|
7
7
|
export type IUseSearchParamsQsResult = {
|
|
8
8
|
params: IParamsQs,
|
|
9
|
-
|
|
9
|
+
pushParams: (params: IParamsQs) => void
|
|
10
|
+
pushParamsSilently: (params: IParamsQs) => void,
|
|
10
11
|
replaceParams: (params: IParamsQs) => void
|
|
12
|
+
replaceParamsSilently: (params: IParamsQs) => void,
|
|
11
13
|
};
|
|
12
14
|
|
|
13
|
-
function replace(search: string) {
|
|
14
|
-
if (window.history) {
|
|
15
|
-
const title = document.title;
|
|
16
|
-
const url = `${window.location.href.split('?')[0]}?${search}`;
|
|
17
|
-
window.history.replaceState(search, title, url);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
15
|
function deserialize(encoded: string | null | undefined, key?: string): IParamsQs {
|
|
22
16
|
let decoded = {};
|
|
23
17
|
if (encoded) {
|
|
@@ -67,20 +61,43 @@ export function updateSearchParamsQs(url: string, value: IParamsQs, key?: string
|
|
|
67
61
|
return { pathname: components.pathname, query: search };
|
|
68
62
|
}
|
|
69
63
|
|
|
64
|
+
export function pushSearchParamsQs(router: NextRouter, value: IParamsQs, key?: string): void {
|
|
65
|
+
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
66
|
+
router.push({ pathname, query });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function historyPushState(search: string) {
|
|
70
|
+
if (window.history) {
|
|
71
|
+
const title = document.title;
|
|
72
|
+
const url = `${window.location.href.split('?')[0]}?${search}`;
|
|
73
|
+
window.history.pushState(search, title, url);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function pushSearchParamsQsSilently(value: IParamsQs, key?: string): void {
|
|
78
|
+
if (isBrowser) {
|
|
79
|
+
const { search } = serialize(window.location.search, value, key);
|
|
80
|
+
historyPushState(search);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
70
84
|
export function replaceSearchParamsQs(router: NextRouter, value: IParamsQs, key?: string): void {
|
|
71
85
|
const { pathname, query } = updateSearchParamsQs(router.asPath, value, key);
|
|
72
86
|
router.replace({ pathname, query });
|
|
73
87
|
}
|
|
74
88
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
89
|
+
function historyReplaceState(search: string) {
|
|
90
|
+
if (window.history) {
|
|
91
|
+
const title = document.title;
|
|
92
|
+
const url = `${window.location.href.split('?')[0]}?${search}`;
|
|
93
|
+
window.history.replaceState(search, title, url);
|
|
94
|
+
}
|
|
78
95
|
}
|
|
79
96
|
|
|
80
97
|
export function replaceSearchParamsQsSilently(value: IParamsQs, key?: string): void {
|
|
81
98
|
if (isBrowser) {
|
|
82
99
|
const { search } = serialize(window.location.search, value, key);
|
|
83
|
-
|
|
100
|
+
historyReplaceState(search);
|
|
84
101
|
}
|
|
85
102
|
}
|
|
86
103
|
|
|
@@ -94,13 +111,21 @@ export function useSearchParamsQs(key?: string): IUseSearchParamsQsResult {
|
|
|
94
111
|
|
|
95
112
|
const params = initialValue;
|
|
96
113
|
|
|
97
|
-
const
|
|
98
|
-
|
|
114
|
+
const pushParams = useCallback((params: IParamsQs) => {
|
|
115
|
+
pushSearchParamsQs(router, params, key);
|
|
116
|
+
}, [key, router]);
|
|
117
|
+
|
|
118
|
+
const pushParamsSilently = useCallback((params: IParamsQs) => {
|
|
119
|
+
pushSearchParamsQsSilently(params, key);
|
|
99
120
|
}, [key]);
|
|
100
121
|
|
|
101
122
|
const replaceParams = useCallback((params: IParamsQs) => {
|
|
102
123
|
replaceSearchParamsQs(router, params, key);
|
|
103
124
|
}, [key, router]);
|
|
104
125
|
|
|
105
|
-
|
|
126
|
+
const replaceParamsSilently = useCallback((params: IParamsQs) => {
|
|
127
|
+
replaceSearchParamsQsSilently(params, key);
|
|
128
|
+
}, [key]);
|
|
129
|
+
|
|
130
|
+
return { params, pushParamsSilently, pushParams, replaceParamsSilently, replaceParams };
|
|
106
131
|
}
|