@wwog/react 1.1.3 → 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.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import React$1, { FC, ReactNode } from 'react';
1
+ import React, { ReactNode, FC } from 'react';
2
2
 
3
3
  interface SwitchProps<T> {
4
4
  value: T;
@@ -7,7 +7,7 @@ interface SwitchProps<T> {
7
7
  * @description_en optional compare function, default to () => a === b
8
8
  **/
9
9
  compare?: (a: T, b: T) => boolean;
10
- children?: React$1.ReactNode;
10
+ children?: React.ReactNode;
11
11
  /**
12
12
  * @description 是否严格模式,默认 false.建议跟随开发环境变化,严格模式下,会循环所有节点来提供更多的错误提示
13
13
  * @description_en strict mode, default to false. It is recommended to follow the development environment changes. In strict mode, all nodes will be looped to provide more error prompts
@@ -22,61 +22,112 @@ interface SwitchProps<T> {
22
22
  }
23
23
  interface SwitchCaseProps<T> {
24
24
  value: T;
25
- children?: React$1.ReactNode;
25
+ children?: React.ReactNode;
26
26
  }
27
27
  interface SwitchDefaultProps {
28
- children?: React$1.ReactNode;
28
+ children?: React.ReactNode;
29
29
  }
30
30
  /**
31
31
  * @description Switch 组件用于根据传入的 value 渲染不同的子组件
32
32
  * @description_en The Switch component is used to render different child components based on the passed value
33
33
  */
34
34
  declare const Switch: {
35
- <T>(props: SwitchProps<T>): React$1.ReactElement | null;
35
+ <T>(props: SwitchProps<T>): React.ReactElement | null;
36
36
  displayName: string;
37
37
  Case: {
38
- <T>(props: SwitchCaseProps<T>): React$1.ReactElement;
38
+ <T>(props: SwitchCaseProps<T>): React.ReactElement;
39
39
  displayName: string;
40
40
  };
41
41
  Default: {
42
- (props: SwitchDefaultProps): React$1.ReactElement;
42
+ (props: SwitchDefaultProps): React.ReactElement;
43
43
  displayName: string;
44
44
  };
45
45
  createTyped<T>(): {
46
- Switch: (props: SwitchProps<T>) => React$1.ReactElement | null;
47
- Case: (props: SwitchCaseProps<T>) => React$1.ReactElement;
48
- Default: (props: SwitchDefaultProps) => React$1.ReactElement;
46
+ Switch: (props: SwitchProps<T>) => React.ReactElement | null;
47
+ Case: (props: SwitchCaseProps<T>) => React.ReactElement;
48
+ Default: (props: SwitchDefaultProps) => React.ReactElement;
49
49
  };
50
50
  };
51
51
 
52
52
  interface IfProps {
53
53
  condition: boolean;
54
- children?: React$1.ReactNode;
54
+ children?: React.ReactNode;
55
55
  }
56
56
  interface ThenProps {
57
- children?: React$1.ReactNode;
57
+ children?: React.ReactNode;
58
58
  }
59
59
  interface ElseProps {
60
- children?: React$1.ReactNode;
60
+ children?: React.ReactNode;
61
61
  }
62
62
  interface ElseIfProps {
63
63
  condition: boolean;
64
- children?: React$1.ReactNode;
64
+ children?: React.ReactNode;
65
65
  }
66
66
  declare const If: {
67
- ({ condition, children, }: IfProps): React$1.ReactElement | null;
67
+ ({ condition, children, }: IfProps): React.ReactElement | null;
68
68
  displayName: string;
69
- Then: React$1.FC<ThenProps>;
70
- ElseIf: React$1.FC<ElseIfProps>;
71
- Else: React$1.FC<ElseProps>;
69
+ Then: React.FC<ThenProps>;
70
+ ElseIf: React.FC<ElseIfProps>;
71
+ Else: React.FC<ElseProps>;
72
72
  createTyped(): {
73
- If: (props: IfProps) => React$1.ReactElement | null;
74
- Then: (props: ThenProps) => React$1.ReactElement;
75
- ElseIf: (props: ElseIfProps) => React$1.ReactElement;
76
- Else: (props: ElseProps) => React$1.ReactElement;
73
+ If: (props: IfProps) => React.ReactElement | null;
74
+ Then: (props: ThenProps) => React.ReactElement;
75
+ ElseIf: (props: ElseIfProps) => React.ReactElement;
76
+ Else: (props: ElseProps) => React.ReactElement;
77
77
  };
78
78
  };
79
79
 
80
+ interface WhenProps {
81
+ /**
82
+ * @description_en An array of conditions where all must be true to render children.
83
+ * @description_zh 一个条件数组,所有条件都为真时渲染子元素。
84
+ * @index 1
85
+ */
86
+ all?: boolean[];
87
+ /**
88
+ * @description_en An array of conditions where at least one must be true to render children.
89
+ * @description_zh 一个条件数组,至少有一个条件为真时渲染子元素。
90
+ * @index 2
91
+ */
92
+ any?: boolean[];
93
+ /**
94
+ * @description_en An array of conditions where all must be false to render children.
95
+ * @description_zh 一个条件数组,所有条件都为假时渲染子元素。
96
+ * @index 3
97
+ */
98
+ none?: boolean[];
99
+ /**
100
+ * @description_en The content to render when conditions are satisfied.
101
+ * @description_zh 满足条件时渲染的内容。
102
+ */
103
+ children?: ReactNode;
104
+ /**
105
+ * @description_en The content to render when conditions are not satisfied (alternative to `Else`).
106
+ * @description_zh 不满足条件时渲染的内容(替代 `Else`)。
107
+ */
108
+ fallback?: ReactNode;
109
+ }
110
+ /**
111
+ * @description_zh 一个声明式组件,用于基于多个条件进行条件渲染。比If更简洁。
112
+ * @description_en A declarative component for conditional rendering based on multiple conditions. More concise than If.
113
+ * @component
114
+ * @example
115
+ * ```tsx
116
+ * <When all={[isAdmin, hasPermission]}>
117
+ * <AdminPanel />
118
+ * </When>
119
+ * ```
120
+ *
121
+ * @example
122
+ * ```tsx
123
+ * <When any={[isLoading, isFetching]} fallback={<ErrorMessage />}>
124
+ * <Spinner />
125
+ * </When>
126
+ * ```
127
+ *
128
+ */
129
+ declare const When: FC<WhenProps>;
130
+
80
131
  interface SizeBoxProps {
81
132
  size?: number | string;
82
133
  height?: number | string;
@@ -101,7 +152,7 @@ declare function ArrayRender<T>(props: ArrayRenderProps<T>): ReactNode;
101
152
  * @description 性能优化,替代 React.Children.forEach, 回调可以返回 false 来中断循环
102
153
  * @description_en Replace React.Children.forEach, the callback can return false to interrupt the loop
103
154
  */
104
- declare function childrenLoop(children: React$1.ReactNode | undefined, callback: (child: React$1.ReactNode, index: number) => boolean | void): void;
155
+ declare function childrenLoop(children: React.ReactNode | undefined, callback: (child: React.ReactNode, index: number) => boolean | void): void;
105
156
 
106
- export { ArrayRender, If, SizeBox, Switch, childrenLoop };
107
- export type { ArrayRenderProps, ElseIfProps, ElseProps, IfProps, SwitchCaseProps, SwitchDefaultProps, SwitchProps, ThenProps };
157
+ export { ArrayRender, If, SizeBox, Switch, When, childrenLoop };
158
+ export type { ArrayRenderProps, ElseIfProps, ElseProps, IfProps, SwitchCaseProps, SwitchDefaultProps, SwitchProps, ThenProps, WhenProps };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import React$1, { Fragment } from 'react';
1
+ import React, { useMemo, Fragment } from 'react';
2
2
 
3
3
  function childrenLoop(children, callback) {
4
4
  if (children === void 0) return;
@@ -19,11 +19,11 @@ const defaultCompare = (a, b) => {
19
19
  return a === b;
20
20
  };
21
21
  const Case = (props) => {
22
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, props.children);
22
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
23
23
  };
24
24
  Case.displayName = "Switch_Case";
25
25
  const Default = (props) => {
26
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, props.children);
26
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
27
27
  };
28
28
  Default.displayName = "Switch_Default";
29
29
  const Switch = (props) => {
@@ -33,7 +33,7 @@ const Switch = (props) => {
33
33
  let defaultChild = null;
34
34
  let hasDefault = false;
35
35
  childrenLoop(children, (child, index) => {
36
- if (!React$1.isValidElement(child)) {
36
+ if (!React.isValidElement(child)) {
37
37
  throw new Error(
38
38
  `Switch Children only accepts valid React elements at index ${index}`
39
39
  );
@@ -74,7 +74,7 @@ const Switch = (props) => {
74
74
  );
75
75
  }
76
76
  });
77
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, matchedChildren ?? defaultChild);
77
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, matchedChildren ?? defaultChild);
78
78
  };
79
79
  Switch.displayName = "Switch";
80
80
  Switch.Case = Case;
@@ -88,13 +88,13 @@ Switch.createTyped = function() {
88
88
  };
89
89
 
90
90
  const Then = (props) => {
91
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, props.children);
91
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
92
92
  };
93
93
  const Else = ({ children }) => {
94
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, children);
94
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
95
95
  };
