@jiovan-labs/react-condition 0.0.0-stage โ†’ 2.0.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.
Files changed (42) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +303 -2
  3. package/README.zh-CN.md +304 -0
  4. package/README.zh-TW.md +304 -0
  5. package/dist/cjs/index.d.ts +8 -0
  6. package/dist/cjs/index.js +50 -0
  7. package/dist/cjs/util/Do.d.ts +6 -0
  8. package/dist/cjs/util/Do.js +44 -0
  9. package/dist/cjs/util/For.d.ts +6 -0
  10. package/dist/cjs/util/For.js +39 -0
  11. package/dist/cjs/util/If.d.ts +10 -0
  12. package/dist/cjs/util/If.js +69 -0
  13. package/dist/cjs/util/MapIterator.d.ts +6 -0
  14. package/dist/cjs/util/MapIterator.js +39 -0
  15. package/dist/cjs/util/ObjectIterator.d.ts +6 -0
  16. package/dist/cjs/util/ObjectIterator.js +42 -0
  17. package/dist/cjs/util/SetIterator.d.ts +6 -0
  18. package/dist/cjs/util/SetIterator.js +39 -0
  19. package/dist/cjs/util/Switch.d.ts +12 -0
  20. package/dist/cjs/util/Switch.js +80 -0
  21. package/dist/cjs/util/While.d.ts +6 -0
  22. package/dist/cjs/util/While.js +44 -0
  23. package/dist/esm/index.d.ts +8 -0
  24. package/dist/esm/index.js +8 -0
  25. package/dist/esm/util/Do.d.ts +6 -0
  26. package/dist/esm/util/Do.js +18 -0
  27. package/dist/esm/util/For.d.ts +6 -0
  28. package/dist/esm/util/For.js +15 -0
  29. package/dist/esm/util/If.d.ts +10 -0
  30. package/dist/esm/util/If.js +32 -0
  31. package/dist/esm/util/MapIterator.d.ts +6 -0
  32. package/dist/esm/util/MapIterator.js +15 -0
  33. package/dist/esm/util/ObjectIterator.d.ts +6 -0
  34. package/dist/esm/util/ObjectIterator.js +18 -0
  35. package/dist/esm/util/SetIterator.d.ts +6 -0
  36. package/dist/esm/util/SetIterator.js +15 -0
  37. package/dist/esm/util/Switch.d.ts +12 -0
  38. package/dist/esm/util/Switch.js +47 -0
  39. package/dist/esm/util/While.d.ts +6 -0
  40. package/dist/esm/util/While.js +18 -0
  41. package/dist/umd/react-condition.min.js +1 -0
  42. package/package.json +122 -5
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jiovan Ltd. All rights reserved.
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 CHANGED
@@ -1,3 +1,304 @@
1
- # Temporary Holding Version
1
+ # @jiovan-labs/react-condition
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![NPM version](https://img.shields.io/npm/v/@jiovan-labs/react-condition.svg?style=flat)](https://npmjs.org/package/@jiovan-labs/react-condition)
4
+ [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)
5
+ [![NPM downloads](http://img.shields.io/npm/dm/@jiovan-labs/react-condition.svg?style=flat)](https://npmjs.org/package/@jiovan-labs/react-condition)
6
+
7
+ ๐Ÿ… Render components conditionally for React ๐Ÿ‘
8
+
9
+ ## Features
10
+
11
+ โœ… Supports 'If' conditions
12
+
13
+ โœ… Supports 'If'-'Then' conditions
14
+
15
+ โœ… Supports 'If'-'Else' conditions
16
+
17
+ โœ… Supports 'If'-'Then'-Else' conditions
18
+
19
+ โœ… Supports 'For' conditions
20
+
21
+ โœ… Supports 'Do' conditions
22
+
23
+ โœ… Supports 'While' conditions
24
+
25
+ โœ… Supports 'MapIterator' conditions
26
+
27
+ โœ… Supports 'SetIterator' conditions
28
+
29
+ โœ… Supports 'ObjectIterator' conditions
30
+
31
+ ## Quickstart
32
+
33
+ You can install this package in your React project as follows:
34
+
35
+ ```bash
36
+ $ npm install @jiovan-labs/react-condition --save
37
+ ```
38
+
39
+ Then, you may import components as follows:
40
+
41
+ ```tsx | pure
42
+ import { If, For, Switch, Do, While } from '@jiovan-labs/react-condition';
43
+ import { MapIterator, SetIterator, ObjectIterator } from '@jiovan-labs/react-condition';
44
+ ```
45
+
46
+ Enjoy your coding journey with `react-condition`. โœŒ๏ธ
47
+
48
+ ## Example
49
+
50
+ ### If
51
+
52
+ > Both of the `If.Then` and `If.Else` have a `render` property (() => React.ReactNode), thus you can customize the rendering contents instead of the React `children`.
53
+
54
+ #### If
55
+
56
+ ```tsx | pure
57
+ import React from 'react';
58
+ import { If } from '@jiovan-labs/react-condition';
59
+
60
+ export default () => {
61
+ const param: boolean = true;
62
+ return (
63
+ <If condition={param}>
64
+ <span>Hello World</span>
65
+ </If>
66
+ );
67
+ }
68
+ ```
69
+
70
+ #### If-Then
71
+
72
+ ```tsx | pure
73
+ import React from 'react';
74
+ import { If } from '@jiovan-labs/react-condition';
75
+
76
+ export default () => {
77
+ const param: number = 1;
78
+ return (
79
+ <If condition={param}>
80
+ <If.Then>
81
+ <span>Hello World</span>
82
+ </If.Then>
83
+ </If>
84
+ );
85
+ }
86
+ ```
87
+
88
+ #### If-Else
89
+
90
+ ```tsx | pure
91
+ import React from 'react';
92
+ import { If } from '@jiovan-labs/react-condition';
93
+
94
+ export default () => {
95
+ const param: boolean = false;
96
+ return (
97
+ <If condition={param}>
98
+ <span>Hello World</span>
99
+ <If.Else>
100
+ <span>Hello Jiovan</span>
101
+ </If.Else>
102
+ </If>
103
+ );
104
+ }
105
+ ```
106
+
107
+ #### If-Then-Else
108
+
109
+ ```tsx | pure
110
+ import React from 'react';
111
+ import { If } from '@jiovan-labs/react-condition';
112
+
113
+ export default () => {
114
+ const param: boolean = false;
115
+ return (
116
+ <If condition={param}>
117
+ <If.Then>
118
+ <span>Hello World</span>
119
+ </If.Then>
120
+ <If.Else>
121
+ <span>Hello Jiovan</span>
122
+ </If.Else>
123
+ </If>
124
+ );
125
+ }
126
+ ```
127
+
128
+ ### For
129
+
130
+ ```tsx | pure
131
+ import React from 'react';
132
+ import { For } from '@jiovan-labs/react-condition';
133
+
134
+ export default () => {
135
+ return (
136
+ <For
137
+ of={['foo', 'bar']}
138
+ render={(item, index) => {
139
+ return (
140
+ <span key={index}>Hello, {item}</span>
141
+ );
142
+ }}
143
+ />
144
+ );
145
+ }
146
+ ```
147
+
148
+ ### Switch
149
+
150
+ > Both of the `Switch.Case` and `Switch.Default` have a `render` property (() => React.ReactNode), thus you can customize the rendering contents instead of the React `children`.
151
+
152
+ ```tsx | pure
153
+ import React from 'react';
154
+ import { Switch } from '@jiovan-labs/react-condition';
155
+
156
+ export default () => {
157
+ const username: string = 'admin';
158
+
159
+ return (
160
+ <Switch>
161
+ <Switch.Case condition={username.includes('admin')}>
162
+ <span>admin</span>
163
+ </Switch.Case>
164
+ <Switch.Case condition={username.includes('guest')}>
165
+ <span>guest</span>
166
+ </Switch.Case>
167
+ <Switch.Default>
168
+ <span>root</span>
169
+ </Switch.Default>
170
+ </Switch>
171
+ );
172
+ }
173
+ ```
174
+
175
+ ### Do
176
+
177
+ ```tsx | pure
178
+ import React from 'react';
179
+ import { Do } from '@jiovan-labs/react-condition';
180
+
181
+ export default () => {
182
+ let param: number = 0;
183
+ return (
184
+ <Do
185
+ condition={() => {
186
+ return param < 2;
187
+ }}
188
+ render={(index) => {
189
+ param++;
190
+ return (
191
+ <span key={index}>Hello, {index}</span>
192
+ );
193
+ }}
194
+ />
195
+ );
196
+ }
197
+ ```
198
+
199
+ ### While
200
+
201
+ ```tsx | pure
202
+ import React from 'react';
203
+ import { While } from '@jiovan-labs/react-condition';
204
+
205
+ export default () => {
206
+ let param: number = 0;
207
+ return (
208
+ <While
209
+ condition={() => {
210
+ return param++ < 2;
211
+ }}
212
+ render={(index) => {
213
+ return (
214
+ <span key={index}>Hello, {index}</span>
215
+ );
216
+ }}
217
+ />
218
+ );
219
+ }
220
+ ```
221
+
222
+ ### MapIterator
223
+
224
+ ```tsx | pure
225
+ import React from 'react';
226
+ import { MapIterator } from '@jiovan-labs/react-condition';
227
+
228
+ export default () => {
229
+ const map = new Map([
230
+ ['foo', 'bar'],
231
+ ['hello', 'world'],
232
+ ]);
233
+ return (
234
+ <MapIterator
235
+ of={map}
236
+ render={(value, key, index) => {
237
+ return (
238
+ <span key={index}>Hooray, {key}-{value}</span>
239
+ );
240
+ }}
241
+ />
242
+ );
243
+ }
244
+ ```
245
+
246
+ ### SetIterator
247
+
248
+ ```tsx | pure
249
+ import React from 'react';
250
+ import { SetIterator } from '@jiovan-labs/react-condition';
251
+
252
+ export default () => {
253
+ const set = new Set<string>([
254
+ 'foo-bar',
255
+ 'hello-world',
256
+ ]);
257
+ return (
258
+ <SetIterator
259
+ of={set}
260
+ render={(item, index) => {
261
+ return (
262
+ <span key={index}>Hooray, {item}</span>
263
+ );
264
+ }}
265
+ />
266
+ );
267
+ }
268
+ ```
269
+
270
+ ### ObjectIterator
271
+
272
+ ```tsx | pure
273
+ import React from 'react';
274
+ import { ObjectIterator } from '@jiovan-labs/react-condition';
275
+
276
+ export default () => {
277
+ const param: object = {
278
+ 'foo': 'bar',
279
+ 'hello': 'world',
280
+ };
281
+ return (
282
+ <ObjectIterator
283
+ of={param}
284
+ render={(value, key, index) => {
285
+ return (
286
+ <span key={index}>Hooray, {key}-{value}</span>
287
+ );
288
+ }}
289
+ />
290
+ );
291
+ }
292
+ ```
293
+
294
+ ## License
295
+
296
+ This project is under the [MIT License](https://mit-license.org/).
297
+
298
+ ## Copyright
299
+
300
+ Beijing Jiovan Technology Ltd.
301
+
302
+ ## Website
303
+
304
+ - Jiovan: [https://joivan.com](https://joivan.com)
@@ -0,0 +1,304 @@
1
+ # @jiovan-labs/react-condition
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@jiovan-labs/react-condition.svg?style=flat)](https://npmjs.org/package/@jiovan-labs/react-condition)
4
+ [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt)
5
+ [![NPM downloads](http://img.shields.io/npm/dm/@jiovan-labs/react-condition.svg?style=flat)](https://npmjs.org/package/@jiovan-labs/react-condition)
6
+
7
+ ๐Ÿ… React ๆกไปถๆ€งๆธฒๆŸ“็ป„ไปถ ๐Ÿ‘
8
+
9
+ ## ็‰นๆ€ง
10
+
11
+ โœ… ๆ”ฏๆŒ 'If' ๆกไปถๆธฒๆŸ“
12
+
13
+ โœ… ๆ”ฏๆŒ 'If'-'Then' ๆกไปถๆธฒๆŸ“
14
+
15
+ โœ… ๆ”ฏๆŒ 'If'-'Else' ๆกไปถๆธฒๆŸ“
16
+
17
+ โœ… ๆ”ฏๆŒ 'If'-'Then'-Else' ๆกไปถๆธฒๆŸ“
18
+
19
+ โœ… ๆ”ฏๆŒ 'For' ๆกไปถๆธฒๆŸ“
20
+
21
+ โœ… ๆ”ฏๆŒ 'Do' ๆกไปถๆธฒๆŸ“
22
+
23
+ โœ… ๆ”ฏๆŒ 'While' ๆกไปถๆธฒๆŸ“
24
+
25
+ โœ… ๆ”ฏๆŒ 'MapIterator' ๆกไปถๆธฒๆŸ“
26
+
27
+ โœ… ๆ”ฏๆŒ 'SetIterator' ๆกไปถๆธฒๆŸ“
28
+
29
+ โœ… ๆ”ฏๆŒ 'ObjectIterator' ๆกไปถๆธฒๆŸ“
30
+
31
+ ## ๅฟซ้€Ÿๅผ€ๅง‹
32
+
33
+ ๆ‚จๅฏไปฅๅœจๆ‚จ็š„ React ้กน็›ฎไธญไฝฟ็”จไปฅไธ‹ๅ‘ฝไปคๆฅๅฎ‰่ฃ…ๆœฌ็ป„ไปถๅบ“๏ผš
34
+
35
+ ```bash
36
+ $ npm install @jiovan-labs/react-condition --save
37
+ ```
38
+
39
+ ็„ถๅŽ๏ผŒๆ‚จๅฏไปฅไฝฟ็”จไปฅไธ‹ๅ‘ฝไปคๆฅๅฏผๅ…ฅ็ป„ไปถ๏ผš
40
+
41
+ ```tsx | pure
42
+ import { If, For, Switch, Do, While } from '@jiovan-labs/react-condition';
43
+ import { MapIterator, SetIterator, ObjectIterator } from '@jiovan-labs/react-condition';
44
+ ```
45
+
46
+ ไบซๅ—ๆ‚จๅ’Œ `react-condition` ็š„็ผ–็จ‹ไน‹ๆ—…ๅง โœŒ๏ธ
47
+
48
+ ## ็คบไพ‹
49
+
50
+ ### If
51
+
52
+ > `If.Then` ๅ’Œ `If.Else` ้ƒฝๆœ‰ไธ€ไธช `render` ๅฑžๆ€ง `() => React.ReactNode`๏ผŒ่ฟ™ๆ ทๆ‚จไนŸๅฏไปฅ้€š่ฟ‡ๅฎƒๆฅ่ฟ”ๅ›ž่‡ชๅฎšไน‰็š„ๆธฒๆŸ“ๅ†…ๅฎน, ๆฏ” React ็š„ `children` ๅฑžๆ€งๆƒ้‡่ฆ้ซ˜ใ€‚
53
+
54
+ #### If
55
+
56
+ ```tsx | pure
57
+ import React from 'react';
58
+ import { If } from '@jiovan-labs/react-condition';
59
+
60
+ export default () => {
61
+ const param: boolean = true;
62
+ return (
63
+ <If condition={param}>
64
+ <span>Hello World</span>
65
+ </If>
66
+ );
67
+ }
68
+ ```
69
+
70
+ #### If-Then
71
+
72
+ ```tsx | pure
73
+ import React from 'react';
74
+ import { If } from '@jiovan-labs/react-condition';
75
+
76
+ export default () => {
77
+ const param: number = 1;
78
+ return (
79
+ <If condition={param}>
80
+ <If.Then>
81
+ <span>Hello World</span>
82
+ </If.Then>
83
+ </If>
84
+ );
85
+ }
86
+ ```
87
+
88
+ #### If-Else
89
+
90
+ ```tsx | pure
91
+ import React from 'react';
92
+ import { If } from '@jiovan-labs/react-condition';
93
+
94
+ export default () => {
95
+ const param: boolean = false;
96
+ return (
97
+ <If condition={param}>
98
+ <span>Hello World</span>
99
+ <If.Else>
100
+ <span>Hello Jiovan</span>
101
+ </If.Else>
102
+ </If>
103
+ );
104
+ }
105
+ ```
106
+
107
+ #### If-Then-Else
108
+
109
+ ```tsx | pure
110
+ import React from 'react';
111
+ import { If } from '@jiovan-labs/react-condition';
112
+
113
+ export default () => {
114
+ const param: boolean = false;
115
+ return (
116
+ <If condition={param}>
117
+ <If.Then>
118
+ <span>Hello World</span>
119
+ </If.Then>
120
+ <If.Else>
121
+ <span>Hello Jiovan</span>
122
+ </If.Else>
123
+ </If>
124
+ );
125
+ }
126
+ ```
127
+
128
+ ### For
129
+
130
+ ```tsx | pure
131
+ import React from 'react';
132
+ import { For } from '@jiovan-labs/react-condition';
133
+
134
+ export default () => {
135
+ return (
136
+ <For
137
+ of={['foo', 'bar']}
138
+ render={(item, index) => {
139
+ return (
140
+ <span key={index}>Hello, {item}</span>
141
+ );
142
+ }}
143
+ />
144
+ );
145
+ }
146
+ ```
147
+
148
+ ### Switch
149
+
150
+ > `Switch.Case` ๅ’Œ `Switch.Default` ้ƒฝๆœ‰ไธ€ไธช `render` ๅฑžๆ€ง `() => React.ReactNode`๏ผŒ่ฟ™ๆ ทๆ‚จไนŸๅฏไปฅ้€š่ฟ‡ๅฎƒๆฅ่ฟ”ๅ›ž่‡ชๅฎšไน‰็š„ๆธฒๆŸ“ๅ†…ๅฎน, ๆฏ” React ็š„ `children` ๅฑžๆ€งๆƒ้‡่ฆ้ซ˜ใ€‚
151
+
152
+ ```tsx | pure
153
+ import React from 'react';
154
+ import { Switch } from '@jiovan-labs/react-condition';
155
+
156
+ export default () => {
157
+ const username: string = 'admin';
158
+
159
+ return (
160
+ <Switch>
161
+ <Switch.Case condition={username.includes('admin')}>
162
+ <span>admin</span>
163
+ </Switch.Case>
164
+ <Switch.Case condition={username.includes('guest')}>
165
+ <span>guest</span>
166
+ </Switch.Case>
167
+ <Switch.Default>
168
+ <span>root</span>
169
+ </Switch.Default>
170
+ </Switch>
171
+ );
172
+ }
173
+ ```
174
+
175
+ ### Do
176
+
177
+ ```tsx | pure
178
+ import React from 'react';
179
+ import { Do } from '@jiovan-labs/react-condition';
180
+
181
+ export default () => {
182
+ let param: number = 0;
183
+ return (
184
+ <Do
185
+ condition={() => {
186
+ return param < 2;
187
+ }}
188
+ render={(index) => {
189
+ param++;
190
+ return (
191
+ <span key={index}>Hello, {index}</span>
192
+ );
193
+ }}
194
+ />
195
+ );
196
+ }
197
+ ```
198
+
199
+ ### While
200
+
201
+ ```tsx | pure
202
+ import React from 'react';
203
+ import { While } from '@jiovan-labs/react-condition';
204
+
205
+ export default () => {
206
+ let param: number = 0;
207
+ return (
208
+ <While
209
+ condition={() => {
210
+ return param++ < 2;
211
+ }}
212
+ render={(index) => {
213
+ return (
214
+ <span key={index}>Hello, {index}</span>
215
+ );
216
+ }}
217
+ />
218
+ );
219
+ }
220
+ ```
221
+
222
+ ### MapIterator
223
+
224
+ ```tsx | pure
225
+ import React from 'react';
226
+ import { MapIterator } from '@jiovan-labs/react-condition';
227
+
228
+ export default () => {
229
+ const map = new Map([
230
+ ['foo', 'bar'],
231
+ ['hello', 'world'],
232
+ ]);
233
+ return (
234
+ <MapIterator
235
+ of={map}
236
+ render={(value, key, index) => {
237
+ return (
238
+ <span key={index}>Hooray, {key}-{value}</span>
239
+ );
240
+ }}
241
+ />
242
+ );
243
+ }
244
+ ```
245
+
246
+ ### SetIterator
247
+
248
+ ```tsx | pure
249
+ import React from 'react';
250
+ import { SetIterator } from '@jiovan-labs/react-condition';
251
+
252
+ export default () => {
253
+ const set = new Set<string>([
254
+ 'foo-bar',
255
+ 'hello-world',
256
+ ]);
257
+ return (
258
+ <SetIterator
259
+ of={set}
260
+ render={(item, index) => {
261
+ return (
262
+ <span key={index}>Hooray, {item}</span>
263
+ );
264
+ }}
265
+ />
266
+ );
267
+ }
268
+ ```
269
+
270
+ ### ObjectIterator
271
+
272
+ ```tsx | pure
273
+ import React from 'react';
274
+ import { ObjectIterator } from '@jiovan-labs/react-condition';
275
+
276
+ export default () => {
277
+ const param: object = {
278
+ 'foo': 'bar',
279
+ 'hello': 'world',
280
+ };
281
+ return (
282
+ <ObjectIterator
283
+ of={param}
284
+ render={(value, key, index) => {
285
+ return (
286
+ <span key={index}>Hooray, {key}-{value}</span>
287
+ );
288
+ }}
289
+ />
290
+ );
291
+ }
292
+ ```
293
+
294
+ ## ๆŽˆๆƒ
295
+
296
+ ๆœฌ็ป„ไปถๅบ“ๆŽˆๆƒๅŸบไบŽ [MIT License](https://mit-license.org/) ๅ่ฎฎ
297
+
298
+ ## ็‰ˆๆƒ
299
+
300
+ ๅŒ—ไบฌ็Ž–ๆผซ็ง‘ๆŠ€ๆœ‰้™ๅ…ฌๅธ
301
+
302
+ ## ็ฝ‘็ซ™
303
+
304
+ - Jiovan: [https://joivan.com](https://joivan.com)