rxtutils 1.1.4-beta.8 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/cjs/_utils/defaultEquals.cjs +4 -19
  2. package/cjs/cache/index.cjs +137 -163
  3. package/cjs/cache/indexDB.cjs +86 -99
  4. package/cjs/downloadBlob.cjs +14 -0
  5. package/cjs/hooks/index.cjs +3 -6
  6. package/cjs/hooks/useCombineControlValue.cjs +30 -41
  7. package/cjs/index.cjs +16 -17
  8. package/cjs/request/defaultHandlers.cjs +5 -27
  9. package/cjs/request/error.cjs +18 -30
  10. package/cjs/request/index.cjs +208 -144
  11. package/cjs/store/createGetter/index.cjs +26 -40
  12. package/cjs/store/createStateStore/index.cjs +47 -90
  13. package/cjs/store/index.cjs +4 -7
  14. package/cjs/validator/decorators.cjs +74 -221
  15. package/cjs/validator/index.cjs +4 -7
  16. package/cjs/validator/validator.cjs +101 -177
  17. package/es/_utils/defaultEquals.mjs +4 -17
  18. package/es/cache/index.d.ts +7 -11
  19. package/es/cache/index.mjs +139 -160
  20. package/es/cache/indexDB.d.ts +1 -3
  21. package/es/cache/indexDB.mjs +87 -98
  22. package/es/downloadBlob.d.ts +8 -0
  23. package/es/downloadBlob.mjs +14 -0
  24. package/es/hooks/index.d.ts +1 -1
  25. package/es/hooks/index.mjs +4 -1
  26. package/es/hooks/useCombineControlValue.d.ts +5 -8
  27. package/es/hooks/useCombineControlValue.mjs +31 -40
  28. package/es/index.d.ts +28 -8
  29. package/es/index.mjs +29 -7
  30. package/es/request/defaultHandlers.d.ts +24 -0
  31. package/es/request/defaultHandlers.mjs +8 -26
  32. package/es/request/error.d.ts +3 -6
  33. package/es/request/error.mjs +18 -28
  34. package/es/request/index.d.ts +35 -16
  35. package/es/request/index.mjs +208 -142
  36. package/es/store/createGetter/index.d.ts +6 -10
  37. package/es/store/createGetter/index.mjs +28 -39
  38. package/es/store/createStateStore/index.d.ts +9 -9
  39. package/es/store/createStateStore/index.mjs +49 -87
  40. package/es/store/index.d.ts +4 -2
  41. package/es/store/index.mjs +7 -2
  42. package/es/validator/decorators.d.ts +12 -21
  43. package/es/validator/decorators.mjs +84 -219
  44. package/es/validator/index.d.ts +2 -2
  45. package/es/validator/index.mjs +16 -2
  46. package/es/validator/validator.d.ts +5 -6
  47. package/es/validator/validator.mjs +102 -176
  48. package/package.json +62 -13
  49. package/cjs/cache/index.d.ts +0 -141
  50. package/cjs/cache/indexDB.d.ts +0 -52
  51. package/cjs/hooks/index.d.ts +0 -1
  52. package/cjs/hooks/useCombineControlValue.d.ts +0 -21
  53. package/cjs/index.d.ts +0 -8
  54. package/cjs/request/error.d.ts +0 -31
  55. package/cjs/request/index.d.ts +0 -140
  56. package/cjs/store/createGetter/index.d.ts +0 -30
  57. package/cjs/store/createStateStore/index.d.ts +0 -42
  58. package/cjs/store/index.d.ts +0 -2
  59. package/cjs/validator/decorators.d.ts +0 -159
  60. package/cjs/validator/index.d.ts +0 -2
  61. package/cjs/validator/validator.d.ts +0 -84
@@ -1,21 +1,6 @@
1
- 'use strict';
2
-
3
- /**
4
- * 默认的相等性比较函数
5
- * 通过将两个值序列化为 JSON 字符串来比较它们是否相等
6
- *
7
- * @template Param 比较值的类型,默认为 any
8
- * @param prev 前一个值
9
- * @param next 后一个值
10
- * @returns {boolean} 如果两个值相等则返回 true,否则返回 false
11
- *
12
- * @remarks
13
- * - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
14
- * - 不适用于包含函数、undefined、Symbol 等无法序列化的值
15
- * - 对于循环引用的对象会抛出错误
16
- */
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
17
3
  function defaultEquals(prev, next) {
18
- return JSON.stringify(prev) === JSON.stringify(next);
4
+ return JSON.stringify(prev) === JSON.stringify(next);
19
5
  }