96
96
  const ElseIf = (props) => {
97
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, props.children);
97
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
98
98
  };
99
99
  Then.displayName = "If_Then";
100
100
  Else.displayName = "If_Else";
@@ -106,8 +106,8 @@ const If = ({
106
106
  let thenChild = null;
107
107
  let elseChild = null;
108
108
  const elseIfChildren = [];
109
- React$1.Children.forEach(children, (child) => {
110
- if (!React$1.isValidElement(child)) {
109
+ React.Children.forEach(children, (child) => {
110
+ if (!React.isValidElement(child)) {
111
111
  throw new Error("If component only accepts valid React elements");
112
112
  }
113
113
  const type = child.type;
@@ -132,15 +132,15 @@ const If = ({
132
132
  }
133
133
  });
134
134
  if (condition) {
135
- return thenChild ? /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, thenChild.props.children) : null;
135
+ return thenChild ? /* @__PURE__ */ React.createElement(React.Fragment, null, thenChild.props.children) : null;
136
136
  }
137
137
  for (const elseIf of elseIfChildren) {
138
138
  if (elseIf.props.condition) {
139
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, elseIf.props.children);
139
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, elseIf.props.children);
140
140
  }
141
141
  }
142
142
  if (elseChild) {
143
- return /* @__PURE__ */ React$1.createElement(React$1.Fragment, null, elseChild.props.children);
143
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, elseChild.props.children);
144
144
  }
145
145
  return null;
146
146
  };
@@ -157,6 +157,21 @@ If.createTyped = function() {
157
157
  };
158
158
  };
