@hairy/react-lib 1.1.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{LICENSE → LICENSE.md} +1 -3
- package/README.md +36 -0
- package/dist/index.cjs +73 -184
- package/dist/index.d.ts +77 -139
- package/dist/index.global.js +526 -0
- package/dist/{index.mjs → index.js} +61 -166
- package/package.json +39 -11
|
@@ -33,7 +33,6 @@ function If(props) {
|
|
|
33
33
|
// src/components/condition/Switch.ts
|
|
34
34
|
import { Children as Children2, isValidElement } from "react";
|
|
35
35
|
function Switch(props) {
|
|
36
|
-
var _a;
|
|
37
36
|
const isUseValue = props.value !== void 0;
|
|
38
37
|
let matchingCase;
|
|
39
38
|
let defaultCase;
|
|
@@ -50,7 +49,7 @@ function Switch(props) {
|
|
|
50
49
|
if (!defaultCase && child.type === Default)
|
|
51
50
|
defaultCase = child;
|
|
52
51
|
});
|
|
53
|
-
return
|
|
52
|
+
return matchingCase ?? defaultCase ?? null;
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
// src/components/condition/Unless.ts
|
|
@@ -80,15 +79,15 @@ function repack(c) {
|
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
// src/components/utils/Trans.ts
|
|
83
|
-
import { useTranslation } from "react-i18next";
|
|
84
|
-
import { createElement as createElement2 } from "react";
|
|
85
82
|
import HTML from "html-parse-stringify";
|
|
83
|
+
import { createElement as createElement2 } from "react";
|
|
84
|
+
import { useTranslation } from "react-i18next";
|
|
86
85
|
function Trans({ i18nKey, ...additionalProps }) {
|
|
87
86
|
const translation = useTranslation().t(i18nKey, additionalProps);
|
|
88
87
|
return renderNodes(HTML.parse(translation), additionalProps);
|
|
89
88
|
}
|
|
90
|
-
var index = 0;
|
|
91
89
|
function renderNodes(tokens, values) {
|
|
90
|
+
let index = 0;
|
|
92
91
|
return tokens.map((token) => {
|
|
93
92
|
if (token.type === "text")
|
|
94
93
|
return token.content;
|
|
@@ -98,10 +97,38 @@ function renderNodes(tokens, values) {
|
|
|
98
97
|
});
|
|
99
98
|
}
|
|
100
99
|
|
|
100
|
+
// src/hooks/useAsyncCallback.ts
|
|
101
|
+
import { useState } from "react";
|
|
102
|
+
function useAsyncCallback(fun) {
|
|
103
|
+
const [error, setError] = useState();
|
|
104
|
+
const [loading, setLoading] = useState(false);
|
|
105
|
+
async function execute(...args) {
|
|
106
|
+
try {
|
|
107
|
+
setLoading(true);
|
|
108
|
+
const result = await fun(...args);
|
|
109
|
+
setLoading(false);
|
|
110
|
+
return result;
|
|
111
|
+
} catch (error2) {
|
|
112
|
+
setLoading(false);
|
|
113
|
+
setError(error2);
|
|
114
|
+
throw error2;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return [loading, execute, error];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/hooks/useAsyncState.ts
|
|
121
|
+
import { useAsyncFn, useMount } from "react-use";
|
|
122
|
+
function useAsyncState(fn, deps, options) {
|
|
123
|
+
const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
|
|
124
|
+
useMount(() => options?.immediate && _fn());
|
|
125
|
+
return [state, _fn];
|
|
126
|
+
}
|
|
127
|
+
|
|
101
128
|
// src/hooks/useDebounce.ts
|
|
102
|
-
import { useEffect, useState } from "react";
|
|
129
|
+
import { useEffect, useState as useState2 } from "react";
|
|
103
130
|
function useDebounce(value, delay) {
|
|
104
|
-
const [debouncedValue, setDebouncedValue] =
|
|
131
|
+
const [debouncedValue, setDebouncedValue] = useState2(value);
|
|
105
132
|
useEffect(() => {
|
|
106
133
|
const handler = setTimeout(() => setDebouncedValue(value), delay);
|
|
107
134
|
return () => clearTimeout(handler);
|
|
@@ -110,8 +137,8 @@ function useDebounce(value, delay) {
|
|
|
110
137
|
}
|
|
111
138
|
|
|
112
139
|
// src/hooks/useEventBus.ts
|
|
113
|
-
import { useEffect as useEffect2, useRef } from "react";
|
|
114
140
|
import mitt from "mitt";
|
|
141
|
+
import { useEffect as useEffect2, useRef } from "react";
|
|
115
142
|
var emitter = mitt();
|
|
116
143
|
function useEventBus(key) {
|
|
117
144
|
const onRef = useRef();
|
|
@@ -140,10 +167,34 @@ function useEventBus(key) {
|
|
|
140
167
|
};
|
|
141
168
|
}
|
|
142
169
|
|
|
170
|
+
// src/hooks/useFetchIntercept.ts
|
|
171
|
+
import { useMount as useMount2 } from "react-use";
|
|
172
|
+
function useFetchResponseIntercept(intercept) {
|
|
173
|
+
useMount2(() => fetchResponseIntercept(intercept));
|
|
174
|
+
}
|
|
175
|
+
function useFetchRequestIntercept(intercept) {
|
|
176
|
+
useMount2(() => fetchRequestIntercept(intercept));
|
|
177
|
+
}
|
|
178
|
+
function fetchResponseIntercept(intercept) {
|
|
179
|
+
const { fetch: originalFetch } = window;
|
|
180
|
+
window.fetch = async (...args) => {
|
|
181
|
+
const [resource, config] = args;
|
|
182
|
+
const response = await originalFetch(resource, config);
|
|
183
|
+
return intercept(response);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function fetchRequestIntercept(intercept) {
|
|
187
|
+
const { fetch: originalFetch } = window;
|
|
188
|
+
window.fetch = async (...args) => {
|
|
189
|
+
const [resource, config] = args;
|
|
190
|
+
return intercept(originalFetch, resource, config);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
143
194
|
// src/hooks/useMounted.ts
|
|
144
|
-
import { useEffect as useEffect3, useState as
|
|
195
|
+
import { useEffect as useEffect3, useState as useState3 } from "react";
|
|
145
196
|
function useMounted() {
|
|
146
|
-
const [mounted, setMounted] =
|
|
197
|
+
const [mounted, setMounted] = useState3(false);
|
|
147
198
|
useEffect3(() => setMounted(true), []);
|
|
148
199
|
return mounted;
|
|
149
200
|
}
|
|
@@ -178,156 +229,6 @@ function useWhenever(source, cb, options) {
|
|
|
178
229
|
useWatch(source, () => source && cb(source), options);
|
|
179
230
|
}
|
|
180
231
|
|
|
181
|
-
// src/hooks/useAsyncCallback.ts
|
|
182
|
-
import { useState as useState3 } from "react";
|
|
183
|
-
function useAsyncCallback(fun) {
|
|
184
|
-
const [error, setError] = useState3();
|
|
185
|
-
const [loading, setLoading] = useState3(false);
|
|
186
|
-
async function execute(...args) {
|
|
187
|
-
try {
|
|
188
|
-
setLoading(true);
|
|
189
|
-
const result = await fun(...args);
|
|
190
|
-
setLoading(false);
|
|
191
|
-
return result;
|
|
192
|
-
} catch (error2) {
|
|
193
|
-
setLoading(false);
|
|
194
|
-
setError(error2);
|
|
195
|
-
throw error2;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return [loading, execute, error];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/hooks/useAsyncState.ts
|
|
202
|
-
import { useAsyncFn, useMount } from "react-use";
|
|
203
|
-
function useAsyncState(fn, deps, options) {
|
|
204
|
-
const [state, _fn] = useAsyncFn(fn, deps, options == null ? void 0 : options.initial);
|
|
205
|
-
useMount(() => (options == null ? void 0 : options.immediate) && _fn());
|
|
206
|
-
return [state, _fn];
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// src/hooks/useFetchIntercept.ts
|
|
210
|
-
import { useMount as useMount2 } from "react-use";
|
|
211
|
-
function useFetchResponseIntercept(intercept) {
|
|
212
|
-
useMount2(() => fetchResponseIntercept(intercept));
|
|
213
|
-
}
|
|
214
|
-
function useFetchRequestIntercept(intercept) {
|
|
215
|
-
useMount2(() => fetchRequestIntercept(intercept));
|
|
216
|
-
}
|
|
217
|
-
function fetchResponseIntercept(intercept) {
|
|
218
|
-
const { fetch: originalFetch } = window;
|
|
219
|
-
window.fetch = async (...args) => {
|
|
220
|
-
const [resource, config] = args;
|
|
221
|
-
const response = await originalFetch(resource, config);
|
|
222
|
-
return intercept(response);
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function fetchRequestIntercept(intercept) {
|
|
226
|
-
const { fetch: originalFetch } = window;
|
|
227
|
-
window.fetch = async (...args) => {
|
|
228
|
-
const [resource, config] = args;
|
|
229
|
-
return intercept(originalFetch, resource, config);
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// src/storage/storeToState.ts
|
|
234
|
-
import { useSnapshot } from "valtio";
|
|
235
|
-
function storeToState(store, key) {
|
|
236
|
-
const snapshot = useSnapshot(store);
|
|
237
|
-
function get() {
|
|
238
|
-
return snapshot[key];
|
|
239
|
-
}
|
|
240
|
-
function set(value) {
|
|
241
|
-
store[key] = value;
|
|
242
|
-
}
|
|
243
|
-
return [get(), set];
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// src/storage/proxyWithPersistant.ts
|
|
247
|
-
import { proxy, subscribe } from "valtio";
|
|
248
|
-
function proxyWithPersistant(key, initialObject, options = {}) {
|
|
249
|
-
const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
|
|
250
|
-
const state = proxy(parse(storage == null ? void 0 : storage.getItem(key)) || initialObject);
|
|
251
|
-
subscribe(state, () => {
|
|
252
|
-
storage == null ? void 0 : storage.setItem(key, JSON.stringify(state));
|
|
253
|
-
});
|
|
254
|
-
return state;
|
|
255
|
-
}
|
|
256
|
-
function parse(text) {
|
|
257
|
-
try {
|
|
258
|
-
return JSON.parse(text || "");
|
|
259
|
-
} catch (e) {
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// src/storage/storeToStates.ts
|
|
264
|
-
import { useSnapshot as useSnapshot2 } from "valtio";
|
|
265
|
-
function storeToStates(store) {
|
|
266
|
-
const snapshot = useSnapshot2(store);
|
|
267
|
-
const states = {};
|
|
268
|
-
function toState(key) {
|
|
269
|
-
const get = () => snapshot[key];
|
|
270
|
-
const set = (value) => {
|
|
271
|
-
store[key] = value;
|
|
272
|
-
};
|
|
273
|
-
return [get(), set];
|
|
274
|
-
}
|
|
275
|
-
for (const key of Object.keys(snapshot))
|
|
276
|
-
states[key] = toState(key);
|
|
277
|
-
return states;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// src/storage/defineStore.ts
|
|
281
|
-
import { proxy as proxy2, useSnapshot as useSnapshot3 } from "valtio";
|
|
282
|
-
function defineStore(store, options = {}) {
|
|
283
|
-
const state = typeof store.state === "function" ? store.state() : store.state;
|
|
284
|
-
const actions = store.actions || {};
|
|
285
|
-
const $state = options.persistant ? proxyWithPersistant(options.persistant, state) : proxy2(state);
|
|
286
|
-
const $actions = {};
|
|
287
|
-
for (const key in actions)
|
|
288
|
-
$actions[key] = actions[key].bind($state);
|
|
289
|
-
return {
|
|
290
|
-
$state: state,
|
|
291
|
-
$actions: actions,
|
|
292
|
-
...actions
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
function useStore(store) {
|
|
296
|
-
return useSnapshot3(store.$state);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// src/hooks/defineAsyncStorage.ts
|
|
300
|
-
function defineAsyncStorage(options) {
|
|
301
|
-
const store = defineStore(
|
|
302
|
-
{
|
|
303
|
-
state: () => ({
|
|
304
|
-
promise: void 0,
|
|
305
|
-
value: options.initial,
|
|
306
|
-
loading: false,
|
|
307
|
-
error: void 0
|
|
308
|
-
})
|
|
309
|
-
},
|
|
310
|
-
{ persistant: options.persistant }
|
|
311
|
-
);
|
|
312
|
-
function use() {
|
|
313
|
-
const fn = options.setup();
|
|
314
|
-
const state = useStore(store);
|
|
315
|
-
function fetch(...args) {
|
|
316
|
-
if (state.loading)
|
|
317
|
-
return;
|
|
318
|
-
store.$state.loading = true;
|
|
319
|
-
store.$state.promise = fn(...args);
|
|
320
|
-
store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
|
|
321
|
-
store.$state.error = error;
|
|
322
|
-
throw error;
|
|
323
|
-
});
|
|
324
|
-
return store.$state.promise;
|
|
325
|
-
}
|
|
326
|
-
return [state, fetch];
|
|
327
|
-
}
|
|
328
|
-
return use;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
232
|
// src/utils/index.ts
|
|
332
233
|
var hasOwn = {}.hasOwnProperty;
|
|
333
234
|
function cls(...args) {
|
|
@@ -371,11 +272,6 @@ export {
|
|
|
371
272
|
Trans,
|
|
372
273
|
Unless,
|
|
373
274
|
cls,
|
|
374
|
-
defineAsyncStorage,
|
|
375
|
-
defineStore,
|
|
376
|
-
proxyWithPersistant,
|
|
377
|
-
storeToState,
|
|
378
|
-
storeToStates,
|
|
379
275
|
useAsyncCallback,
|
|
380
276
|
useAsyncState,
|
|
381
277
|
useDebounce,
|
|
@@ -383,7 +279,6 @@ export {
|
|
|
383
279
|
useFetchRequestIntercept,
|
|
384
280
|
useFetchResponseIntercept,
|
|
385
281
|
useMounted,
|
|
386
|
-
useStore,
|
|
387
282
|
useWatch,
|
|
388
283
|
useWhenever
|
|
389
284
|
};
|
package/package.json
CHANGED
|
@@ -1,31 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/react-lib",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.5.0",
|
|
5
|
+
"description": "Library for react",
|
|
6
|
+
"author": "Hairyf <wwu710632@gmail.com>",
|
|
4
7
|
"license": "MIT",
|
|
5
|
-
"
|
|
8
|
+
"funding": "https://github.com/sponsors/hairyf",
|
|
9
|
+
"homepage": "https://github.com/hairyf/hairylib#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/hairyf/hairylib.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/hairyf/hairylib/issues",
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.js",
|
|
6
18
|
"publishConfig": {
|
|
7
|
-
"jsdelivr": "./dist/index.
|
|
19
|
+
"jsdelivr": "./dist/index.global.js"
|
|
8
20
|
},
|
|
9
21
|
"files": [
|
|
10
22
|
"dist"
|
|
11
23
|
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": "^18.2.0",
|
|
26
|
+
"react-dom": "^18.2.0",
|
|
27
|
+
"react-i18next": "^14.1.2"
|
|
28
|
+
},
|
|
12
29
|
"dependencies": {
|
|
13
30
|
"html-parse-stringify": "^3.0.1",
|
|
14
31
|
"mitt": "^3.0.1",
|
|
15
|
-
"react": "^
|
|
16
|
-
"react-dom": "^18.2.0",
|
|
17
|
-
"react-i18next": "^14.1.2",
|
|
18
|
-
"valtio": "^1.13.0"
|
|
32
|
+
"react-use": "^17.6.0"
|
|
19
33
|
},
|
|
20
34
|
"devDependencies": {
|
|
21
35
|
"@types/node": "^20.11.7",
|
|
22
36
|
"@types/react": "^18.2.43",
|
|
23
|
-
"@types/react-dom": "^18.2.17"
|
|
37
|
+
"@types/react-dom": "^18.2.17",
|
|
38
|
+
"react": "^18.2.0",
|
|
39
|
+
"react-dom": "^18.2.0",
|
|
40
|
+
"react-i18next": "^14.1.2",
|
|
41
|
+
"@hairy/utils": "1.5.0"
|
|
24
42
|
},
|
|
25
43
|
"scripts": {
|
|
26
|
-
"build": "
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"dev": "tsup --watch",
|
|
46
|
+
"start": "tsx src/index.ts"
|
|
27
47
|
},
|
|
28
|
-
"module": "./dist/index.
|
|
48
|
+
"module": "./dist/index.js",
|
|
29
49
|
"types": "./dist/index.d.ts",
|
|
30
|
-
"unpkg": "./dist/index.
|
|
50
|
+
"unpkg": "./dist/index.global.js",
|
|
51
|
+
"exports": {
|
|
52
|
+
".": {
|
|
53
|
+
"import": "./dist/index.js",
|
|
54
|
+
"require": "./dist/index.cjs",
|
|
55
|
+
"types": "./dist/index.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"./*": "./*"
|
|
58
|
+
}
|
|
31
59
|
}
|