@wwog/react 1.1.4 → 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 +54 -3
- package/dist/index.js +17 -2
- package/package.json +1 -1
- package/src/ProcessControl/When.tsx +67 -0
- package/src/ProcessControl/index.ts +1 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, { ReactNode, FC } from 'react';
|
|
2
2
|
|
|
3
3
|
interface SwitchProps<T> {
|
|
4
4
|
value: T;
|
|
@@ -77,6 +77,57 @@ declare const If: {
|
|
|
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;
|
|
@@ -103,5 +154,5 @@ declare function ArrayRender<T>(props: ArrayRenderProps<T>): ReactNode;
|
|
|
103
154
|
*/
|
|
104
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, { 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;
|
|
@@ -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
|
@@ -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
|
+
};
|