@hairy/react-lib 1.9.0 → 1.10.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/dist/index.cjs CHANGED
@@ -74,50 +74,95 @@ __export(index_exports, {
74
74
  useMounted: () => useMounted,
75
75
  useStore: () => useStore,
76
76
  useWatch: () => useWatch,
77
- useWhenever: () => useWhenever
77
+ useWhenever: () => useWhenever,
78
+ wrapper: () => wrapper
78
79
  });
79
80
  module.exports = __toCommonJS(index_exports);
80
81
 
82
+ // src/utils/cls.ts
83
+ var hasOwn = {}.hasOwnProperty;
84
+ function cls(...args) {
85
+ let classes = "";
86
+ for (let i = 0; i < args.length; i++) {
87
+ const arg = args[i];
88
+ if (arg)
89
+ classes = cls.append(classes, cls.parse(arg));
90
+ }
91
+ return classes;
92
+ }
93
+ cls.parse = function(arg) {
94
+ if (typeof arg === "string")
95
+ return arg;
96
+ if (typeof arg !== "object")
97
+ return "";
98
+ if (Array.isArray(arg))
99
+ return cls.apply(null, arg);
100
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
101
+ return cls.toString();
102
+ let classes = "";
103
+ for (const key in arg) {
104
+ if (hasOwn.call(arg, key) && arg[key])
105
+ classes = cls.append(classes, key);
106
+ }
107
+ return classes;
108
+ };
109
+ cls.append = function(value, newClass) {
110
+ if (!newClass)
111
+ return value;
112
+ return value ? `${value} ${newClass}` : newClass;
113
+ };
114
+
115
+ // src/utils/wrapper.ts
116
+ var import_react = require("react");
117
+ function wrapper(tag, props, children) {
118
+ return tag ? (0, import_react.createElement)(tag, props, children) : children;
119
+ }
120
+
81
121
  // src/components/condition/Case.ts
82
122
  function Case(props) {
83
- return props.children;
123
+ const { cond, children, tag, ...attrs } = props;
124
+ return wrapper(tag, attrs, children);
84
125
  }
85
126
 
86
127
  // src/components/condition/Default.ts
87
128
  function Default(props) {
88
- return props.children;
89
- }
90
-
91
- // src/components/condition/Else.ts
92
- function Else(props) {
93
- return props.children;
129
+ const { children, tag, ...attrs } = props;
130
+ return wrapper(tag, attrs, children);
94
131
  }
95
132
 
96
133
  // src/components/condition/If.ts
97
- var import_react = require("react");
134
+ var import_react2 = require("react");
98
135
 
99
136
  // src/components/condition/Then.ts
100
137
  function Then(props) {
101
- return props.children;
138
+ const { children, cond, else: _else, then, tag, ...attrs } = props;
139
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
102
140
  }
103
141
 
104
142
  // src/components/condition/If.ts
105
143
  function If(props) {
106
- const children = props.then || props.children;
107
- const elements = import_react.Children.toArray(children);
144
+ const { then, cond, else: _else, children = props.then, tag, ...attrs } = props;
145
+ const elements = import_react2.Children.toArray(children);
108
146
  const thenChild = elements.find((c) => c.type === Then);
109
147
  const elseChild = elements.find((c) => c.type === Else);
110
- return thenChild || elseChild ? props.cond ? thenChild : elseChild : props.cond ? children : props.else;
148
+ const child = thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else;
149
+ return wrapper(tag, attrs, child);
150
+ }
151
+
152
+ // src/components/condition/Else.ts
153
+ function Else(props) {
154
+ const { children, tag, ...attrs } = props;
155
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
111
156
  }
112
157
 
113
158
  // src/components/condition/Switch.ts
