@hairy/react-lib 1.1.2 → 1.6.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.
@@ -0,0 +1,526 @@
1
+ "use strict";
2
+ var LibReact = (() => {
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __esm = (fn, res) => function __init() {
10
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
+ };
12
+ var __commonJS = (cb, mod) => function __require() {
13
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
14
+ };
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
28
+ // If the importer is in node compatibility mode or this is not an ESM
29
+ // file that has been converted to a CommonJS file using a Babel-
30
+ // compatible transform (i.e. "__esModule" has not been set), then set
31
+ // "default" to the CommonJS "module.exports" for node compatibility.
32
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
33
+ mod
34
+ ));
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+
37
+ // globals:react
38
+ var require_react = __commonJS({
39
+ "globals:react"(exports, module) {
40
+ "use strict";
41
+ module.exports = React;
42
+ }
43
+ });
44
+
45
+ // ../../node_modules/.pnpm/void-elements@3.1.0/node_modules/void-elements/index.js
46
+ var require_void_elements = __commonJS({
47
+ "../../node_modules/.pnpm/void-elements@3.1.0/node_modules/void-elements/index.js"(exports, module) {
48
+ "use strict";
49
+ module.exports = {
50
+ "area": true,
51
+ "base": true,
52
+ "br": true,
53
+ "col": true,
54
+ "embed": true,
55
+ "hr": true,
56
+ "img": true,
57
+ "input": true,
58
+ "link": true,
59
+ "meta": true,
60
+ "param": true,
61
+ "source": true,
62
+ "track": true,
63
+ "wbr": true
64
+ };
65
+ }
66
+ });
67
+
68
+ // globals:react-i18next
69
+ var require_react_i18next = __commonJS({
70
+ "globals:react-i18next"(exports, module) {
71
+ "use strict";
72
+ module.exports = reactI18next;
73
+ }
74
+ });
75
+
76
+ // ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
77
+ var __assign;
78
+ var init_tslib_es6 = __esm({
79
+ "../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs"() {
80
+ "use strict";
81
+ __assign = function() {
82
+ __assign = Object.assign || function __assign2(t2) {
83
+ for (var s2, i2 = 1, n2 = arguments.length; i2 < n2; i2++) {
84
+ s2 = arguments[i2];
85
+ for (var p in s2) if (Object.prototype.hasOwnProperty.call(s2, p)) t2[p] = s2[p];
86
+ }
87
+ return t2;
88
+ };
89
+ return __assign.apply(this, arguments);
90
+ };
91
+ }
92
+ });
93
+
94
+ // src/index.ts
95
+ var index_exports = {};
96
+ __export(index_exports, {
97
+ Case: () => Case,
98
+ Default: () => Default,
99
+ Else: () => Else,
100
+ If: () => If,
101
+ Injector: () => Injector,
102
+ Switch: () => Switch,
103
+ Then: () => Then,
104
+ Trans: () => Trans,
105
+ Unless: () => Unless,
106
+ cls: () => cls,
107
+ useAsyncCallback: () => useAsyncCallback,
108
+ useAsyncState: () => useAsyncState,
109
+ useDebounce: () => useDebounce,
110
+ useEventBus: () => useEventBus,
111
+ useFetchRequestIntercept: () => useFetchRequestIntercept,
112
+ useFetchResponseIntercept: () => useFetchResponseIntercept,
113
+ useMounted: () => useMounted,
114
+ useWatch: () => useWatch,
115
+ useWhenever: () => useWhenever
116
+ });
117
+
118
+ // src/components/condition/Case.ts
119
+ function Case(props) {
120
+ return props.children;
121
+ }
122
+
123
+ // src/components/condition/Default.ts
124
+ function Default(props) {
125
+ return props.children;
126
+ }
127
+
128
+ // src/components/condition/Else.ts
129
+ function Else(props) {
130
+ return props.children;
131
+ }
132
+
133
+ // src/components/condition/If.ts
134
+ var import_react = __toESM(require_react(), 1);
135
+
136
+ // src/components/condition/Then.ts
137
+ function Then(props) {
138
+ return props.children;
139
+ }
140
+
141
+ // src/components/condition/If.ts
142
+ function If(props) {
143
+ const children = props.then || props.children;
144
+ const elements = import_react.Children.toArray(children);
145
+ const thenChild = elements.find((c2) => c2.type === Then);
146
+ const elseChild = elements.find((c2) => c2.type === Else);
147
+ return thenChild || elseChild ? props.cond ? thenChild : elseChild : props.cond ? children : props.else;
148
+ }
149
+
150
+ // src/components/condition/Switch.ts
151
+ var import_react2 = __toESM(require_react(), 1);
152
+ function Switch(props) {
153
+ const isUseValue = props.value !== void 0;
154
+ let matchingCase;
155
+ let defaultCase;
156
+ import_react2.Children.forEach(props.children, (child) => {
157
+ if (!(0, import_react2.isValidElement)(child) || matchingCase)
158
+ return;
159
+ if (child.type === Case) {
160
+ const cond = child.props.cond;
161
+ if (isUseValue ? props.value === cond : cond) {
162
+ matchingCase = child;
163
+ return;
164
+ }
165
+ }
166
+ if (!defaultCase && child.type === Default)
167
+ defaultCase = child;
168
+ });
169
+ return matchingCase ?? defaultCase ?? null;
170
+ }
171
+
172
+ // src/components/condition/Unless.ts
173
+ var import_react3 = __toESM(require_react(), 1);
174
+ function Unless(props) {
175
+ const children = props.then || props.children;
176
+ const elements = import_react3.Children.toArray(children);
177
+ const thenChild = elements.find((c2) => c2.type === Then);
178
+ const elseChild = elements.find((c2) => c2.type === Else);
179
+ return thenChild || elseChild ? !props.cond ? elseChild : thenChild : !props.cond ? props.children : props.else;
180
+ }
181
+
182
+ // src/components/utils/Injector.ts
183
+ var import_react4 = __toESM(require_react(), 1);
184
+ function Injector(props) {
185
+ const installs = (0, import_react4.useMemo)(
186
+ () => props.install.map(repack).reverse(),
187
+ [props.install]
188
+ );
189
+ return installs.reduce(
190
+ (child, { component: Component, props: props2 }) => (0, import_react4.createElement)(Component, props2, child),
191
+ props.children
192
+ );
193
+ }
194
+ function repack(c2) {
195
+ return c2.component ? c2 : { component: c2 };
196
+ }
197
+
198
+ // ../../node_modules/.pnpm/html-parse-stringify@3.0.1/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js
199
+ var import_void_elements = __toESM(require_void_elements());
200
+ var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
201
+ function n(n2) {
202
+ var r2 = { type: "tag", name: "", voidElement: false, attrs: {}, children: [] }, i2 = n2.match(/<\/?([^\s]+?)[/\s>]/);
203
+ if (i2 && (r2.name = i2[1], (import_void_elements.default[i2[1]] || "/" === n2.charAt(n2.length - 2)) && (r2.voidElement = true), r2.name.startsWith("!--"))) {
204
+ var s2 = n2.indexOf("-->");
205
+ return { type: "comment", comment: -1 !== s2 ? n2.slice(4, s2) : "" };
206
+ }
207
+ for (var a2 = new RegExp(t), c2 = null; null !== (c2 = a2.exec(n2)); ) if (c2[0].trim()) if (c2[1]) {
208
+ var o = c2[1].trim(), l = [o, ""];
209
+ o.indexOf("=") > -1 && (l = o.split("=")), r2.attrs[l[0]] = l[1], a2.lastIndex--;
210
+ } else c2[2] && (r2.attrs[c2[2]] = c2[3].trim().substring(1, c2[3].length - 1));
211
+ return r2;
212
+ }
213
+ var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g;
214
+ var i = /^\s*$/;
215
+ var s = /* @__PURE__ */ Object.create(null);
216
+ function a(e2, t2) {
217
+ switch (t2.type) {
218
+ case "text":
219
+ return e2 + t2.content;
220
+ case "tag":
221
+ return e2 += "<" + t2.name + (t2.attrs ? function(e3) {
222
+ var t3 = [];
223
+ for (var n2 in e3) t3.push(n2 + '="' + e3[n2] + '"');
224
+ return t3.length ? " " + t3.join(" ") : "";
225
+ }(t2.attrs) : "") + (t2.voidElement ? "/>" : ">"), t2.voidElement ? e2 : e2 + t2.children.reduce(a, "") + "</" + t2.name + ">";
226
+ case "comment":
227
+ return e2 + "<!--" + t2.comment + "-->";
228
+ }
229
+ }
230
+ var c = { parse: function(e2, t2) {
231
+ t2 || (t2 = {}), t2.components || (t2.components = s);
232
+ var a2, c2 = [], o = [], l = -1, m = false;
233
+ if (0 !== e2.indexOf("<")) {
234
+ var u = e2.indexOf("<");
235
+ c2.push({ type: "text", content: -1 === u ? e2 : e2.substring(0, u) });
236
+ }
237
+ return e2.replace(r, function(r2, s2) {
238
+ if (m) {
239
+ if (r2 !== "</" + a2.name + ">") return;
240
+ m = false;
241
+ }
242
+ var u2, f = "/" !== r2.charAt(1), h = r2.startsWith("<!--"), p = s2 + r2.length, d = e2.charAt(p);
243
+ if (h) {
244
+ var v = n(r2);
245
+ return l < 0 ? (c2.push(v), c2) : ((u2 = o[l]).children.push(v), c2);
246
+ }
247
+ if (f && (l++, "tag" === (a2 = n(r2)).type && t2.components[a2.name] && (a2.type = "component", m = true), a2.voidElement || m || !d || "<" === d || a2.children.push({ type: "text", content: e2.slice(p, e2.indexOf("<", p)) }), 0 === l && c2.push(a2), (u2 = o[l - 1]) && u2.children.push(a2), o[l] = a2), (!f || a2.voidElement) && (l > -1 && (a2.voidElement || a2.name === r2.slice(2, -1)) && (l--, a2 = -1 === l ? c2 : o[l]), !m && "<" !== d && d)) {
248
+ u2 = -1 === l ? c2 : o[l].children;
249
+ var x = e2.indexOf("<", p), g = e2.slice(p, -1 === x ? void 0 : x);
250
+ i.test(g) && (g = " "), (x > -1 && l + u2.length >= 0 || " " !== g) && u2.push({ type: "text", content: g });
251
+ }
252
+ }), c2;
253
+ }, stringify: function(e2) {
254
+ return e2.reduce(function(e3, t2) {
255
+ return e3 + a("", t2);
256
+ }, "");
257
+ } };
258
+ var html_parse_stringify_module_default = c;
259
+
260
+ // src/components/utils/Trans.ts
261
+ var import_react5 = __toESM(require_react(), 1);
262
+ var import_react_i18next = __toESM(require_react_i18next(), 1);
263
+ function Trans({ i18nKey, ...additionalProps }) {
264
+ const translation = (0, import_react_i18next.useTranslation)().t(i18nKey, additionalProps);
265
+ return renderNodes(html_parse_stringify_module_default.parse(translation), additionalProps);
266
+ }
267
+ function renderNodes(tokens, values) {
268
+ let index = 0;
269
+ return tokens.map((token) => {
270
+ if (token.type === "text")
271
+ return token.content;
272
+ index++;
273
+ const props = { ...token.attrs, key: index };
274
+ return token.voidElement ? values[token.name] ? (0, import_react5.createElement)("span", { key: index }, values[token.name]) : (0, import_react5.createElement)(token.name, props) : (0, import_react5.createElement)(token.name, props, renderNodes(token.children, {}));
275
+ });
276
+ }
277
+
278
+ // src/hooks/useAsyncCallback.ts
279
+ var import_react6 = __toESM(require_react(), 1);
280
+ function useAsyncCallback(fun) {
281
+ const [error, setError] = (0, import_react6.useState)();
282
+ const [loading, setLoading] = (0, import_react6.useState)(false);
283
+ async function execute(...args) {
284
+ try {
285
+ setLoading(true);
286
+ const result = await fun(...args);
287
+ setLoading(false);
288
+ return result;
289
+ } catch (error2) {
290
+ setLoading(false);
291
+ setError(error2);
292
+ throw error2;
293
+ }
294
+ }
295
+ return [loading, execute, error];
296
+ }
297
+
298
+ // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useAsyncFn.js
299
+ init_tslib_es6();
300
+ var import_react8 = __toESM(require_react());
301
+
302
+ // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMountedState.js
303
+ var import_react7 = __toESM(require_react());
304
+ function useMountedState() {
305
+ var mountedRef = (0, import_react7.useRef)(false);
306
+ var get = (0, import_react7.useCallback)(function() {
307
+ return mountedRef.current;
308
+ }, []);
309
+ (0, import_react7.useEffect)(function() {
310
+ mountedRef.current = true;
311
+ return function() {
312
+ mountedRef.current = false;
313
+ };
314
+ }, []);
315
+ return get;
316
+ }
317
+
318
+ // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useAsyncFn.js
319
+ function useAsyncFn(fn, deps, initialState) {
320
+ if (deps === void 0) {
321
+ deps = [];
322
+ }
323
+ if (initialState === void 0) {
324
+ initialState = { loading: false };
325
+ }
326
+ var lastCallId = (0, import_react8.useRef)(0);
327
+ var isMounted = useMountedState();
328
+ var _a = (0, import_react8.useState)(initialState), state = _a[0], set = _a[1];
329
+ var callback = (0, import_react8.useCallback)(function() {
330
+ var args = [];
331
+ for (var _i = 0; _i < arguments.length; _i++) {
332
+ args[_i] = arguments[_i];
333
+ }
334
+ var callId = ++lastCallId.current;
335
+ if (!state.loading) {
336
+ set(function(prevState) {
337
+ return __assign(__assign({}, prevState), { loading: true });
338
+ });
339
+ }
340
+ return fn.apply(void 0, args).then(function(value) {
341
+ isMounted() && callId === lastCallId.current && set({ value, loading: false });
342
+ return value;
343
+ }, function(error) {
344
+ isMounted() && callId === lastCallId.current && set({ error, loading: false });
345
+ return error;
346
+ });
347
+ }, deps);
348
+ return [state, callback];
349
+ }
350
+
351
+ // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useEffectOnce.js
352
+ var import_react9 = __toESM(require_react());
353
+ var useEffectOnce = function(effect) {
354
+ (0, import_react9.useEffect)(effect, []);
355
+ };
356
+ var useEffectOnce_default = useEffectOnce;
357
+
358
+ // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMount.js
359
+ var useMount = function(fn) {
360
+ useEffectOnce_default(function() {
361
+ fn();
362
+ });
363
+ };
364
+ var useMount_default = useMount;
365
+
366
+ // src/hooks/useAsyncState.ts
367
+ function useAsyncState(fn, deps, options) {
368
+ const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
369
+ useMount_default(() => options?.immediate && _fn());
370
+ return [state, _fn];
371
+ }
372
+
373
+ // src/hooks/useDebounce.ts
374
+ var import_react10 = __toESM(require_react(), 1);
375
+ function useDebounce(value, delay) {
376
+ const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
377
+ (0, import_react10.useEffect)(() => {
378
+ const handler = setTimeout(() => setDebouncedValue(value), delay);
379
+ return () => clearTimeout(handler);
380
+ }, [value, delay]);
381
+ return debouncedValue;
382
+ }
383
+
384
+ // ../../node_modules/.pnpm/mitt@3.0.1/node_modules/mitt/dist/mitt.mjs
385
+ function mitt_default(n2) {
386
+ return { all: n2 = n2 || /* @__PURE__ */ new Map(), on: function(t2, e2) {
387
+ var i2 = n2.get(t2);
388
+ i2 ? i2.push(e2) : n2.set(t2, [e2]);
389
+ }, off: function(t2, e2) {
390
+ var i2 = n2.get(t2);
391
+ i2 && (e2 ? i2.splice(i2.indexOf(e2) >>> 0, 1) : n2.set(t2, []));
392
+ }, emit: function(t2, e2) {
393
+ var i2 = n2.get(t2);
394
+ i2 && i2.slice().map(function(n3) {
395
+ n3(e2);
396
+ }), (i2 = n2.get("*")) && i2.slice().map(function(n3) {
397
+ n3(t2, e2);
398
+ });
399
+ } };
400
+ }
401
+
402
+ // src/hooks/useEventBus.ts
403
+ var import_react11 = __toESM(require_react(), 1);
404
+ var emitter = mitt_default();
405
+ function useEventBus(key) {
406
+ const onRef = (0, import_react11.useRef)();
407
+ function on(listener) {
408
+ emitter.on(key, listener);
409
+ onRef.current = listener;
410
+ (0, import_react11.useEffect)(() => {
411
+ if (!onRef.current)
412
+ return;
413
+ emitter.off(key, onRef.current);
414
+ emitter.on(key, listener);
415
+ onRef.current = listener;
416
+ return () => emitter.off(key, listener);
417
+ }, [listener]);
418
+ }
419
+ function emit(event) {
420
+ emitter.emit(key, event);
421
+ }
422
+ function off(listener) {
423
+ emitter.off(key, listener);
424
+ }
425
+ return {
426
+ on,
427
+ emit,
428
+ off
429
+ };
430
+ }
431
+
432
+ // src/hooks/useFetchIntercept.ts
433
+ function useFetchResponseIntercept(intercept) {
434
+ useMount_default(() => fetchResponseIntercept(intercept));
435
+ }
436
+ function useFetchRequestIntercept(intercept) {
437
+ useMount_default(() => fetchRequestIntercept(intercept));
438
+ }
439
+ function fetchResponseIntercept(intercept) {
440
+ const { fetch: originalFetch } = window;
441
+ window.fetch = async (...args) => {
442
+ const [resource, config] = args;
443
+ const response = await originalFetch(resource, config);
444
+ return intercept(response);
445
+ };
446
+ }
447
+ function fetchRequestIntercept(intercept) {
448
+ const { fetch: originalFetch } = window;
449
+ window.fetch = async (...args) => {
450
+ const [resource, config] = args;
451
+ return intercept(originalFetch, resource, config);
452
+ };
453
+ }
454
+
455
+ // src/hooks/useMounted.ts
456
+ var import_react12 = __toESM(require_react(), 1);
457
+ function useMounted() {
458
+ const [mounted, setMounted] = (0, import_react12.useState)(false);
459
+ (0, import_react12.useEffect)(() => setMounted(true), []);
460
+ return mounted;
461
+ }
462
+
463
+ // src/hooks/useWatch.ts
464
+ var import_react13 = __toESM(require_react(), 1);
465
+ function useWatch(source, callback, options = {}) {
466
+ const firstUpdate = (0, import_react13.useRef)(false);
467
+ const then = (0, import_react13.useRef)();
468
+ const deps = (0, import_react13.useMemo)(
469
+ () => Array.isArray(source) ? source : [source],
470
+ [source]
471
+ );
472
+ (0, import_react13.useEffect)(() => {
473
+ if (!firstUpdate.current)
474
+ recordFirst();
475
+ else
476
+ callback(source);
477
+ }, deps);
478
+ async function recordFirst() {
479
+ if (then.current)
480
+ return;
481
+ then.current = Promise.resolve(source);
482
+ then.current.then(() => firstUpdate.current = true);
483
+ if (options.immediate)
484
+ then.current.then((value) => callback(value));
485
+ }
486
+ }
487
+
488
+ // src/hooks/useWhenever.ts
489
+ function useWhenever(source, cb, options) {
490
+ useWatch(source, () => source && cb(source), options);
491
+ }
492
+
493
+ // src/utils/index.ts
494
+ var hasOwn = {}.hasOwnProperty;
495
+ function cls(...args) {
496
+ let classes = "";
497
+ for (let i2 = 0; i2 < arguments.length; i2++) {
498
+ const arg = arguments[i2];
499
+ if (arg)
500
+ classes = appendClass(classes, parseValue(arg));
501
+ }
502
+ return classes;
503
+ }
504
+ function parseValue(arg) {
505
+ if (typeof arg === "string")
506
+ return arg;
507
+ if (typeof arg !== "object")
508
+ return "";
509
+ if (Array.isArray(arg))
510
+ return cls.apply(null, arg);
511
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
512
+ return cls.toString();
513
+ let classes = "";
514
+ for (const key in arg) {
515
+ if (hasOwn.call(arg, key) && arg[key])
516
+ classes = appendClass(classes, key);
517
+ }
518
+ return classes;
519
+ }
520
+ function appendClass(value, newClass) {
521
+ if (!newClass)
522
+ return value;
523
+ return value ? `${value} ${newClass}` : newClass;
524
+ }
525
+ return __toCommonJS(index_exports);
526
+ })();