@modular-component/with-conditional-render 0.2.3 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @modular-component/with-conditional-render
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1784a2b: Add a clean way to extend the ModularComponent API with new dedicated stage functions
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [1784a2b]
12
+ - @modular-component/core@0.3.0
13
+
3
14
  ## 0.2.3
4
15
 
5
16
  ### Patch Changes
package/README.md CHANGED
@@ -1,18 +1,21 @@
1
1
  # @modular-component/with-conditional-render
2
2
 
3
- Provides three stages that allow conditional rendering in `ModularComponent`s:
3
+ Provides three stages that allow conditional rendering in `ModularComponents`:
4
4
 
5
- - `with(condition)` will set a customizable argument to either `true` or `false`, based
5
+ - `condition()` will set a customizable argument to either `true` or `false`, based
6
6
  on current arguments,
7
- - `with(conditionalFallback)` takes a `FunctionComponent` as parameter, and
7
+ - `conditionalFallback()` takes a `FunctionComponent` as parameter, and
8
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.
9
+ - `conditionalRender()` also takes a `FunctionComponent` as parameter, and
10
+ renders it only if the `render` argument was not filled earlier.
11
11
 
12
12
  ## Usage
13
13
 
14
+ **Stage function imports**
15
+
14
16
  ```tsx
15
17
  import { ModularComponent } from '@modular-component/core'
18
+ import { lifecycle } from '@modular-component/with-lifecycle'
16
19
  import {
17
20
  condition,
18
21
  conditionalFallback,
@@ -20,15 +23,15 @@ import {
20
23
  } from '@modular-component/with-conditional-render'
21
24
 
22
25
  const ConditionalComponent = ModularComponent<{ enabled?: boolean }>()
23
- .with(condition('disabled', ({ props }) => props.enabled !== true))
24
- .with(conditionalFallback('disabled', () => <>I'm disabled!</>))
25
26
  .with(
26
27
  lifecycle(() => {
27
28
  // Some data fetching logic...
28
29
  return { loading, data }
29
30
  }),
30
31
  )
31
- .with(condition('loading', ({ lifecycle }) => lifecycle.loading === false))
32
+ .with(condition('disabled', ({ props }) => props.enabled !== true))
33
+ .with(conditionalFallback('disabled', () => <>I'm disabled!</>))
34
+ .with(condition('loading', ({ lifecycle }) => lifecycle.loading))
32
35
  .with(conditionalFallback('loading', () => <>I'm loading!</>))
33
36
  .with(
34
37
  conditionalRender(({ lifecycle }) => (
@@ -37,57 +40,57 @@ const ConditionalComponent = ModularComponent<{ enabled?: boolean }>()
37
40
  )
38
41
  ```
39
42
 
43
+ **Stage registration**
44
+
45
+ ```tsx
46
+ import { ModularComponent } from '@modular-component/core'
47
+ import '@modular-component/with-lifecycle/register'
48
+ import '@modular-component/with-conditional-render/register'
49
+
50
+ const ConditionalComponent = ModularComponent<{ enabled?: boolean }>()
51
+ .withLifecycle(() => {
52
+ // Some data fetching logic...
53
+ return { loading, data }
54
+ })
55
+ .withCondition('disabled', ({ props }) => props.enabled !== true)
56
+ .withConditionalFallback('disabled', () => <>I'm disabled!</>)
57
+ .withCondition('loading', ({ lifecycle }) => lifecycle.loading)
58
+ .withConditionalFallback('loading', () => <>I'm loading!</>)
59
+ .withConditionalRender(({ lifecycle }) => (
60
+ <>I'm enabled and loaded, here is the content: {lifecycle.data}</>
61
+ ))
62
+ ```
63
+
40
64
  ## Multiple conditions and fallbacks
41
65
 
42
66
  You can use the `condition` and `conditionalFallback` multiple times in the same pipeline by providing different
43
67
  argument names as the first parameter.
44
68
 
45
- ## Implementation
69
+ ## Stage registration
46
70
 
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.
71
+ You can either automatically register the stages on `withCondition`, `withConditionalFallback` and `withConditionalRender` by importing `@modular-component/with-conditional-render/register`,
72
+ or handle the registration manually thanks to the `condition`, `conditionalFallback`, `conditionalRender` functions and `WithCondition`, `WithConditionalFallback`, `WithConditionalRender` types exports.
49
73
 
