@naderikladious/react-flow 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Nader Ikladious
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # react-flow
2
+
3
+ Utilities for declarative control-flow primitives in React, written in TypeScript and packaged for ESM/CJS consumers.
4
+
5
+ ## Motivation
6
+ - Keep conditional and iterative UI logic readable without custom hooks per component.
7
+ - Provide type-safe, render-prop based building blocks (`Flow.Condition`, `Flow.If`, `Flow.ForEach`, etc.).
8
+ - Ship tree-shakeable named exports for both bundlers and Node targets.
9
+
10
+ ## Installation
11
+ ```bash
12
+ npm install react-flow
13
+ ```
14
+
15
+ If working from this repo locally, install and build the library first:
16
+ ```bash
17
+ npm install
18
+ npm run build
19
+ ```
20
+
21
+ To explore the Vite example app:
22
+ ```bash
23
+ cd example
24
+ npm install
25
+ npm run dev
26
+ ```
27
+
28
+ ## API
29
+ - `Flow.Condition` — provides a boolean value to descendants.
30
+ - `Flow.If` — renders children when the condition is true (prop overrides context).
31
+ - `Flow.Else` — renders children when the condition is false (prop overrides context).
32
+ - `Flow.ForEach<T>` — iterates an array with an optional `keyExtractor` and render-function children `(item, index) => ReactNode`.
33
+ - `Flow.For` — iterates `count` times with optional `start` and `step`, render-function children `(index) => ReactNode`.
34
+ - `Flow.Batch<T>` — groups items into chunks of `batchSize` and renders `(batch, batchIndex) => ReactNode` using `Flow.ForEach` under the hood.
35
+ - `useFlowCondition` — hook to read the nearest `Flow.Condition` value (throws if missing).
36
+
37
+ All components are also available as named exports (tree-shakeable) in addition to the `Flow` namespace object.
38
+
39
+ ## Examples
40
+ Basic conditionals:
41
+ ```tsx
42
+ <Flow.Condition value={isEnabled}>
43
+ <Flow.If>
44
+ <div>Enabled</div>
45
+ </Flow.If>
46
+ <Flow.Else>
47
+ <div>Disabled</div>
48
+ </Flow.Else>
49
+ </Flow.Condition>
50
+ ```
51
+
52
+ Lists and batches:
53
+ ```tsx
54
+ <Flow.ForEach items={items} keyExtractor={(item) => item.id}>
55
+ {(item) => <div>{item.name}</div>}
56
+ </Flow.ForEach>
57
+
58
+ <Flow.Batch items={products} batchSize={3}>
59
+ {(batch, i) => (
60
+ <section>
61
+ <h3>Batch {i + 1}</h3>
62
+ {batch.map((product) => (
63
+ <div key={product.id}>{product.title}</div>
64
+ ))}
65
+ </section>
66
+ )}
67
+ </Flow.Batch>
68
+ ```
69
+
70
+ Numeric loops:
71
+ ```tsx
72
+ <Flow.For count={5} start={1}>
73
+ {(index) => <span key={index}>{index}</span>}
74
+ </Flow.For>
75
+ ```
76
+
77
+ ## Scripts
78
+ - `npm run build` — bundle to `dist/` (CJS, ESM, and `.d.ts`).
79
+ - `npm run clean` — clear and recreate `dist/`.
80
+ - `npm run lint` — placeholder.
81
+
82
+ ## Project structure
83
+ - `src/` — library source.
84
+ - `dist/` — build output (CJS, ESM, types).
85
+ - `example/` — Vite app consuming the local package via `file:..`.
package/dist/index.cjs ADDED
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var React = require('react');
7
+
8
+ const FlowConditionContext = React.createContext({
9
+ value: false,
10
+ hasProvider: false,
11
+ });
12
+ const Condition = ({ value, children }) => {
13
+ return (jsxRuntime.jsx(FlowConditionContext.Provider, { value: { value, hasProvider: true }, children: children }));
14
+ };
15
+ const resolveCondition = (condition, context, componentName) => {
16
+ if (typeof condition === "boolean") {
17
+ return condition;
18
+ }
19
+ if (context.hasProvider) {
20
+ return context.value;
21
+ }
22
+ throw new Error(`${componentName} requires a condition prop or Flow.Condition parent.`);
23
+ };
24
+ const If = ({ condition, children }) => {
25
+ const context = React.useContext(FlowConditionContext);
26
+ const active = resolveCondition(condition, context, "Flow.If");
27
+ return active ? jsxRuntime.jsx(jsxRuntime.Fragment, { children: children }) : null;
28
+ };
29
+ const Else = ({ condition, children }) => {
30
+ const context = React.useContext(FlowConditionContext);
31
+ const active = resolveCondition(condition, context, "Flow.Else");
32
+ return !active ? jsxRuntime.jsx(jsxRuntime.Fragment, { children: children }) : null;
33
+ };
34
+ const ForEach = ({ items, keyExtractor, children }) => {
35
+ return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: items.map((item, index) => (jsxRuntime.jsx(React.Fragment, { children: children(item, index) }, keyExtractor ? keyExtractor(item, index) : index))) }));
36
+ };
37
+ const For = ({ count, start = 0, step = 1, children }) => {
38
+ if (!Number.isFinite(count) || count < 0) {
39
+ console.warn("Flow.For received an invalid count; rendering nothing.");
40
+ return null;
41
+ }
42
+ if (!Number.isFinite(step) || step === 0) {
43
+ throw new Error("Flow.For requires a finite non-zero step.");
44
+ }
45
+ const iterations = Math.min(count, 10000);
46
+ const items = Array.from({ length: iterations }, (_, i) => start + i * step);
47
+ if (iterations !== count) {
48
+ // Guardrail against accidental infinite or huge loops.
49
+ console.warn("Flow.For capped iterations at 10,000 to avoid infinite loops.");
50
+ }
51
+ return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: items.map((value, idx) => (jsxRuntime.jsx(React.Fragment, { children: children(value) }, value))) }));
52
+ };
53
+ const chunkItems = (items, batchSize) => {
54
+ const batches = [];
55
+ for (let i = 0; i < items.length; i += batchSize) {
56
+ batches.push(items.slice(i, i + batchSize));
57
+ }
58
+ return batches;
59
+ };
60
+ const Batch = ({ items, batchSize, children }) => {
61
+ if (!Number.isInteger(batchSize) || batchSize <= 0) {
62
+ throw new Error("Flow.Batch requires a positive integer batchSize.");
63
+ }
64
+ const batches = chunkItems(items, batchSize);
65
+ return (jsxRuntime.jsx(ForEach, { items: batches, keyExtractor: (_, index) => index, children: (batch, batchIndex) => children(batch, batchIndex) }));
66
+ };
67
+ const useFlowCondition = () => {
68
+ const context = React.useContext(FlowConditionContext);
69
+ if (!context.hasProvider) {
70
+ throw new Error("useFlowCondition must be used within a Flow.Condition.");
71
+ }
72
+ return context.value;
73
+ };
74
+ const Flow = {
75
+ Condition,
76
+ If,
77
+ Else,
78
+ ForEach,
79
+ For,
80
+ Batch,
81
+ };
82
+ const ReactFlow = ({ message = "Hello from react-flow" }) => {
83
+ return jsxRuntime.jsx("div", { children: message });
84
+ };
85
+
86
+ exports.Batch = Batch;
87
+ exports.Condition = Condition;
88
+ exports.Else = Else;
89
+ exports.Flow = Flow;
90
+ exports.For = For;
91
+ exports.ForEach = ForEach;
92
+ exports.If = If;
93
+ exports.ReactFlow = ReactFlow;
94
+ exports.default = ReactFlow;
95
+ exports.useFlowCondition = useFlowCondition;
96
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.tsx"],"sourcesContent":["import React from \"react\";\n\ntype FlowConditionContextValue = {\n value: boolean;\n hasProvider: boolean;\n};\n\nconst FlowConditionContext = React.createContext<FlowConditionContextValue>({\n value: false,\n hasProvider: false,\n});\n\nexport type FlowConditionProps = {\n value: boolean;\n children?: React.ReactNode;\n};\n\nexport const Condition: React.FC<FlowConditionProps> = ({ value, children }) => {\n return (\n <FlowConditionContext.Provider value={{ value, hasProvider: true }}>\n {children}\n </FlowConditionContext.Provider>\n );\n};\n\nexport type FlowConditionalBranchProps = {\n condition?: boolean;\n children?: React.ReactNode;\n};\n\nconst resolveCondition = (\n condition: boolean | undefined,\n context: FlowConditionContextValue,\n componentName: string\n) => {\n if (typeof condition === \"boolean\") {\n return condition;\n }\n if (context.hasProvider) {\n return context.value;\n }\n throw new Error(`${componentName} requires a condition prop or Flow.Condition parent.`);\n};\n\nexport const If: React.FC<FlowConditionalBranchProps> = ({ condition, children }) => {\n const context = React.useContext(FlowConditionContext);\n const active = resolveCondition(condition, context, \"Flow.If\");\n return active ? <>{children}</> : null;\n};\n\nexport const Else: React.FC<FlowConditionalBranchProps> = ({ condition, children }) => {\n const context = React.useContext(FlowConditionContext);\n const active = resolveCondition(condition, context, \"Flow.Else\");\n return !active ? <>{children}</> : null;\n};\n\nexport type FlowForEachProps<T> = {\n items: T[];\n keyExtractor?: (item: T, index: number) => React.Key;\n children: (item: T, index: number) => React.ReactNode;\n};\n\nexport const ForEach = <T,>({ items, keyExtractor, children }: FlowForEachProps<T>): React.ReactElement => {\n return (\n <>\n {items.map((item, index) => (\n <React.Fragment key={keyExtractor ? keyExtractor(item, index) : index}>{children(item, index)}</React.Fragment>\n ))}\n </>\n );\n};\n\nexport type FlowForProps = {\n count: number;\n start?: number;\n step?: number;\n children: (index: number) => React.ReactNode;\n};\n\nexport const For: React.FC<FlowForProps> = ({ count, start = 0, step = 1, children }) => {\n if (!Number.isFinite(count) || count < 0) {\n console.warn(\"Flow.For received an invalid count; rendering nothing.\");\n return null;\n }\n if (!Number.isFinite(step) || step === 0) {\n throw new Error(\"Flow.For requires a finite non-zero step.\");\n }\n\n const iterations = Math.min(count, 10_000);\n const items = Array.from({ length: iterations }, (_, i) => start + i * step);\n\n if (iterations !== count) {\n // Guardrail against accidental infinite or huge loops.\n console.warn(\"Flow.For capped iterations at 10,000 to avoid infinite loops.\");\n }\n\n return (\n <>\n {items.map((value, idx) => (\n <React.Fragment key={value}>{children(value)}</React.Fragment>\n ))}\n </>\n );\n};\n\nexport type FlowBatchProps<T> = {\n items: T[];\n batchSize: number;\n children: (batch: T[], batchIndex: number) => React.ReactNode;\n};\n\nconst chunkItems = <T,>(items: T[], batchSize: number): T[][] => {\n const batches: T[][] = [];\n for (let i = 0; i < items.length; i += batchSize) {\n batches.push(items.slice(i, i + batchSize));\n }\n return batches;\n};\n\nexport const Batch = <T,>({ items, batchSize, children }: FlowBatchProps<T>): React.ReactElement => {\n if (!Number.isInteger(batchSize) || batchSize <= 0) {\n throw new Error(\"Flow.Batch requires a positive integer batchSize.\");\n }\n\n const batches = chunkItems(items, batchSize);\n\n return (\n <ForEach\n items={batches}\n keyExtractor={(_, index) => index}\n >\n {(batch, batchIndex) => children(batch, batchIndex)}\n </ForEach>\n );\n};\n\nexport const useFlowCondition = () => {\n const context = React.useContext(FlowConditionContext);\n if (!context.hasProvider) {\n throw new Error(\"useFlowCondition must be used within a Flow.Condition.\");\n }\n return context.value;\n};\n\nexport const Flow = {\n Condition,\n If,\n Else,\n ForEach,\n For,\n Batch,\n};\n\nexport type ReactFlowProps = {\n message?: string;\n};\n\nexport const ReactFlow: React.FC<ReactFlowProps> = ({ message = \"Hello from react-flow\" }) => {\n return <div>{message}</div>;\n};\n\nexport default ReactFlow;\n"],"names":["_jsx","_Fragment"],"mappings":";;;;;;;AAOA,MAAM,oBAAoB,GAAG,KAAK,CAAC,aAAa,CAA4B;AAC1E,IAAA,KAAK,EAAE,KAAK;AACZ,IAAA,WAAW,EAAE,KAAK;AACnB,CAAA,CAAC;AAOK,MAAM,SAAS,GAAiC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAI;AA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G,uBAAuB,EAAE,KAAI;IAC3F,OAAOA,cAAA,CAAA,KAAA,EAAA,EAAA,QAAA,EAAM,OAAO,EAAA,CAAO;AAC7B;;;;;;;;;;;;;"}
@@ -0,0 +1,48 @@
1
+ import React from 'react';
2
+
3
+ type FlowConditionProps = {
4
+ value: boolean;
5
+ children?: React.ReactNode;
6
+ };
7
+ declare const Condition: React.FC<FlowConditionProps>;
8
+ type FlowConditionalBranchProps = {
9
+ condition?: boolean;
10
+ children?: React.ReactNode;
11
+ };
12
+ declare const If: React.FC<FlowConditionalBranchProps>;
13
+ declare const Else: React.FC<FlowConditionalBranchProps>;
14
+ type FlowForEachProps<T> = {
15
+ items: T[];
16
+ keyExtractor?: (item: T, index: number) => React.Key;
17
+ children: (item: T, index: number) => React.ReactNode;
18
+ };
19
+ declare const ForEach: <T>({ items, keyExtractor, children }: FlowForEachProps<T>) => React.ReactElement;
20
+ type FlowForProps = {
21
+ count: number;
22
+ start?: number;
23
+ step?: number;
24
+ children: (index: number) => React.ReactNode;
25
+ };
26
+ declare const For: React.FC<FlowForProps>;
27
+ type FlowBatchProps<T> = {
28
+ items: T[];
29
+ batchSize: number;
30
+ children: (batch: T[], batchIndex: number) => React.ReactNode;
31
+ };
32
+ declare const Batch: <T>({ items, batchSize, children }: FlowBatchProps<T>) => React.ReactElement;
33
+ declare const useFlowCondition: () => boolean;
34
+ declare const Flow: {
35
+ Condition: React.FC<FlowConditionProps>;
36
+ If: React.FC<FlowConditionalBranchProps>;
37
+ Else: React.FC<FlowConditionalBranchProps>;
38
+ ForEach: <T>({ items, keyExtractor, children }: FlowForEachProps<T>) => React.ReactElement;
39
+ For: React.FC<FlowForProps>;
40
+ Batch: <T>({ items, batchSize, children }: FlowBatchProps<T>) => React.ReactElement;
41
+ };
42
+ type ReactFlowProps = {
43
+ message?: string;
44
+ };
45
+ declare const ReactFlow: React.FC<ReactFlowProps>;
46
+
47
+ export { Batch, Condition, Else, Flow, For, ForEach, If, ReactFlow, ReactFlow as default, useFlowCondition };
48
+ export type { FlowBatchProps, FlowConditionProps, FlowConditionalBranchProps, FlowForEachProps, FlowForProps, ReactFlowProps };
package/dist/index.mjs ADDED
@@ -0,0 +1,83 @@
1
+ import { jsx, Fragment } from 'react/jsx-runtime';
2
+ import React from 'react';
3
+
4
+ const FlowConditionContext = React.createContext({
5
+ value: false,
6
+ hasProvider: false,
7
+ });
8
+ const Condition = ({ value, children }) => {
9
+ return (jsx(FlowConditionContext.Provider, { value: { value, hasProvider: true }, children: children }));
10
+ };
11
+ const resolveCondition = (condition, context, componentName) => {
12
+ if (typeof condition === "boolean") {
13
+ return condition;
14
+ }
15
+ if (context.hasProvider) {
16
+ return context.value;
17
+ }
18
+ throw new Error(`${componentName} requires a condition prop or Flow.Condition parent.`);
19
+ };
20
+ const If = ({ condition, children }) => {
21
+ const context = React.useContext(FlowConditionContext);
22
+ const active = resolveCondition(condition, context, "Flow.If");
23
+ return active ? jsx(Fragment, { children: children }) : null;
24
+ };
25
+ const Else = ({ condition, children }) => {
26
+ const context = React.useContext(FlowConditionContext);
27
+ const active = resolveCondition(condition, context, "Flow.Else");
28
+ return !active ? jsx(Fragment, { children: children }) : null;
29
+ };
30
+ const ForEach = ({ items, keyExtractor, children }) => {
31
+ return (jsx(Fragment, { children: items.map((item, index) => (jsx(React.Fragment, { children: children(item, index) }, keyExtractor ? keyExtractor(item, index) : index))) }));
32
+ };
33
+ const For = ({ count, start = 0, step = 1, children }) => {
34
+ if (!Number.isFinite(count) || count < 0) {
35
+ console.warn("Flow.For received an invalid count; rendering nothing.");
36
+ return null;
37
+ }
38
+ if (!Number.isFinite(step) || step === 0) {
39
+ throw new Error("Flow.For requires a finite non-zero step.");
40
+ }
41
+ const iterations = Math.min(count, 10000);
42
+ const items = Array.from({ length: iterations }, (_, i) => start + i * step);
43
+ if (iterations !== count) {
44
+ // Guardrail against accidental infinite or huge loops.
45
+ console.warn("Flow.For capped iterations at 10,000 to avoid infinite loops.");
46
+ }
47
+ return (jsx(Fragment, { children: items.map((value, idx) => (jsx(React.Fragment, { children: children(value) }, value))) }));
48
+ };
49
+ const chunkItems = (items, batchSize) => {
50
+ const batches = [];
51
+ for (let i = 0; i < items.length; i += batchSize) {
52
+ batches.push(items.slice(i, i + batchSize));
53
+ }
54
+ return batches;
55
+ };
56
+ const Batch = ({ items, batchSize, children }) => {
57
+ if (!Number.isInteger(batchSize) || batchSize <= 0) {
58
+ throw new Error("Flow.Batch requires a positive integer batchSize.");
59
+ }
60
+ const batches = chunkItems(items, batchSize);
61
+ return (jsx(ForEach, { items: batches, keyExtractor: (_, index) => index, children: (batch, batchIndex) => children(batch, batchIndex) }));
62
+ };
63
+ const useFlowCondition = () => {
64
+ const context = React.useContext(FlowConditionContext);
65
+ if (!context.hasProvider) {
66
+ throw new Error("useFlowCondition must be used within a Flow.Condition.");
67
+ }
68
+ return context.value;
69
+ };
70
+ const Flow = {
71
+ Condition,
72
+ If,
73
+ Else,
74
+ ForEach,
75
+ For,
76
+ Batch,
77
+ };
78
+ const ReactFlow = ({ message = "Hello from react-flow" }) => {
79
+ return jsx("div", { children: message });
80
+ };
81
+
82
+ export { Batch, Condition, Else, Flow, For, ForEach, If, ReactFlow, ReactFlow as default, useFlowCondition };
83
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.tsx"],"sourcesContent":["import React from \"react\";\n\ntype FlowConditionContextValue = {\n value: boolean;\n hasProvider: boolean;\n};\n\nconst FlowConditionContext = React.createContext<FlowConditionContextValue>({\n value: false,\n hasProvider: false,\n});\n\nexport type FlowConditionProps = {\n value: boolean;\n children?: React.ReactNode;\n};\n\nexport const Condition: React.FC<FlowConditionProps> = ({ value, children }) => {\n return (\n <FlowConditionContext.Provider value={{ value, hasProvider: true }}>\n {children}\n </FlowConditionContext.Provider>\n );\n};\n\nexport type FlowConditionalBranchProps = {\n condition?: boolean;\n children?: React.ReactNode;\n};\n\nconst resolveCondition = (\n condition: boolean | undefined,\n context: FlowConditionContextValue,\n componentName: string\n) => {\n if (typeof condition === \"boolean\") {\n return condition;\n }\n if (context.hasProvider) {\n return context.value;\n }\n throw new Error(`${componentName} requires a condition prop or Flow.Condition parent.`);\n};\n\nexport const If: React.FC<FlowConditionalBranchProps> = ({ condition, children }) => {\n const context = React.useContext(FlowConditionContext);\n const active = resolveCondition(condition, context, \"Flow.If\");\n return active ? <>{children}</> : null;\n};\n\nexport const Else: React.FC<FlowConditionalBranchProps> = ({ condition, children }) => {\n const context = React.useContext(FlowConditionContext);\n const active = resolveCondition(condition, context, \"Flow.Else\");\n return !active ? <>{children}</> : null;\n};\n\nexport type FlowForEachProps<T> = {\n items: T[];\n keyExtractor?: (item: T, index: number) => React.Key;\n children: (item: T, index: number) => React.ReactNode;\n};\n\nexport const ForEach = <T,>({ items, keyExtractor, children }: FlowForEachProps<T>): React.ReactElement => {\n return (\n <>\n {items.map((item, index) => (\n <React.Fragment key={keyExtractor ? keyExtractor(item, index) : index}>{children(item, index)}</React.Fragment>\n ))}\n </>\n );\n};\n\nexport type FlowForProps = {\n count: number;\n start?: number;\n step?: number;\n children: (index: number) => React.ReactNode;\n};\n\nexport const For: React.FC<FlowForProps> = ({ count, start = 0, step = 1, children }) => {\n if (!Number.isFinite(count) || count < 0) {\n console.warn(\"Flow.For received an invalid count; rendering nothing.\");\n return null;\n }\n if (!Number.isFinite(step) || step === 0) {\n throw new Error(\"Flow.For requires a finite non-zero step.\");\n }\n\n const iterations = Math.min(count, 10_000);\n const items = Array.from({ length: iterations }, (_, i) => start + i * step);\n\n if (iterations !== count) {\n // Guardrail against accidental infinite or huge loops.\n console.warn(\"Flow.For capped iterations at 10,000 to avoid infinite loops.\");\n }\n\n return (\n <>\n {items.map((value, idx) => (\n <React.Fragment key={value}>{children(value)}</React.Fragment>\n ))}\n </>\n );\n};\n\nexport type FlowBatchProps<T> = {\n items: T[];\n batchSize: number;\n children: (batch: T[], batchIndex: number) => React.ReactNode;\n};\n\nconst chunkItems = <T,>(items: T[], batchSize: number): T[][] => {\n const batches: T[][] = [];\n for (let i = 0; i < items.length; i += batchSize) {\n batches.push(items.slice(i, i + batchSize));\n }\n return batches;\n};\n\nexport const Batch = <T,>({ items, batchSize, children }: FlowBatchProps<T>): React.ReactElement => {\n if (!Number.isInteger(batchSize) || batchSize <= 0) {\n throw new Error(\"Flow.Batch requires a positive integer batchSize.\");\n }\n\n const batches = chunkItems(items, batchSize);\n\n return (\n <ForEach\n items={batches}\n keyExtractor={(_, index) => index}\n >\n {(batch, batchIndex) => children(batch, batchIndex)}\n </ForEach>\n );\n};\n\nexport const useFlowCondition = () => {\n const context = React.useContext(FlowConditionContext);\n if (!context.hasProvider) {\n throw new Error(\"useFlowCondition must be used within a Flow.Condition.\");\n }\n return context.value;\n};\n\nexport const Flow = {\n Condition,\n If,\n Else,\n ForEach,\n For,\n Batch,\n};\n\nexport type ReactFlowProps = {\n message?: string;\n};\n\nexport const ReactFlow: React.FC<ReactFlowProps> = ({ message = \"Hello from react-flow\" }) => {\n return <div>{message}</div>;\n};\n\nexport default ReactFlow;\n"],"names":["_jsx","_Fragment"],"mappings":";;;AAOA,MAAM,oBAAoB,GAAG,KAAK,CAAC,aAAa,CAA4B;AAC1E,IAAA,KAAK,EAAE,KAAK;AACZ,IAAA,WAAW,EAAE,KAAK;AACnB,CAAA,CAAC;AAOK,MAAM,SAAS,GAAiC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAI;AAC7E,IAAA,QACEA,GAAA,CAAC,oBAAoB,CAAC,QAAQ,EAAA,EAAC,KAAK,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,YAC/D,QAAQ,EAAA,CACqB;AAEpC;AAOA,MAAM,gBAAgB,GAAG,CACvB,SAA8B,EAC9B,OAAkC,EAClC,aAAqB,KACnB;AACF,IAAA,IAAI,OAAO,SAAS,KAAK,SAAS,EAAE;AAClC,QAAA,OAAO,SAAS;IAClB;AACA,IAAA,IAAI,OAAO,CAAC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AChB,CAAC;AAEM,MAAM,KAAK,GAAG,CAAK,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAqB,KAAwB;AACjG,IAAA,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,SAAS,IAAI,CAAC,EAAE;AAClD,QAAA,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC;IACtE;IAEA,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;AAE5C,IAAA,QACEA,GAAA,CAAC,OAAO,EAAA,EACN,KAAK,EAAE,OAAO,EACd,YAAY,EAAE,CAAC,CAAC,EAAE,KAAK,KAAK,KAAK,EAAA,QAAA,EAEhC,CAAC,KAAK,EAAE,UAAU,KAAK,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,EAAA,CAC3C;AAEd;AAEO,MAAM,gBAAgB,GAAG,MAAK;IACnC,MAAM,OAAO,GAAG,KAAK,CAAC,UAAU,CAAC,oBAAoB,CAAC;AACtD,IAAA,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;AACxB,QAAA,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC;IAC3E;IACA,OAAO,OAAO,CAAC,KAAK;AACtB;AAEO,MAAM,IAAI,GAAG;IAClB,SAAS;IACT,EAAE;IACF,IAAI;IACJ,OAAO;IACP,GAAG;IACH,KAAK;;AAOA,MAAM,SAAS,GAA6B,CAAC,EAAE,OAAO,GAAG,uBAAuB,EAAE,KAAI;IAC3F,OAAOA,GAAA,CAAA,KAAA,EAAA,EAAA,QAAA,EAAM,OAAO,EAAA,CAAO;AAC7B;;;;"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@naderikladious/react-flow",
3
+ "version": "0.1.0",
4
+ "description": "Declarative control-flow primitives for React (Condition/If/Else, For/ForEach, Batch) with TypeScript types.",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/NaderIkladious/react-flow.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/NaderIkladious/react-flow/issues"
17
+ },
18
+ "homepage": "https://github.com/NaderIkladious/react-flow#readme",
19
+ "scripts": {
20
+ "build": "npm run clean && rollup -c",
21
+ "clean": "rm -rf dist && mkdir -p dist",
22
+ "dev": "npm run build -- --watch",
23
+ "lint": "echo 'No linter configured yet'",
24
+ "release": "npm run build"
25
+ },
26
+ "keywords": [
27
+ "react",
28
+ "typescript",
29
+ "library"
30
+ ],
31
+ "author": "Nader Ikladious",
32
+ "license": "MIT",
33
+ "dependencies": {
34
+ "tslib": "^2.6.2"
35
+ },
36
+ "peerDependencies": {
37
+ "react": ">=17",
38
+ "react-dom": ">=17"
39
+ },
40
+ "devDependencies": {
41
+ "@types/react": "^18.2.0",
42
+ "@types/react-dom": "^18.2.0",
43
+ "@rollup/plugin-commonjs": "^25.0.7",
44
+ "@rollup/plugin-node-resolve": "^15.2.3",
45
+ "@rollup/plugin-typescript": "^11.1.6",
46
+ "rollup": "^4.9.1",
47
+ "rollup-plugin-dts": "^6.1.0",
48
+ "typescript": "^5.3.0"
49
+ }
50
+ }