114
- var import_react2 = require("react");
159
+ var import_react3 = require("react");
115
160
  function Switch(props) {
116
161
  const isUseValue = props.value !== void 0;
117
162
  let matchingCase;
118
163
  let defaultCase;
119
- import_react2.Children.forEach(props.children, (child) => {
120
- if (!(0, import_react2.isValidElement)(child) || matchingCase)
164
+ import_react3.Children.forEach(props.children, (child) => {
165
+ if (!(0, import_react3.isValidElement)(child) || matchingCase)
121
166
  return;
122
167
  if (child.type === Case) {
123
168
  const cond = child.props.cond;
@@ -133,24 +178,25 @@ function Switch(props) {
133
178
  }
134
179
 
135
180
  // src/components/condition/Unless.ts
136
- var import_react3 = require("react");
181
+ var import_react4 = require("react");
137
182
  function Unless(props) {
138
- const children = props.then || props.children;
139
- const elements = import_react3.Children.toArray(children);
183
+ const { cond, then, else: _else, tag, children = props.then, ...attrs } = props;
184
+ const elements = import_react4.Children.toArray(children);
140
185
  const thenChild = elements.find((c) => c.type === Then);
141
186
  const elseChild = elements.find((c) => c.type === Else);
142
- return thenChild || elseChild ? !props.cond ? elseChild : thenChild : !props.cond ? props.children : props.else;
187
+ const child = thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else;
188
+ return wrapper(tag, attrs, child);
143
189
  }
144
190
 
145
191
  // src/components/utils/Injector.ts
146
- var import_react4 = require("react");
192
+ var import_react5 = require("react");
147
193
  function Injector(props) {
148
- const installs = (0, import_react4.useMemo)(
194
+ const installs = (0, import_react5.useMemo)(
149
195
  () => props.install.map(repack).reverse(),
150
196
  [props.install]
151
197
  );
152
198
  return installs.reduce(
153
- (child, { component: Component, props: props2 }) => (0, import_react4.createElement)(Component, props2, child),
199
+ (child, { component: Component, props: props2 }) => (0, import_react5.createElement)(Component, props2, child),
154
200
  props.children
155
201
  );
156
202
  }
@@ -160,7 +206,7 @@ function repack(c) {
160
206
 
161
207
  // src/components/utils/Trans.ts
162
208
  var import_html_parse_stringify = __toESM(require("html-parse-stringify"), 1);
163
- var import_react5 = require("react");
209
+ var import_react6 = require("react");
164
210
  var import_react_i18next = require("react-i18next");
165
211
  function Trans({ i18nKey, ...additionalProps }) {
166
212
  const translation = (0, import_react_i18next.useTranslation)().t(i18nKey, additionalProps);
@@ -173,15 +219,15 @@ function renderNodes(tokens, values) {
173
219
  return token.content;
174
220
  index++;
175
221
  const props = { ...token.attrs, key: index };
176
- 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, {}));
222
+ return token.voidElement ? values[token.name] ? (0, import_react6.createElement)("span", { key: index }, values[token.name]) : (0, import_react6.createElement)(token.name, props) : (0, import_react6.createElement)(token.name, props, renderNodes(token.children, {}));
177
223
  });
178
224
  }
179
225
 
180
226
  // src/hooks/useAsyncCallback.ts
181
- var import_react6 = require("react");
227
+ var import_react7 = require("react");
182
228
  function useAsyncCallback(fun) {
183
- const [error, setError] = (0, import_react6.useState)();
184
- const [loading, setLoading] = (0, import_react6.useState)(false);
229
+ const [error, setError] = (0, import_react7.useState)();
230
+ const [loading, setLoading] = (0, import_react7.useState)(false);
185
231
  async function execute(...args) {
186
232
  try {
187
233
  setLoading(true);
@@ -199,16 +245,16 @@ function useAsyncCallback(fun) {
199
245
 
200
246
  // ../../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
201
247
  init_tslib_es6();
202
- var import_react8 = require("react");
248
+ var import_react9 = require("react");
203
249
 
204
250
  // ../../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
205
- var import_react7 = require("react");
251
+ var import_react8 = require("react");
206
252
  function useMountedState() {
207
- var mountedRef = (0, import_react7.useRef)(false);
208
- var get = (0, import_react7.useCallback)(function() {
253
+ var mountedRef = (0, import_react8.useRef)(false);
254
+ var get = (0, import_react8.useCallback)(function() {
209
255
  return mountedRef.current;
210
256
  }, []);
211
- (0, import_react7.useEffect)(function() {
257
+ (0, import_react8.useEffect)(function() {
212
258
  mountedRef.current = true;
213
259
  return function() {
214
260
  mountedRef.current = false;
@@ -225,10 +271,10 @@ function useAsyncFn(fn, deps, initialState) {
225
271
  if (initialState === void 0) {
226
272
  initialState = { loading: false };
227
273
  }
228
- var lastCallId = (0, import_react8.useRef)(0);
274
+ var lastCallId = (0, import_react9.useRef)(0);
229
275
  var isMounted = useMountedState();
230
- var _a = (0, import_react8.useState)(initialState), state = _a[0], set = _a[1];
231
- var callback = (0, import_react8.useCallback)(function() {
276
+ var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
277
+ var callback = (0, import_react9.useCallback)(function() {
232
278
  var args = [];
233
279
  for (var _i = 0; _i < arguments.length; _i++) {
234
280
  args[_i] = arguments[_i];
@@ -251,9 +297,9 @@ function useAsyncFn(fn, deps, initialState) {
251
297
  }
252
298
 
253
299
  // ../../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
254
- var import_react9 = require("react");
300
+ var import_react10 = require("react");
255
301
  var useEffectOnce = function(effect) {
256
- (0, import_react9.useEffect)(effect, []);
302
+ (0, import_react10.useEffect)(effect, []);
257
303
  };
258
304
  var useEffectOnce_default = useEffectOnce;
259
305
 
@@ -273,10 +319,10 @@ function useAsyncState(fn, deps, options) {
273
319
  }
274
320
 
275
321
  // src/hooks/useDebounce.ts
276
- var import_react10 = require("react");
322
+ var import_react11 = require("react");
277
323
  function useDebounce(value, delay) {
278
- const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
279
- (0, import_react10.useEffect)(() => {
324
+ const [debouncedValue, setDebouncedValue] = (0, import_react11.useState)(value);
325
+ (0, import_react11.useEffect)(() => {
280
326
  const handler = setTimeout(() => setDebouncedValue(value), delay);
281
327
  return () => clearTimeout(handler);
282
328
  }, [value, delay]);
@@ -285,14 +331,14 @@ function useDebounce(value, delay) {
285
331
 
286
332
  // src/hooks/useEventBus.ts
287
333
  var import_mitt = __toESM(require("mitt"), 1);
288
- var import_react11 = require("react");
334
+ var import_react12 = require("react");
289
335
  var emitter = (0, import_mitt.default)();
290
336
  function useEventBus(key) {
291
- const onRef = (0, import_react11.useRef)();
337
+ const onRef = (0, import_react12.useRef)();
292
338
  function on(listener) {
293
339
  emitter.on(key, listener);
294
340
  onRef.current = listener;
295
- (0, import_react11.useEffect)(() => {
341
+ (0, import_react12.useEffect)(() => {
296
342
  if (!onRef.current)
297
343
  return;
298
344
  emitter.off(key, onRef.current);
@@ -338,23 +384,23 @@ function fetchRequestIntercept(intercept) {
338
384
  }
339
385
 
340
386
  // src/hooks/useMounted.ts
341
- var import_react12 = require("react");
387
+ var import_react13 = require("react");
342
388
  function useMounted() {
343
- const [mounted, setMounted] = (0, import_react12.useState)(false);
344
- (0, import_react12.useEffect)(() => setMounted(true), []);
389
+ const [mounted, setMounted] = (0, import_react13.useState)(false);
390
+ (0, import_react13.useEffect)(() => setMounted(true), []);
345
391
  return mounted;
346
392
  }
347
393
 
348
394
  // src/hooks/useWatch.ts
349
- var import_react13 = require("react");
395
+ var import_react14 = require("react");
350
396
  function useWatch(source, callback, options = {}) {
351
- const firstUpdate = (0, import_react13.useRef)(false);
352
- const then = (0, import_react13.useRef)();
353
- const deps = (0, import_react13.useMemo)(
397
+ const firstUpdate = (0, import_react14.useRef)(false);
398
+ const then = (0, import_react14.useRef)();
399
+ const deps = (0, import_react14.useMemo)(
354
400
  () => Array.isArray(source) ? source : [source],
355
401
  [source]
356
402
  );
357
- (0, import_react13.useEffect)(() => {
403
+ (0, import_react14.useEffect)(() => {
358
404
  if (!firstUpdate.current)
359
405
  recordFirst();
360
406
  else
@@ -484,39 +530,6 @@ function defineAsyncStorePlain(fn, options = {}) {
484
530
  persistant: options.persistant
485
531
  });
486
532
  }
487
-
488
- // src/utils/index.ts
489
- var hasOwn = {}.hasOwnProperty;
490
- function cls(...args) {
491
- let classes = "";
492
- for (let i = 0; i < args.length; i++) {
493
- const arg = args[i];
494
- if (arg)
495
- classes = cls.append(classes, cls.parse(arg));
496
- }
497
- return classes;
498
- }
499
- cls.parse = function(arg) {
500
- if (typeof arg === "string")
501
- return arg;
502
- if (typeof arg !== "object")
503
- return "";
504
- if (Array.isArray(arg))
505
- return cls.apply(null, arg);
506
- if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
507
- return cls.toString();
508
- let classes = "";
509
- for (const key in arg) {
510
- if (hasOwn.call(arg, key) && arg[key])
511
- classes = cls.append(classes, key);
512
- }
513
- return classes;
514
- };
515
- cls.append = function(value, newClass) {
516
- if (!newClass)
517
- return value;
518
- return value ? `${value} ${newClass}` : newClass;
519
- };
520
533
  // Annotate the CommonJS export names for ESM import in node:
521
534
  0 && (module.exports = {
522
535
  Case,
@@ -542,5 +555,6 @@ cls.append = function(value, newClass) {
542
555
  useMounted,
543
556
  useStore,
544
557
  useWatch,
545
- useWhenever
558
+ useWhenever,
559
+ wrapper
546
560
  });
package/dist/index.d.ts CHANGED
@@ -1,41 +1,67 @@
1
1
  import { BooleanLike } from '@hairy/utils';
2
2
  import * as react from 'react';
3
- import { ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass, DependencyList, DetailedHTMLProps, HTMLAttributes } from 'react';
3
+ import { JSX, ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass, DependencyList, DetailedHTMLProps, HTMLAttributes } from 'react';
4
4
  import { FunctionReturningPromise as FunctionReturningPromise$1, PromiseType as PromiseType$1 } from './misc/types';
5
5
  import * as valtio from 'valtio';
6
6
 
7
- interface CaseProps {
8
- cond?: BooleanLike;
9
- children?: ReactNode;
7
+ type Value = string | boolean | undefined | null;
8
+ type Mapping = Record<string, any>;
9
+ interface ArgumentArray extends Array<Argument> {
10
+ }
11
+ interface ReadonlyArgumentArray extends ReadonlyArray<Argument> {
12
+ }
13
+ type Argument = Value | Mapping | ArgumentArray | ReadonlyArgumentArray;
14
+ /**
15
+ * A simple JavaScript utility for conditionally joining classNames together.
16
+ */
17
+ declare function cls(...args: ArgumentArray): string;
18
+ declare namespace cls {
19
+ var parse: (arg: any) => string;
20
+ var append: (value: any, newClass: any) => any;
10
21
  }
11
- declare function Case(props: CaseProps): ReactNode;
12
22
 
13
- declare function Default(props: PropsWithChildren): react.ReactNode;
23
+ type WrapperProps<K extends keyof JSX.IntrinsicElements | unknown, P = unknown> = {
24
+ tag?: K | React.FC<P>;
25
+ } & (K extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[K] : unknown) & P;
26
+ declare function wrapper(tag: any, props: unknown, children?: React.ReactNode): react.ReactNode;
14
27
 
15
- declare function Else(props: PropsWithChildren): react.ReactNode;
28
+ type CaseProps<K, P> = WrapperProps<K, P> & {
29
+ cond?: BooleanLike;
30
+ children?: ReactNode;
31
+ };
32
+ declare function Case<K, P>(props: CaseProps<K, P>): ReactNode;
33
+
34
+ type DefaultProps<K, P> = WrapperProps<K, P> & {
35
+ children?: ReactNode;
36
+ };
37
+ declare function Default<K, P>(props: DefaultProps<K, P>): ReactNode;
16
38
 
17
- interface IfProps {
39
+ type IfProps<K, P> = WrapperProps<K, P> & {
18
40
  cond?: BooleanLike;
19
41
  then?: ReactNode;
20
42
  else?: ReactNode;
21
43
  children?: ReactNode;
22
- }
23
- declare function If(props: IfProps): ReactNode;
44
+ };
45
+ declare function If<K, P>(props: IfProps<K, P>): ReactNode;
46
+
47
+ type ElseProps<K, P> = IfProps<K, P>;
48
+ declare function Else<K, P>(props: ElseProps<K, P>): react.ReactNode;
24
49
 
25
50
  interface SwitchProps extends PropsWithChildren {
26
51
  value?: BooleanLike;
27
52
  }
28
53
  declare function Switch(props: SwitchProps): ReactElement<any, string | react.JSXElementConstructor<any>> | null;
29
54
 
30
- declare function Then(props: PropsWithChildren): react.ReactNode;
55
+ type ThenProps<K, P> = IfProps<K, P>;
56
+ declare function Then<K, P>(props: ThenProps<K, P>): react.ReactNode;
31
57
 
32
- interface UnlessProps {
58
+ type UnlessProps<K, P> = WrapperProps<K, P> & {
33
59
  cond?: BooleanLike;
34
60
  then?: ReactNode;
35
61
  else?: ReactNode;
36
62
  children?: ReactNode;
37
- }
38
- declare function Unless(props: UnlessProps): ReactNode;
63
+ };
64
+ declare function Unless<K, P>(props: UnlessProps<K, P>): ReactNode;
39
65
 
40
66
  interface InjectComponent<P> {
41
67
  component: FC<P> | ComponentClass<P>;
@@ -182,20 +208,4 @@ declare function useStore<S extends object, A extends Actions<S>>(store: Store<S
182
208
 
183
209
  type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
184
210
 
185
- type Value = string | boolean | undefined | null;
186
- type Mapping = Record<string, any>;
187
- interface ArgumentArray extends Array<Argument> {
188
- }
189
- interface ReadonlyArgumentArray extends ReadonlyArray<Argument> {
190
- }
191
- type Argument = Value | Mapping | ArgumentArray | ReadonlyArgumentArray;
192
- /**
193
- * A simple JavaScript utility for conditionally joining classNames together.
194
- */
195
- declare function cls(...args: ArgumentArray): string;
196
- declare namespace cls {
197
- var parse: (arg: any) => string;
198
- var append: (value: any, newClass: any) => any;
199
- }
200
-
201
- export { type Actions, type ActionsOmitThisParameter, type Argument, type ArgumentArray, type AsyncStoreOptions, type AsyncStorePlainOptions, Case, type CaseProps, Default, Else, type EventBusListener, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StateFromFunctionReturningPromise, type Store, type StoreDefine, type StoreOptions, Switch, type SwitchProps, Then, Trans, type TransProps, Unless, type UnlessProps, type UseAsyncStateOptions, type Value, type WatchCallback, type WatchOptions, cls, defineAsyncStore, defineAsyncStorePlain, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStore, useWatch, useWhenever };
211
+ export { type Actions, type ActionsOmitThisParameter, type Argument, type ArgumentArray, type AsyncStoreOptions, type AsyncStorePlainOptions, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StateFromFunctionReturningPromise, type Store, type StoreDefine, type StoreOptions, Switch, type SwitchProps, Then, type ThenProps, Trans, type TransProps, Unless, type UnlessProps, type UseAsyncStateOptions, type Value, type WatchCallback, type WatchOptions, type WrapperProps, cls, defineAsyncStore, defineAsyncStorePlain, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStore, useWatch, useWhenever, wrapper };
@@ -117,49 +117,94 @@ var LibReact = (() => {
117
117
  useMounted: () => useMounted,
118
118
  useStore: () => useStore,
119
119
  useWatch: () => useWatch,
120
- useWhenever: () => useWhenever
120
+ useWhenever: () => useWhenever,
121
+ wrapper: () => wrapper
121
122
  });
122
123
 
124
+ // src/utils/cls.ts
125
+ var hasOwn = {}.hasOwnProperty;
126
+ function cls(...args) {
127
+ let classes = "";
128
+ for (let i2 = 0; i2 < args.length; i2++) {
129
+ const arg = args[i2];
130
+ if (arg)
131
+ classes = cls.append(classes, cls.parse(arg));
132
+ }
133
+ return classes;
134
+ }
135
+ cls.parse = function(arg) {
136
+ if (typeof arg === "string")
137
+ return arg;
138
+ if (typeof arg !== "object")
139
+ return "";
140
+ if (Array.isArray(arg))
141
+ return cls.apply(null, arg);
142
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
143
+ return cls.toString();
144
+ let classes = "";
145
+ for (const key in arg) {
146
+ if (hasOwn.call(arg, key) && arg[key])
147
+ classes = cls.append(classes, key);
148
+ }
149
+ return classes;
150
+ };
151
+ cls.append = function(value, newClass) {
152
+ if (!newClass)
153
+ return value;
154
+ return value ? `${value} ${newClass}` : newClass;
155
+ };
156
+
157
+ // src/utils/wrapper.ts
158
+ var import_react = __toESM(require_react(), 1);
159
+ function wrapper(tag, props, children) {
160
+ return tag ? (0, import_react.createElement)(tag, props, children) : children;
161
+ }
162
+
123
163
  // src/components/condition/Case.ts
124
164
  function Case(props) {
125
- return props.children;
165
+ const { cond, children, tag, ...attrs } = props;
166
+ return wrapper(tag, attrs, children);
126
167
  }
127
168
 
128
169
  // src/components/condition/Default.ts
129
170
  function Default(props) {
130
- return props.children;
131
- }
132
-
133
- // src/components/condition/Else.ts
134
- function Else(props) {
135
- return props.children;
171
+ const { children, tag, ...attrs } = props;
172
+ return wrapper(tag, attrs, children);
136
173
  }
137
174
 
138
175
  // src/components/condition/If.ts
139
- var import_react = __toESM(require_react(), 1);
176
+ var import_react2 = __toESM(require_react(), 1);
140
177
 
141
178
  // src/components/condition/Then.ts
142
179
  function Then(props) {
143
- return props.children;
180
+ const { children, cond, else: _else, then, tag, ...attrs } = props;
181
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
144
182
  }
145
183
 
146
184
  // src/components/condition/If.ts
147
185
  function If(props) {
148
- const children = props.then || props.children;
149
- const elements = import_react.Children.toArray(children);
186
+ const { then, cond, else: _else, children = props.then, tag, ...attrs } = props;
187
+ const elements = import_react2.Children.toArray(children);
150
188
  const thenChild = elements.find((c2) => c2.type === Then);
151
189
  const elseChild = elements.find((c2) => c2.type === Else);
152
- return thenChild || elseChild ? props.cond ? thenChild : elseChild : props.cond ? children : props.else;
190
+ const child = thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else;
191
+ return wrapper(tag, attrs, child);
192
+ }
193
+
194
+ // src/components/condition/Else.ts
195
+ function Else(props) {
196
+ const { children, tag, ...attrs } = props;
197
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
153
198
  }
154
199
 
155
200
  // src/components/condition/Switch.ts
156
- var import_react2 = __toESM(require_react(), 1);
201
+ var import_react3 = __toESM(require_react(), 1);
157
202
  function Switch(props) {
158
203
  const isUseValue = props.value !== void 0;
159
204
  let matchingCase;
160
205
  let defaultCase;
161
- import_react2.Children.forEach(props.children, (child) => {
162
- if (!(0, import_react2.isValidElement)(child) || matchingCase)
206
+ import_react3.Children.forEach(props.children, (child) => {
207
+ if (!(0, import_react3.isValidElement)(child) || matchingCase)
163
208
  return;
164
209
  if (child.type === Case) {
165
210
  const cond = child.props.cond;
@@ -175,24 +220,25 @@ var LibReact = (() => {
175
220
  }
176
221
 
177
222
  // src/components/condition/Unless.ts
178
- var import_react3 = __toESM(require_react(), 1);
223
+ var import_react4 = __toESM(require_react(), 1);
179
224
  function Unless(props) {
180
- const children = props.then || props.children;
181
- const elements = import_react3.Children.toArray(children);
225
+ const { cond, then, else: _else, tag, children = props.then, ...attrs } = props;
226
+ const elements = import_react4.Children.toArray(children);
182
227
  const thenChild = elements.find((c2) => c2.type === Then);
183
228
  const elseChild = elements.find((c2) => c2.type === Else);
184
- return thenChild || elseChild ? !props.cond ? elseChild : thenChild : !props.cond ? props.children : props.else;
229
+ const child = thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else;
230
+ return wrapper(tag, attrs, child);
185
231
  }
186
232
 
187
233
  // src/components/utils/Injector.ts
188
- var import_react4 = __toESM(require_react(), 1);
234
+ var import_react5 = __toESM(require_react(), 1);
189
235
  function Injector(props) {
190
- const installs = (0, import_react4.useMemo)(
236
+ const installs = (0, import_react5.useMemo)(
191
237
  () => props.install.map(repack).reverse(),
192
238
  [props.install]
193
239
  );
194
240
  return installs.reduce(
195
- (child, { component: Component, props: props2 }) => (0, import_react4.createElement)(Component, props2, child),
241
+ (child, { component: Component, props: props2 }) => (0, import_react5.createElement)(Component, props2, child),
196
242
  props.children
197
243
  );
198
244
  }
@@ -263,7 +309,7 @@ var LibReact = (() => {
263
309
  var html_parse_stringify_module_default = c;
264
310
 
265
311
  // src/components/utils/Trans.ts
266
- var import_react5 = __toESM(require_react(), 1);
312
+ var import_react6 = __toESM(require_react(), 1);
267
313
  var import_react_i18next = __toESM(require_react_i18next(), 1);
268
314
  function Trans({ i18nKey, ...additionalProps }) {
269
315
  const translation = (0, import_react_i18next.useTranslation)().t(i18nKey, additionalProps);
@@ -276,15 +322,15 @@ var LibReact = (() => {
276
322
  return token.content;
277
323
  index++;
278
324
  const props = { ...token.attrs, key: index };
279
- 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, {}));
325
+ return token.voidElement ? values[token.name] ? (0, import_react6.createElement)("span", { key: index }, values[token.name]) : (0, import_react6.createElement)(token.name, props) : (0, import_react6.createElement)(token.name, props, renderNodes(token.children, {}));
280
326
  });
281
327
  }
282
328
 
283
329
  // src/hooks/useAsyncCallback.ts
284
- var import_react6 = __toESM(require_react(), 1);
330
+ var import_react7 = __toESM(require_react(), 1);
285
331
  function useAsyncCallback(fun) {
286
- const [error, setError] = (0, import_react6.useState)();
287
- const [loading, setLoading] = (0, import_react6.useState)(false);
332
+ const [error, setError] = (0, import_react7.useState)();
333
+ const [loading, setLoading] = (0, import_react7.useState)(false);
288
334
  async function execute(...args) {
289
335
  try {
290
336
  setLoading(true);
@@ -302,16 +348,16 @@ var LibReact = (() => {
302
348
 
303
349
  // ../../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
304
350
  init_tslib_es6();
305
- var import_react8 = __toESM(require_react());
351
+ var import_react9 = __toESM(require_react());
306
352
 
307
353
  // ../../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
308
- var import_react7 = __toESM(require_react());
354
+ var import_react8 = __toESM(require_react());
309
355
  function useMountedState() {
310
- var mountedRef = (0, import_react7.useRef)(false);
311
- var get = (0, import_react7.useCallback)(function() {
356
+ var mountedRef = (0, import_react8.useRef)(false);
357
+ var get = (0, import_react8.useCallback)(function() {
312
358
  return mountedRef.current;
313
359
  }, []);
314
- (0, import_react7.useEffect)(function() {
360
+ (0, import_react8.useEffect)(function() {
315
361
  mountedRef.current = true;
316
362
  return function() {
317
363
  mountedRef.current = false;
@@ -328,10 +374,10 @@ var LibReact = (() => {
328
374
  if (initialState === void 0) {
329
375
  initialState = { loading: false };
330
376
  }
331
- var lastCallId = (0, import_react8.useRef)(0);
377
+ var lastCallId = (0, import_react9.useRef)(0);
332
378
  var isMounted = useMountedState();
333
- var _a = (0, import_react8.useState)(initialState), state = _a[0], set = _a[1];
334
- var callback = (0, import_react8.useCallback)(function() {
379
+ var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
380
+ var callback = (0, import_react9.useCallback)(function() {
335
381
  var args = [];
336
382
  for (var _i = 0; _i < arguments.length; _i++) {
337
383
  args[_i] = arguments[_i];
@@ -354,9 +400,9 @@ var LibReact = (() => {
354
400
  }
355
401
 
356
402
  // ../../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
357
- var import_react9 = __toESM(require_react());
403
+ var import_react10 = __toESM(require_react());
358
404
  var useEffectOnce = function(effect) {
359
- (0, import_react9.useEffect)(effect, []);
405
+ (0, import_react10.useEffect)(effect, []);
360
406
  };
361
407
  var useEffectOnce_default = useEffectOnce;
362
408
 
@@ -376,10 +422,10 @@ var LibReact = (() => {
376
422
  }
377
423
 
378
424
  // src/hooks/useDebounce.ts
379
- var import_react10 = __toESM(require_react(), 1);
425
+ var import_react11 = __toESM(require_react(), 1);
380
426
  function useDebounce(value, delay) {
381
- const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
382
- (0, import_react10.useEffect)(() => {
427
+ const [debouncedValue, setDebouncedValue] = (0, import_react11.useState)(value);
428
+ (0, import_react11.useEffect)(() => {
383
429
  const handler = setTimeout(() => setDebouncedValue(value), delay);
384
430
  return () => clearTimeout(handler);
385
431
  }, [value, delay]);
@@ -405,14 +451,14 @@ var LibReact = (() => {
405
451
  }
406
452
 
407
453
  // src/hooks/useEventBus.ts
408
- var import_react11 = __toESM(require_react(), 1);
454
+ var import_react12 = __toESM(require_react(), 1);
409
455
  var emitter = mitt_default();
410
456
  function useEventBus(key) {
411
- const onRef = (0, import_react11.useRef)();
457
+ const onRef = (0, import_react12.useRef)();
412
458
  function on(listener) {
413
459
  emitter.on(key, listener);
414
460
  onRef.current = listener;
415
- (0, import_react11.useEffect)(() => {
461
+ (0, import_react12.useEffect)(() => {
416
462
  if (!onRef.current)
417
463
  return;
418
464
  emitter.off(key, onRef.current);
@@ -458,23 +504,23 @@ var LibReact = (() => {
458
504
  }
459
505
 
460
506
  // src/hooks/useMounted.ts
461
- var import_react12 = __toESM(require_react(), 1);
507
+ var import_react13 = __toESM(require_react(), 1);
462
508
  function useMounted() {
463
- const [mounted, setMounted] = (0, import_react12.useState)(false);
464
- (0, import_react12.useEffect)(() => setMounted(true), []);
509
+ const [mounted, setMounted] = (0, import_react13.useState)(false);
510
+ (0, import_react13.useEffect)(() => setMounted(true), []);
465
511
  return mounted;
466
512
  }
467
513
 
468
514
  // src/hooks/useWatch.ts
469
- var import_react13 = __toESM(require_react(), 1);
515
+ var import_react14 = __toESM(require_react(), 1);
470
516
  function useWatch(source, callback, options = {}) {
471
- const firstUpdate = (0, import_react13.useRef)(false);
472
- const then = (0, import_react13.useRef)();
473
- const deps = (0, import_react13.useMemo)(
517
+ const firstUpdate = (0, import_react14.useRef)(false);
518
+ const then = (0, import_react14.useRef)();
519
+ const deps = (0, import_react14.useMemo)(
474
520
  () => Array.isArray(source) ? source : [source],
475
521
  [source]
476
522
  );
477
- (0, import_react13.useEffect)(() => {
523
+ (0, import_react14.useEffect)(() => {
478
524
  if (!firstUpdate.current)
479
525
  recordFirst();
480
526
  else
@@ -932,27 +978,27 @@ var LibReact = (() => {
932
978
  }
933
979
 
934
980
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/react.mjs
935
- var import_react14 = __toESM(require_react(), 1);
981
+ var import_react15 = __toESM(require_react(), 1);
936
982
  var import_meta2 = {};
937
983
  var useAffectedDebugValue = (state, affected) => {
938
- const pathList = (0, import_react14.useRef)(void 0);
939
- (0, import_react14.useEffect)(() => {
984
+ const pathList = (0, import_react15.useRef)(void 0);
985
+ (0, import_react15.useEffect)(() => {
940
986
  pathList.current = affectedToPathList(state, affected, true);
941
987
  });
942
- (0, import_react14.useDebugValue)(pathList.current);
988
+ (0, import_react15.useDebugValue)(pathList.current);
943
989
  };
944
990
  var condUseAffectedDebugValue = useAffectedDebugValue;
945
991
  var targetCache = /* @__PURE__ */ new WeakMap();
946
992
  function useSnapshot(proxyObject, options) {
947
993
  const notifyInSync = options == null ? void 0 : options.sync;
948
- const affected = (0, import_react14.useMemo)(
994
+ const affected = (0, import_react15.useMemo)(
949
995
  () => proxyObject && /* @__PURE__ */ new WeakMap(),
950
996
  [proxyObject]
951
997
  );
952
- const lastSnapshot = (0, import_react14.useRef)(void 0);
998
+ const lastSnapshot = (0, import_react15.useRef)(void 0);
953
999
  let inRender = true;
954
- const currSnapshot = (0, import_react14.useSyncExternalStore)(
955
- (0, import_react14.useCallback)(
1000
+ const currSnapshot = (0, import_react15.useSyncExternalStore)(
1001
+ (0, import_react15.useCallback)(
956
1002
  (callback) => {
957
1003
  const unsub = subscribe(proxyObject, callback, notifyInSync);
958
1004
  callback();
@@ -978,13 +1024,13 @@ var LibReact = (() => {
978
1024
  () => snapshot(proxyObject)
979
1025
  );
980
1026
  inRender = false;
981
- (0, import_react14.useLayoutEffect)(() => {
1027
+ (0, import_react15.useLayoutEffect)(() => {
982
1028
  lastSnapshot.current = currSnapshot;
983
1029
  });
984
1030
  if ((import_meta2.env ? import_meta2.env.MODE : void 0) !== "production") {
985
1031
  condUseAffectedDebugValue(currSnapshot, affected);
986
1032
  }
987
- const proxyCache2 = (0, import_react14.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
1033
+ const proxyCache2 = (0, import_react15.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
988
1034
  return createProxy(currSnapshot, affected, proxyCache2, targetCache);
989
1035
  }
990
1036
 
@@ -1092,38 +1138,5 @@ var LibReact = (() => {
1092
1138
  persistant: options.persistant
1093
1139
  });
1094
1140
  }
1095
-
1096
- // src/utils/index.ts
1097
- var hasOwn = {}.hasOwnProperty;
1098
- function cls(...args) {
1099
- let classes = "";
1100
- for (let i2 = 0; i2 < args.length; i2++) {
1101
- const arg = args[i2];
1102
- if (arg)
1103
- classes = cls.append(classes, cls.parse(arg));
1104
- }
1105
- return classes;
1106
- }
1107
- cls.parse = function(arg) {
1108
- if (typeof arg === "string")
1109
- return arg;
1110
- if (typeof arg !== "object")
1111
- return "";
1112
- if (Array.isArray(arg))
1113
- return cls.apply(null, arg);
1114
- if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
1115
- return cls.toString();
1116
- let classes = "";
1117
- for (const key in arg) {
1118
- if (hasOwn.call(arg, key) && arg[key])
1119
- classes = cls.append(classes, key);
1120
- }
1121
- return classes;
1122
- };
1123
- cls.append = function(value, newClass) {
1124
- if (!newClass)
1125
- return value;
1126
- return value ? `${value} ${newClass}` : newClass;
1127
- };
1128
1141
  return __toCommonJS(index_exports);
1129
1142
  })();
package/dist/index.js CHANGED
@@ -21,19 +21,55 @@ var init_tslib_es6 = __esm({
21
21
  }
22
22
  });
23
23
 
24
+ // src/utils/cls.ts
25
+ var hasOwn = {}.hasOwnProperty;
26
+ function cls(...args) {
27
+ let classes = "";
28
+ for (let i = 0; i < args.length; i++) {
29
+ const arg = args[i];
30
+ if (arg)
31
+ classes = cls.append(classes, cls.parse(arg));
32
+ }
33
+ return classes;
34
+ }
35
+ cls.parse = function(arg) {
36
+ if (typeof arg === "string")
37
+ return arg;
38
+ if (typeof arg !== "object")
39
+ return "";
40
+ if (Array.isArray(arg))
41
+ return cls.apply(null, arg);
42
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
43
+ return cls.toString();
44
+ let classes = "";
45
+ for (const key in arg) {
46
+ if (hasOwn.call(arg, key) && arg[key])
47
+ classes = cls.append(classes, key);
48
+ }
49
+ return classes;
50
+ };
51
+ cls.append = function(value, newClass) {
52
+ if (!newClass)
53
+ return value;
54
+ return value ? `${value} ${newClass}` : newClass;
55
+ };
56
+
57
+ // src/utils/wrapper.ts
58
+ import { createElement } from "react";
59
+ function wrapper(tag, props, children) {
60
+ return tag ? createElement(tag, props, children) : children;
61
+ }
62
+
24
63
  // src/components/condition/Case.ts
25
64
  function Case(props) {
26
- return props.children;
65
+ const { cond, children, tag, ...attrs } = props;
66
+ return wrapper(tag, attrs, children);
27
67
  }
28
68
 
29
69
  // src/components/condition/Default.ts
30
70
  function Default(props) {
31
- return props.children;
32
- }
33
-
34
- // src/components/condition/Else.ts
35
- function Else(props) {
36
- return props.children;
71
+ const { children, tag, ...attrs } = props;
72
+ return wrapper(tag, attrs, children);
37
73
  }
38
74
 
39
75
  // src/components/condition/If.ts
@@ -41,16 +77,24 @@ import { Children } from "react";
41
77
 
42
78
  // src/components/condition/Then.ts
43
79
  function Then(props) {
44
- return props.children;
80
+ const { children, cond, else: _else, then, tag, ...attrs } = props;
81
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
45
82
  }
46
83
 
47
84
  // src/components/condition/If.ts
48
85
  function If(props) {
49
- const children = props.then || props.children;
86
+ const { then, cond, else: _else, children = props.then, tag, ...attrs } = props;
50
87
  const elements = Children.toArray(children);
51
88
  const thenChild = elements.find((c) => c.type === Then);
52
89
  const elseChild = elements.find((c) => c.type === Else);
53
- return thenChild || elseChild ? props.cond ? thenChild : elseChild : props.cond ? children : props.else;
90
+ const child = thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else;
91
+ return wrapper(tag, attrs, child);
92
+ }
93
+
94
+ // src/components/condition/Else.ts
95
+ function Else(props) {
96
+ const { children, tag, ...attrs } = props;
97
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag, attrs, children);
54
98
  }
55
99
 
56
100
  // src/components/condition/Switch.ts
@@ -78,22 +122,23 @@ function Switch(props) {
78
122
  // src/components/condition/Unless.ts
79
123
  import { Children as Children3 } from "react";
80
124
  function Unless(props) {
81
- const children = props.then || props.children;
125
+ const { cond, then, else: _else, tag, children = props.then, ...attrs } = props;
82
126
  const elements = Children3.toArray(children);
83
127
  const thenChild = elements.find((c) => c.type === Then);
84
128
  const elseChild = elements.find((c) => c.type === Else);
85
- return thenChild || elseChild ? !props.cond ? elseChild : thenChild : !props.cond ? props.children : props.else;
129
+ const child = thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else;
130
+ return wrapper(tag, attrs, child);
86
131
  }
87
132
 
88
133
  // src/components/utils/Injector.ts
89
- import { createElement, useMemo } from "react";
134
+ import { createElement as createElement2, useMemo } from "react";
90
135
  function Injector(props) {
91
136
  const installs = useMemo(
92
137
  () => props.install.map(repack).reverse(),
93
138
  [props.install]
94
139
  );
95
140
  return installs.reduce(
96
- (child, { component: Component, props: props2 }) => createElement(Component, props2, child),
141
+ (child, { component: Component, props: props2 }) => createElement2(Component, props2, child),
97
142
  props.children
98
143
  );
99
144
  }
@@ -103,7 +148,7 @@ function repack(c) {
103
148
 
104
149
  // src/components/utils/Trans.ts
105
150
  import HTML from "html-parse-stringify";
106
- import { createElement as createElement2 } from "react";
151
+ import { createElement as createElement3 } from "react";
107
152
  import { useTranslation } from "react-i18next";
108
153
  function Trans({ i18nKey, ...additionalProps }) {
109
154
  const translation = useTranslation().t(i18nKey, additionalProps);
@@ -116,7 +161,7 @@ function renderNodes(tokens, values) {
116
161
  return token.content;
117
162
  index++;
118
163
  const props = { ...token.attrs, key: index };
119
- return token.voidElement ? values[token.name] ? createElement2("span", { key: index }, values[token.name]) : createElement2(token.name, props) : createElement2(token.name, props, renderNodes(token.children, {}));
164
+ return token.voidElement ? values[token.name] ? createElement3("span", { key: index }, values[token.name]) : createElement3(token.name, props) : createElement3(token.name, props, renderNodes(token.children, {}));
120
165
  });
121
166
  }
122
167
 
@@ -427,39 +472,6 @@ function defineAsyncStorePlain(fn, options = {}) {
427
472
  persistant: options.persistant
428
473
  });
429
474
  }
430
-
431
- // src/utils/index.ts
432
- var hasOwn = {}.hasOwnProperty;
433
- function cls(...args) {
434
- let classes = "";
435
- for (let i = 0; i < args.length; i++) {
436
- const arg = args[i];
437
- if (arg)
438
- classes = cls.append(classes, cls.parse(arg));
439
- }
440
- return classes;
441
- }
442
- cls.parse = function(arg) {
443
- if (typeof arg === "string")
444
- return arg;
445
- if (typeof arg !== "object")
446
- return "";
447
- if (Array.isArray(arg))
448
- return cls.apply(null, arg);
449
- if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
450
- return cls.toString();
451
- let classes = "";
452
- for (const key in arg) {
453
- if (hasOwn.call(arg, key) && arg[key])
454
- classes = cls.append(classes, key);
455
- }
456
- return classes;
457
- };
458
- cls.append = function(value, newClass) {
459
- if (!newClass)
460
- return value;
461
- return value ? `${value} ${newClass}` : newClass;
462
- };
463
475
  export {
464
476
  Case,
465
477
  Default,
@@ -484,5 +496,6 @@ export {
484
496
  useMounted,
485
497
  useStore,
486
498
  useWatch,
487
- useWhenever
499
+ useWhenever,
500
+ wrapper
488
501
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.9.0",
4
+ "version": "1.10.0",
5
5
  "description": "Library for react",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "react-dom": "^18.2.0",
39
39
  "react-i18next": "^14.1.2",
40
40
  "react-use": "^17.6.0",
41
- "@hairy/utils": "1.9.0"
41
+ "@hairy/utils": "1.10.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",