159
159
 
160
+ const When = ({ all, any, none, children, fallback }) => {
161
+ const shouldRender = useMemo(() => {
162
+ if (all && (any || none)) {
163
+ console.warn(
164
+ 'When: Multiple condition types (all, any, none) provided; "all" takes precedence.'
165
+ );
166
+ }
167
+ if (all && all.length > 0 && all.every(Boolean)) return true;
168
+ if (any && any.length > 0 && any.some(Boolean)) return true;
169
+ if (none && none.length > 0 && none.every((v) => !v)) return true;
170
+ return false;
171
+ }, [all, any, none]);
172
+ return shouldRender ? /* @__PURE__ */ React.createElement(React.Fragment, null, children) : /* @__PURE__ */ React.createElement(React.Fragment, null, fallback || null);
173
+ };
174
+
160
175
  const SizeBox = (props) => {
161
176
  const { children, h, w, size, height, width } = props;
162
177
  const widthValue = size || w || width;
@@ -178,4 +193,4 @@ function ArrayRender(props) {
178
193
  }));
179
194
  }
180
195
 
181
- export { ArrayRender, If, SizeBox, Switch, childrenLoop };
196
+ export { ArrayRender, If, SizeBox, Switch, When, childrenLoop };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwog/react",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "wwog",
@@ -1,4 +1,4 @@
1
- import { Fragment, type ReactNode } from "react";
1
+ import React, { Fragment, type ReactNode } from "react";
2
2
 
