@wwog/react 1.1.6 → 1.1.8

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 CHANGED
@@ -5,6 +5,10 @@
5
5
  [![npm version](https://img.shields.io/npm/v/@wwog/react.svg)](https://www.npmjs.com/package/@wwog/react)
6
6
  [![ESM](https://img.shields.io/badge/📦-ESM%20only-brightgreen.svg)](https://nodejs.org/api/esm.html)
7
7
 
8
+ ---
9
+
10
+ [English Documentation](./README_en.md)
11
+
8
12
  ## 安装
9
13
 
10
14
  ```bash
@@ -141,7 +145,7 @@ function Example({ isActive }) {
141
145
 
142
146
  #### `<ArrayRender>`
143
147
 
144
- 高效渲染数组数据的工具组件,支持过滤和自定义渲染。
148
+ 内部仅单次循环。高效渲染数组数据的工具组件,支持过滤和自定义渲染。
145
149
 
146
150
  ```tsx
147
151
  import { ArrayRender } from "@wwog/react";
@@ -161,10 +165,81 @@ function UserList({ users }) {
161
165
  }
162
166
  ```
163
167
 
168
+ #### `<Pipe>` (v1.1.7+)
169
+
170
+ 声明式的数据管道处理组件,适合多步骤数据转换和链式处理。
171
+
172
+ > 声明式数据处理,替代嵌套函数调用。
173
+ > 提高代码可读性,逻辑清晰。
174
+ > 适合数据清洗、格式化等场景。
175
+
176
+ ```tsx
177
+ import { Pipe } from "@wwog/react";
178
+
179
+ function Example({ users }) {
180
+ return (
181
+ <Pipe
182
+ data={users}
183
+ transform={[
184
+ (data) => data.filter((user) => user.active),
185
+ (data) => data.map((user) => user.name),
186
+ ]}
187
+ render={(names) => <div>{names.join(", ")}</div>}
188
+ fallback={<div>No Data</div>}
189
+ />
190
+ );
191
+ }
192
+ ```
193
+
194
+ - `data`:初始数据。
195
+ - `transform`:数据转换函数数组,按顺序依次处理。
196
+ - `render`:渲染最终结果。
197
+ - `fallback`:结果为 null/undefined 时的兜底内容。
198
+
199
+ #### `<Scope>` (v1.1.7+)
200
+
201
+ 为子节点提供局部作用域,声明式定义临时变量,简化复杂渲染逻辑。
202
+
203
+ > 避免在组件外定义临时状态或计算。
204
+ > 声明式定义局部变量,增强代码自包含性。
205
+ > 适合表单、计算密集型渲染等场景。
206
+
207
+ ```tsx
208
+ import { Scope } from "@wwog/react";
209
+
210
+ function Example() {
211
+ return (
212
+ <Scope let={{ count: 1, text: "Hello" }}>
213
+ {({ count, text }) => (
214
+ <div>
215
+ {text} {count}
216
+ </div>
217
+ )}
218
+ </Scope>
219
+ );
220
+ }
221
+
222
+ // 支持函数式 let
223
+ <Scope
224
+ let={(props) => ({ total: props.items.length })}
225
+ props={{ items: [1, 2] }}
226
+ fallback={<div>Empty</div>}
227
+ >
228
+ {({ total }) => <div>Total: {total}</div>}
229
+ </Scope>;
230
+ ```
231
+
232
+ - `let`:对象或函数,定义作用域变量。
233
+ - `props`:传递给 let 函数的参数。
234
+ - `children`:作用域变量的渲染函数。
235
+ - `fallback`:无内容时的兜底渲染。
236
+
164
237
  #### `<SizeBox>`
165
238
 
166
239
  创建固定尺寸的容器,用于布局调整和间距控制。
167
240
 
241
+ > v1.1.8: Fixed SizeBox not working in 'flex' layouts, add classname props
242
+
168
243
  ```tsx
169
244
  import { SizeBox } from "@wwog/react";
170
245
 
@@ -184,6 +259,18 @@ function Layout() {
184
259
  }
185
260
  ```
186
261
 
262
+ ### Ideas
263
+
264
+ > 需求不高,但有用的组件
265
+
266
+ - Loop:灵活的迭代渲染,支持数组、对象和范围。
267
+ - Try:封装异步逻辑,处理 Promise 状态。
268
+ - Toggle:基于布尔状态切换 on 和 off 内容。
269
+ - Pick:轻量版值选择渲染,类似枚举匹配。
270
+ - Render:动态渲染函数,简化复杂渲染逻辑。
271
+ - Once:确保内容仅渲染一次,适合初始化。
272
+ - Each:增强列表渲染,支持过滤和排序。
273
+
187
274
  ## License
188
275
 
189
276
  MIT
package/dist/index.d.mts CHANGED
@@ -138,6 +138,58 @@ interface WhenProps {
138
138
  */
139
139
  declare const When: FC<WhenProps>;
140
140
 
141
+ /**
142
+ * Props for the `Pipe` component.
143
+ *
144
+ * @interface PipeProps
145
+ * @property {any} data - The initial data to process.
146
+ * @property {((input: any) => any)[]} transform - Array of functions to transform the data.
147
+ * @property {(result: any) => ReactNode} render - Function to render the transformed data.
148
+ * @property {ReactNode} [fallback] - Content to render if the result is null or undefined.
149
+ */
150
+ interface PipeProps {
151
+ /**
152
+ * @description_en The initial data to process.
153
+ * @description_zh 要处理的初始数据。
154
+ */
155
+ data: any;
156
+ /**
157
+ * @description_en Array of functions to transform the data.
158
+ * @description_zh 转换数据的函数数组。
159
+ */
160
+ transform: ((input: any) => any)[];
161
+ /**
162
+ * @description_en Function to render the transformed data.
163
+ * @description_zh 渲染转换后数据的函数。
164
+ */
165
+ render: (result: any) => ReactNode;
166
+ /**
167
+ * @description_en Content to render if the result is null or undefined.
168
+ * @description_zh 如果结果为 null 或 undefined 时渲染的内容。
169
+ * @optional
170
+ * @default null
171
+ */
172
+ fallback?: ReactNode;
173
+ }
174
+ /**
175
+ * @description_zh 一个声明式组件,用于通过管道式处理数据并渲染,简化多步骤数据转换。
176
+ * @description_en A declarative component for processing data through a pipeline and rendering, simplifying multi-step data transformations.
177
+ * @component
178
+ * @example
179
+ * ```tsx
180
+ * <Pipe
181
+ * data={users}
182
+ * transform={[
183
+ * (data) => data.filter(user => user.active),
184
+ * (data) => data.map(user => user.name)
185
+ * ]}
186
+ * render={(names) => <div>{names.join(", ")}</div>}
187
+ * fallback={<div>No Data</div>}
188
+ * />
189
+ * ```
190
+ */
191
+ declare const Pipe: FC<PipeProps>;
192
+
141
193
  interface SizeBoxProps {
142
194
  size?: number | string;
143
195
  height?: number | string;
@@ -145,6 +197,7 @@ interface SizeBoxProps {
145
197
  h?: number | string;
146
198
  w?: number | string;
147
199
  children?: ReactNode;
200
+ className?: string;
148
201
  }
149
202
  /**
150
203
  * @description SizeBox 组件用于设置一个固定大小的盒子
@@ -158,11 +211,66 @@ interface ArrayRenderProps<T> {
158
211
  }
159
212
  declare function ArrayRender<T>(props: ArrayRenderProps<T>): ReactNode;
160
213
 
214
+ /**
215
+ * Props for the `Scope` component.
216
+ *
217
+ * @interface ScopeProps
218
+ * @property {Record<string, any> | ((props: any) => Record<string, any>)} let - An object or function defining local variables for the scope.
219
+ * @property {any} [props] - Optional props passed to the let function.
220
+ * @property {(scope: Record<string, any>) => ReactNode} [children] - Render function to access the scope variables.
221
+ * @property {ReactNode} [fallback] - Content to render if children or scope is empty.
222
+ */
223
+ interface ScopeProps {
224
+ /**
225
+ * @description_en An object or function defining local variables for the scope.
226
+ * @description_zh 定义作用域局部变量的对象或函数。
227
+ */
228
+ let: Record<string, any> | ((props: any) => Record<string, any>);
229
+ /**
230
+ * @description_en Optional props passed to the let function.
231
+ * @description_zh 传递给 let 函数的可选属性。
232
+ * @optional
233
+ */
234
+ props?: any;
235
+ /**
236
+ * @description_en Render function to access the scope variables.
237
+ * @description_zh 访问作用域变量的渲染函数。
238
+ * @optional
239
+ */
240
+ children?: (scope: Record<string, any>) => ReactNode;
241
+ /**
242
+ * @description_en Content to render if children or scope is empty.
243
+ * @description_zh 如果 children 或 scope 为空时渲染的内容。
244
+ * @optional
245
+ * @default null
246
+ */
247
+ fallback?: ReactNode;
248
+ }
249
+ /**
250
+ * @description_zh 一个声明式组件,为子节点提供局部作用域,简化临时状态或上下文管理。
251
+ * @description_en A declarative component that provides a local scope for children, simplifying temporary state or context management.
252
+ * @component
253
+ * @example
254
+ * ```tsx
255
+ * <Scope let={{ count: 1, text: "Hello" }}>
256
+ * {({ count, text }) => <div>{text} {count}</div>}
257
+ * </Scope>
258
+ * ```
259
+ *
260
+ * @example
261
+ * ```tsx
262
+ * <Scope let={(props) => ({ total: props.items.length })} props={{ items: [1, 2] }} fallback={<div>Empty</div>}>
263
+ * {({ total }) => <div>Total: {total}</div>}
264
+ * </Scope>
265
+ * ```
266
+ */
267
+ declare const Scope: FC<ScopeProps>;
268
+
161
269
  /**
162
270
  * @description 性能优化,替代 React.Children.forEach, 回调可以返回 false 来中断循环
163
271
  * @description_en Replace React.Children.forEach, the callback can return false to interrupt the loop
164
272
  */
165
273
  declare function childrenLoop(children: React.ReactNode | undefined, callback: (child: React.ReactNode, index: number) => boolean | void): void;
166
274
 
167
- export { ArrayRender, False, If, SizeBox, Switch, True, When, childrenLoop };
168
- export type { ArrayRenderProps, ElseIfProps, ElseProps, FalseProps, IfProps, SwitchCaseProps, SwitchDefaultProps, SwitchProps, ThenProps, TrueProps, WhenProps };
275
+ export { ArrayRender, False, If, Pipe, Scope, SizeBox, Switch, True, When, childrenLoop };
276
+ export type { ArrayRenderProps, ElseIfProps, ElseProps, FalseProps, IfProps, PipeProps, ScopeProps, SwitchCaseProps, SwitchDefaultProps, SwitchProps, ThenProps, TrueProps, WhenProps };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import n,{useMemo as F,Fragment as I}from"react";function v(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 S=(e,l)=>e===l,f=e=>n.createElement(n.Fragment,null,e.children);f.displayName="Switch_Case";const p=e=>n.createElement(n.Fragment,null,e.children);p.displayName="Switch_Default";const c=e=>{const{value:l,compare:t=S,children:a,strict:i=!1}=e,r=new Set;let o=null,g=null,N=!1;return v(a,(h,u)=>{if(!n.isValidElement(h))throw new Error(`Switch Children only accepts valid React elements at index ${u}`);const d=h.type;if(d.displayName===f.displayName){const m=h.props;if(r.has(m.value))throw new Error(`Switch found duplicate Case value at index ${u}: ${JSON.stringify(m.value)}${i?" (detected in strict mode)":""}`);if(r.add(m.value),!o&&t(l,m.value)&&(o=m.children,i===!1))return!1}else if(d.displayName===p.displayName){if(N)throw new Error(`Switch can only have one Default child at index ${u}`);if(N=!0,g=h.props.children,!i&&o)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(d.displayName||d.name||d)} at index ${u}`)}),n.createElement(n.Fragment,null,o??g)};c.displayName="Switch",c.Case=f,c.Default=p,c.createTyped=function(){return{Switch:c,Case:f,Default:p}};const y=e=>n.createElement(n.Fragment,null,e.children),E=({children:e})=>n.createElement(n.Fragment,null,e),w=e=>n.createElement(n.Fragment,null,e.children);y.displayName="If_Then",E.displayName="If_Else",w.displayName="If_ElseIf";const s=({condition:e,children:l})=>{let t=null,a=null;const i=[];if(n.Children.forEach(l,r=>{if(!n.isValidElement(r))throw new Error("If component only accepts valid React elements");const o=r.type;if(o.displayName===y.displayName){if(t)throw new Error("If component can only have one Then child");t=r}else if(o.displayName===w.displayName)i.push(r);else if(o.displayName===E.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(o.displayName||o.name||o)}`)}),e)return t?n.createElement(n.Fragment,null,t.props.children):null;for(const r of i)if(r.props.condition)return n.createElement(n.Fragment,null,r.props.children);return a?n.createElement(n.Fragment,null,a.props.children):null};s.displayName="If",s.Then=y,s.ElseIf=w,s.Else=E,s.createTyped=function(){return{If:s,Then:y,ElseIf:w,Else:E}};const C=({condition:e,children:l})=>e?n.createElement(n.Fragment,null,l):null,T=({condition:e,children:l})=>e===!1?n.createElement(n.Fragment,null,l):null,x=({all:e,any:l,none:t,children:a,fallback:i})=>F(()=>(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,i||null),$=e=>{const{children:l,h:t,w:a,size:i,height:r,width:o}=e;return n.createElement("div",{style:{width:i||a||o,height:i||t||r}},l)};function D(e){const{items:l,renderItem:t,filter:a}=e;return l?n.createElement(I,null,l.map((i,r)=>a&&!a(i)?null:t(i,r))):(console.error("ArrayRender: items is null"),null)}export{D as ArrayRender,T as False,s as If,$ as SizeBox,c as Switch,C as True,x as When,v as childrenLoop};
1
+ import n,{useMemo as N,Fragment as S}from"react";function v(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 I=(e,l)=>e===l,p=e=>n.createElement(n.Fragment,null,e.children);p.displayName="Switch_Case";const y=e=>n.createElement(n.Fragment,null,e.children);y.displayName="Switch_Default";const o=e=>{const{value:l,compare:t=I,children:a,strict:r=!1}=e,i=new Set;let c=null,u=null,F=!1;return v(a,(h,f)=>{if(!n.isValidElement(h))throw new Error(`Switch Children only accepts valid React elements at index ${f}`);const d=h.type;if(d.displayName===p.displayName){const m=h.props;if(i.has(m.value))throw new Error(`Switch found duplicate Case value at index ${f}: ${JSON.stringify(m.value)}${r?" (detected in strict mode)":""}`);if(i.add(m.value),!c&&t(l,m.value)&&(c=m.children,r===!1))return!1}else if(d.displayName===y.displayName){if(F)throw new Error(`Switch can only have one Default child at index ${f}`);if(F=!0,u=h.props.children,!r&&c)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(d.displayName||d.name||d)} at index ${f}`)}),n.createElement(n.Fragment,null,c??u)};o.displayName="Switch",o.Case=p,o.Default=y,o.createTyped=function(){return{Switch:o,Case:p,Default:y}};const E=e=>n.createElement(n.Fragment,null,e.children),w=({children:e})=>n.createElement(n.Fragment,null,e),g=e=>n.createElement(n.Fragment,null,e.children);E.displayName="If_Then",w.displayName="If_Else",g.displayName="If_ElseIf";const s=({condition:e,children:l})=>{let t=null,a=null;const r=[];if(n.Children.forEach(l,i=>{if(!n.isValidElement(i))throw new Error("If component only accepts valid React elements");const c=i.type;if(c.displayName===E.displayName){if(t)throw new Error("If component can only have one Then child");t=i}else if(c.displayName===g.displayName)r.push(i);else if(c.displayName===w.displayName){if(a)throw new Error("If component can only have one Else child");a=i}else throw new Error(`If component only accepts 'Then', 'ElseIf', or 'Else' elements as children, found: ${String(c.displayName||c.name||c)}`)}),e)return t?n.createElement(n.Fragment,null,t.props.children):null;for(const i of r)if(i.props.condition)return n.createElement(n.Fragment,null,i.props.children);return a?n.createElement(n.Fragment,null,a.props.children):null};s.displayName="If",s.Then=E,s.ElseIf=g,s.Else=w,s.createTyped=function(){return{If:s,Then:E,ElseIf:g,Else:w}};const C=({condition:e,children:l})=>e?n.createElement(n.Fragment,null,l):null,T=({condition:e,children:l})=>e===!1?n.createElement(n.Fragment,null,l):null,k=({all:e,any:l,none:t,children:a,fallback:r})=>N(()=>(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(i=>!i))),[e,l,t])?n.createElement(n.Fragment,null,a):n.createElement(n.Fragment,null,r||null),x=({data:e,transform:l,render:t,fallback:a})=>{const r=N(()=>l.reduce((i,c)=>c(i),e),[e,l]);return r==null?n.createElement(n.Fragment,null,a||null):n.createElement(n.Fragment,null,t(r))},$=e=>{const{children:l,h:t,w:a,size:r,height:i,width:c,className:u}=e;return n.createElement("div",{style:{width:r||a||c,height:r||t||i,flexShrink:0},className:u},l)};function b(e){const{items:l,renderItem:t,filter:a}=e;return l?n.createElement(S,null,l.map((r,i)=>a&&!a(r)?null:t(r,i))):(console.error("ArrayRender: items is null"),null)}const D=({let:e,props:l,children:t,fallback:a})=>{const r=N(()=>typeof e=="function"?e(l):e,[e,l]);return!t||!Object.keys(r).length?n.createElement(n.Fragment,null,a||null):n.createElement(n.Fragment,null,t(r))};export{b as ArrayRender,T as False,s as If,x as Pipe,D as Scope,$ as SizeBox,o as Switch,C as True,k as When,v as childrenLoop};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwog/react",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "wwog",
@@ -0,0 +1,77 @@
1
+ import React, { FC, ReactNode, useMemo } from "react";
2
+
3
+ /**
4
+ * Props for the `Scope` component.
5
+ *
6
+ * @interface ScopeProps
7
+ * @property {Record<string, any> | ((props: any) => Record<string, any>)} let - An object or function defining local variables for the scope.
8
+ * @property {any} [props] - Optional props passed to the let function.
9
+ * @property {(scope: Record<string, any>) => ReactNode} [children] - Render function to access the scope variables.
10
+ * @property {ReactNode} [fallback] - Content to render if children or scope is empty.
11
+ */
12
+ export interface ScopeProps {
13
+ /**
14
+ * @description_en An object or function defining local variables for the scope.
15
+ * @description_zh 定义作用域局部变量的对象或函数。
16
+ */
17
+ let: Record<string, any> | ((props: any) => Record<string, any>);
18
+ /**
19
+ * @description_en Optional props passed to the let function.
20
+ * @description_zh 传递给 let 函数的可选属性。
21
+ * @optional
22
+ */
23
+ props?: any;
24
+ /**
25
+ * @description_en Render function to access the scope variables.
26
+ * @description_zh 访问作用域变量的渲染函数。
27
+ * @optional
28
+ */
29
+ children?: (scope: Record<string, any>) => ReactNode;
30
+ /**
31
+ * @description_en Content to render if children or scope is empty.
32
+ * @description_zh 如果 children 或 scope 为空时渲染的内容。
33
+ * @optional
34
+ * @default null
35
+ */
36
+ fallback?: ReactNode;
37
+ }
38
+ /*
39
+ 设计原因
40
+ 避免在组件外定义临时状态或计算。
41
+ 声明式定义局部变量,增强代码自包含性。
42
+ 适合表单、计算密集型渲染等场景。
43
+ */
44
+ /**
45
+ * @description_zh 一个声明式组件,为子节点提供局部作用域,简化临时状态或上下文管理。
46
+ * @description_en A declarative component that provides a local scope for children, simplifying temporary state or context management.
47
+ * @component
48
+ * @example
49
+ * ```tsx
50
+ * <Scope let={{ count: 1, text: "Hello" }}>
51
+ * {({ count, text }) => <div>{text} {count}</div>}
52
+ * </Scope>
53
+ * ```
54
+ *
55
+ * @example
56
+ * ```tsx
57
+ * <Scope let={(props) => ({ total: props.items.length })} props={{ items: [1, 2] }} fallback={<div>Empty</div>}>
58
+ * {({ total }) => <div>Total: {total}</div>}
59
+ * </Scope>
60
+ * ```
61
+ */
62
+ export const Scope: FC<ScopeProps> = ({
63
+ let: letValue,
64
+ props,
65
+ children,
66
+ fallback,
67
+ }) => {
68
+ const scope = useMemo(() => {
69
+ return typeof letValue === "function" ? letValue(props) : letValue;
70
+ }, [letValue, props]);
71
+
72
+ if (!children || !Object.keys(scope).length) {
73
+ return <>{fallback || null}</>;
74
+ }
75
+
76
+ return <>{children(scope)}</>;
77
+ };
@@ -7,16 +7,22 @@ interface SizeBoxProps {
7
7
  h?: number | string;
8
8
  w?: number | string;
9
9
  children?: ReactNode;
10
+ className?: string;
10
11
  }
11
12
  /**
12
13
  * @description SizeBox 组件用于设置一个固定大小的盒子
13
14
  */
14
15
  export const SizeBox: FC<SizeBoxProps> = (props) => {
15
- const { children, h, w, size, height, width } = props;
16
+ const { children, h, w, size, height, width, className } = props;
16
17
 
17
18
  const widthValue = size || w || width;
18
19
  const heightValue = size || h || height;
19
20
  return (
20
- <div style={{ width: widthValue, height: heightValue }}>{children}</div>
21
+ <div
22
+ style={{ width: widthValue, height: heightValue, flexShrink: 0 }}
23
+ className={className}
24
+ >
25
+ {children}
26
+ </div>
21
27
  );
22
28
  };
@@ -1,2 +1,3 @@
1
1
  export * from "./SizeBox";
2
2
  export * from "./ArrayRender";
3
+ export * from "./Scope";
@@ -0,0 +1,64 @@
1
+ import React, { FC, ReactNode, useMemo } from "react";
2
+
3
+ /**
4
+ * Props for the `Pipe` component.
5
+ *
6
+ * @interface PipeProps
7
+ * @property {any} data - The initial data to process.
8
+ * @property {((input: any) => any)[]} transform - Array of functions to transform the data.
9
+ * @property {(result: any) => ReactNode} render - Function to render the transformed data.
10
+ * @property {ReactNode} [fallback] - Content to render if the result is null or undefined.
11
+ */
12
+ export interface PipeProps {
13
+ /**
14
+ * @description_en The initial data to process.
15
+ * @description_zh 要处理的初始数据。
16
+ */
17
+ data: any;
18
+ /**
19
+ * @description_en Array of functions to transform the data.
20
+ * @description_zh 转换数据的函数数组。
21
+ */
22
+ transform: ((input: any) => any)[];
23
+ /**
24
+ * @description_en Function to render the transformed data.
25
+ * @description_zh 渲染转换后数据的函数。
26
+ */
27
+ render: (result: any) => ReactNode;
28
+ /**
29
+ * @description_en Content to render if the result is null or undefined.
30
+ * @description_zh 如果结果为 null 或 undefined 时渲染的内容。
31
+ * @optional
32
+ * @default null
33
+ */
34
+ fallback?: ReactNode;
35
+ }
36
+
37
+ /**
38
+ * @description_zh 一个声明式组件,用于通过管道式处理数据并渲染,简化多步骤数据转换。
39
+ * @description_en A declarative component for processing data through a pipeline and rendering, simplifying multi-step data transformations.
40
+ * @component
41
+ * @example
42
+ * ```tsx
43
+ * <Pipe
44
+ * data={users}
45
+ * transform={[
46
+ * (data) => data.filter(user => user.active),
47
+ * (data) => data.map(user => user.name)
48
+ * ]}
49
+ * render={(names) => <div>{names.join(", ")}</div>}
50
+ * fallback={<div>No Data</div>}
51
+ * />
52
+ * ```
53
+ */
54
+ export const Pipe: FC<PipeProps> = ({ data, transform, render, fallback }) => {
55
+ const result = useMemo(() => {
56
+ return transform.reduce((acc, fn) => fn(acc), data);
57
+ }, [data, transform]);
58
+
59
+ if (result == null) {
60
+ return <>{fallback || null}</>;
61
+ }
62
+
63
+ return <>{render(result)}</>;
64
+ };
@@ -1,3 +1,4 @@
1
1
  export * from "./Switch";
2
2
  export * from "./If";
3
3
  export * from "./When";
4
+ export * from "./Pipe";