react-ternary-be-gone 0.1.0 → 0.1.2
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/.babelrc +3 -0
- package/Case.js +8 -0
- package/Conditional.js +133 -0
- package/Else.js +8 -0
- package/ElseIf.js +8 -0
- package/If.js +8 -0
- package/README.md +117 -1
- package/babel.config.js +3 -0
- package/index.js +5 -1
- package/package.json +9 -4
package/.babelrc
ADDED
package/Case.js
ADDED
package/Conditional.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import React, { useMemo, useEffect } from 'react';
|
|
2
|
+
import Case from './Case';
|
|
3
|
+
import If from './If';
|
|
4
|
+
import ElseIf from './ElseIf';
|
|
5
|
+
import Else from './Else';
|
|
2
6
|
|
|
3
7
|
const Conditional = ({
|
|
4
8
|
when,
|
|
@@ -27,6 +31,8 @@ const Conditional = ({
|
|
|
27
31
|
startsWith = null,
|
|
28
32
|
endsWith = null,
|
|
29
33
|
match = null, // regex match
|
|
34
|
+
// Switch-case feature
|
|
35
|
+
switch: switchValue,
|
|
30
36
|
}) => {
|
|
31
37
|
const hasCondition = when !== undefined;
|
|
32
38
|
const hasIteration = each !== undefined;
|
|
@@ -139,6 +145,133 @@ const Conditional = ({
|
|
|
139
145
|
// Wrapper component
|
|
140
146
|
const WrapperComponent = wrapper;
|
|
141
147
|
|
|
148
|
+
// Switch-case logic
|
|
149
|
+
if (switchValue !== undefined) {
|
|
150
|
+
// Find all Case children
|
|
151
|
+
const caseChildren = React.Children.toArray(children).filter(
|
|
152
|
+
child => child && child.type === Case
|
|
153
|
+
);
|
|
154
|
+
// Find matching case
|
|
155
|
+
let match = null;
|
|
156
|
+
let defaultCase = null;
|
|
157
|
+
caseChildren.forEach(child => {
|
|
158
|
+
if (child.props.default) {
|
|
159
|
+
defaultCase = child;
|
|
160
|
+
} else if (child.props.when === switchValue) {
|
|
161
|
+
match = child;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
if (match) {
|
|
165
|
+
return <WrapperComponent>{match.props.children}</WrapperComponent>;
|
|
166
|
+
} else if (defaultCase) {
|
|
167
|
+
return <WrapperComponent>{defaultCase.props.children}</WrapperComponent>;
|
|
168
|
+
} else {
|
|
169
|
+
return fallback;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// If-ElseIf-Else logic
|
|
174
|
+
const childrenArray = React.Children.toArray(children);
|
|
175
|
+
const ifElseBlocks = childrenArray.filter(child =>
|
|
176
|
+
child && (child.type === If || child.type === ElseIf || child.type === Else)
|
|
177
|
+
);
|
|
178
|
+
if (ifElseBlocks.length > 0) {
|
|
179
|
+
let rendered = null;
|
|
180
|
+
for (let i = 0; i < ifElseBlocks.length; i++) {
|
|
181
|
+
const child = ifElseBlocks[i];
|
|
182
|
+
// Extract Conditional props from child
|
|
183
|
+
const {
|
|
184
|
+
when,
|
|
185
|
+
each,
|
|
186
|
+
filter,
|
|
187
|
+
sort,
|
|
188
|
+
limit,
|
|
189
|
+
reverse,
|
|
190
|
+
animate,
|
|
191
|
+
wrapper: childWrapper,
|
|
192
|
+
keyExtractor: childKeyExtractor,
|
|
193
|
+
empty: childEmpty,
|
|
194
|
+
fallback: childFallback,
|
|
195
|
+
...rest
|
|
196
|
+
} = child.props;
|
|
197
|
+
|
|
198
|
+
// Evaluate condition
|
|
199
|
+
let condition = true;
|
|
200
|
+
if (child.type === If || child.type === ElseIf) {
|
|
201
|
+
condition = Boolean(when);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Array processing for each
|
|
205
|
+
let arrayItems = [];
|
|
206
|
+
if (each && Array.isArray(each)) {
|
|
207
|
+
arrayItems = [...each];
|
|
208
|
+
if (filter && typeof filter === 'function') {
|
|
209
|
+
arrayItems = arrayItems.filter(filter);
|
|
210
|
+
}
|
|
211
|
+
if (sort && typeof sort === 'function') {
|
|
212
|
+
arrayItems = arrayItems.sort(sort);
|
|
213
|
+
}
|
|
214
|
+
if (reverse) {
|
|
215
|
+
arrayItems = arrayItems.reverse();
|
|
216
|
+
}
|
|
217
|
+
if (limit && typeof limit === 'number') {
|
|
218
|
+
arrayItems = arrayItems.slice(0, limit);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Render logic
|
|
223
|
+
if ((child.type === If || child.type === ElseIf) && condition) {
|
|
224
|
+
if (each && Array.isArray(each)) {
|
|
225
|
+
if (arrayItems.length === 0) {
|
|
226
|
+
rendered = childEmpty || childFallback || null;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
const elements = arrayItems.map((item, idx) => (
|
|
230
|
+
<React.Fragment key={childKeyExtractor ? childKeyExtractor(item, idx) : idx}>
|
|
231
|
+
{typeof child.props.children === 'function'
|
|
232
|
+
? child.props.children(item, idx, arrayItems)
|
|
233
|
+
: child.props.children}
|
|
234
|
+
</React.Fragment>
|
|
235
|
+
));
|
|
236
|
+
rendered = childWrapper
|
|
237
|
+
? React.createElement(childWrapper, null, elements)
|
|
238
|
+
: <>{elements}</>;
|
|
239
|
+
break;
|
|
240
|
+
} else {
|
|
241
|
+
rendered = childWrapper
|
|
242
|
+
? React.createElement(childWrapper, null, child.props.children)
|
|
243
|
+
: <>{child.props.children}</>;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (child.type === Else) {
|
|
248
|
+
if (each && Array.isArray(each)) {
|
|
249
|
+
if (arrayItems.length === 0) {
|
|
250
|
+
rendered = childEmpty || childFallback || null;
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
const elements = arrayItems.map((item, idx) => (
|
|
254
|
+
<React.Fragment key={childKeyExtractor ? childKeyExtractor(item, idx) : idx}>
|
|
255
|
+
{typeof child.props.children === 'function'
|
|
256
|
+
? child.props.children(item, idx, arrayItems)
|
|
257
|
+
: child.props.children}
|
|
258
|
+
</React.Fragment>
|
|
259
|
+
));
|
|
260
|
+
rendered = childWrapper
|
|
261
|
+
? React.createElement(childWrapper, null, elements)
|
|
262
|
+
: <>{elements}</>;
|
|
263
|
+
break;
|
|
264
|
+
} else {
|
|
265
|
+
rendered = childWrapper
|
|
266
|
+
? React.createElement(childWrapper, null, child.props.children)
|
|
267
|
+
: <>{child.props.children}</>;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return rendered;
|
|
273
|
+
}
|
|
274
|
+
|
|
142
275
|
// Only condition
|
|
143
276
|
if (hasCondition && !hasIteration) {
|
|
144
277
|
return evaluateCondition ? (
|
package/Else.js
ADDED
package/ElseIf.js
ADDED
package/If.js
ADDED
package/README.md
CHANGED
|
@@ -11,10 +11,12 @@ npm install react-ternary-be-gone
|
|
|
11
11
|
yarn add react-ternary-be-gone
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
|
|
15
|
+
|
|
14
16
|
## Usage
|
|
15
17
|
|
|
16
18
|
```javascript
|
|
17
|
-
import Conditional from 'react-ternary-be-gone';
|
|
19
|
+
import Conditional, { Case, If, ElseIf, Else } from 'react-ternary-be-gone';
|
|
18
20
|
|
|
19
21
|
// Basic conditional rendering
|
|
20
22
|
<Conditional when={true}>
|
|
@@ -35,6 +37,120 @@ const items = ['Item 1', 'Item 2', 'Item 3'];
|
|
|
35
37
|
<Conditional each={items}>
|
|
36
38
|
{(item, index) => <p key={index}>{item}</p>}
|
|
37
39
|
</Conditional>
|
|
40
|
+
|
|
41
|
+
// Switch-case rendering
|
|
42
|
+
const value = 'b';
|
|
43
|
+
|
|
44
|
+
<Conditional switch={value}>
|
|
45
|
+
<Case when="a">A seçildi</Case>
|
|
46
|
+
<Case when="b">B seçildi</Case>
|
|
47
|
+
<Case default>Hiçbiri seçilmedi</Case>
|
|
48
|
+
</Conditional>
|
|
49
|
+
|
|
50
|
+
// If-ElseIf-Else rendering
|
|
51
|
+
const status = 'loading';
|
|
52
|
+
|
|
53
|
+
<Conditional>
|
|
54
|
+
<If when={status === 'loading'}>
|
|
55
|
+
<p>Yükleniyor...</p>
|
|
56
|
+
</If>
|
|
57
|
+
<ElseIf when={status === 'success'}>
|
|
58
|
+
<p>Başarılı!</p>
|
|
59
|
+
</ElseIf>
|
|
60
|
+
<Else>
|
|
61
|
+
<p>Durum bilinmiyor.</p>
|
|
62
|
+
</Else>
|
|
63
|
+
</Conditional>
|
|
64
|
+
|
|
65
|
+
// If-ElseIf-Else with advanced props
|
|
66
|
+
const users = [{ id: 1, name: 'Alice', active: true }, { id: 2, name: 'Bob', active: false }];
|
|
67
|
+
|
|
68
|
+
<Conditional>
|
|
69
|
+
<If when={users.length > 0} each={users} filter={user => user.active}>
|
|
70
|
+
{(user) => <p>{user.name}</p>}
|
|
71
|
+
</If>
|
|
72
|
+
<Else>
|
|
73
|
+
<p>Kullanıcı yok.</p>
|
|
74
|
+
</Else>
|
|
75
|
+
</Conditional>
|
|
76
|
+
```
|
|
77
|
+
### If-ElseIf-Else Blocks
|
|
78
|
+
|
|
79
|
+
You can use `<If>`, `<ElseIf>`, and `<Else>` as children of `<Conditional>` (without any prop on Conditional itself) to mimic if-else if-else logic. The first matching block is rendered. You can use all Conditional props (each, filter, sort, etc.) on these blocks.
|
|
80
|
+
|
|
81
|
+
**Example:**
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
import Conditional, { If, ElseIf, Else } from 'react-ternary-be-gone';
|
|
85
|
+
|
|
86
|
+
const status = 'success';
|
|
87
|
+
|
|
88
|
+
<Conditional>
|
|
89
|
+
<If when={status === 'loading'}>
|
|
90
|
+
<p>Yükleniyor...</p>
|
|
91
|
+
</If>
|
|
92
|
+
<ElseIf when={status === 'success'}>
|
|
93
|
+
<p>Başarılı!</p>
|
|
94
|
+
</ElseIf>
|
|
95
|
+
<Else>
|
|
96
|
+
<p>Durum bilinmiyor.</p>
|
|
97
|
+
</Else>
|
|
98
|
+
</Conditional>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Advanced Example (with array props):**
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const users = [{ id: 1, name: 'Alice', active: true }, { id: 2, name: 'Bob', active: false }];
|
|
105
|
+
|
|
106
|
+
<Conditional>
|
|
107
|
+
<If when={users.length > 0} each={users} filter={user => user.active}>
|
|
108
|
+
{(user) => <p>{user.name}</p>}
|
|
109
|
+
</If>
|
|
110
|
+
<Else>
|
|
111
|
+
<p>Kullanıcı yok.</p>
|
|
112
|
+
</Else>
|
|
113
|
+
</Conditional>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
You can use multiple `<ElseIf>` blocks. The first matching block is rendered. All Conditional props (each, filter, sort, etc.) are supported on If/ElseIf/Else blocks.
|
|
117
|
+
### `switch` (Switch-Case Rendering)
|
|
118
|
+
|
|
119
|
+
Allows you to use switch-case style rendering with `<Case>` children. The `switch` prop sets the value to match, and each `<Case when={...}>...</Case>` child is checked. If no match is found, `<Case default>...</Case>` is rendered if present.
|
|
120
|
+
|
|
121
|
+
**Type:** `any`
|
|
122
|
+
|
|
123
|
+
**Example:**
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
import Conditional, { Case } from 'react-ternary-be-gone';
|
|
127
|
+
|
|
128
|
+
const value = 'b';
|
|
129
|
+
|
|
130
|
+
<Conditional switch={value}>
|
|
131
|
+
<Case when="a">A seçildi</Case>
|
|
132
|
+
<Case when="b">B seçildi</Case>
|
|
133
|
+
<Case default>Hiçbiri seçilmedi</Case>
|
|
134
|
+
</Conditional>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### `<Case>`
|
|
138
|
+
|
|
139
|
+
Child component for use with `switch` prop. Use `when` for matching value, and `default` for the default case.
|
|
140
|
+
|
|
141
|
+
**Props:**
|
|
142
|
+
|
|
143
|
+
- `when`: Value to match against the `switch` prop.
|
|
144
|
+
- `default`: Boolean, renders if no other case matches.
|
|
145
|
+
|
|
146
|
+
**Example:**
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
<Conditional switch={status}>
|
|
150
|
+
<Case when="loading">Yükleniyor...</Case>
|
|
151
|
+
<Case when="success">Başarılı!</Case>
|
|
152
|
+
<Case default>Durum bilinmiyor.</Case>
|
|
153
|
+
</Conditional>
|
|
38
154
|
```
|
|
39
155
|
|
|
40
156
|
## Props
|
package/babel.config.js
ADDED
package/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import Conditional from './Conditional';
|
|
2
|
+
import Case from './Case';
|
|
3
|
+
import If from './If';
|
|
4
|
+
import ElseIf from './ElseIf';
|
|
5
|
+
import Else from './Else';
|
|
2
6
|
import { useConditionalHelpers } from './useConditionalHelpers';
|
|
3
7
|
|
|
4
8
|
export default Conditional;
|
|
5
|
-
export { useConditionalHelpers };
|
|
9
|
+
export { Case, If, ElseIf, Else, useConditionalHelpers };
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-ternary-be-gone",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A React component designed to simplify conditional rendering and list iteration, providing a more readable and maintainable alternative to ternary operators and verbose conditional logic.",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "babel . --out-dir dist"
|
|
8
9
|
},
|
|
9
10
|
"peerDependencies": {
|
|
10
11
|
"react": ">=16.8.0"
|
|
@@ -23,5 +24,9 @@
|
|
|
23
24
|
"bugs": {
|
|
24
25
|
"url": "https://github.com/sundowatch/react-ternary-be-gone/issues"
|
|
25
26
|
},
|
|
26
|
-
"homepage": "https://github.com/sundowatch/react-ternary-be-gone#readme"
|
|
27
|
+
"homepage": "https://github.com/sundowatch/react-ternary-be-gone#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@babel/cli": "^7.28.0",
|
|
30
|
+
"@babel/preset-react": "^7.27.1"
|
|
31
|
+
}
|
|
27
32
|
}
|