xmirror-components 1.0.33 → 1.0.34-alpha.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.
@@ -36,7 +36,11 @@ var baseConfig = {
36
36
  }
37
37
  },
38
38
  iconFont: {},
39
- locale: 'zh'
39
+ locale: 'zh',
40
+ useSearchParams: {
41
+ storage: 'state',
42
+ stateKey: '__searchParams__'
43
+ }
40
44
  };
41
45
  export var ConfigContext = React.createContext(baseConfig);
42
46
  export default function ConfigProvider(_a) {
@@ -1,6 +1,7 @@
1
1
  import React from "react";
2
2
  import { IconButtonProps } from "../IconButton";
3
3
  import { IconFontProps } from "../IconFont";
4
+ import { UseSearchParamsOptions } from "../../hooks/useSearchParams/types";
4
5
  import { type MessagesKeys } from "../../locale";
5
6
  type IconButtonConfig = Pick<IconButtonProps, 'size' | 'className' | 'style'> & {
6
7
  render: (props: IconButtonProps & {
@@ -12,6 +13,7 @@ export interface ConfigContextProps {
12
13
  iconButton: IconButtonConfig;
13
14
  iconFont: IconFontConfig;
14
15
  locale: MessagesKeys;
16
+ useSearchParams: UseSearchParamsOptions;
15
17
  }
16
18
  export type ConfigProviderConfigProps = Partial<ConfigContextProps>;
17
19
  export {};
@@ -0,0 +1,3 @@
1
+ import type { SearchParams, SearchParamsType, UseSearchParamsOptions } from './types.ts';
2
+ declare const useSearchParams: <T extends SearchParamsType>(initialValue: T, options?: UseSearchParamsOptions) => SearchParams<T>;
3
+ export default useSearchParams;
@@ -0,0 +1,177 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
23
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ import { useRef, useEffect, useContext } from 'react';
49
+ import { useBlocker, useNavigate, useLocation } from 'react-router-dom';
50
+ import useUrlState from '@ahooksjs/use-url-state';
51
+ import { useUpdate } from 'ahooks';
52
+ import { noop } from 'lodash-es';
53
+ import { encodeParams, decodeParams } from './util';
54
+ import { ConfigContext } from '../../components/ConfigProvider';
55
+ var initialSearchParams = {
56
+ y: undefined
57
+ };
58
+ var useSearchParams = function (initialValue, options) {
59
+ var _a;
60
+ var defaultConfigOptions = useContext(ConfigContext).useSearchParams;
61
+ var _b = options || defaultConfigOptions || {}, _c = _b.storage, storage = _c === void 0 ? (defaultConfigOptions === null || defaultConfigOptions === void 0 ? void 0 : defaultConfigOptions.storage) || 'state' : _c, _d = _b.stateKey, stateKey = _d === void 0 ? (defaultConfigOptions === null || defaultConfigOptions === void 0 ? void 0 : defaultConfigOptions.stateKey) || '__searchParams__' : _d;
62
+ var update = useUpdate();
63
+ var navigator = useNavigate();
64
+ var location = useLocation();
65
+ var _e = useUrlState(encodeParams(__assign(__assign({}, initialSearchParams), initialValue)), {
66
+ navigateMode: 'replace'
67
+ }), urlParams = _e[0], setUrlParams = _e[1];
68
+ var initialDecodedParams = storage === 'url'
69
+ ? decodeParams(urlParams)
70
+ : (((_a = location.state) === null || _a === void 0 ? void 0 : _a[stateKey]) || {});
71
+ var searchParams = useRef(__assign(__assign({}, initialValue), initialDecodedParams)).current;
72
+ /**
73
+ * 设置筛选参数
74
+ */
75
+ var setSearchParams = function (value) {
76
+ if (typeof value === 'function') {
77
+ Object.assign(searchParams, value(searchParams));
78
+ }
79
+ else {
80
+ Object.assign(searchParams, value);
81
+ }
82
+ update();
83
+ };
84
+ /**
85
+ * 根据配置更新存储
86
+ */
87
+ var persistParams = function (params) { return __awaiter(void 0, void 0, void 0, function () {
88
+ var payload;
89
+ var _a;
90
+ return __generator(this, function (_b) {
91
+ payload = __assign(__assign({}, params), { y: window.scrollY });
92
+ if (storage === 'url') {
93
+ setUrlParams(encodeParams(payload));
94
+ return [2 /*return*/];
95
+ }
96
+ navigator(location.pathname, {
97
+ replace: true,
98
+ state: __assign(__assign({}, location.state), (_a = {}, _a[stateKey] = payload, _a))
99
+ });
100
+ return [2 /*return*/];
101
+ });
102
+ }); };
103
+ /**
104
+ * 清除 URL Params
105
+ */
106
+ var clearUrlParams = function () {
107
+ if (storage !== 'url')
108
+ return;
109
+ var params = decodeParams(urlParams);
110
+ Object.keys(params).forEach(function (key) {
111
+ params[key] = undefined;
112
+ });
113
+ setUrlParams(params);
114
+ };
115
+ useEffect(function () {
116
+ /**
117
+ * 初始化时清除 URL Params
118
+ */
119
+ if (storage === 'url' && urlParams) {
120
+ clearUrlParams();
121
+ }
122
+ else if (storage === 'state') {
123
+ persistParams({}).catch(noop);
124
+ }
125
+ }, []);
126
+ var targetLocationRef = useRef(null);
127
+ var navEndRef = useRef(false);
128
+ /**
129
+ * 自动保存状态:在路由跳转前自动保存 searchParams 和滚动位置
130
+ * useBlocker 会在导航发生前被调用,此时可以保存当前页面的状态
131
+ * useBlocker return true 表示 “阻止路由跳转”,return false 表示 “允许路由跳转”
132
+ */
133
+ useBlocker(function (_a) {
134
+ var currentLocation = _a.currentLocation, nextLocation = _a.nextLocation, historyAction = _a.historyAction;
135
+ if (currentLocation.pathname !==
136
+ nextLocation.pathname) {
137
+ if (storage === 'url') {
138
+ if (currentLocation.search) {
139
+ return false;
140
+ }
141
+ targetLocationRef.current = __assign(__assign({}, nextLocation), { historyAction: historyAction });
142
+ // 同步保存状态(不等待,因为这是阻塞性的)
143
+ persistParams(searchParams).catch(noop);
144
+ return true;
145
+ }
146
+ if (navEndRef.current) {
147
+ return false;
148
+ }
149
+ targetLocationRef.current = __assign(__assign({}, nextLocation), { historyAction: historyAction });
150
+ persistParams(searchParams).catch(noop);
151
+ return true;
152
+ }
153
+ else if (nextLocation.pathname === currentLocation.pathname &&
154
+ targetLocationRef.current !== null && ((storage === 'url' &&
155
+ nextLocation.search !== currentLocation.search) || (storage === 'state' &&
156
+ !navEndRef.current))) {
157
+ setTimeout(function () {
158
+ var targetLocation = targetLocationRef.current;
159
+ var path = "".concat(targetLocation.pathname).concat(targetLocation.search);
160
+ navigator(path, {
161
+ replace: targetLocation.historyAction !== 'PUSH',
162
+ state: targetLocation.state
163
+ });
164
+ navEndRef.current = false;
165
+ targetLocationRef.current = null;
166
+ }, 0);
167
+ navEndRef.current = true;
168
+ return false;
169
+ }
170
+ return false;
171
+ });
172
+ return {
173
+ searchParams: searchParams,
174
+ setSearchParams: setSearchParams
175
+ };
176
+ };
177
+ export default useSearchParams;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * 搜索参数类型
3
+ */
4
+ export type SearchParamsType = Record<string, any>;
5
+ /**
6
+ * 编码后的搜索参数类型
7
+ */
8
+ export type EncodeSearchParamsType = Record<string, string>;
9
+ /**
10
+ * 搜索参数接口
11
+ */
12
+ export interface SearchParams<T extends SearchParamsType> {
13
+ /**
14
+ * 当前搜索参数
15
+ */
16
+ searchParams: T;
17
+ /**
18
+ * 设置搜索参数
19
+ */
20
+ setSearchParams: (value: T | Partial<T> | ((prev: T) => T)) => void;
21
+ }
22
+ export interface UseSearchParamsOptions {
23
+ /**
24
+ * 存储方式:url 参数或路由 state
25
+ */
26
+ storage?: 'url' | 'state';
27
+ /**
28
+ * 路由 state 模式下存储的 key
29
+ */
30
+ stateKey?: string;
31
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { EncodeSearchParamsType, SearchParamsType } from './types.ts';
2
+ /**
3
+ * 加密
4
+ * @param params
5
+ */
6
+ export declare const encodeParams: (params: SearchParamsType) => EncodeSearchParamsType;
7
+ /**
8
+ * 解密
9
+ * @param params
10
+ */
11
+ export declare const decodeParams: (params: EncodeSearchParamsType) => SearchParamsType;
@@ -0,0 +1,89 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { isArray, toNumber } from 'lodash-es';
13
+ var typeFlags = {
14
+ Array: 'a',
15
+ Boolean: 'b',
16
+ Number: 'n',
17
+ String: 's',
18
+ Undefined: 'u',
19
+ Null: 'N'
20
+ };
21
+ var pack = function (value, array) {
22
+ var type = Object.prototype.toString.call(value).slice(8, -1);
23
+ if (type === 'Array') {
24
+ if (value.length) {
25
+ return "".concat(typeFlags[type]).concat(pack(value[0], value));
26
+ }
27
+ else {
28
+ return 'u_u';
29
+ }
30
+ }
31
+ var v = array || [value];
32
+ if (type === 'Boolean') {
33
+ return "".concat(typeFlags[type], "_").concat(v.map(function (item) { return (item ? 1 : 0); }).join());
34
+ }
35
+ if (type === 'String') {
36
+ return "".concat(typeFlags[type], "_").concat(v.join());
37
+ }
38
+ if (type === 'Null') {
39
+ return "".concat(typeFlags[type], "_").concat(v.map(function () { return 'n'; }).join());
40
+ }
41
+ if (type === 'Number') {
42
+ return "".concat(typeFlags[type], "_").concat(v.join());
43
+ }
44
+ return "u_".concat(v.map(function () { return 'u'; }).join());
45
+ };
46
+ var unpack = function (value) {
47
+ value = decodeURIComponent(value);
48
+ var _isArray = value.slice(0, 1) === 'a';
49
+ var type = _isArray ? value.slice(1, 2) : value.slice(0, 1);
50
+ var _value = _isArray ? value.slice(3).split(',') : value.slice(2);
51
+ if (type === 'b') {
52
+ return isArray(_value) ? _value.map(function (item) { return item === '1'; }) : _value === '1';
53
+ }
54
+ if (type === 's') {
55
+ return _value;
56
+ }
57
+ if (type === 'n') {
58
+ return isArray(_value) ? _value.map(function (item) { return toNumber(item); }) : toNumber(_value);
59
+ }
60
+ if (type === 'u') {
61
+ return isArray(_value) ? _value.map(function () { return undefined; }) : undefined;
62
+ }
63
+ if (type === 'N') {
64
+ return isArray(_value) ? _value.map(function () { return null; }) : null;
65
+ }
66
+ return _value;
67
+ };
68
+ /**
69
+ * 加密
70
+ * @param params
71
+ */
72
+ export var encodeParams = function (params) {
73
+ return Object.entries(__assign({}, params)).reduce(function (previousValue, _a) {
74
+ var key = _a[0], value = _a[1];
75
+ previousValue[key] = pack(value);
76
+ return previousValue;
77
+ }, {});
78
+ };
79
+ /**
80
+ * 解密
81
+ * @param params
82
+ */
83
+ export var decodeParams = function (params) {
84
+ return Object.entries(__assign({}, params)).reduce(function (previousValue, _a) {
85
+ var key = _a[0], value = _a[1];
86
+ previousValue[key] = unpack(value);
87
+ return previousValue;
88
+ }, {});
89
+ };
@@ -233,9 +233,28 @@ function useTable(request, options) {
233
233
  /**
234
234
  * 刷新
235
235
  */
236
- var refresh = useCallback(function () {
237
- initList(paginationRef.current.current, paginationRef.current.pageSize);
238
- }, [initList]);
236
+ var refresh = useCallback(function (searchData) {
237
+ if (searchData) {
238
+ var _searchParams = {
239
+ column: searchParams.column,
240
+ order: searchParams.order
241
+ };
242
+ if (searchData === null || searchData === void 0 ? void 0 : searchData.pageSize) {
243
+ setPagination(__assign(__assign({}, pagination), { pageSize: searchData.pageSize }));
244
+ }
245
+ if (searchData === null || searchData === void 0 ? void 0 : searchData.pageNum) {
246
+ setPagination(__assign(__assign({}, pagination), { current: searchData.pageNum }));
247
+ }
248
+ for (var key in searchParams) {
249
+ delete searchParams[key];
250
+ }
251
+ Object.assign(searchParams, _searchParams, searchData);
252
+ initList((searchData === null || searchData === void 0 ? void 0 : searchData.pageNum) || pagination.current, (searchData === null || searchData === void 0 ? void 0 : searchData.pageSize) || pagination.pageSize);
253
+ }
254
+ else {
255
+ initList(pagination.current, pagination.pageSize);
256
+ }
257
+ }, [initList, pagination, searchParams]);
239
258
  /**
240
259
  * 列表上的筛选排序执行
241
260
  * @param pagination 分页信息
@@ -105,7 +105,7 @@ export interface ReturnData<T = AnyObject> {
105
105
  /**
106
106
  * 刷新页面
107
107
  */
108
- refresh: () => void;
108
+ refresh: (searchData?: Record<string, unknown>) => void;
109
109
  };
110
110
  /**
111
111
  * 多选条件
@@ -9,3 +9,4 @@ export { default as Table } from './components/Table';
9
9
  export { default as SearchArea } from './components/SearchArea';
10
10
  export { default as useRequest } from './hooks/useRequest';
11
11
  export { default as useTable } from './hooks/useTable';
12
+ export { default as useSearchParams } from './hooks/useSearchParams';
package/build/es/index.js CHANGED
@@ -9,3 +9,4 @@ export { default as Table } from './components/Table';
9
9
  export { default as SearchArea } from './components/SearchArea';
10
10
  export { default as useRequest } from './hooks/useRequest';
11
11
  export { default as useTable } from './hooks/useTable';
12
+ export { default as useSearchParams } from './hooks/useSearchParams';
@@ -43,7 +43,11 @@ var baseConfig = {
43
43
  }
44
44
  },
45
45
  iconFont: {},
46
- locale: 'zh'
46
+ locale: 'zh',
47
+ useSearchParams: {
48
+ storage: 'state',
49
+ stateKey: '__searchParams__'
50
+ }
47
51
  };
48
52
  exports.ConfigContext = react_1.default.createContext(baseConfig);
49
53
  function ConfigProvider(_a) {
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var react_1 = require("react");
54
+ var react_router_dom_1 = require("react-router-dom");
55
+ var use_url_state_1 = __importDefault(require("@ahooksjs/use-url-state"));
56
+ var ahooks_1 = require("ahooks");
57
+ var lodash_es_1 = require("lodash-es");
58
+ var util_1 = require("./util");
59
+ var ConfigProvider_1 = require("../../components/ConfigProvider");
60
+ var initialSearchParams = {
61
+ y: undefined
62
+ };
63
+ var useSearchParams = function (initialValue, options) {
64
+ var _a;
65
+ var defaultConfigOptions = (0, react_1.useContext)(ConfigProvider_1.ConfigContext).useSearchParams;
66
+ var _b = options || defaultConfigOptions || {}, _c = _b.storage, storage = _c === void 0 ? (defaultConfigOptions === null || defaultConfigOptions === void 0 ? void 0 : defaultConfigOptions.storage) || 'state' : _c, _d = _b.stateKey, stateKey = _d === void 0 ? (defaultConfigOptions === null || defaultConfigOptions === void 0 ? void 0 : defaultConfigOptions.stateKey) || '__searchParams__' : _d;
67
+ var update = (0, ahooks_1.useUpdate)();
68
+ var navigator = (0, react_router_dom_1.useNavigate)();
69
+ var location = (0, react_router_dom_1.useLocation)();
70
+ var _e = (0, use_url_state_1.default)((0, util_1.encodeParams)(__assign(__assign({}, initialSearchParams), initialValue)), {
71
+ navigateMode: 'replace'
72
+ }), urlParams = _e[0], setUrlParams = _e[1];
73
+ var initialDecodedParams = storage === 'url'
74
+ ? (0, util_1.decodeParams)(urlParams)
75
+ : (((_a = location.state) === null || _a === void 0 ? void 0 : _a[stateKey]) || {});
76
+ var searchParams = (0, react_1.useRef)(__assign(__assign({}, initialValue), initialDecodedParams)).current;
77
+ /**
78
+ * 设置筛选参数
79
+ */
80
+ var setSearchParams = function (value) {
81
+ if (typeof value === 'function') {
82
+ Object.assign(searchParams, value(searchParams));
83
+ }
84
+ else {
85
+ Object.assign(searchParams, value);
86
+ }
87
+ update();
88
+ };
89
+ /**
90
+ * 根据配置更新存储
91
+ */
92
+ var persistParams = function (params) { return __awaiter(void 0, void 0, void 0, function () {
93
+ var payload;
94
+ var _a;
95
+ return __generator(this, function (_b) {
96
+ payload = __assign(__assign({}, params), { y: window.scrollY });
97
+ if (storage === 'url') {
98
+ setUrlParams((0, util_1.encodeParams)(payload));
99
+ return [2 /*return*/];
100
+ }
101
+ navigator(location.pathname, {
102
+ replace: true,
103
+ state: __assign(__assign({}, location.state), (_a = {}, _a[stateKey] = payload, _a))
104
+ });
105
+ return [2 /*return*/];
106
+ });
107
+ }); };
108
+ /**
109
+ * 清除 URL Params
110
+ */
111
+ var clearUrlParams = function () {
112
+ if (storage !== 'url')
113
+ return;
114
+ var params = (0, util_1.decodeParams)(urlParams);
115
+ Object.keys(params).forEach(function (key) {
116
+ params[key] = undefined;
117
+ });
118
+ setUrlParams(params);
119
+ };
120
+ (0, react_1.useEffect)(function () {
121
+ /**
122
+ * 初始化时清除 URL Params
123
+ */
124
+ if (storage === 'url' && urlParams) {
125
+ clearUrlParams();
126
+ }
127
+ else if (storage === 'state') {
128
+ persistParams({}).catch(lodash_es_1.noop);
129
+ }
130
+ }, []);
131
+ var targetLocationRef = (0, react_1.useRef)(null);
132
+ var navEndRef = (0, react_1.useRef)(false);
133
+ /**
134
+ * 自动保存状态:在路由跳转前自动保存 searchParams 和滚动位置
135
+ * useBlocker 会在导航发生前被调用,此时可以保存当前页面的状态
136
+ * useBlocker return true 表示 “阻止路由跳转”,return false 表示 “允许路由跳转”
137
+ */
138
+ (0, react_router_dom_1.useBlocker)(function (_a) {
139
+ var currentLocation = _a.currentLocation, nextLocation = _a.nextLocation, historyAction = _a.historyAction;
140
+ if (currentLocation.pathname !==
141
+ nextLocation.pathname) {
142
+ if (storage === 'url') {
143
+ if (currentLocation.search) {
144
+ return false;
145
+ }
146
+ targetLocationRef.current = __assign(__assign({}, nextLocation), { historyAction: historyAction });
147
+ // 同步保存状态(不等待,因为这是阻塞性的)
148
+ persistParams(searchParams).catch(lodash_es_1.noop);
149
+ return true;
150
+ }
151
+ if (navEndRef.current) {
152
+ return false;
153
+ }
154
+ targetLocationRef.current = __assign(__assign({}, nextLocation), { historyAction: historyAction });
155
+ persistParams(searchParams).catch(lodash_es_1.noop);
156
+ return true;
157
+ }
158
+ else if (nextLocation.pathname === currentLocation.pathname &&
159
+ targetLocationRef.current !== null && ((storage === 'url' &&
160
+ nextLocation.search !== currentLocation.search) || (storage === 'state' &&
161
+ !navEndRef.current))) {
162
+ setTimeout(function () {
163
+ var targetLocation = targetLocationRef.current;
164
+ var path = "".concat(targetLocation.pathname).concat(targetLocation.search);
165
+ navigator(path, {
166
+ replace: targetLocation.historyAction !== 'PUSH',
167
+ state: targetLocation.state
168
+ });
169
+ navEndRef.current = false;
170
+ targetLocationRef.current = null;
171
+ }, 0);
172
+ navEndRef.current = true;
173
+ return false;
174
+ }
175
+ return false;
176
+ });
177
+ return {
178
+ searchParams: searchParams,
179
+ setSearchParams: setSearchParams
180
+ };
181
+ };
182
+ exports.default = useSearchParams;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });