@modular-component/with-default-props 0.2.0 → 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 CHANGED
@@ -1,5 +1,13 @@
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
+
3
11
  ## 0.2.0
4
12
 
5
13
  ### Minor Changes
package/README.md CHANGED
@@ -1,42 +1,75 @@
1
- # `@modular-component/with-default-props`
1
+ # @modular-component/with-default-props
2
2
 
3
- Provides a `withDefaultProps` stage providing default values for props.
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
- Correctly updates typing of `props` argument to mark props with default
6
- values as non-nullable.
7
+ ## Usage
7
8
 
8
- ## Installation and usage
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
- ```bash
11
- yarn add @modular-component/core @modular-component/with-default-props
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 { modularFactory } from '@modular-component/core'
16
- import { WithDefaultProps } from '@modular-component/with-default-props'
46
+ import { ModularStage } from '@modular-component/core'
17
47
 
18
- const ModularComponent = modularFactory.extend(WithDefaultProps).build()
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
- const MyModularComponent = ModularComponent<{
21
- content?: {
22
- title: string
23
- subtitle: string
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.
@@ -1 +1 @@
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,CACb,OAAO,EACP,CAAC,IAAI,EAAE,IAAI,KAAK,KAAK,CAAC,KAAK,EAAE,YAAY,CAAC,CAC3C,CASA"}
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
@@ -2,7 +2,9 @@ export function defaultProps(defaultProps) {
2
2
  return {
3
3
  field: 'props',
4
4
  useStage: (args) => ({
5
- ...(typeof defaultProps === 'function' ? defaultProps(args) : defaultProps),
5
+ ...(typeof defaultProps === 'function'
6
+ ? defaultProps(args)
7
+ : defaultProps),
6
8
  ...args.props,
7
9
  }),
8
10
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,MAAM,UAAU,YAAY,CAK1B,YAA2D;IAK3D,OAAO;QACL,KAAK,EAAE,OAAO;QACd,QAAQ,EAAE,CAAC,IAAU,EAAE,EAAE,CACvB,CAAC;YACC,GAAG,CAAC,OAAO,YAAY,KAAK,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;YAC3E,GAAG,IAAI,CAAC,KAAK;SACiB,CAAA;KACnC,CAAA;AACH,CAAC"}
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.2.0",
11
+ "version": "0.2.1",
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.1"
30
30
  },
31
31
  "devDependencies": {
32
32
  "typescript": "^5.2.2"
package/src/index.ts CHANGED
@@ -14,15 +14,14 @@ export function defaultProps<
14
14
  DefaultProps extends Partial<Props>,
15
15
  >(
16
16
  defaultProps: DefaultProps | ((args: Args) => DefaultProps),
17
- ): ModularStage<
18
- 'props',
19
- (args: Args) => Merge<Props, DefaultProps>
20
- > {
17
+ ): ModularStage<'props', (args: Args) => Merge<Props, DefaultProps>> {
21
18
  return {
22
19
  field: 'props',
23
20
  useStage: (args: Args) =>
24
21
  ({
25
- ...(typeof defaultProps === 'function' ? defaultProps(args) : defaultProps),
22
+ ...(typeof defaultProps === 'function'
23
+ ? defaultProps(args)
24
+ : defaultProps),
26
25
  ...args.props,
27
26
  } as Merge<Props, DefaultProps>),
28
27
  }