50
- ```tsx
51
- import React, { FunctionComponent } from 'react'
52
- import { ModularStage } from '@modular-component/core'
53
-
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
- }
74
+ ```ts
75
+ import { ModularComponent, ModularContext } from '@modular-component/core'
76
+ import {
77
+ condition,
78
+ conditionalFallback,
79
+ conditionalRender,
80
+ WithCondition,
81
+ WithConditionalFallback,
82
+ WithConditionalRender,
83
+ } from '@modular-component/with-conditional-render'
60
84
 
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
- }
85
+ // Register the stages on the factory
86
+ ModularComponent.register({ condition, conditionalFallback, conditionalRender })
77
87
 
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),
88
+ // Extend the type definition
89
+ declare module '@modular-component/stages' {
90
+ export interface ModularComponentStages<Context extends ModularContext> {
91
+ withCondition: WithCondition<Context>
92
+ withConditionalFallback: WithConditionalFallback<Context>
93
+ withConditionalRender: WithConditionalRender<Context>
91
94
  }
92
95
  }
93
96
  ```
package/dist/index.d.ts CHANGED
@@ -1,10 +1,22 @@
1
- import React, { FunctionComponent } from 'react';
2
- import { ModularStage } from '@modular-component/core';
3
- export declare function condition<Args, Name extends string>(name: Name, useCondition: (args: Args) => boolean): ModularStage<Name, (args: Args) => boolean>;
4
- export declare function conditionalFallback<Args extends {
5
- [key in Name]: boolean;
6
- } & {
7
- render?: ReturnType<FunctionComponent>;
8
- }, Name extends string>(name: Name, useRender: (args: Args) => ReturnType<FunctionComponent>): ModularStage<`render-${Name}`, (args: Args) => void>;
9
- export declare function conditionalRender<Args, Ref>(useRender: (args: Args, ref: React.ForwardedRef<Ref>) => ReturnType<FunctionComponent>): ModularStage<'render', (args: Args, ref: React.ForwardedRef<Ref>) => ReturnType<FunctionComponent>>;
1
+ import { FunctionComponent } from 'react';
2
+ import { ModularContext, GetValueGetterFor, StageParams, StageReturn } from '@modular-component/core/extend';
3
+ type GetConditions<Context extends ModularContext> = {
4
+ [key in keyof Context['arguments']]: Context['arguments'][key] extends boolean ? key extends string ? key : never : never;
5
+ }[keyof Context['arguments']];
6
+ export declare function condition<Context extends ModularContext, Field extends string>(field: Field, useCondition: GetValueGetterFor<Context, Field, boolean>): (_?: Context | undefined) => {
7
+ field: Field;
8
+ provide: (args: import("@modular-component/core/extend").GetArgsFor<Context, Field>) => boolean;
9
+ };
10
+ export type WithCondition<Context extends ModularContext> = <Field extends string>(...args: StageParams<typeof condition<Context, Field>>) => StageReturn<typeof condition<Context, Field>>;
11
+ export declare function conditionalFallback<Context extends ModularContext, Condition extends GetConditions<Context>>(condition: Condition, useRender: GetValueGetterFor<Context, `render-${Condition}`, ReturnType<FunctionComponent>>): (_?: Context | undefined) => {
12
+ field: `render-${Condition}`;
13
+ provide: (args: import("@modular-component/core/extend").GetArgsFor<Context, `render-${Condition}`>) => boolean;
14
+ };
15
+ export type WithConditionalFallback<Context extends ModularContext> = <Condition extends GetConditions<Context>>(...args: StageParams<typeof conditionalFallback<Context, Condition>>) => StageReturn<typeof conditionalFallback<Context, Condition>>;
16
+ export declare function conditionalRender<Context extends ModularContext>(useRender: GetValueGetterFor<Context, 'render', ReturnType<FunctionComponent>>): (_?: Context | undefined) => {
17
+ field: "render";
18
+ provide: (args: import("@modular-component/core/extend").GetArgsFor<Context, "render">) => ReturnType<FunctionComponent>;
19
+ };
20
+ export type WithConditionalRender<Context extends ModularContext> = (...args: StageParams<typeof conditionalRender<Context>>) => StageReturn<typeof conditionalRender<Context>>;
21
+ export {};
10
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,SAAS,MAAM,EACjD,IAAI,EAAE,IAAI,EACV,YAAY,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,GACpC,YAAY,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,CAE7C;AAED,wBAAgB,mBAAmB,CACjC,IAAI,SAAS;KAAG,GAAG,IAAI,IAAI,GAAG,OAAO;CAAE,GAAG;IACxC,MAAM,CAAC,EAAE,UAAU,CAAC,iBAAiB,CAAC,CAAA;CACvC,EACD,IAAI,SAAS,MAAM,EAEnB,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,UAAU,CAAC,iBAAiB,CAAC,GACvD,YAAY,CAAC,UAAU,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC,CAOtD;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,EACzC,SAAS,EAAE,CACT,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,KACzB,UAAU,CAAC,iBAAiB,CAAC,GACjC,YAAY,CACb,QAAQ,EACR,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,UAAU,CAAC,iBAAiB,CAAC,CAC5E,CAMA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAGL,cAAc,EACd,iBAAiB,EACjB,WAAW,EACX,WAAW,EACZ,MAAM,gCAAgC,CAAA;AAEvC,KAAK,aAAa,CAAC,OAAO,SAAS,cAAc,IAAI;KAClD,GAAG,IAAI,MAAM,OAAO,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,SAAS,OAAO,GAC1E,GAAG,SAAS,MAAM,GAChB,GAAG,GACH,KAAK,GACP,KAAK;CACV,CAAC,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC,CAAA;AAE7B,wBAAgB,SAAS,CAAC,OAAO,SAAS,cAAc,EAAE,KAAK,SAAS,MAAM,EAC5E,KAAK,EAAE,KAAK,EACZ,YAAY,EAAE,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC;;;EAGzD;AAED,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,cAAc,IAAI,CAC1D,KAAK,SAAS,MAAM,EAEpB,GAAG,IAAI,EAAE,WAAW,CAAC,OAAO,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,KACnD,WAAW,CAAC,OAAO,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAA;AAElD,wBAAgB,mBAAmB,CACjC,OAAO,SAAS,cAAc,EAC9B,SAAS,SAAS,aAAa,CAAC,OAAO,CAAC,EAExC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,iBAAiB,CAC1B,OAAO,EACP,UAAU,SAAS,EAAE,EACrB,UAAU,CAAC,iBAAiB,CAAC,CAC9B;;;EAcF;AAED,MAAM,MAAM,uBAAuB,CAAC,OAAO,SAAS,cAAc,IAAI,CACpE,SAAS,SAAS,aAAa,CAAC,OAAO,CAAC,EAExC,GAAG,IAAI,EAAE,WAAW,CAAC,OAAO,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,KACjE,WAAW,CAAC,OAAO,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAAA;AAEhE,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,cAAc,EAC9D,SAAS,EAAE,iBAAiB,CAC1B,OAAO,EACP,QAAQ,EACR,UAAU,CAAC,iBAAiB,CAAC,CAC9B;;+FAKW,UAAU,CAAC,iBAAiB,CAAC;EAI1C;AAED,MAAM,MAAM,qBAAqB,CAAC,OAAO,SAAS,cAAc,IAAI,CAClE,GAAG,IAAI,EAAE,WAAW,CAAC,OAAO,iBAAiB,CAAC,OAAO,CAAC,CAAC,KACpD,WAAW,CAAC,OAAO,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAA"}
package/dist/index.js CHANGED
@@ -1,18 +1,22 @@
1
- export function condition(name, useCondition) {
2
- return { field: name, useStage: useCondition };
1
+ import { addTo, wrap, } from '@modular-component/core/extend';
2
+ export function condition(field, useCondition) {
3
+ return addTo().on(field).provide(wrap(useCondition));
3
4
  }
4
- export function conditionalFallback(name, useRender) {
5
- return {
6
- field: `render-${name}`,
7
- useStage: (args) => {
8
- args.render = !args[name] || args.render ? args.render : useRender(args);
9
- },
10
- };
5
+ export function conditionalFallback(condition, useRender) {
6
+ return addTo()
7
+ .on(`render-${condition}`)
8
+ .provide((args) => {
9
+ const _args = args;
10
+ if (_args[condition] && !_args.render) {
11
+ _args.render = wrap(useRender)(args);
12
+ }
13
+ return !!_args[condition];
14
+ });
11
15
  }
12
16
  export function conditionalRender(useRender) {
13
- return {
14
- field: 'render',
15
- useStage: (args, ref) => args.render ?? useRender(args, ref),
16
- };
17
+ return addTo()
18
+ .on('render')
19
+ .provide((args) => args.render ??
20
+ wrap(useRender)(args));
17
21
  }
18
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,SAAS,CACvB,IAAU,EACV,YAAqC;IAErC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAA;AAChD,CAAC;AAED,MAAM,UAAU,mBAAmB,CAMjC,IAAU,EACV,SAAwD;IAExD,OAAO;QACL,KAAK,EAAE,UAAU,IAAI,EAAE;QACvB,QAAQ,EAAE,CAAC,IAAU,EAAE,EAAE;YACvB,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QAC1E,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,SAGkC;IAKlC,OAAO;QACL,KAAK,EAAE,QAAQ;QACf,QAAQ,EAAE,CAAC,IAAU,EAAE,GAA4B,EAAE,EAAE,CACpD,IAAY,CAAC,MAAM,IAAI,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC;KAC/C,CAAA;AACH,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,IAAI,GAKL,MAAM,gCAAgC,CAAA;AAUvC,MAAM,UAAU,SAAS,CACvB,KAAY,EACZ,YAAwD;IAExD,OAAO,KAAK,EAAW,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAA;AAC/D,CAAC;AAQD,MAAM,UAAU,mBAAmB,CAIjC,SAAoB,EACpB,SAIC;IAED,OAAO,KAAK,EAAW;SACpB,EAAE,CAAC,UAAU,SAAS,EAAE,CAAC;SACzB,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAChB,MAAM,KAAK,GAAG,IAGb,CAAA;QACD,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACtC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAA;QACtC,CAAC;QACD,OAAO,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAC3B,CAAC,CAAC,CAAA;AACN,CAAC;AAQD,MAAM,UAAU,iBAAiB,CAC/B,SAIC;IAED,OAAO,KAAK,EAAW;SACpB,EAAE,CAAC,QAAQ,CAAC;SACZ,OAAO,CACN,CAAC,IAAI,EAAiC,EAAE,CACrC,IAAmD,CAAC,MAAM;QAC3D,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CACxB,CAAA;AACL,CAAC"}
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "Default",
9
9
  "Conditional"
10
10
  ],
11
- "version": "0.2.3",
11
+ "version": "0.3.0",
12
12
  "type": "module",
13
13
  "license": "MIT",
14
14
  "publishConfig": {
@@ -17,23 +17,24 @@
17
17
  "files": [
18
18
  "src",
19
19
  "dist",
20
+ "register.js",
21
+ "register.d.ts",
20
22
  "CHANGELOG.md"
21
23
  ],
22
24
  "scripts": {
23
25
  "build": "yarn build:core && yarn build:self",
24
26
  "build:core": "yarn workspace @modular-component/core build",
25
- "build:self": "tsc -p tsconfig.build.json",
27
+ "build:self": "tsc -p tsconfig.build.json && tsc --noEmit register",
26
28
  "license": "cp ../../LICENSE ./LICENSE"
27
29
  },
28
- "dependencies": {
29
- "@modular-component/core": "0.2.3"
30
- },
31
30
  "peerDependencies": {
31
+ "@modular-component/core": "0.3.0",
32
32
  "react": ">=17 <19"
33
33
  },
34
34
  "devDependencies": {
35
+ "@modular-component/core": "*",
35
36
  "@types/react": "^18.0.17",
36
- "typescript": "^5.2.2"
37
+ "typescript": "^5.9.3"
37
38
  },
38
39
  "main": "dist/index.js",
39
40
  "types": "dist/index.d.ts"
package/register.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { ModularContext } from '@modular-component/core'
2
+ import {
3
+ WithCondition,
4
+ WithConditionalFallback,
5
+ WithConditionalRender,
6
+ } from '@modular-component/with-conditional-render'
7
+
8
+ declare module '@modular-component/stages' {
9
+ export interface ModularComponentStages<Context extends ModularContext> {
10
+ withCondition: WithCondition<Context>
11
+ withConditionalFallback: WithConditionalFallback<Context>
12
+ withConditionalRender: WithConditionalRender<Context>
13
+ }
14
+ }
package/register.js ADDED
@@ -0,0 +1,8 @@
1
+ import { ModularComponent } from '@modular-component/core'
2
+ import {
3
+ condition,
4
+ conditionalFallback,
5
+ conditionalRender,
6
+ } from '@modular-component/with-conditional-render'
7
+
8
+ ModularComponent.register({ condition, conditionalFallback, conditionalRender })
package/src/index.ts CHANGED
@@ -1,42 +1,81 @@
1
- import React, { FunctionComponent } from 'react'
2
- import { ModularStage } from '@modular-component/core'
1
+ import { FunctionComponent } from 'react'
2
+ import {
3
+ addTo,
4
+ wrap,
5
+ ModularContext,
6
+ GetValueGetterFor,
7
+ StageParams,
8
+ StageReturn,
9
+ } from '@modular-component/core/extend'
3
10
 
4
- export function condition<Args, Name extends string>(
5
- name: Name,
6
- useCondition: (args: Args) => boolean,
7
- ): ModularStage<Name, (args: Args) => boolean> {
8
- return { field: name, useStage: useCondition }
11
+ type GetConditions<Context extends ModularContext> = {
12
+ [key in keyof Context['arguments']]: Context['arguments'][key] extends boolean
13
+ ? key extends string
14
+ ? key
15
+ : never
16
+ : never
17
+ }[keyof Context['arguments']]
18
+
19
+ export function condition<Context extends ModularContext, Field extends string>(
20
+ field: Field,
21
+ useCondition: GetValueGetterFor<Context, Field, boolean>,
22
+ ) {
23
+ return addTo<Context>().on(field).provide(wrap(useCondition))
9
24
  }
10
25
 
26
+ export type WithCondition<Context extends ModularContext> = <
27
+ Field extends string,
28
+ >(
29
+ ...args: StageParams<typeof condition<Context, Field>>
30
+ ) => StageReturn<typeof condition<Context, Field>>
31
+
11
32
  export function conditionalFallback<
12
- Args extends { [key in Name]: boolean } & {
13
- render?: ReturnType<FunctionComponent>
14
- },
15
- Name extends string,
33
+ Context extends ModularContext,
34
+ Condition extends GetConditions<Context>,
16
35
  >(
17
- name: Name,
18
- useRender: (args: Args) => ReturnType<FunctionComponent>,
19
- ): ModularStage<`render-${Name}`, (args: Args) => void> {
20
- return {
21
- field: `render-${name}`,
22
- useStage: (args: Args) => {
23
- args.render = !args[name] || args.render ? args.render : useRender(args)
24
- },
25
- }
36
+ condition: Condition,
37
+ useRender: GetValueGetterFor<
38
+ Context,
39
+ `render-${Condition}`,
40
+ ReturnType<FunctionComponent>
41
+ >,
42
+ ) {
43
+ return addTo<Context>()
44
+ .on(`render-${condition}`)
45
+ .provide((args) => {
46
+ const _args = args as {
47
+ [condition]?: boolean
48
+ render?: ReturnType<FunctionComponent>
49
+ }
50
+ if (_args[condition] && !_args.render) {
51
+ _args.render = wrap(useRender)(args)
52
+ }
53
+ return !!_args[condition]
54
+ })
26
55
  }
27
56
 
28
- export function conditionalRender<Args, Ref>(
29
- useRender: (
30
- args: Args,
31
- ref: React.ForwardedRef<Ref>,
32
- ) => ReturnType<FunctionComponent>,
33
- ): ModularStage<
34
- 'render',
35
- (args: Args, ref: React.ForwardedRef<Ref>) => ReturnType<FunctionComponent>
36
- > {
37
- return {
38
- field: 'render',
39
- useStage: (args: Args, ref: React.ForwardedRef<Ref>) =>
40
- (args as any).render ?? useRender(args, ref),
41
- }
57
+ export type WithConditionalFallback<Context extends ModularContext> = <
58
+ Condition extends GetConditions<Context>,
59
+ >(
60
+ ...args: StageParams<typeof conditionalFallback<Context, Condition>>
61
+ ) => StageReturn<typeof conditionalFallback<Context, Condition>>
62
+
63
+ export function conditionalRender<Context extends ModularContext>(
64
+ useRender: GetValueGetterFor<
65
+ Context,
66
+ 'render',
67
+ ReturnType<FunctionComponent>
68
+ >,
69
+ ) {
70
+ return addTo<Context>()
71
+ .on('render')
72
+ .provide(
73
+ (args): ReturnType<FunctionComponent> =>
74
+ (args as { render?: ReturnType<FunctionComponent> }).render ??
75
+ wrap(useRender)(args),
76
+ )
42
77
  }
78
+
79
+ export type WithConditionalRender<Context extends ModularContext> = (
80
+ ...args: StageParams<typeof conditionalRender<Context>>
81
+ ) => StageReturn<typeof conditionalRender<Context>>