@modular-component/with-conditional-render 0.2.0 → 0.2.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +81 -38
  3. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @modular-component/with-conditional-render
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 77c46f4: Fix an issue in core rendering system
8
+ - Updated dependencies [77c46f4]
9
+ - @modular-component/core@0.2.2
10
+
11
+ ## 0.2.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 09b9673: Update readmes
16
+ - Updated dependencies [09b9673]
17
+ - @modular-component/core@0.2.1
18
+
3
19
  ## 0.2.0
4
20
 
5
21
  ### Minor Changes
package/README.md CHANGED
@@ -1,50 +1,93 @@
1
- # `@modular-component/with-conditional-render`
1
+ # @modular-component/with-conditional-render
2
2
 
3
3
  Provides three stages that allow conditional rendering in `ModularComponent`s:
4
4
 
5
- - `withCondition` will set a `condition` argument to either `true` or `false`, based
5
+ - `with(condition)` will set a customizable argument to either `true` or `false`, based
6
6
  on current arguments,
7
- - `withConditionalFallback` takes a `FunctionComponent` as parameter, and
8
- renders it when the `condition` argument is set to `false`,
9
- - `withConditionalRender` also takes a `FunctionComponent` as parameter, and
10
- renders it when the `condition` argument is _not_ set to `false`.
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
- `withCondition` and `withConditionalFallback` are multiple, so it's possible
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
- ## Installation and usage
14
+ ```tsx
15
+ import { ModularComponent } from '@modular-component/core'
16
+ import {
17
+ condition,
18
+ conditionalFallback,
19
+ conditionalRender,
20
+ } from '@modular-component/with-conditional-render'
18
21
 
19
- ```bash
20
- yarn add @modular-component/core @modular-component/with-conditional-render
22
+ const ConditionalComponent = ModularComponent<{ enabled?: boolean }>()
23
+ .with(condition('disabled', ({ props }) => props.enabled !== true))
24
+ .with(conditionalFallback('disabled', () => <>I'm disabled!</>))
25
+ .with(
26
+ lifecycle(() => {
27
+ // Some data fetching logic...
28
+ return { loading, data }
29
+ }),
30
+ )
31
+ .with(condition('loading', ({ lifecycle }) => lifecycle.loading === false))
32
+ .with(conditionalFallback('loading', () => <>I'm loading!</>))
33
+ .with(
34
+ conditionalRender(({ lifecycle }) => (
35
+ <>I'm enabled and loaded, here is the content: {lifecycle.data}</>
36
+ )),
37
+ )
21
38
  ```
22
39
 
40
+ ## Multiple conditions and fallbacks
41
+
42
+ You can use the `condition` and `conditionalFallback` multiple times in the same pipeline by providing different
43
+ argument names as the first parameter.
44
+
45
+ ## Implementation
46
+
47
+ The implementation for those stages is a bit more involved than other official extensions. Here, we have restrictions
48
+ for each stage, as well as stage hooks.
49
+
23
50
  ```tsx
24
- import { modularFactory } from '@modular-component/core'
25
- import { WithConditionalRender } from '@modular-component/with-conditional-render'
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
- ```
51
+ import React, { FunctionComponent } from 'react'
52
+ import { ModularStage } from '@modular-component/core'
47
53
 
48
- ## Learn more
54
+ export function condition<Args, Name extends string>(
55
+ name: Name,
56
+ useCondition: (args: Args) => boolean,
57
+ ): ModularStage<Name, (args: Args) => boolean> {
58
+ return { field: name, useStage: useCondition }
59
+ }
49
60
 
50
- Read the [`ModularComponent` ReadMe](https://github.com/jvdsande/modular-component/blob/master/README.md) for more information about the `ModularComponent` system.
61
+ export function conditionalFallback<
62
+ Args extends { [key in Name]: boolean } & {
63
+ render?: ReturnType<FunctionComponent>
64
+ },
65
+ Name extends string,
66
+ >(
67
+ name: Name,
68
+ useRender: (args: Args) => ReturnType<FunctionComponent>,
69
+ ): ModularStage<`render-${Name}`, (args: Args) => void> {
70
+ return {
71
+ field: `render-${name}`,
72
+ useStage: (args: Args) => {
73
+ args.render = !args[name] || args.render ? args.render : useRender(args)
74
+ },
75
+ }
76
+ }
77
+
78
+ export function conditionalRender<Args, Ref>(
79
+ useRender: (
80
+ args: Args,
81
+ ref: React.ForwardedRef<Ref>,
82
+ ) => ReturnType<FunctionComponent>,
83
+ ): ModularStage<
84
+ 'render',
85
+ (args: Args, ref: React.ForwardedRef<Ref>) => ReturnType<FunctionComponent>
86
+ > {
87
+ return {
88
+ field: 'render',
89
+ useStage: (args: Args, ref: React.ForwardedRef<Ref>) =>
90
+ (args as any).render ?? useRender(args, ref),
91
+ }
92
+ }
93
+ ```
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "Default",
9
9
  "Conditional"
10
10
  ],
11
- "version": "0.2.0",
11
+ "version": "0.2.2",
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.0"
29
+ "@modular-component/core": "0.2.2"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "react": ">=17 <19"