@modular-component/with-conditional-render 0.2.0 → 0.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/CHANGELOG.md +8 -0
- package/README.md +73 -38
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,50 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @modular-component/with-conditional-render
|
|
2
2
|
|
|
3
3
|
Provides three stages that allow conditional rendering in `ModularComponent`s:
|
|
4
4
|
|
|
5
|
-
- `
|
|
5
|
+
- `with(condition)` will set a customizable argument to either `true` or `false`, based
|
|
6
6
|
on current arguments,
|
|
7
|
-
- `
|
|
8
|
-
renders it when
|
|
9
|
-
- `
|
|
10
|
-
renders it
|
|
7
|
+
- `with(conditionalFallback)` takes a `FunctionComponent` as parameter, and
|
|
8
|
+
renders it when a customizable argument is set to `true`, filling the `render` argument in the process,
|
|
9
|
+
- `with(conditionalRender)` also takes a `FunctionComponent` as parameter, and
|
|
10
|
+
renders it if the `render` argument was not filled earlier.
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
to chain multiple conditions with a different fallback for each. Subsequent calls
|
|
14
|
-
to `withCondition` will take into account preceding conditions, so that `withConditionalRender`
|
|
15
|
-
is only called when all conditions return `true`.
|
|
12
|
+
## Usage
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
```tsx
|
|
15
|
+
import { ModularComponent } from '@modular-component/core'
|
|
16
|
+
import { condition, conditionalFallback, conditionalRender } from '@modular-component/with-conditional-render'
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const ConditionalComponent = ModularComponent<{ enabled?: boolean }>()
|
|
19
|
+
.with(condition('disabled', ({ props }) => props.enabled !== true))
|
|
20
|
+
.with(conditionalFallback('disabled', () => <>I'm disabled!</>))
|
|
21
|
+
.with(lifecycle(() => {
|
|
22
|
+
// Some data fetching logic...
|
|
23
|
+
return { loading, data }
|
|
24
|
+
}))
|
|
25
|
+
.with(condition('loading', ({ lifecycle }) => lifecycle.loading === false))
|
|
26
|
+
.with(conditionalFallback('loading', () => <>I'm loading!</>))
|
|
27
|
+
.with(conditionalRender(({ lifecycle }) => (
|
|
28
|
+
<>I'm enabled and loaded, here is the content: {lifecycle.data}</>
|
|
29
|
+
)))
|
|
21
30
|
```
|
|
22
31
|
|
|
32
|
+
## Multiple conditions and fallbacks
|
|
33
|
+
|
|
34
|
+
You can use the `condition` and `conditionalFallback` multiple times in the same pipeline by providing different
|
|
35
|
+
argument names as the first parameter.
|
|
36
|
+
|
|
37
|
+
## Implementation
|
|
38
|
+
|
|
39
|
+
The implementation for those stages is a bit more involved than other official extensions. Here, we have restrictions
|
|
40
|
+
for each stage, as well as stage hooks.
|
|
41
|
+
|
|
23
42
|
```tsx
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
|
|
27
|
-
const ModularComponent = modularFactory.extend(WithConditionalRender).build()
|
|
28
|
-
|
|
29
|
-
const MyModularComponent = ModularComponent<{
|
|
30
|
-
isAuthenticated: boolean
|
|
31
|
-
isOwner: boolean
|
|
32
|
-
}>()
|
|
33
|
-
.withCondition(({ props }) => props.isAuthenticated)
|
|
34
|
-
.withConditionalFallback(() => (
|
|
35
|
-
<div>You need to be authenticated to see this page</div>
|
|
36
|
-
))
|
|
37
|
-
.withCondition(({ props }) => props.isOwner)
|
|
38
|
-
.withConditionalFallback(() => (
|
|
39
|
-
<div>You don't have enough permissions to see this page</div>
|
|
40
|
-
))
|
|
41
|
-
.withConditionalRender(({ components }) => (
|
|
42
|
-
<>
|
|
43
|
-
<h1>Hello, Owner</h1>
|
|
44
|
-
</>
|
|
45
|
-
))
|
|
46
|
-
```
|
|
43
|
+
import React, { FunctionComponent } from 'react'
|
|
44
|
+
import { ModularStage } from '@modular-component/core'
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
export function condition<Args, Name extends string>(
|
|
47
|
+
name: Name,
|
|
48
|
+
useCondition: (args: Args) => boolean,
|
|
49
|
+
): ModularStage<Name, (args: Args) => boolean> {
|
|
50
|
+
return { field: name, useStage: useCondition }
|
|
51
|
+
}
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
export function conditionalFallback<
|
|
54
|
+
Args extends { [key in Name]: boolean } & {
|
|
55
|
+
render?: ReturnType<FunctionComponent>
|
|
56
|
+
},
|
|
57
|
+
Name extends string,
|
|
58
|
+
>(
|
|
59
|
+
name: Name,
|
|
60
|
+
useRender: (args: Args) => ReturnType<FunctionComponent>,
|
|
61
|
+
): ModularStage<`render-${Name}`, (args: Args) => void> {
|
|
62
|
+
return {
|
|
63
|
+
field: `render-${name}`,
|
|
64
|
+
useStage: (args: Args) => {
|
|
65
|
+
args.render = !args[name] || args.render ? args.render : useRender(args)
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function conditionalRender<Args, Ref>(
|
|
71
|
+
useRender: (
|
|
72
|
+
args: Args,
|
|
73
|
+
ref: React.ForwardedRef<Ref>,
|
|
74
|
+
) => ReturnType<FunctionComponent>,
|
|
75
|
+
): ModularStage<
|
|
76
|
+
'render',
|
|
77
|
+
(args: Args, ref: React.ForwardedRef<Ref>) => ReturnType<FunctionComponent>
|
|
78
|
+
> {
|
|
79
|
+
return {
|
|
80
|
+
field: 'render',
|
|
81
|
+
useStage: (args: Args, ref: React.ForwardedRef<Ref>) =>
|
|
82
|
+
(args as any).render ?? useRender(args, ref),
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"Default",
|
|
9
9
|
"Conditional"
|
|
10
10
|
],
|
|
11
|
-
"version": "0.2.
|
|
11
|
+
"version": "0.2.1",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"publishConfig": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"license": "cp ../../LICENSE ./LICENSE"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@modular-component/core": "0.2.
|
|
29
|
+
"@modular-component/core": "0.2.1"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">=17 <19"
|