20
-
21
- module.exports = defaultEquals;
6
+ exports.default = defaultEquals;
@@ -1,170 +1,144 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var tslib = require('tslib');
6
- var moment = require('moment');
7
- var indexDB = require('./indexDB.cjs');
8
- var defaultEquals = require('../_utils/defaultEquals.cjs');
9
-
10
- /** 存储类型映射表 */
11
- var StorageMap = {
12
- localStorage: localStorage,
13
- sessionStorage: sessionStorage,
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const moment = require("moment");
4
+ const indexDB = require("./indexDB.cjs");
5
+ const defaultEquals = require("../_utils/defaultEquals.cjs");
6
+ const StorageMap = {
7
+ localStorage,
8
+ sessionStorage
14
9
  };
15
- /**
16
- * 缓存类
17
- * @template Param 缓存参数类型
18
- * @template Data 缓存数据类型
19
- */
20
- var Cache = /** @class */ (function () {
21
- /**
22
- * 构造函数
23
- * @param cacheType 存储类型
24
- * @param cacheKey 缓存键名
25
- * @param cacheTime 缓存时间(秒)
26
- * @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
27
- * @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
28
- */
29
- function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
30
- if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
31
- if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
32
- /** 内存中的缓存数组 */
33
- this.cache = [];
34
- this.cacheOptions = {
35
- storageType: cacheType,
36
- cacheKey: cacheKey,
37
- cacheTime: cacheTime,
38
- indexDBName: indexDBName,
39
- cacheKeyEquals: cacheKeyEquals,
40
- };
41
- if (cacheType === "indexedDB") {
42
- this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
43
- }
44
- else if (typeof cacheType === "string") {
45
- this.storage = StorageMap[cacheType];
46
- }
47
- this._init();
48
- }
49
- /**
50
- * 初始化缓存
51
- * 从存储中加载已保存的缓存数据,并进行解析和过期处理
52
- * @private
53
- */
54
- Cache.prototype._init = function () {
55
- return tslib.__awaiter(this, void 0, void 0, function () {
56
- var _a, cacheType, cacheKey, _b, _c, _d;
57
- return tslib.__generator(this, function (_e) {
58
- switch (_e.label) {
59
- case 0:
60
- _a = this.cacheOptions, cacheType = _a.storageType, cacheKey = _a.cacheKey;
61
- if (!(this.storage instanceof indexDB.IndexedDBStorage)) return [3 /*break*/, 2];
62
- _b = this;
63
- _d = (_c = JSON).parse;
64
- return [4 /*yield*/, this.storage.getItem(cacheKey)];
65
- case 1:
66
- _b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
67
- return [3 /*break*/, 3];
68
- case 2:
69
- if (this.storage instanceof Storage) {
70
- this.storage = StorageMap[cacheType];
71
- if (this.storage) {
72
- if (typeof cacheKey === "string") {
73
- try {
74
- this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
75
- }
76
- catch (e) {
77
- this.cache = [];
78
- console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
79
- }
80
- }
81
- }
82
- }
83
- _e.label = 3;
84
- case 3:
85
- this._filterExpired();
86
- this._saveToStorage();
87
- return [2 /*return*/];
88
- }
89
- });
90
- });
91
- };
92
- /**
93
- * 过滤掉已过期的缓存项
94
- * 通过比较当前时间和过期时间,移除过期的缓存项
95
- * @private
96
- */
97
- Cache.prototype._filterExpired = function () {
98
- var newCache = this.cache.filter(function (item) {
99
- return moment(item.expireTime).isAfter(moment());
100
- });
101
- this.cache = newCache;
10
+ class Cache {
11
+ /**
12
+ * 构造函数
13
+ * @param cacheType 存储类型
14
+ * @param cacheKey 缓存键名
15
+ * @param cacheTime 缓存时间(秒)
16
+ * @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
17
+ * @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
18
+ */
19
+ constructor(cacheType, cacheKey, cacheTime, indexDBName = "__apiCacheDatabase__", cacheKeyEquals = defaultEquals.default) {
20
+ this.cache = [];
21
+ this.cacheOptions = {
22
+ storageType: cacheType,
23
+ cacheKey,
24
+ cacheTime,
25
+ indexDBName,
26
+ cacheKeyEquals
102
27
  };
103
- /**
104
- * 将当前缓存数据保存到存储中
105
- * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
106
- * @private
107
- */
108
- Cache.prototype._saveToStorage = function () {
109
- if (this.storage) {
110
- if (typeof this.cacheOptions.cacheKey === "string") {
111
- this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
112
- }
113
- }
114
- };
115
- /**
116
- * 设置缓存数据
117
- * @param params 缓存的参数
118
- * @param data 要缓存的数据
119
- * @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
120
- */
121
- Cache.prototype.setCache = function (params, data, cacheOptions) {
122
- var _a = tslib.__assign(tslib.__assign({}, this.cacheOptions), cacheOptions), cacheTime = _a.cacheTime, _b = _a.cacheKeyEquals, cacheKeyEquals = _b === void 0 ? defaultEquals : _b;
123
- var cacheItemIndex = this.cache.findIndex(function (item) {
124
- return cacheKeyEquals(item.params, params);
125
- });
126
- if (cacheItemIndex > -1) {
127
- this.cache.splice(cacheItemIndex, 1);
128
- }
129
- this.cache.push({
130
- params: params,
131
- data: data,
132
- expireTime: moment().add(cacheTime, "seconds").toJSON(),
133
- });
134
- this._saveToStorage();
135
- };
136
- /**
137
- * 获取缓存数据
138
- * @param params 查询参数
139
- * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
140
- */
141
- Cache.prototype.getCache = function (params) {
142
- var _this = this;
143
- var itemIndex = this.cache.findIndex(function (item) {
144
- return _this.cacheOptions.cacheKeyEquals(item.params, params);
145
- });
146
- var item = this.cache[itemIndex];
147
- if (item) {
148
- if (moment(item.expireTime).isAfter(moment())) {
149
- return item.data;
150
- }
151
- else {
152
- this.cache.splice(itemIndex, 1);
153
- this._saveToStorage();
154
- }
28
+ if (cacheType === "indexedDB") {
29
+ this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
30
+ } else if (typeof cacheType === "string") {
31
+ this.storage = StorageMap[cacheType];
32
+ }
33
+ this._init();
34
+ }
35
+ /**
36
+ * 初始化缓存
37
+ * 从存储中加载已保存的缓存数据,并进行解析和过期处理
38
+ * @private
39
+ */
40
+ async _init() {
41
+ const { storageType: cacheType, cacheKey } = this.cacheOptions;
42
+ if (this.storage instanceof indexDB.IndexedDBStorage) {
43
+ this.cache = JSON.parse(
44
+ await this.storage.getItem(cacheKey) || "[]"
45
+ );
46
+ } else if (this.storage instanceof Storage) {
47
+ this.storage = StorageMap[cacheType];
48
+ if (this.storage) {
49
+ if (typeof cacheKey === "string") {
50
+ try {
51
+ this.cache = JSON.parse(
52
+ this.storage.getItem(cacheKey) || "[]"
53
+ );
54
+ } catch (e) {
55
+ this.cache = [];
56
+ console.error(`缓存数据解析失败,key:${cacheKey}`);
57
+ }
155
58
  }
156
- return null;
59
+ }
60
+ }
61
+ this._filterExpired();
62
+ this._saveToStorage();
63
+ }
64
+ /**
65
+ * 过滤掉已过期的缓存项
66
+ * 通过比较当前时间和过期时间,移除过期的缓存项
67
+ * @private
68
+ */
69
+ _filterExpired() {
70
+ const newCache = this.cache.filter((item) => {
71
+ return moment(item.expireTime).isAfter(moment());
72
+ });
73
+ this.cache = newCache;
74
+ }
75
+ /**
76
+ * 将当前缓存数据保存到存储中
77
+ * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
78
+ * @private
79
+ */
80
+ _saveToStorage() {
81
+ if (this.storage) {
82
+ if (typeof this.cacheOptions.cacheKey === "string") {
83
+ this.storage.setItem(
84
+ this.cacheOptions.cacheKey,
85
+ JSON.stringify(this.cache)
86
+ );
87
+ }
88
+ }
89
+ }
90
+ /**
91
+ * 设置缓存数据
92
+ * @param params 缓存的参数
93
+ * @param data 要缓存的数据
94
+ * @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
95
+ */
96
+ setCache(params, data, cacheOptions) {
97
+ const { cacheTime, cacheKeyEquals = defaultEquals.default } = {
98
+ ...this.cacheOptions,
99
+ ...cacheOptions
157
100
  };
158
- /**
159
- * 清空所有缓存数据
160
- * 清空内存中的缓存数组并同步到存储中
161
- */
162
- Cache.prototype.clear = function () {
163
- this.cache = [];
101
+ const cacheItemIndex = this.cache.findIndex((item) => {
102
+ return cacheKeyEquals(item.params, params);
103
+ });
104
+ if (cacheItemIndex > -1) {
105
+ this.cache.splice(cacheItemIndex, 1);
106
+ }
107
+ this.cache.push({
108
+ params,
109
+ data,
110
+ expireTime: moment().add(cacheTime, "seconds").toJSON()
111
+ });
112
+ this._saveToStorage();
113
+ }
114
+ /**
115
+ * 获取缓存数据
116
+ * @param params 查询参数
117
+ * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
118
+ */
119
+ getCache(params) {
120
+ const itemIndex = this.cache.findIndex((item2) => {
121
+ return this.cacheOptions.cacheKeyEquals(item2.params, params);
122
+ });
123
+ const item = this.cache[itemIndex];
124
+ if (item) {
125
+ if (moment(item.expireTime).isAfter(moment())) {
126
+ return item.data;
127
+ } else {
128
+ this.cache.splice(itemIndex, 1);
164
129
  this._saveToStorage();
165
- };
166
- return Cache;
167
- }());
168
-
130
+ }
131
+ }
132
+ return null;
133
+ }
134
+ /**
135
+ * 清空所有缓存数据
136
+ * 清空内存中的缓存数组并同步到存储中
137
+ */
138
+ clear() {
139
+ this.cache = [];
140
+ this._saveToStorage();
141
+ }
142
+ }
169
143
  exports.StorageMap = StorageMap;
170
144
  exports.default = Cache;
@@ -1,100 +1,87 @@
1
- 'use strict';
2
-
3
- /**
4
- * IndexedDB 存储类
5
- * 提供了对 IndexedDB 数据库操作的简单封装
6
- */
7
- var IndexedDBStorage = /** @class */ (function () {
8
- /**
9
- * 构造函数
10
- * @param dbName 数据库名称
11
- * @param storeName 存储对象名称
12
- */
13
- function IndexedDBStorage(dbName, storeName) {
14
- /** 数据库连接实例 */
15
- this.db = null;
16
- this.dbName = dbName;
17
- this.storeName = storeName;
18
- }
19
- /**
20
- * 打开数据库连接
21
- * 如果数据库不存在则创建新的数据库和存储对象
22
- * @private
23
- * @returns Promise<IDBDatabase> 数据库连接实例
24
- */
25
- IndexedDBStorage.prototype._open = function () {
26
- var _this = this;
27
- return new Promise(function (resolve, reject) {
28
- if (_this.db) {
29
- resolve(_this.db);
30
- return;
31
- }
32
- var request = indexedDB.open(_this.dbName);
33
- request.onupgradeneeded = function (event) {
34
- var db = event.target.result;
35
- db.createObjectStore(_this.storeName, { keyPath: 'key' });
36
- };
37
- request.onerror = function () {
38
- var _a;
39
- reject("IndexedDB open request error: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message));
40
- };
41
- request.onsuccess = function (event) {
42
- _this.db = event.target.result;
43
- resolve(_this.db);
44
- };
45
- });
46
- };
47
- /**
48
- * 获取存储对象
49
- * @param mode 事务模式,默认为只读模式
50
- * - readonly: 只读模式
51
- * - readwrite: 读写模式
52
- * @private
53
- * @returns Promise<IDBObjectStore> 存储对象实例
54
- */
55
- IndexedDBStorage.prototype._getStore = function (mode) {
56
- var _this = this;
57
- if (mode === void 0) { mode = 'readonly'; }
58
- return this._open().then(function (db) {
59
- var transaction = db.transaction(_this.storeName, mode);
60
- return transaction.objectStore(_this.storeName);
61
- });
62
- };
63
- /**
64
- * 设置键值对
65
- * @param key 键名
66
- * @param value 要存储的值
67
- * @returns Promise<void> 存储操作的结果
68
- * @throws 当存储操作失败时抛出错误
69
- */
70
- IndexedDBStorage.prototype.setItem = function (key, value) {
71
- return this._getStore('readwrite').then(function (store) {
72
- return new Promise(function (resolve, reject) {
73
- var request = store.put({ key: key, value: value });
74
- request.onsuccess = function () { return resolve(); };
75
- request.onerror = function () { var _a; return reject("Could not set the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
76
- });
77
- });
78
- };
79
- /**
80
- * 获取键对应的值
81
- * @param key 要获取的键名
82
- * @returns Promise<T> 返回存储的值,如果不存在则返回 undefined
83
- * @throws 当获取操作失败时抛出错误
84
- * @template T 存储值的类型,默认为 any
85
- */
86
- IndexedDBStorage.prototype.getItem = function (key) {
87
- return this._getStore().then(function (store) {
88
- return new Promise(function (resolve, reject) {
89
- var request = store.get(key);
90
- request.onsuccess = function () {
91
- return resolve(request.result ? request.result.value : undefined);
92
- };
93
- request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
94
- });
95
- });
96
- };
97
- return IndexedDBStorage;
98
- }());
99
-
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class IndexedDBStorage {
4
+ /**
5
+ * 构造函数
6
+ * @param dbName 数据库名称
7
+ * @param storeName 存储对象名称
8
+ */
9
+ constructor(dbName, storeName) {
10
+ this.db = null;
11
+ this.dbName = dbName;
12
+ this.storeName = storeName;
13
+ }
14
+ /**
15
+ * 打开数据库连接
16
+ * 如果数据库不存在则创建新的数据库和存储对象
17
+ * @private
18
+ * @returns Promise<IDBDatabase> 数据库连接实例
19
+ */
20
+ _open() {
21
+ return new Promise((resolve, reject) => {
22
+ if (this.db) {
23
+ resolve(this.db);
24
+ return;
25
+ }
26
+ const request = indexedDB.open(this.dbName);
27
+ request.onupgradeneeded = (event) => {
28
+ const db = event.target.result;
29
+ db.createObjectStore(this.storeName, { keyPath: "key" });
30
+ };
31
+ request.onerror = () => {
32
+ reject(`IndexedDB open request error: ${request.error?.message}`);
33
+ };
34
+ request.onsuccess = (event) => {
35
+ this.db = event.target.result;
36
+ resolve(this.db);
37
+ };
38
+ });
39
+ }
40
+ /**
41
+ * 获取存储对象
42
+ * @param mode 事务模式,默认为只读模式
43
+ * - readonly: 只读模式
44
+ * - readwrite: 读写模式
45
+ * @private
46
+ * @returns Promise<IDBObjectStore> 存储对象实例
47
+ */
48
+ _getStore(mode = "readonly") {
49
+ return this._open().then((db) => {
50
+ const transaction = db.transaction(this.storeName, mode);
51
+ return transaction.objectStore(this.storeName);
52
+ });
53
+ }
54
+ /**
55
+ * 设置键值对
56
+ * @param key 键名
57
+ * @param value 要存储的值
58
+ * @returns Promise<void> 存储操作的结果
59
+ * @throws 当存储操作失败时抛出错误
60
+ */
61
+ setItem(key, value) {
62
+ return this._getStore("readwrite").then((store) => {
63
+ return new Promise((resolve, reject) => {
64
+ const request = store.put({ key, value });
65
+ request.onsuccess = () => resolve();
66
+ request.onerror = () => reject(`Could not set the item: ${request.error?.message}`);
67
+ });
68
+ });
69
+ }
70
+ /**
71
+ * 获取键对应的值
72
+ * @param key 要获取的键名
73
+ * @returns Promise<T> 返回存储的值,如果不存在则返回 undefined
74
+ * @throws 当获取操作失败时抛出错误
75
+ * @template T 存储值的类型,默认为 any
76
+ */
77
+ getItem(key) {
78
+ return this._getStore().then((store) => {
79
+ return new Promise((resolve, reject) => {
80
+ const request = store.get(key);
81
+ request.onsuccess = () => resolve(request.result ? request.result.value : void 0);
82
+ request.onerror = () => reject(`Could not get the item: ${request.error?.message}`);
83
+ });
84
+ });
85
+ }
86
+ }
100
87
  exports.IndexedDBStorage = IndexedDBStorage;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const downloadBlob = (blob, fileName, autoRevoke = true) => {
4
+ const url = URL.createObjectURL(blob);
5
+ const a = document.createElement("a");
6
+ a.href = url;
7
+ a.download = fileName;
8
+ a.click();
9
+ if (autoRevoke) {
10
+ URL.revokeObjectURL(url);
11
+ }
12
+ return url;
13
+ };
14
+ exports.downloadBlob = downloadBlob;
@@ -1,7 +1,4 @@
1
- 'use strict';
2
-
3
- var useCombineControlValue = require('./useCombineControlValue.cjs');
4
-
5
-
6
-
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const useCombineControlValue = require("./useCombineControlValue.cjs");
7
4
  exports.useCombineControlValue = useCombineControlValue.useCombineControlValue;
@@ -1,43 +1,32 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
-
5
- /**
6
- * @param param props 组件属性
7
- * @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
8
- * @param param defaultValue 默认值,当 value 和 defaultValue 同时存在时,以 value 为默认值
9
- * @param param onChange 值改变时的回调
10
- * @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
11
- */
12
- function useCombineControlValue(_a, resolveFn) {
13
- var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
14
- var _c = props, _d = valueKey, value = _c[_d];
15
- var hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
16
- var _e = react.useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
17
- var handleChange = react.useCallback(function () {
18
- var params = [];
19
- for (var _i = 0; _i < arguments.length; _i++) {
20
- params[_i] = arguments[_i];
21
- }
22
- var realNextVal;
23
- if (typeof resolveFn === 'function') {
24
- realNextVal = resolveFn.apply(void 0, params);
25
- }
26
- else {
27
- realNextVal = params[0];
28
- }
29
- setInternalValue(realNextVal);
30
- onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, params);
31
- }, [onChange, resolveFn]);
32
- var finalValue = react.useMemo(function () {
33
- if (hasValue)
34
- return value;
35
- return internalValue;
36
- }, [hasValue, internalValue, value]);
37
- return {
38
- value: finalValue,
39
- onChange: handleChange
40
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const react = require("react");
4
+ function useCombineControlValue({
5
+ props,
6
+ valueKey = "value",
7
+ defaultValue,
8
+ onChange
9
+ }, resolveFn) {
10
+ const { [valueKey]: value } = props;
11
+ const hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
12
+ const [internalValue, setInternalValue] = react.useState(value ?? defaultValue);
13
+ const handleChange = react.useCallback((...params) => {
14
+ let realNextVal;
15
+ if (typeof resolveFn === "function") {
16
+ realNextVal = resolveFn(...params);
17
+ } else {
18
+ realNextVal = params[0];
19
+ }
20
+ setInternalValue(realNextVal);
21
+ onChange?.(...params);
22
+ }, [onChange, resolveFn]);
23
+ const finalValue = react.useMemo(() => {
24
+ if (hasValue) return value;
25
+ return internalValue;
26
+ }, [hasValue, internalValue, value]);
27
+ return {
28
+ value: finalValue,
29
+ onChange: handleChange
30
+ };
41
31
  }
42
-
43
32
  exports.useCombineControlValue = useCombineControlValue;