@modular-component/core 0.2.3 → 0.3.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 +18 -0
- package/README.md +21 -7
- package/dist/extend.d.ts +52 -0
- package/dist/extend.d.ts.map +1 -0
- package/dist/extend.js +22 -0
- package/dist/extend.js.map +1 -0
- package/dist/index.d.ts +49 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -24
- package/dist/index.js.map +1 -1
- package/dist/render.d.ts +8 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +5 -0
- package/dist/render.js.map +1 -0
- package/extend.d.ts +1 -0
- package/extend.js +1 -0
- package/package.json +17 -5
- package/register.d.ts +7 -0
- package/register.js +3 -0
- package/src/extend.ts +111 -0
- package/src/index.ts +144 -138
- package/src/render.ts +23 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @modular-component/core
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6fa514f: Fix exports compatibility with some bundlers
|
|
8
|
+
- Updated dependencies [6fa514f]
|
|
9
|
+
- @modular-component/stages@0.3.1
|
|
10
|
+
|
|
11
|
+
## 0.3.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- 1784a2b: Add a clean way to extend the ModularComponent API with new dedicated stage functions
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- @modular-component/stages@0.3.0
|
|
20
|
+
|
|
3
21
|
## 0.2.3
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @modular-component/core
|
|
2
2
|
|
|
3
|
-
Core system for creating
|
|
4
|
-
|
|
3
|
+
Core system for creating Modular components. Exports the `ModularComponent` factory function, as well as
|
|
4
|
+
helpers for creating stages.
|
|
5
5
|
|
|
6
6
|
## Installation and usage
|
|
7
7
|
|
|
@@ -19,14 +19,28 @@ const MyComponent = ModularComponent().with(
|
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
```tsx
|
|
22
|
-
//
|
|
23
|
-
import {
|
|
22
|
+
// Or by extending the factory
|
|
23
|
+
import { ModularComponent } from '@modular-component/core'
|
|
24
|
+
import '@modular-component/core/register'
|
|
25
|
+
|
|
26
|
+
const MyComponent = ModularComponent().withRender(() => (
|
|
27
|
+
<div>Hello Modular!</div>
|
|
28
|
+
))
|
|
29
|
+
```
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
```tsx
|
|
32
|
+
// Usage in extensions
|
|
33
|
+
import { ModularContext, addTo } from '@modular-component/core/extend'
|
|
34
|
+
|
|
35
|
+
export function extension<Context extends ModularContext>() {
|
|
36
|
+
return addTo<Context>()
|
|
37
|
+
.on('field')
|
|
38
|
+
.provide((args) => {
|
|
39
|
+
// Compute value for argument 'field' from previous stage arguments
|
|
40
|
+
})
|
|
27
41
|
}
|
|
28
42
|
```
|
|
29
43
|
|
|
30
44
|
## Learn more
|
|
31
45
|
|
|
32
|
-
Read the [`ModularComponent` ReadMe](https://github.com/
|
|
46
|
+
Read the [`ModularComponent` ReadMe](https://github.com/modular-component/modular-component/blob/master/README.md) for more information about the `ModularComponent` system.
|
package/dist/extend.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { FunctionComponent } from 'react';
|
|
2
|
+
import type { ModularContext } from '@modular-component/stages';
|
|
3
|
+
import type { ModularComponent } from './index';
|
|
4
|
+
export type { ModularContext } from '@modular-component/stages';
|
|
5
|
+
export type GetArgsFor<Context extends ModularContext, Field extends string> = Field extends keyof Context['stages'] ? Pick<Context['arguments'], Context['stages'][Field]> : Context['arguments'];
|
|
6
|
+
export type GetConstraintFor<Context extends ModularContext, Field extends string, Default = any> = Field extends 'render' ? ReturnType<FunctionComponent> : Field extends keyof Context['constraints'] ? Context['constraints'][Field] : Default;
|
|
7
|
+
export type GetValueGetterFor<Context extends ModularContext, Field extends string, Type> = Type | ((args: GetArgsFor<Context, Field>) => Type);
|
|
8
|
+
type AppendArguments<Arguments, Field extends string, Type> = {
|
|
9
|
+
[key in Exclude<keyof Arguments, Field>]: Arguments[key];
|
|
10
|
+
} & {
|
|
11
|
+
[key in Field]: Type;
|
|
12
|
+
} extends infer U ? {
|
|
13
|
+
[key in keyof U]: U[key];
|
|
14
|
+
} : never;
|
|
15
|
+
type AppendConstraints<Constraints, Backup, Field extends string, Type> = {
|
|
16
|
+
[key in Exclude<keyof Constraints, Field>]: Constraints[key];
|
|
17
|
+
} & {
|
|
18
|
+
[key in Exclude<keyof Backup, Field>]: Backup[key];
|
|
19
|
+
} & {
|
|
20
|
+
[key in Field]: Type;
|
|
21
|
+
} extends infer U ? {
|
|
22
|
+
[key in keyof U]: U[key];
|
|
23
|
+
} : never;
|
|
24
|
+
type AppendStages<Stages, Arguments, Field extends string, Type> = {
|
|
25
|
+
[key in Exclude<keyof Stages, Field>]: Stages[key];
|
|
26
|
+
} & {
|
|
27
|
+
[key in Field]: key extends keyof Stages ? Stages[key] : keyof Arguments;
|
|
28
|
+
} extends infer U ? {
|
|
29
|
+
[key in keyof U]: U[key];
|
|
30
|
+
} : never;
|
|
31
|
+
export type AppendStage<Context extends ModularContext, Field extends string, Type> = Pick<Context, 'props' | 'ref'> & {
|
|
32
|
+
arguments: AppendArguments<Context['arguments'], Field, Type>;
|
|
33
|
+
constraints: AppendConstraints<Context['constraints'], Context['_constraints'], Field, Type>;
|
|
34
|
+
stages: AppendStages<Context['stages'], Context['arguments'], Field, Type>;
|
|
35
|
+
} extends infer U ? {
|
|
36
|
+
[key in keyof U]: U[key];
|
|
37
|
+
} : never;
|
|
38
|
+
export type StageParams<Fn extends (...args: any[]) => any> = Parameters<Fn>;
|
|
39
|
+
export type StageReturn<Fn extends (...args: any[]) => (ctx?: any) => {
|
|
40
|
+
field: string;
|
|
41
|
+
provide: (args: any) => any;
|
|
42
|
+
}> = ModularComponent<AppendStage<NonNullable<Parameters<ReturnType<Fn>>[0]>, ReturnType<ReturnType<Fn>>['field'], ReturnType<ReturnType<ReturnType<Fn>>['provide']>>> extends ModularComponent<infer U> ? ModularComponent<U> : never;
|
|
43
|
+
export declare function addTo<Context extends ModularContext>(): {
|
|
44
|
+
on<Field extends string>(field: Field): {
|
|
45
|
+
provide<Stage extends (args: GetArgsFor<Context, Field>) => any>(stage: Stage): (_?: Context) => {
|
|
46
|
+
field: Field;
|
|
47
|
+
provide: Stage;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export declare function wrap<Args, Type>(useFn: Type | ((args: Args) => Type)): (args: Args) => Type;
|
|
52
|
+
//# sourceMappingURL=extend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend.d.ts","sourceRoot":"","sources":["../src/extend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C,YAAY,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAE/D,MAAM,MAAM,UAAU,CACpB,OAAO,SAAS,cAAc,EAC9B,KAAK,SAAS,MAAM,IAClB,KAAK,SAAS,MAAM,OAAO,CAAC,QAAQ,CAAC,GACrC,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,GACpD,OAAO,CAAC,WAAW,CAAC,CAAA;AAExB,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SAAS,cAAc,EAC9B,KAAK,SAAS,MAAM,EACpB,OAAO,GAAG,GAAG,IACX,KAAK,SAAS,QAAQ,G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CACT,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1C,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,EACnC,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAClD,CACF,SAAS,gBAAgB,CAAC,MAAM,CAAC,CAAC,GAC/B,gBAAgB,CAAC,CAAC,CAAC,GACnB,KAAK,CAAA;AAET,wBAAgB,KAAK,CAAC,OAAO,SAAS,cAAc;OAE7C,KAAK,SAAS,MAAM,SAAS,KAAK;gBAEzB,KAAK,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,GAAG,SACtD,KAAK,IAEJ,IAAI,OAAO;;;;;EAQ5B;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC,IAC3D,MAAM,IAAI,UAKnB"}
|
package/dist/extend.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function addTo() {
|
|
2
|
+
return {
|
|
3
|
+
on(field) {
|
|
4
|
+
return {
|
|
5
|
+
provide(stage) {
|
|
6
|
+
return (_) => ({
|
|
7
|
+
field,
|
|
8
|
+
provide: stage,
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function wrap(useFn) {
|
|
16
|
+
return (args) => {
|
|
17
|
+
return typeof useFn === 'function'
|
|
18
|
+
? useFn(args)
|
|
19
|
+
: useFn;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=extend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend.js","sourceRoot":"","sources":["../src/extend.ts"],"names":[],"mappings":"AAuFA,MAAM,UAAU,KAAK;IACnB,OAAO;QACL,EAAE,CAAuB,KAAY;YACnC,OAAO;gBACL,OAAO,CACL,KAAY;oBAEZ,OAAO,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC;wBACvB,KAAK;wBACL,OAAO,EAAE,KAAK;qBACf,CAAC,CAAA;gBACJ,CAAC;aACF,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,IAAI,CAAa,KAAoC;IACnE,OAAO,CAAC,IAAU,EAAE,EAAE;QACpB,OAAO,OAAO,KAAK,KAAK,UAAU;YAChC,CAAC,CAAE,KAA8B,CAAC,IAAI,CAAC;YACvC,CAAC,CAAC,KAAK,CAAA;IACX,CAAC,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,55 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
never
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
1
|
+
import { ForwardRefRenderFunction, FunctionComponent, PropsWithChildren } from 'react';
|
|
2
|
+
import { ModularComponentStages, ModularContext } from '@modular-component/stages';
|
|
3
|
+
import { AppendStage, GetArgsFor, GetConstraintFor } from './extend';
|
|
4
|
+
type FunctionComponentOrRefRenderFunction<Props, Ref> = [Ref] extends [never] ? FunctionComponent<PropsWithChildren<Props>> : ForwardRefRenderFunction<Ref, PropsWithChildren<Props>>;
|
|
5
|
+
export type { ModularContext } from '@modular-component/stages';
|
|
6
|
+
type MapToForce<Stages> = {
|
|
7
|
+
[key in keyof Stages as key extends `with${infer K}` ? `force${K}` : never]: Stages[key];
|
|
8
|
+
};
|
|
9
|
+
type MapToStage<Stages> = Pick<Stages, {
|
|
10
|
+
[key in keyof Stages]: key extends `with${string}` ? key : never;
|
|
11
|
+
}[keyof Stages]>;
|
|
12
|
+
type MapToRaw<Stages> = {
|
|
13
|
+
[key in keyof Stages as key extends `with${infer K}` ? Uncapitalize<K> : never]: Stages[key];
|
|
14
|
+
};
|
|
15
|
+
type Force<Context extends ModularContext> = Omit<Context, 'constraints'> & {
|
|
16
|
+
constraints: {};
|
|
17
|
+
_constraints: Context['constraints'];
|
|
18
|
+
};
|
|
19
|
+
export type ModularComponent<Context extends ModularContext> = FunctionComponentOrRefRenderFunction<Context['props'], Context['ref']> & MapToStage<ModularComponentStages<Context>> & MapToForce<ModularComponentStages<Force<Context>>> & {
|
|
20
|
+
with<Field extends string, Type extends GetConstraintFor<Context, Field>>(stage: (context?: Context) => {
|
|
13
21
|
field: Field;
|
|
14
|
-
|
|
15
|
-
}): ModularComponent<
|
|
16
|
-
|
|
17
|
-
}>;
|
|
18
|
-
force<Field extends string, Type>(stage: {
|
|
22
|
+
provide: (args: GetArgsFor<Context, Field>) => Type;
|
|
23
|
+
}): ModularComponent<AppendStage<Context, Field, Type>>;
|
|
24
|
+
force<Field extends string, Type>(stage: (context?: Force<Context>) => {
|
|
19
25
|
field: Field;
|
|
20
|
-
|
|
21
|
-
}): ModularComponent<
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
stage<Field extends keyof Args>(key: Field): (args: Partial<Args>) => Args[Field];
|
|
27
|
-
withDisplayName(displayName: string): ModularComponent<Props, Ref, Args>;
|
|
26
|
+
provide: (args: GetArgsFor<Context, Field>) => Type;
|
|
27
|
+
}): ModularComponent<AppendStage<Context, Field, Type>>;
|
|
28
|
+
use<Field extends keyof Context['arguments']>(key: Field): {} extends Context['arguments']['props'] ? () => Context['arguments'][Field] : (props: PropsWithChildren<Context['arguments']['props']>) => Context['arguments'][Field];
|
|
29
|
+
use(): {} extends Context['props'] ? () => Context['arguments'] : (props: PropsWithChildren<Context['props']>) => Context['arguments'];
|
|
30
|
+
stage<Field extends keyof Context['arguments'] & string>(key: Field): (args: Partial<GetArgsFor<Context, Field>>) => Context['arguments'][Field];
|
|
31
|
+
withDisplayName(displayName: string): ModularComponent<Context>;
|
|
28
32
|
};
|
|
29
|
-
export declare function ModularComponent<Props extends {} = {}, Ref = never>(displayName?: string): ModularComponent<
|
|
33
|
+
export declare function ModularComponent<Props extends {} = {}, Ref = never>(displayName?: string): ModularComponent<{
|
|
30
34
|
props: Props;
|
|
31
|
-
|
|
35
|
+
ref: Ref;
|
|
36
|
+
stages: {};
|
|
37
|
+
arguments: {
|
|
38
|
+
props: Props;
|
|
39
|
+
ref: Ref;
|
|
40
|
+
render: ReturnType<FunctionComponent>;
|
|
41
|
+
};
|
|
42
|
+
constraints: {
|
|
43
|
+
props: Props;
|
|
44
|
+
ref: Ref;
|
|
45
|
+
render: ReturnType<FunctionComponent>;
|
|
46
|
+
};
|
|
32
47
|
}>;
|
|
33
|
-
export declare
|
|
48
|
+
export declare namespace ModularComponent {
|
|
49
|
+
var register: (functions: Partial<Record<keyof MapToRaw<ModularComponentStages<any>>, (...args: any[]) => (ctx?: ModularContext) => {
|
|
50
|
+
field: string;
|
|
51
|
+
provide: (args: any) => any;
|
|
52
|
+
}>>) => void;
|
|
53
|
+
}
|
|
54
|
+
export * from './render.js';
|
|
34
55
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,iBAAiB,EACjB,iBAAiB,EAClB,MAAM,OAAO,CAAA;AACd,OAAO,EACL,sBAAsB,EACtB,cAAc,EACf,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAEpE,KAAK,oCAAoC,CAAC,KAAK,EAA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AK,CAAC,KAAK,SAAS,MAAM,OAAO,CAAC,WAAW,CAAC,GAAG,MAAM,EACrD,GAAG,EAAE,KAAK,GACT,CACD,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,KACtC,OAAO,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAA;IAChC,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAA;CAChE,CAAA;AA2EL,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,KAAK,EACjE,WAAW,CAAC,EAAE,MAAM;WAGX,KAAK;SACP,GAAG;YACA,EAAE;eACC;QACT,KAAK,EAAE,KAAK,CAAA;QACZ,GAAG,EAAE,GAAG,CAAA;QACR,MAAM,EAAE,UAAU,CAAC,iBAAiB,CAAC,CAAA;KACtC;iBACY;QACX,KAAK,EAAE,KAAK,CAAA;QACZ,GAAG,EAAE,GAAG,CAAA;QACR,MAAM,EAAE,UAAU,CAAC,iBAAiB,CAAC,CAAA;KACtC;GAEJ;yBAlBe,gBAAgB;8BAqBnB,OAAO,CAChB,MAAM,CACJ,MAAM,QAAQ,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC,EAC3C,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,cAAc,KAAK;QAC5C,KAAK,EAAE,MAAM,CAAA;QACb,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,CAAA;KAC5B,CACF,CACF;;AAKH,cAAc,aAAa,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
let customFunctions = {};
|
|
1
2
|
function InternalFactory(stages, displayName) {
|
|
2
3
|
const UseComponent = function (props, ref) {
|
|
3
|
-
if (!stages.some((stage) => stage.field === 'render')) {
|
|
4
|
-
stages = [...stages, render(() => null)];
|
|
5
|
-
}
|
|
6
4
|
return UseComponent.use('render')(props, ref);
|
|
7
5
|
};
|
|
8
6
|
UseComponent.displayName = displayName;
|
|
9
|
-
UseComponent.with = (
|
|
7
|
+
UseComponent.with = (_stage) => {
|
|
8
|
+
const stage = _stage();
|
|
10
9
|
const index = stages.findIndex((s) => s.field === stage.field);
|
|
11
10
|
if (index !== -1) {
|
|
12
11
|
const next = [...stages];
|
|
@@ -17,29 +16,30 @@ function InternalFactory(stages, displayName) {
|
|
|
17
16
|
};
|
|
18
17
|
UseComponent.force = UseComponent.with;
|
|
19
18
|
UseComponent.use = (field) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
for (let stage of stages) {
|
|
24
|
-
args[stage.field] = stage.useStage(args, ref);
|
|
25
|
-
}
|
|
26
|
-
return args;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
const index = stages.findIndex((stage) => stage.field === field);
|
|
19
|
+
const index = field
|
|
20
|
+
? stages.findIndex((stage) => stage.field === field)
|
|
21
|
+
: -1;
|
|
30
22
|
const argStages = index === -1 ? stages.slice(0) : stages.slice(0, index + 1);
|
|
31
23
|
return (props = {}, ref = null) => {
|
|
32
|
-
const args = { props };
|
|
24
|
+
const args = { props, ref };
|
|
25
|
+
if (field === 'render') {
|
|
26
|
+
args.render = null;
|
|
27
|
+
}
|
|
33
28
|
for (let stage of argStages) {
|
|
34
|
-
args[stage.field] = stage.
|
|
29
|
+
args[stage.field] = stage.provide(args);
|
|
35
30
|
}
|
|
36
|
-
return args[field];
|
|
31
|
+
return field ? args[field] : args;
|
|
37
32
|
};
|
|
38
33
|
};
|
|
39
34
|
UseComponent.stage = (field) => {
|
|
40
35
|
const stage = stages.find((stage) => stage.field === field);
|
|
41
|
-
return stage?.
|
|
36
|
+
return stage?.provide ?? (() => null);
|
|
42
37
|
};
|
|
38
|
+
Object.entries(customFunctions).forEach(([name, fn]) => {
|
|
39
|
+
;
|
|
40
|
+
UseComponent[`with${name[0].toUpperCase()}${name.slice(1)}`] = (...args) => UseComponent.with(fn(...args));
|
|
41
|
+
UseComponent[`force${name[0].toUpperCase()}${name.slice(1)}`] = (...args) => UseComponent.with(fn(...args));
|
|
42
|
+
});
|
|
43
43
|
UseComponent.withDisplayName = (displayName) => {
|
|
44
44
|
return InternalFactory([...stages], displayName);
|
|
45
45
|
};
|
|
@@ -48,10 +48,8 @@ function InternalFactory(stages, displayName) {
|
|
|
48
48
|
export function ModularComponent(displayName) {
|
|
49
49
|
return InternalFactory([], displayName);
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
}
|
|
51
|
+
ModularComponent.register = (functions) => {
|
|
52
|
+
customFunctions = { ...customFunctions, ...functions };
|
|
53
|
+
};
|
|
54
|
+
export * from './render.js';
|
|
57
55
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4EA,IAAI,eAAe,GAMf,EAAE,CAAA;AAEN,SAAS,eAAe,CACtB,MAAwD,EACxD,WAA+B;IAE/B,MAAM,YAAY,GAAG,UAAU,KAAuB,EAAE,GAAmB;QACzE,OAAO,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IAC/C,CAAC,CAAA;IACD,YAAY,CAAC,WAAW,GAAG,WAAW,CAAA;IAEtC,YAAY,CAAC,IAAI,GAAG,CAClB,MAAyE,EACzE,EAAE;QACF,MAAM,KAAK,GAAG,MAAM,EAAE,CAAA;QACtB,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,C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B,CAAC;AAED,gBAAgB,CAAC,QAAQ,GAAG,CAC1B,SAQC,EACD,EAAE;IACF,eAAe,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,SAAS,EAAE,CAAA;AACxD,CAAC,CAAA;AAED,cAAc,aAAa,CAAA"}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FunctionComponent } from 'react';
|
|
2
|
+
import { ModularContext, GetValueGetterFor, StageParams, StageReturn } from './extend.js';
|
|
3
|
+
export declare function render<Context extends ModularContext>(useRender: GetValueGetterFor<Context, 'render', ReturnType<FunctionComponent>>): (_?: Context | undefined) => {
|
|
4
|
+
field: "render";
|
|
5
|
+
provide: (args: import("./extend.js").GetArgsFor<Context, "render">) => import("react").ReactElement<any, any> | null;
|
|
6
|
+
};
|
|
7
|
+
export type WithRender<Context extends ModularContext> = (...args: StageParams<typeof render<Context>>) => StageReturn<typeof render<Context>>;
|
|
8
|
+
//# sourceMappingURL=render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAGL,cAAc,EACd,iBAAiB,EACjB,WAAW,EACX,WAAW,EACZ,MAAM,aAAa,CAAA;AAEpB,wBAAgB,MAAM,CAAC,OAAO,SAAS,cAAc,EACnD,SAAS,EAAE,iBAAiB,CAC1B,OAAO,EACP,QAAQ,EACR,UAAU,CAAC,iBAAiB,CAAC,CAC9B;;;EAGF;AAED,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,cAAc,IAAI,CACvD,GAAG,IAAI,EAAE,WAAW,CAAC,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,KACzC,WAAW,CAAC,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA"}
|
package/dist/render.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,IAAI,GAKL,MAAM,aAAa,CAAA;AAEpB,MAAM,UAAU,MAAM,CACpB,SAIC;IAED,OAAO,KAAK,EAAW,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAA;AAC/D,CAAC"}
|
package/extend.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/extend'
|
package/extend.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/extend.js'
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"Factory",
|
|
8
8
|
"Test"
|
|
9
9
|
],
|
|
10
|
-
"version": "0.
|
|
10
|
+
"version": "0.3.1",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"publishConfig": {
|
|
@@ -16,20 +16,32 @@
|
|
|
16
16
|
"files": [
|
|
17
17
|
"src",
|
|
18
18
|
"dist",
|
|
19
|
+
"extend.js",
|
|
20
|
+
"extend.d.ts",
|
|
21
|
+
"register.js",
|
|
22
|
+
"register.d.ts",
|
|
19
23
|
"CHANGELOG.md"
|
|
20
24
|
],
|
|
21
25
|
"scripts": {
|
|
22
26
|
"build": "yarn build:self",
|
|
23
|
-
"build:self": "tsc",
|
|
27
|
+
"build:self": "tsc && tsc --noEmit extend register",
|
|
24
28
|
"license": "cp ../../LICENSE ./LICENSE"
|
|
25
29
|
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@modular-component/stages": "0.3.1"
|
|
32
|
+
},
|
|
26
33
|
"peerDependencies": {
|
|
27
|
-
"react": ">=17
|
|
34
|
+
"react": ">=17"
|
|
28
35
|
},
|
|
29
36
|
"devDependencies": {
|
|
30
37
|
"@types/react": "^18.0.17",
|
|
31
|
-
"typescript": "^5.
|
|
38
|
+
"typescript": "^5.9.3"
|
|
32
39
|
},
|
|
33
40
|
"main": "dist/index.js",
|
|
34
|
-
"types": "dist/index.d.ts"
|
|
41
|
+
"types": "dist/index.d.ts",
|
|
42
|
+
"exports": {
|
|
43
|
+
".": "./dist/index.js",
|
|
44
|
+
"./register": "./register.js",
|
|
45
|
+
"./extend": "./extend.js"
|
|
46
|
+
}
|
|
35
47
|
}
|
package/register.d.ts
ADDED
package/register.js
ADDED
package/src/extend.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { FunctionComponent } from 'react'
|
|
2
|
+
import type { ModularContext } from '@modular-component/stages'
|
|
3
|
+
import type { ModularComponent } from './index'
|
|
4
|
+
|
|
5
|
+
export type { ModularContext } from '@modular-component/stages'
|
|
6
|
+
|
|
7
|
+
export type GetArgsFor<
|
|
8
|
+
Context extends ModularContext,
|
|
9
|
+
Field extends string,
|
|
10
|
+
> = Field extends keyof Context['stages']
|
|
11
|
+
? Pick<Context['arguments'], Context['stages'][Field]>
|
|
12
|
+
: Context['arguments']
|
|
13
|
+
|
|
14
|
+
export type GetConstraintFor<
|
|
15
|
+
Context extends ModularContext,
|
|
16
|
+
Field extends string,
|
|
17
|
+
Default = any,
|
|
18
|
+
> = Field extends 'render'
|
|
19
|
+
? ReturnType<FunctionComponent>
|
|
20
|
+
: Field extends keyof Context['constraints']
|
|
21
|
+
? Context['constraints'][Field]
|
|
22
|
+
: Default
|
|
23
|
+
|
|
24
|
+
export type GetValueGetterFor<
|
|
25
|
+
Context extends ModularContext,
|
|
26
|
+
Field extends string,
|
|
27
|
+
Type,
|
|
28
|
+
> = Type | ((args: GetArgsFor<Context, Field>) => Type)
|
|
29
|
+
|
|
30
|
+
type AppendArguments<Arguments, Field extends string, Type> = {
|
|
31
|
+
[key in Exclude<keyof Arguments, Field>]: Arguments[key]
|
|
32
|
+
} & {
|
|
33
|
+
[key in Field]: Type
|
|
34
|
+
} extends infer U
|
|
35
|
+
? { [key in keyof U]: U[key] }
|
|
36
|
+
: never
|
|
37
|
+
|
|
38
|
+
type AppendConstraints<Constraints, Backup, Field extends string, Type> = {
|
|
39
|
+
[key in Exclude<keyof Constraints, Field>]: Constraints[key]
|
|
40
|
+
} & {
|
|
41
|
+
[key in Exclude<keyof Backup, Field>]: Backup[key]
|
|
42
|
+
} & {
|
|
43
|
+
[key in Field]: Type
|
|
44
|
+
} extends infer U
|
|
45
|
+
? { [key in keyof U]: U[key] }
|
|
46
|
+
: never
|
|
47
|
+
|
|
48
|
+
type AppendStages<Stages, Arguments, Field extends string, Type> = {
|
|
49
|
+
[key in Exclude<keyof Stages, Field>]: Stages[key]
|
|
50
|
+
} & {
|
|
51
|
+
[key in Field]: key extends keyof Stages ? Stages[key] : keyof Arguments
|
|
52
|
+
} extends infer U
|
|
53
|
+
? { [key in keyof U]: U[key] }
|
|
54
|
+
: never
|
|
55
|
+
|
|
56
|
+
export type AppendStage<
|
|
57
|
+
Context extends ModularContext,
|
|
58
|
+
Field extends string,
|
|
59
|
+
Type,
|
|
60
|
+
> = Pick<Context, 'props' | 'ref'> & {
|
|
61
|
+
arguments: AppendArguments<Context['arguments'], Field, Type>
|
|
62
|
+
constraints: AppendConstraints<
|
|
63
|
+
Context['constraints'],
|
|
64
|
+
Context['_constraints'],
|
|
65
|
+
Field,
|
|
66
|
+
Type
|
|
67
|
+
>
|
|
68
|
+
stages: AppendStages<Context['stages'], Context['arguments'], Field, Type>
|
|
69
|
+
} extends infer U
|
|
70
|
+
? { [key in keyof U]: U[key] }
|
|
71
|
+
: never
|
|
72
|
+
|
|
73
|
+
export type StageParams<Fn extends (...args: any[]) => any> = Parameters<Fn>
|
|
74
|
+
export type StageReturn<
|
|
75
|
+
Fn extends (
|
|
76
|
+
...args: any[]
|
|
77
|
+
) => (ctx?: any) => { field: string; provide: (args: any) => any },
|
|
78
|
+
> = ModularComponent<
|
|
79
|
+
AppendStage<
|
|
80
|
+
NonNullable<Parameters<ReturnType<Fn>>[0]>,
|
|
81
|
+
ReturnType<ReturnType<Fn>>['field'],
|
|
82
|
+
ReturnType<ReturnType<ReturnType<Fn>>['provide']>
|
|
83
|
+
>
|
|
84
|
+
> extends ModularComponent<infer U>
|
|
85
|
+
? ModularComponent<U>
|
|
86
|
+
: never
|
|
87
|
+
|
|
88
|
+
export function addTo<Context extends ModularContext>() {
|
|
89
|
+
return {
|
|
90
|
+
on<Field extends string>(field: Field) {
|
|
91
|
+
return {
|
|
92
|
+
provide<Stage extends (args: GetArgsFor<Context, Field>) => any>(
|
|
93
|
+
stage: Stage,
|
|
94
|
+
) {
|
|
95
|
+
return (_?: Context) => ({
|
|
96
|
+
field,
|
|
97
|
+
provide: stage,
|
|
98
|
+
})
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function wrap<Args, Type>(useFn: Type | ((args: Args) => Type)) {
|
|
106
|
+
return (args: Args) => {
|
|
107
|
+
return typeof useFn === 'function'
|
|
108
|
+
? (useFn as (args: Args) => Type)(args)
|
|
109
|
+
: useFn
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,178 +1,184 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ForwardRefRenderFunction,
|
|
3
|
+
FunctionComponent,
|
|
4
|
+
PropsWithChildren,
|
|
5
|
+
} from 'react'
|
|
6
|
+
import {
|
|
7
|
+
ModularComponentStages,
|
|
8
|
+
ModularContext,
|
|
9
|
+
} from '@modular-component/stages'
|
|
10
|
+
import { AppendStage, GetArgsFor, GetConstraintFor } from './extend'
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
never,
|
|
13
|
-
]
|
|
12
|
+
type FunctionComponentOrRefRenderFunction<Props, Ref> = [Ref] extends [never]
|
|
14
13
|
? FunctionComponent<PropsWithChildren<Props>>
|
|
15
|
-
: ForwardRefRenderFunction<Ref, Props
|
|
16
|
-
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
force<Field extends string, Type>(stage: {
|
|
42
|
-
field: Field
|
|
43
|
-
useStage: (
|
|
44
|
-
args: Args,
|
|
45
|
-
ref: React.ForwardedRef<Ref>,
|
|
46
|
-
) => Field extends 'render' ? Args['render'] : Type
|
|
47
|
-
}): ModularComponent<
|
|
48
|
-
Props,
|
|
49
|
-
Ref,
|
|
50
|
-
{
|
|
51
|
-
[key in keyof Args | Field]: key extends 'render'
|
|
52
|
-
? ReturnType<FunctionComponent>
|
|
53
|
-
: key extends Field
|
|
54
|
-
? Type
|
|
55
|
-
: key extends keyof Args
|
|
56
|
-
? Args[key]
|
|
57
|
-
: never
|
|
58
|
-
}
|
|
59
|
-
>
|
|
60
|
-
use<Field extends keyof Args>(
|
|
61
|
-
key: Field,
|
|
62
|
-
): {} extends Props
|
|
63
|
-
? () => Args[Field]
|
|
64
|
-
: (props: PropsWithChildren<Props>) => Args[Field]
|
|
65
|
-
use(): {} extends Props
|
|
66
|
-
? () => Args
|
|
67
|
-
: (props: PropsWithChildren<Props>) => Args
|
|
68
|
-
stage<Field extends keyof Args>(
|
|
69
|
-
key: Field,
|
|
70
|
-
): (args: Partial<Args>) => Args[Field]
|
|
71
|
-
withDisplayName(displayName: string): ModularComponent<Props, Ref, Args>
|
|
14
|
+
: ForwardRefRenderFunction<Ref, PropsWithChildren<Props>>
|
|
15
|
+
|
|
16
|
+
export type { ModularContext } from '@modular-component/stages'
|
|
17
|
+
|
|
18
|
+
type MapToForce<Stages> = {
|
|
19
|
+
[key in keyof Stages as key extends `with${infer K}`
|
|
20
|
+
? `force${K}`
|
|
21
|
+
: never]: Stages[key]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type MapToStage<Stages> = Pick<
|
|
25
|
+
Stages,
|
|
26
|
+
{
|
|
27
|
+
[key in keyof Stages]: key extends `with${string}` ? key : never
|
|
28
|
+
}[keyof Stages]
|
|
29
|
+
>
|
|
30
|
+
|
|
31
|
+
type MapToRaw<Stages> = {
|
|
32
|
+
[key in keyof Stages as key extends `with${infer K}`
|
|
33
|
+
? Uncapitalize<K>
|
|
34
|
+
: never]: Stages[key]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type Force<Context extends ModularContext> = Omit<Context, 'constraints'> & {
|
|
38
|
+
constraints: {}
|
|
39
|
+
_constraints: Context['constraints']
|
|
72
40
|
}
|
|
73
41
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
42
|
+
export type ModularComponent<Context extends ModularContext> =
|
|
43
|
+
FunctionComponentOrRefRenderFunction<Context['props'], Context['ref']> &
|
|
44
|
+
MapToStage<ModularComponentStages<Context>> &
|
|
45
|
+
MapToForce<ModularComponentStages<Force<Context>>> & {
|
|
46
|
+
with<Field extends string, Type extends GetConstraintFor<Context, Field>>(
|
|
47
|
+
stage: (context?: Context) => {
|
|
48
|
+
field: Field
|
|
49
|
+
provide: (args: GetArgsFor<Context, Field>) => Type
|
|
50
|
+
},
|
|
51
|
+
): ModularComponent<AppendStage<Context, Field, Type>>
|
|
52
|
+
force<Field extends string, Type>(
|
|
53
|
+
stage: (context?: Force<Context>) => {
|
|
54
|
+
field: Field
|
|
55
|
+
provide: (args: GetArgsFor<Context, Field>) => Type
|
|
56
|
+
},
|
|
57
|
+
): ModularComponent<AppendStage<Context, Field, Type>>
|
|
58
|
+
|
|
59
|
+
use<Field extends keyof Context['arguments']>(
|
|
60
|
+
key: Field,
|
|
61
|
+
): {} extends Context['arguments']['props']
|
|
62
|
+
? () => Context['arguments'][Field]
|
|
63
|
+
: (
|
|
64
|
+
props: PropsWithChildren<Context['arguments']['props']>,
|
|
65
|
+
) => Context['arguments'][Field]
|
|
66
|
+
use(): {} extends Context['props']
|
|
67
|
+
? () => Context['arguments']
|
|
68
|
+
: (props: PropsWithChildren<Context['props']>) => Context['arguments']
|
|
69
|
+
stage<Field extends keyof Context['arguments'] & string>(
|
|
70
|
+
key: Field,
|
|
71
|
+
): (
|
|
72
|
+
args: Partial<GetArgsFor<Context, Field>>,
|
|
73
|
+
) => Context['arguments'][Field]
|
|
74
|
+
withDisplayName(displayName: string): ModularComponent<Context>
|
|
88
75
|
}
|
|
76
|
+
|
|
77
|
+
let customFunctions: Record<
|
|
78
|
+
string,
|
|
79
|
+
(...args: any[]) => (ctx?: ModularContext) => {
|
|
80
|
+
field: string
|
|
81
|
+
provide: (args: any) => any
|
|
82
|
+
}
|
|
83
|
+
> = {}
|
|
84
|
+
|
|
85
|
+
function InternalFactory<Context extends ModularContext>(
|
|
86
|
+
stages: { field: string; provide: (args: any) => any }[],
|
|
87
|
+
displayName: string | undefined,
|
|
88
|
+
): ModularComponent<Context> {
|
|
89
|
+
const UseComponent = function (props: Context['props'], ref: Context['ref']) {
|
|
89
90
|
return UseComponent.use('render')(props, ref)
|
|
90
91
|
}
|
|
91
92
|
UseComponent.displayName = displayName
|
|
92
93
|
|
|
93
|
-
UseComponent.with = (
|
|
94
|
+
UseComponent.with = (
|
|
95
|
+
_stage: (ctx?: Context) => { field: string; provide: (args: any) => any },
|
|
96
|
+
) => {
|
|
97
|
+
const stage = _stage()
|
|
94
98
|
const index = stages.findIndex((s) => s.field === stage.field)
|
|
95
99
|
|
|
96
100
|
if (index !== -1) {
|
|
97
101
|
const next = [...stages]
|
|
98
102
|
next[index] = stage
|
|
99
|
-
return InternalFactory<
|
|
103
|
+
return InternalFactory<Context>(next, displayName)
|
|
100
104
|
}
|
|
101
105
|
|
|
102
|
-
return InternalFactory<
|
|
106
|
+
return InternalFactory<Context>([...stages, stage], displayName)
|
|
103
107
|
}
|
|
104
108
|
UseComponent.force = UseComponent.with
|
|
105
109
|
|
|
106
|
-
UseComponent.use = (field
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
ref: React.ForwardedRef<Ref> = null,
|
|
111
|
-
) => {
|
|
112
|
-
const args: Record<string, any> = { props }
|
|
113
|
-
for (let stage of stages) {
|
|
114
|
-
args[stage.field] = stage.useStage(args as Args, ref)
|
|
115
|
-
}
|
|
116
|
-
return args
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const index = stages.findIndex((stage) => stage.field === field)
|
|
110
|
+
UseComponent.use = (field?: string) => {
|
|
111
|
+
const index = field
|
|
112
|
+
? stages.findIndex((stage) => stage.field === field)
|
|
113
|
+
: -1
|
|
121
114
|
const argStages =
|
|
122
115
|
index === -1 ? stages.slice(0) : stages.slice(0, index + 1)
|
|
123
116
|
|
|
124
|
-
return (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
return (props = {}, ref = null) => {
|
|
118
|
+
const args: Record<string, any> = { props, ref }
|
|
119
|
+
if (field === 'render') {
|
|
120
|
+
args.render = null
|
|
121
|
+
}
|
|
129
122
|
for (let stage of argStages) {
|
|
130
|
-
args[stage.field] = stage.
|
|
123
|
+
args[stage.field] = stage.provide(args)
|
|
131
124
|
}
|
|
132
|
-
return args[field
|
|
125
|
+
return field ? args[field] : args
|
|
133
126
|
}
|
|
134
127
|
}
|
|
135
128
|
|
|
136
|
-
UseComponent.stage = (field:
|
|
129
|
+
UseComponent.stage = (field: string) => {
|
|
137
130
|
const stage = stages.find((stage) => stage.field === field)
|
|
138
|
-
return stage?.
|
|
131
|
+
return stage?.provide ?? (() => null)
|
|
139
132
|
}
|
|
140
133
|
|
|
134
|
+
Object.entries(customFunctions).forEach(([name, fn]) => {
|
|
135
|
+
;(UseComponent as any)[`with${name[0].toUpperCase()}${name.slice(1)}`] = (
|
|
136
|
+
...args: any
|
|
137
|
+
) => UseComponent.with(fn(...args))
|
|
138
|
+
;(UseComponent as any)[
|
|
139
|
+
`force${name[0].toUpperCase()}${name.slice(1)}`
|
|
140
|
+
] = (...args: any) => UseComponent.with(fn(...args))
|
|
141
|
+
})
|
|
142
|
+
|
|
141
143
|
UseComponent.withDisplayName = (displayName: string) => {
|
|
142
|
-
return InternalFactory<
|
|
144
|
+
return InternalFactory<Context>([...stages], displayName)
|
|
143
145
|
}
|
|
144
146
|
|
|
145
|
-
return UseComponent as unknown as ModularComponent<
|
|
147
|
+
return UseComponent as unknown as ModularComponent<Context>
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
export function ModularComponent<Props extends {} = {}, Ref = never>(
|
|
149
151
|
displayName?: string,
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
152
|
+
) {
|
|
153
|
+
return InternalFactory<{
|
|
154
|
+
props: Props
|
|
155
|
+
ref: Ref
|
|
156
|
+
stages: {}
|
|
157
|
+
arguments: {
|
|
158
|
+
props: Props
|
|
159
|
+
ref: Ref
|
|
160
|
+
render: ReturnType<FunctionComponent>
|
|
161
|
+
}
|
|
162
|
+
constraints: {
|
|
163
|
+
props: Props
|
|
164
|
+
ref: Ref
|
|
165
|
+
render: ReturnType<FunctionComponent>
|
|
166
|
+
}
|
|
167
|
+
}>([], displayName)
|
|
160
168
|
}
|
|
161
169
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return {
|
|
175
|
-
field: 'render',
|
|
176
|
-
useStage: render,
|
|
177
|
-
}
|
|
170
|
+
ModularComponent.register = (
|
|
171
|
+
functions: Partial<
|
|
172
|
+
Record<
|
|
173
|
+
keyof MapToRaw<ModularComponentStages<any>>,
|
|
174
|
+
(...args: any[]) => (ctx?: ModularContext) => {
|
|
175
|
+
field: string
|
|
176
|
+
provide: (args: any) => any
|
|
177
|
+
}
|
|
178
|
+
>
|
|
179
|
+
>,
|
|
180
|
+
) => {
|
|
181
|
+
customFunctions = { ...customFunctions, ...functions }
|
|
178
182
|
}
|
|
183
|
+
|
|
184
|
+
export * from './render.js'
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FunctionComponent } from 'react'
|
|
2
|
+
import {
|
|
3
|
+
addTo,
|
|
4
|
+
wrap,
|
|
5
|
+
ModularContext,
|
|
6
|
+
GetValueGetterFor,
|
|
7
|
+
StageParams,
|
|
8
|
+
StageReturn,
|
|
9
|
+
} from './extend.js'
|
|
10
|
+
|
|
11
|
+
export function render<Context extends ModularContext>(
|
|
12
|
+
useRender: GetValueGetterFor<
|
|
13
|
+
Context,
|
|
14
|
+
'render',
|
|
15
|
+
ReturnType<FunctionComponent>
|
|
16
|
+
>,
|
|
17
|
+
) {
|
|
18
|
+
return addTo<Context>().on('render').provide(wrap(useRender))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type WithRender<Context extends ModularContext> = (
|
|
22
|
+
...args: StageParams<typeof render<Context>>
|
|
23
|
+
) => StageReturn<typeof render<Context>>
|