@modular-component/with-default-props 0.1.7 → 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 +19 -0
- package/README.md +64 -31
- package/dist/index.d.ts +8 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -9
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +24 -29
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @modular-component/with-default-props
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 09b9673: Update readmes
|
|
8
|
+
- Updated dependencies [09b9673]
|
|
9
|
+
- @modular-component/core@0.2.1
|
|
10
|
+
|
|
11
|
+
## 0.2.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- 83d453f: Rework API for better TypeScript performance and improved DX
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [83d453f]
|
|
20
|
+
- @modular-component/core@0.2.0
|
|
21
|
+
|
|
3
22
|
## 0.1.7
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,42 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @modular-component/with-default-props
|
|
2
2
|
|
|
3
|
-
Provides a `
|
|
3
|
+
Provides a `with(defaultProps)` stage allowing to set default value for props. Contrary to the standard React `defaultProps`
|
|
4
|
+
field, the `with(defaultProps)` stage can also _set new props_ that are not surfaced by the component, and react to passed
|
|
5
|
+
props (or other previous stages) to dynamically compute a default value.
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
values as non-nullable.
|
|
7
|
+
## Usage
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
```tsx
|
|
10
|
+
import { ModularComponent } from '@modular-component/core'
|
|
11
|
+
import { defaultProps } from '@modular-component/with-default-props'
|
|
12
|
+
|
|
13
|
+
const MyComponent = ModularComponent<{ someFlag?: boolean }>()
|
|
14
|
+
.with(defaultProps({
|
|
15
|
+
someFlag: false,
|
|
16
|
+
someNewProp: 'hello world'
|
|
17
|
+
}))
|
|
18
|
+
.with(render(({ props }) => (
|
|
19
|
+
// props is inferred as { someFlag: boolean; someNewProp: string } at this point
|
|
20
|
+
)))
|
|
9
21
|
|
|
10
|
-
|
|
11
|
-
|
|
22
|
+
const MyDynamicProps = ModularComponent<{
|
|
23
|
+
role: 'user' | 'owner' | 'admin',
|
|
24
|
+
canEdit?: boolean,
|
|
25
|
+
canDelete?: boolean
|
|
26
|
+
}>()
|
|
27
|
+
.with(defaultProps(({ props }) => ({
|
|
28
|
+
canEdit: ['owner', 'admin'].includes(props.role),
|
|
29
|
+
canDelete: ['owner'].includes(props.role)
|
|
30
|
+
}))
|
|
31
|
+
.with(render(({ props }) => {
|
|
32
|
+
// props is inferred as { role: 'user' | 'owner' | 'admin'; canEdit: boolean; canDelete: boolean }
|
|
33
|
+
// canEdit defaults to true if the role is not "user", false otherwise
|
|
34
|
+
// canDelete defaults to true if the role is "admin", false otherwise
|
|
35
|
+
// canEdit and canDelete can still be controlled by explicitely setting the property
|
|
36
|
+
})))
|
|
12
37
|
```
|
|
13
38
|
|
|
39
|
+
## Implementation
|
|
40
|
+
|
|
41
|
+
`with(defaultProps)` runs a custom stage hook to shallowly merge the default props to the received component props.
|
|
42
|
+
Accepted values are restricted to a partial map of the original props to only accept correct types for defined props.
|
|
43
|
+
The value can also be a function of the current args.
|
|
44
|
+
|
|
14
45
|
```tsx
|
|
15
|
-
import {
|
|
16
|
-
import { WithDefaultProps } from '@modular-component/with-default-props'
|
|
46
|
+
import { ModularStage } from '@modular-component/core'
|
|
17
47
|
|
|
18
|
-
|
|
48
|
+
type Merge<Props, DefaultProps extends Partial<Props>> = {
|
|
49
|
+
[key in keyof Props | keyof DefaultProps]-?: key extends keyof Props
|
|
50
|
+
? key extends keyof DefaultProps
|
|
51
|
+
? NonNullable<Props[key]>
|
|
52
|
+
: Props[key]
|
|
53
|
+
: DefaultProps[key]
|
|
54
|
+
}
|
|
19
55
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
56
|
+
export function defaultProps<
|
|
57
|
+
Args extends { props: {} },
|
|
58
|
+
Props extends Args extends { props: infer U } ? U : {},
|
|
59
|
+
DefaultProps extends Partial<Props>,
|
|
60
|
+
>(
|
|
61
|
+
defaultProps: DefaultProps | ((args: Args) => DefaultProps),
|
|
62
|
+
): ModularStage<
|
|
63
|
+
'props',
|
|
64
|
+
(args: Args) => Merge<Props, DefaultProps>
|
|
65
|
+
> {
|
|
66
|
+
return {
|
|
67
|
+
field: 'props',
|
|
68
|
+
useStage: (args: Args) =>
|
|
69
|
+
({
|
|
70
|
+
...(typeof defaultProps === 'function' ? defaultProps(args) : defaultProps),
|
|
71
|
+
...args.props,
|
|
72
|
+
} as Merge<Props, DefaultProps>),
|
|
24
73
|
}
|
|
25
|
-
}
|
|
26
|
-
.withDefaultProps({
|
|
27
|
-
content: {
|
|
28
|
-
title: 'Default title',
|
|
29
|
-
subtitle: 'Default subtitle',
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
.withRender(({ props }) => (
|
|
33
|
-
<>
|
|
34
|
-
<h1>{props.content.title}</h1>
|
|
35
|
-
<h2>{props.content.subtitle}</h2>
|
|
36
|
-
</>
|
|
37
|
-
))
|
|
74
|
+
}
|
|
38
75
|
```
|
|
39
|
-
|
|
40
|
-
## Learn more
|
|
41
|
-
|
|
42
|
-
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
CHANGED
|
@@ -1,28 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
[withDefaultProps]: {
|
|
5
|
-
restrict: Partial<Args extends {
|
|
6
|
-
props: infer P;
|
|
7
|
-
} ? P : {}> | ((args: Args) => Partial<Args extends {
|
|
8
|
-
props: infer P;
|
|
9
|
-
} ? P : {}>);
|
|
10
|
-
transform: Value extends ((args: Args) => infer T) | infer T ? Args extends {
|
|
11
|
-
props: infer P;
|
|
12
|
-
} ? {
|
|
13
|
-
[key in keyof T]: key extends keyof P ? NonNullable<P[key]> : T[key];
|
|
14
|
-
} : Value : never;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export declare const WithDefaultProps: {
|
|
19
|
-
readonly DefaultProps: {
|
|
20
|
-
readonly symbol: typeof withDefaultProps;
|
|
21
|
-
readonly field: "props";
|
|
22
|
-
readonly transform: <A extends {
|
|
23
|
-
props: {};
|
|
24
|
-
}, P>(args: A, useProps: P) => any;
|
|
25
|
-
};
|
|
1
|
+
import { ModularStage } from '@modular-component/core';
|
|
2
|
+
type Merge<Props, DefaultProps extends Partial<Props>> = {
|
|
3
|
+
[key in keyof Props | keyof DefaultProps]-?: key extends keyof Props ? key extends keyof DefaultProps ? NonNullable<Props[key]> : Props[key] : DefaultProps[key];
|
|
26
4
|
};
|
|
5
|
+
export declare function defaultProps<Args extends {
|
|
6
|
+
props: {};
|
|
7
|
+
}, Props extends Args extends {
|
|
8
|
+
props: infer U;
|
|
9
|
+
} ? U : {}, DefaultProps extends Partial<Props>>(defaultProps: DefaultProps | ((args: Args) => DefaultProps)): ModularStage<'props', (args: Args) => Merge<Props, DefaultProps>>;
|
|
27
10
|
export {};
|
|
28
11
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,KAAK,KAAK,CAAC,KAAK,EAAE,YAAY,SAAS,OAAO,CAAC,KAAK,CAAC,IAAI;KACtD,GAAG,IAAI,MAAM,KAAK,GAAG,MAAM,YAAY,CAAC,CAAC,GAAG,GAAG,SAAS,MAAM,KAAK,GAChE,GAAG,SAAS,MAAM,YAAY,GAC5B,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GACvB,KAAK,CAAC,GAAG,CAAC,GACZ,YAAY,CAAC,GAAG,CAAC;CACtB,CAAA;AAED,wBAAgB,YAAY,CAC1B,IAAI,SAAS;IAAE,KAAK,EAAE,EAAE,CAAA;CAAE,EAC1B,KAAK,SAAS,IAAI,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC,CAAA;CAAE,GAAG,CAAC,GAAG,EAAE,EACtD,YAAY,SAAS,OAAO,CAAC,KAAK,CAAC,EAEnC,YAAY,EAAE,YAAY,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,GAC1D,YAAY,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,KAAK,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAWnE"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export const WithDefaultProps = createMethodRecord({
|
|
4
|
-
DefaultProps: {
|
|
5
|
-
symbol: withDefaultProps,
|
|
1
|
+
export function defaultProps(defaultProps) {
|
|
2
|
+
return {
|
|
6
3
|
field: 'props',
|
|
7
|
-
|
|
8
|
-
...(typeof
|
|
4
|
+
useStage: (args) => ({
|
|
5
|
+
...(typeof defaultProps === 'function'
|
|
6
|
+
? defaultProps(args)
|
|
7
|
+
: defaultProps),
|
|
9
8
|
...args.props,
|
|
10
9
|
}),
|
|
11
|
-
}
|
|
12
|
-
}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
13
12
|
//# 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":"AAUA,MAAM,UAAU,YAAY,CAK1B,YAA2D;IAE3D,OAAO;QACL,KAAK,EAAE,OAAO;QACd,QAAQ,EAAE,CAAC,IAAU,EAAE,EAAE,CACvB,CAAC;YACC,GAAG,CAAC,OAAO,YAAY,KAAK,UAAU;gBACpC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC;gBACpB,CAAC,CAAC,YAAY,CAAC;YACjB,GAAG,IAAI,CAAC,KAAK;SACiB,CAAA;KACnC,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"Default",
|
|
9
9
|
"Conditional"
|
|
10
10
|
],
|
|
11
|
-
"version": "0.1
|
|
11
|
+
"version": "0.2.1",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"publishConfig": {
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"license": "cp ../../LICENSE ./LICENSE"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@modular-component/core": "0.1
|
|
29
|
+
"@modular-component/core": "0.2.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"typescript": "^
|
|
32
|
+
"typescript": "^5.2.2"
|
|
33
33
|
},
|
|
34
34
|
"main": "dist/index.js",
|
|
35
35
|
"types": "dist/index.d.ts"
|
package/src/index.ts
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ModularStage } from '@modular-component/core'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
| Partial<Args extends { props: infer P } ? P : {}>
|
|
10
|
-
| ((args: Args) => Partial<Args extends { props: infer P } ? P : {}>)
|
|
11
|
-
transform: Value extends ((args: Args) => infer T) | infer T
|
|
12
|
-
? Args extends { props: infer P }
|
|
13
|
-
? {
|
|
14
|
-
[key in keyof T]: key extends keyof P
|
|
15
|
-
? NonNullable<P[key]>
|
|
16
|
-
: T[key]
|
|
17
|
-
}
|
|
18
|
-
: Value
|
|
19
|
-
: never
|
|
20
|
-
}
|
|
21
|
-
}
|
|
3
|
+
type Merge<Props, DefaultProps extends Partial<Props>> = {
|
|
4
|
+
[key in keyof Props | keyof DefaultProps]-?: key extends keyof Props
|
|
5
|
+
? key extends keyof DefaultProps
|
|
6
|
+
? NonNullable<Props[key]>
|
|
7
|
+
: Props[key]
|
|
8
|
+
: DefaultProps[key]
|
|
22
9
|
}
|
|
23
10
|
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
export function defaultProps<
|
|
12
|
+
Args extends { props: {} },
|
|
13
|
+
Props extends Args extends { props: infer U } ? U : {},
|
|
14
|
+
DefaultProps extends Partial<Props>,
|
|
15
|
+
>(
|
|
16
|
+
defaultProps: DefaultProps | ((args: Args) => DefaultProps),
|
|
17
|
+
): ModularStage<'props', (args: Args) => Merge<Props, DefaultProps>> {
|
|
18
|
+
return {
|
|
27
19
|
field: 'props',
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
useStage: (args: Args) =>
|
|
21
|
+
({
|
|
22
|
+
...(typeof defaultProps === 'function'
|
|
23
|
+
? defaultProps(args)
|
|
24
|
+
: defaultProps),
|
|
25
|
+
...args.props,
|
|
26
|
+
} as Merge<Props, DefaultProps>),
|
|
27
|
+
}
|
|
28
|
+
}
|