@wwog/react 1.2.0 → 1.2.1
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/README.md +13 -3
- package/dist/index.d.mts +9 -6
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/ProcessControl/Toggle.tsx +28 -17
package/README.md
CHANGED
|
@@ -141,10 +141,12 @@ function Example({ isActive }) {
|
|
|
141
141
|
- `<True condition={...}>`:当 condition 为 true 时渲染子内容。
|
|
142
142
|
- `<False condition={...}>`:当 condition 为 false 时渲染子内容。
|
|
143
143
|
|
|
144
|
-
#### `<Toggle>`
|
|
144
|
+
#### `<Toggle>` (v1.2.0+)
|
|
145
145
|
|
|
146
146
|
声明式切换组件,在预定义选项中切换值,并通过指定属性传递给子组件,支持自定义切换逻辑。
|
|
147
147
|
|
|
148
|
+
> v1.2.1 Indexing is now used to fix bugs with arbitrary values
|
|
149
|
+
|
|
148
150
|
```tsx
|
|
149
151
|
import { Toggle } from "@wwog/react";
|
|
150
152
|
|
|
@@ -161,8 +163,8 @@ const ThemeChild: React.FC<ThemeChildProps> = ({ theme, toggleTheme }) => (
|
|
|
161
163
|
</Toggle>
|
|
162
164
|
```
|
|
163
165
|
|
|
164
|
-
- `source`:初始切换值。
|
|
165
166
|
- `options`:可切换的值数组。
|
|
167
|
+
- `index`:默认:0。
|
|
166
168
|
- `target`:传递切换值给子节点的属性名,默认 value。
|
|
167
169
|
- `toggleTarget`:传递切换函数给子节点的属性名,默认 toggle。
|
|
168
170
|
- `next`:自定义切换逻辑函数。
|
|
@@ -292,12 +294,20 @@ function Layout() {
|
|
|
292
294
|
|
|
293
295
|
- Loop:灵活的迭代渲染,支持数组、对象和范围。
|
|
294
296
|
- Try:封装异步逻辑,处理 Promise 状态。
|
|
295
|
-
- Toggle:基于布尔状态切换 on 和 off 内容。
|
|
296
297
|
- Pick:轻量版值选择渲染,类似枚举匹配。
|
|
297
298
|
- Render:动态渲染函数,简化复杂渲染逻辑。
|
|
298
299
|
- Once:确保内容仅渲染一次,适合初始化。
|
|
299
300
|
- Each:增强列表渲染,支持过滤和排序。
|
|
300
301
|
|
|
302
|
+
### hooks
|
|
303
|
+
|
|
304
|
+
- 一些常用的hooks的封装
|
|
305
|
+
|
|
306
|
+
### useControlled (v1.2.0+)
|
|
307
|
+
|
|
308
|
+
- 受控组件和非受控组件的切换,方便组件开发
|
|
309
|
+
|
|
310
|
+
|
|
301
311
|
## License
|
|
302
312
|
|
|
303
313
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -194,8 +194,9 @@ interface ToggleProps<T = boolean> {
|
|
|
194
194
|
/**
|
|
195
195
|
* @description_en The initial value to toggle.
|
|
196
196
|
* @description_zh 初始切换值。
|
|
197
|
+
* @default 0
|
|
197
198
|
*/
|
|
198
|
-
|
|
199
|
+
index?: number;
|
|
199
200
|
/**
|
|
200
201
|
* @description_en Array of values to toggle between.
|
|
201
202
|
* @description_zh 可切换的值数组。
|
|
@@ -214,11 +215,11 @@ interface ToggleProps<T = boolean> {
|
|
|
214
215
|
*/
|
|
215
216
|
toggleTarget?: string;
|
|
216
217
|
/**
|
|
217
|
-
* @description_en Function to determine the next value in the toggle sequence.
|
|
218
|
-
* @description_zh
|
|
218
|
+
* @description_en Function to determine the next value index in the toggle sequence.
|
|
219
|
+
* @description_zh 确定切换序列中下一个值索引的函数。
|
|
219
220
|
* @optional
|
|
220
221
|
*/
|
|
221
|
-
next?: (
|
|
222
|
+
next?: (curIndex: number, options: T[]) => number;
|
|
222
223
|
/**
|
|
223
224
|
* @description_en Content to render, receiving the toggled value and toggle function via the target and toggleTarget props.
|
|
224
225
|
* @description_zh 渲染的内容,通过 target 和 toggleTarget 属性接收切换后的值和切换函数。
|
|
@@ -251,13 +252,15 @@ interface ToggleProps<T = boolean> {
|
|
|
251
252
|
* options={[1, 2, 3]}
|
|
252
253
|
* target="value"
|
|
253
254
|
* toggleTarget="toggleValue"
|
|
254
|
-
* next={(
|
|
255
|
+
* next={(currentIndex, options) => {
|
|
256
|
+
* return (currentIndex + 1) % options.length;
|
|
257
|
+
* }}
|
|
255
258
|
* >
|
|
256
259
|
* <ValueChild />
|
|
257
260
|
* </Toggle>
|
|
258
261
|
* ```
|
|
259
262
|
*/
|
|
260
|
-
declare const Toggle: <T>(props: ToggleProps<T>) => (string | number | bigint |
|
|
263
|
+
declare const Toggle: <T>(props: ToggleProps<T>) => (string | number | bigint | Iterable<React$1.ReactNode> | Promise<string | number | bigint | boolean | React$1.ReactPortal | React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>> | Iterable<React$1.ReactNode> | null | undefined> | React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>>)[] | null | undefined;
|
|
261
264
|
|
|
262
265
|
interface SizeBoxProps {
|
|
263
266
|
size?: number | string;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import n,{useMemo as y,useState as S,Fragment as
|
|
1
|
+
import n,{useMemo as y,useEffect as C,useState as S,Fragment as T,useCallback as x}from"react";function I(e,l){if(e===void 0)return;let t=0;if(Array.isArray(e)){for(const a of e)if(l(a,t++)===!1)break}else l(e,t)}const $=(e,l)=>e===l,g=e=>n.createElement(n.Fragment,null,e.children);g.displayName="Switch_Case";const E=e=>n.createElement(n.Fragment,null,e.children);E.displayName="Switch_Default";const p=e=>{const{value:l,compare:t=$,children:a,strict:o=!1}=e,r=new Set;let i=null,u=null,h=!1;return I(a,(s,d)=>{if(!n.isValidElement(s))throw new Error(`Switch Children only accepts valid React elements at index ${d}`);const c=s.type;if(c.displayName===g.displayName){const m=s.props;if(r.has(m.value))throw new Error(`Switch found duplicate Case value at index ${d}: ${JSON.stringify(m.value)}${o?" (detected in strict mode)":""}`);if(r.add(m.value),!i&&t(l,m.value)&&(i=m.children,o===!1))return!1}else if(c.displayName===E.displayName){if(h)throw new Error(`Switch can only have one Default child at index ${d}`);if(h=!0,u=s.props.children,!o&&i)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(c.displayName||c.name||c)} at index ${d}`)}),n.createElement(n.Fragment,null,i??u)};p.displayName="Switch",p.Case=g,p.Default=E,p.createTyped=function(){return{Switch:p,Case:g,Default:E}};const w=e=>n.createElement(n.Fragment,null,e.children),N=({children:e})=>n.createElement(n.Fragment,null,e),v=e=>n.createElement(n.Fragment,null,e.children);w.displayName="If_Then",N.displayName="If_Else",v.displayName="If_ElseIf";const f=({condition:e,children:l})=>{let t=null,a=null;const o=[];if(n.Children.forEach(l,r=>{if(!n.isValidElement(r))throw new Error("If component only accepts valid React elements");const i=r.type;if(i.displayName===w.displayName){if(t)throw new Error("If component can only have one Then child");t=r}else if(i.displayName===v.displayName)o.push(r);else if(i.displayName===N.displayName){if(a)throw new Error("If component can only have one Else child");a=r}else throw new Error(`If component only accepts 'Then', 'ElseIf', or 'Else' elements as children, found: ${String(i.displayName||i.name||i)}`)}),e)return t?n.createElement(n.Fragment,null,t.props.children):null;for(const r of o)if(r.props.condition)return n.createElement(n.Fragment,null,r.props.children);return a?n.createElement(n.Fragment,null,a.props.children):null};f.displayName="If",f.Then=w,f.ElseIf=v,f.Else=N,f.createTyped=function(){return{If:f,Then:w,ElseIf:v,Else:N}};const b=({condition:e,children:l})=>e?n.createElement(n.Fragment,null,l):null,k=({condition:e,children:l})=>e===!1?n.createElement(n.Fragment,null,l):null,D=({all:e,any:l,none:t,children:a,fallback:o})=>y(()=>(e&&(l||t)&&console.warn('When: Multiple condition types (all, any, none) provided; "all" takes precedence.'),!!(e&&e.length>0&&e.every(Boolean)||l&&l.length>0&&l.some(Boolean)||t&&t.length>0&&t.every(r=>!r))),[e,l,t])?n.createElement(n.Fragment,null,a):n.createElement(n.Fragment,null,o||null),_=({data:e,transform:l,render:t,fallback:a})=>{const o=y(()=>l.reduce((r,i)=>i(r),e),[e,l]);return o==null?n.createElement(n.Fragment,null,a||null):n.createElement(n.Fragment,null,t(o))},A=e=>{const{index:l=0,options:t,target:a="value",toggleTarget:o="toggle",next:r,children:i}=e;C(()=>{if(t.length<l+1)throw new Error(`Index ${l} is out of bounds for options array of length ${t.length}. Defaulting to first option.`)},[l,t]);const[u,h]=S(l),s=()=>r?r(u,t):(u+1)%t.length,d=()=>{h(c=>t.length?s():c)};return n.Children.map(i,c=>n.isValidElement(c)?n.cloneElement(c,{[a]:u,[o]:d}):c)},B=e=>{const{children:l,h:t,w:a,size:o,height:r,width:i,className:u}=e;return n.createElement("div",{style:{width:o||a||i,height:o||t||r,flexShrink:0},className:u},l)};function O(e){const{items:l,renderItem:t,filter:a}=e;return l?n.createElement(T,null,l.map((o,r)=>a&&!a(o)?null:t(o,r))):(console.error("ArrayRender: items is null"),null)}const R=({let:e,props:l,children:t,fallback:a})=>{const o=y(()=>typeof e=="function"?e(l):e,[e,l]);return!t||!Object.keys(o).length?n.createElement(n.Fragment,null,a||null):n.createElement(n.Fragment,null,t(o))},V="onChange",P="value";function j(e){const{defaultValue:l,onBeforeChange:t,trigger:a=V,valuePropName:o=P,props:r}=e,i=Object.prototype.hasOwnProperty.call(r,o),[u,h]=S(l),s=i?r[o]:u,d=y(()=>r[a],[r,a]),c=x(m=>{const F=typeof m=="function"?m(s):m;t&&t(F,s)===!1||(i||h(F),d&&d(F))},[i,t,a,s,d]);return[s,c]}export{O as ArrayRender,k as False,f as If,_ as Pipe,R as Scope,B as SizeBox,p as Switch,A as Toggle,b as True,D as When,I as childrenLoop,j as useControlled};
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import React, { ReactNode, useState, useMemo } from "react";
|
|
1
|
+
import React, { ReactNode, useState, useMemo, useEffect } from "react";
|
|
2
2
|
|
|
3
3
|
export interface ToggleProps<T = boolean> {
|
|
4
4
|
/**
|
|
5
5
|
* @description_en The initial value to toggle.
|
|
6
6
|
* @description_zh 初始切换值。
|
|
7
|
+
* @default 0
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
+
index?: number;
|
|
9
10
|
/**
|
|
10
11
|
* @description_en Array of values to toggle between.
|
|
11
12
|
* @description_zh 可切换的值数组。
|
|
@@ -24,11 +25,11 @@ export interface ToggleProps<T = boolean> {
|
|
|
24
25
|
*/
|
|
25
26
|
toggleTarget?: string;
|
|
26
27
|
/**
|
|
27
|
-
* @description_en Function to determine the next value in the toggle sequence.
|
|
28
|
-
* @description_zh
|
|
28
|
+
* @description_en Function to determine the next value index in the toggle sequence.
|
|
29
|
+
* @description_zh 确定切换序列中下一个值索引的函数。
|
|
29
30
|
* @optional
|
|
30
31
|
*/
|
|
31
|
-
next?: (
|
|
32
|
+
next?: (curIndex: number, options: T[]) => number;
|
|
32
33
|
/**
|
|
33
34
|
* @description_en Content to render, receiving the toggled value and toggle function via the target and toggleTarget props.
|
|
34
35
|
* @description_zh 渲染的内容,通过 target 和 toggleTarget 属性接收切换后的值和切换函数。
|
|
@@ -62,7 +63,9 @@ export interface ToggleProps<T = boolean> {
|
|
|
62
63
|
* options={[1, 2, 3]}
|
|
63
64
|
* target="value"
|
|
64
65
|
* toggleTarget="toggleValue"
|
|
65
|
-
* next={(
|
|
66
|
+
* next={(currentIndex, options) => {
|
|
67
|
+
* return (currentIndex + 1) % options.length;
|
|
68
|
+
* }}
|
|
66
69
|
* >
|
|
67
70
|
* <ValueChild />
|
|
68
71
|
* </Toggle>
|
|
@@ -70,33 +73,41 @@ export interface ToggleProps<T = boolean> {
|
|
|
70
73
|
*/
|
|
71
74
|
export const Toggle = <T,>(props: ToggleProps<T>) => {
|
|
72
75
|
const {
|
|
73
|
-
|
|
76
|
+
index = 0,
|
|
74
77
|
options,
|
|
75
78
|
target = "value",
|
|
76
79
|
toggleTarget = "toggle",
|
|
77
80
|
next,
|
|
78
81
|
children,
|
|
79
82
|
} = props;
|
|
80
|
-
const defaultValue = useMemo(() => {
|
|
81
|
-
return options.includes(source) ? source : options[0];
|
|
82
|
-
}, [source, options]);
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (options.length < index + 1) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Index ${index} is out of bounds for options array of length ${options.length}. Defaulting to first option.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}, [index, options]);
|
|
91
|
+
|
|
92
|
+
const [curIndex, setCurIndex] = useState<number>(index);
|
|
93
|
+
const nextIndex = () => {
|
|
94
|
+
if (next) {
|
|
95
|
+
return next(curIndex, options);
|
|
96
|
+
}
|
|
97
|
+
return (curIndex + 1) % options.length;
|
|
98
|
+
};
|
|
85
99
|
|
|
86
100
|
const toggle = () => {
|
|
87
|
-
|
|
101
|
+
setCurIndex((prev) => {
|
|
88
102
|
if (!options.length) return prev;
|
|
89
|
-
|
|
90
|
-
? next(prev, options)
|
|
91
|
-
: options[(options.indexOf(prev) + 1) % options.length] || options[0];
|
|
92
|
-
return nextValue;
|
|
103
|
+
return nextIndex();
|
|
93
104
|
});
|
|
94
105
|
};
|
|
95
106
|
|
|
96
107
|
return React.Children.map(children, (child) => {
|
|
97
108
|
if (React.isValidElement(child)) {
|
|
98
109
|
return React.cloneElement(child, {
|
|
99
|
-
[target]:
|
|
110
|
+
[target]: curIndex,
|
|
100
111
|
[toggleTarget]: toggle,
|
|
101
112
|
} as { [key: string]: any });
|
|
102
113
|
}
|