3
3
  //#region component Types
4
4
  export interface ArrayRenderProps<T> {
@@ -1,4 +1,4 @@
1
- import { FC, ReactNode } from "react";
1
+ import React, { FC, ReactNode } from "react";
2
2
 
3
3
  interface SizeBoxProps {
4
4
  size?: number | string;
@@ -0,0 +1,67 @@
1
+ import React, { FC, ReactNode, useMemo } from "react";
2
+
3
+ export interface WhenProps {
4
+ /**
5
+ * @description_en An array of conditions where all must be true to render children.
6
+ * @description_zh 一个条件数组,所有条件都为真时渲染子元素。
7
+ * @index 1
8
+ */
9
+ all?: boolean[];
10
+ /**
11
+ * @description_en An array of conditions where at least one must be true to render children.
12
+ * @description_zh 一个条件数组,至少有一个条件为真时渲染子元素。
13
+ * @index 2
14
+ */
15
+ any?: boolean[];
16
+ /**
17
+ * @description_en An array of conditions where all must be false to render children.
18
+ * @description_zh 一个条件数组,所有条件都为假时渲染子元素。
19
+ * @index 3
20
+ */
21
+ none?: boolean[];
22
+ /**
23
+ * @description_en The content to render when conditions are satisfied.
24
+ * @description_zh 满足条件时渲染的内容。
25
+ */
26
+ children?: ReactNode;
27
+ /**
28
+ * @description_en The content to render when conditions are not satisfied (alternative to `Else`).
29
+ * @description_zh 不满足条件时渲染的内容(替代 `Else`)。
30
+ */
31
+ fallback?: ReactNode;
32
+ }
33
+
34
+ /**
35
+ * @description_zh 一个声明式组件,用于基于多个条件进行条件渲染。比If更简洁。
36
+ * @description_en A declarative component for conditional rendering based on multiple conditions. More concise than If.
37
+ * @component
38
+ * @example
39
+ * ```tsx
40
+ * <When all={[isAdmin, hasPermission]}>
41
+ * <AdminPanel />
42
+ * </When>
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```tsx
47
+ * <When any={[isLoading, isFetching]} fallback={<ErrorMessage />}>
48
+ * <Spinner />
49
+ * </When>
50
+ * ```
51
+ *
52
+ */
53
+ export const When: FC<WhenProps> = ({ all, any, none, children, fallback }) => {
54
+ const shouldRender = useMemo(() => {
55
+ if (all && (any || none)) {
56
+ console.warn(
57
+ 'When: Multiple condition types (all, any, none) provided; "all" takes precedence.'
58
+ );
59
+ }
60
+ if (all && all.length > 0 && all.every(Boolean)) return true;
61
+ if (any && any.length > 0 && any.some(Boolean)) return true;
62
+ if (none && none.length > 0 && none.every((v) => !v)) return true;
63
+ return false;
64
+ }, [all, any, none]);
65
+
66
+ return shouldRender ? <>{children}</> : <>{fallback || null}</>;
67
+ };
@@ -1,2 +1,3 @@
1
1
  export * from "./Switch";
2
2
  export * from "./If";
3
+ export * from "./When";