@modular-component/with-conditional-render 0.1.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 +12 -0
- package/LICENSE +13 -0
- package/README.md +50 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
- package/src/index.ts +61 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright © Jérémie van der Sande
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
4
|
+
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
|
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
6
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the
|
|
11
|
+
warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or
|
|
12
|
+
copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise,
|
|
13
|
+
arising from, out of or in connection with the software or the use or other dealings in the Software.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# `@modular-component/with-conditional-render`
|
|
2
|
+
|
|
3
|
+
Provides three stages that allow conditional rendering in `ModularComponent`s:
|
|
4
|
+
|
|
5
|
+
- `withCondition` will set a `condition` argument to either `true` or `false`, based
|
|
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`.
|
|
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`.
|
|
16
|
+
|
|
17
|
+
## Installation and usage
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add @modular-component/core @modular-component/with-conditional-render
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```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
|
+
```
|
|
47
|
+
|
|
48
|
+
## Learn more
|
|
49
|
+
|
|
50
|
+
Read the [`ModularComponent` ReadMe](https://github.com/jvdsande/modular-component/blob/master/README.md) for more information about the `ModularComponent` system.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { FunctionComponent } from 'react';
|
|
2
|
+
export declare const WithConditionalRender: {
|
|
3
|
+
readonly withCondition: {
|
|
4
|
+
readonly field: "condition";
|
|
5
|
+
readonly multiple: true;
|
|
6
|
+
readonly transform: <A extends {
|
|
7
|
+
condition?: boolean | undefined;
|
|
8
|
+
}, C extends (args: A) => boolean>(args: A, useCondition: C) => boolean;
|
|
9
|
+
readonly restrict: boolean;
|
|
10
|
+
};
|
|
11
|
+
readonly withConditionalFallback: {
|
|
12
|
+
readonly field: "render";
|
|
13
|
+
readonly multiple: true;
|
|
14
|
+
readonly transform: <A_1 extends {
|
|
15
|
+
condition?: boolean | undefined;
|
|
16
|
+
render?: import("react").ReactElement<any, any> | null | undefined;
|
|
17
|
+
}, P extends FunctionComponent<A_1>>(args: A_1, useRender: P) => import("react").ReactElement<any, any> | null | undefined;
|
|
18
|
+
readonly restrict: import("react").ReactElement<any, any> | null;
|
|
19
|
+
};
|
|
20
|
+
readonly withConditionalRender: {
|
|
21
|
+
readonly field: "render";
|
|
22
|
+
readonly transform: <A_2 extends {
|
|
23
|
+
condition?: boolean | undefined;
|
|
24
|
+
render?: import("react").ReactElement<any, any> | null | undefined;
|
|
25
|
+
}, P_1 extends FunctionComponent<A_2>>(args: A_2, useRender: P_1) => import("react").ReactElement<any, any> | null | undefined;
|
|
26
|
+
readonly restrict: import("react").ReactElement<any, any> | null;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
declare module '@modular-component/core' {
|
|
30
|
+
interface ModularStageTransform<T> {
|
|
31
|
+
withConditionalFallback: T | null;
|
|
32
|
+
withConditionalRender: T | null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AAEzC,eAAO,MAAM,qBAAqB;;;;;;kCAML,OAAO;;;;;;;;;;;;;;;;;;;;CA4CzB,CAAA;AAEX,OAAO,QAAQ,yBAAyB,CAAC;IACvC,UAAiB,qBAAqB,CAAC,CAAC;QACtC,uBAAuB,EAAE,CAAC,GAAG,IAAI,CAAA;QACjC,qBAAqB,EAAE,CAAC,GAAG,IAAI,CAAA;KAChC;CACF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createMethodRecord } from '@modular-component/core';
|
|
2
|
+
export const WithConditionalRender = createMethodRecord({
|
|
3
|
+
withCondition: {
|
|
4
|
+
field: 'condition',
|
|
5
|
+
multiple: true,
|
|
6
|
+
transform: (args, useCondition) => args.condition !== false &&
|
|
7
|
+
(typeof useCondition === 'function' ? useCondition(args) : useCondition),
|
|
8
|
+
restrict: {},
|
|
9
|
+
},
|
|
10
|
+
withConditionalFallback: {
|
|
11
|
+
field: 'render',
|
|
12
|
+
multiple: true,
|
|
13
|
+
transform: (args, useRender) => {
|
|
14
|
+
if (args.condition !== false || args.render) {
|
|
15
|
+
return args.render;
|
|
16
|
+
}
|
|
17
|
+
return typeof useRender === 'function' ? useRender(args) : useRender;
|
|
18
|
+
},
|
|
19
|
+
restrict: {},
|
|
20
|
+
},
|
|
21
|
+
withConditionalRender: {
|
|
22
|
+
field: 'render',
|
|
23
|
+
transform: (args, useRender) => {
|
|
24
|
+
if (args.condition === false) {
|
|
25
|
+
return args.render;
|
|
26
|
+
}
|
|
27
|
+
return typeof useRender === 'function' ? useRender(args) : useRender;
|
|
28
|
+
},
|
|
29
|
+
restrict: {},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAG5D,MAAM,CAAC,MAAM,qBAAqB,GAAG,kBAAkB,CAAC;IACtD,aAAa,EAAE;QACb,KAAK,EAAE,WAAW;QAClB,QAAQ,EAAE,IAAI;QACd,SAAS,EAAE,CAIT,IAAO,EACP,YAAe,EACf,EAAE,CACF,IAAI,CAAC,SAAS,KAAK,KAAK;YACxB,CAAC,OAAO,YAAY,KAAK,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QAC1E,QAAQ,EAAE,EAAa;KACxB;IACD,uBAAuB,EAAE;QACvB,KAAK,EAAE,QAAQ;QACf,QAAQ,EAAE,IAAI;QACd,SAAS,EAAE,CAIT,IAAO,EACP,SAAY,EACZ,EAAE;YACF,IAAI,IAAI,CAAC,SAAS,KAAK,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE;gBAC3C,OAAO,IAAI,CAAC,MAAM,CAAA;aACnB;YAED,OAAO,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACtE,CAAC;QACD,QAAQ,EAAE,EAAmC;KAC9C;IACD,qBAAqB,EAAE;QACrB,KAAK,EAAE,QAAQ;QACf,SAAS,EAAE,CAIT,IAAO,EACP,SAAY,EACZ,EAAE;YACF,IAAI,IAAI,CAAC,SAAS,KAAK,KAAK,EAAE;gBAC5B,OAAO,IAAI,CAAC,MAAM,CAAA;aACnB;YAED,OAAO,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACtE,CAAC;QACD,QAAQ,EAAE,EAAmC;KAC9C;CACO,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@modular-component/with-conditional-render",
|
|
3
|
+
"description": "ModularComponent stage for handling conditional rendering and fallbacks",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"React",
|
|
6
|
+
"Modular",
|
|
7
|
+
"Factory",
|
|
8
|
+
"Default",
|
|
9
|
+
"Conditional"
|
|
10
|
+
],
|
|
11
|
+
"version": "0.1.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"dist",
|
|
20
|
+
"CHANGELOG.md"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "yarn build:core && yarn build:self",
|
|
24
|
+
"build:core": "yarn workspace @modular-component/core build",
|
|
25
|
+
"build:self": "tsc -p tsconfig.build.json",
|
|
26
|
+
"license": "cp ../../LICENSE ./LICENSE"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@modular-component/core": "0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=17 <19"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/react": "^18.0.17",
|
|
36
|
+
"typescript": "^4.6.4"
|
|
37
|
+
},
|
|
38
|
+
"main": "dist/index.js",
|
|
39
|
+
"types": "dist/index.d.ts"
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createMethodRecord } from '@modular-component/core'
|
|
2
|
+
import { FunctionComponent } from 'react'
|
|
3
|
+
|
|
4
|
+
export const WithConditionalRender = createMethodRecord({
|
|
5
|
+
withCondition: {
|
|
6
|
+
field: 'condition',
|
|
7
|
+
multiple: true,
|
|
8
|
+
transform: <
|
|
9
|
+
A extends { condition?: boolean },
|
|
10
|
+
C extends (args: A) => boolean,
|
|
11
|
+
>(
|
|
12
|
+
args: A,
|
|
13
|
+
useCondition: C,
|
|
14
|
+
) =>
|
|
15
|
+
args.condition !== false &&
|
|
16
|
+
(typeof useCondition === 'function' ? useCondition(args) : useCondition),
|
|
17
|
+
restrict: {} as boolean,
|
|
18
|
+
},
|
|
19
|
+
withConditionalFallback: {
|
|
20
|
+
field: 'render',
|
|
21
|
+
multiple: true,
|
|
22
|
+
transform: <
|
|
23
|
+
A extends { condition?: boolean; render?: ReturnType<FunctionComponent> },
|
|
24
|
+
P extends FunctionComponent<A>,
|
|
25
|
+
>(
|
|
26
|
+
args: A,
|
|
27
|
+
useRender: P,
|
|
28
|
+
) => {
|
|
29
|
+
if (args.condition !== false || args.render) {
|
|
30
|
+
return args.render
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return typeof useRender === 'function' ? useRender(args) : useRender
|
|
34
|
+
},
|
|
35
|
+
restrict: {} as ReturnType<FunctionComponent>,
|
|
36
|
+
},
|
|
37
|
+
withConditionalRender: {
|
|
38
|
+
field: 'render',
|
|
39
|
+
transform: <
|
|
40
|
+
A extends { condition?: boolean; render?: ReturnType<FunctionComponent> },
|
|
41
|
+
P extends FunctionComponent<A>,
|
|
42
|
+
>(
|
|
43
|
+
args: A,
|
|
44
|
+
useRender: P,
|
|
45
|
+
) => {
|
|
46
|
+
if (args.condition === false) {
|
|
47
|
+
return args.render
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return typeof useRender === 'function' ? useRender(args) : useRender
|
|
51
|
+
},
|
|
52
|
+
restrict: {} as ReturnType<FunctionComponent>,
|
|
53
|
+
},
|
|
54
|
+
} as const)
|
|
55
|
+
|
|
56
|
+
declare module '@modular-component/core' {
|
|
57
|
+
export interface ModularStageTransform<T> {
|
|
58
|
+
withConditionalFallback: T | null
|
|
59
|
+
withConditionalRender: T | null
|
|
60
|
+
}
|
|
61
|
+
}
|