@modular-component/with-components 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.
Files changed (3) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +57 -34
  3. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @modular-component/with-components
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,48 +1,71 @@
1
- # `@modular-component/with-components`
1
+ # @modular-component/with-components
2
2
 
3
- Provides a `withComponents` stage that fills the `components` argument with
3
+ Provides a `with(components)` stage that fills the `components` argument with
4
4
  a map of React components. Useful when running tests in an environment that
5
5
  does not allow module mocking: sub-components can be stubbed in tests by
6
- calling the stage again to replace their implementations.
6
+ mocking the stage to replace their implementations.
7
7
 
8
- ## Installation and usage
9
-
10
- ```bash
11
- yarn add @modular-component/core @modular-component/with-components
12
- ```
8
+ ## Usage
13
9
 
14
10
  ```tsx
15
- // Usage in apps
16
- import { modularFactory } from '@modular-component/core'
17
- import { WithComponents } from '@modular-component/with-components'
18
-
19
- const ModularComponent = modularFactory.extend(WithComponents).build()
20
-
21
- const MyModularComponent = ModularComponent()
22
- .withComponents({
23
- SubComponent: () => <h2>Subtitle</h2>,
24
- })
25
- .withRender(({ components }) => (
26
- <>
27
- <h1>Main Title</h1>
28
- <components.SubComponent />
29
- </>
30
- ))
11
+ import { ModularComponent } from '@modular-component/core'
12
+ import { components } from '@modular-component/with-components'
13
+
14
+ import { SomeComponent } from 'some-component'
15
+
16
+ const MyComponent = ModularComponent()
17
+ .with(components({ SomeComponent }))
18
+ .with(render(({ props, components }) => (
19
+ <components.SomeComponent />
20
+ )))
31
21
  ```
32
22
 
33
- ```tsx
34
- // Usage in tests
23
+ ## Replacing sub-components
35
24
 
36
- const SubComponentMock = someMock()
37
- const TestMyModularComponent = MyModularComponent.withComponents({
38
- SubComponent: SubComponentMock,
39
- })
25
+ Replacing sub-components can be done either by updating or mocking the stage.
26
+ It allows creating a clone of the component with a different sub-component implementation,
27
+ either for tests or for content.
28
+ For instance, one could imagine a `Layout` base component taking advantage of this functionality:
40
29
 
41
- render(<TestMyModularComponent />)
30
+ ```tsx
31
+ const PageLayout = ModularComponent()
32
+ .with(components({
33
+ Title: React.Fragment,
34
+ Subtitle: React.Fragment,
35
+ Content: React.Fragment,
36
+ Footer: React.Fragment
37
+ }))
38
+ .with(render(({ components }) => (
39
+ // Build a layout using <components.Title />, <components.Subtitle />...
40
+ )))
42
41
 
43
- expect(someMock).toHaveBeenCalledOnce()
42
+ const PageOne = PageLayout.with(components({
43
+ Title: () => <>First page</>,
44
+ Subtitle: () => <>I have a subtitle but no footer</>,
45
+ Content: () => <>First page content</>,
46
+ Footer: React.Fragment
47
+ }))
48
+
49
+ const PageTwo = PageLayout.with(components({
50
+ Title: () => <>Second page</>,
51
+ Subtitle: React.Fragment,
52
+ Content: () => <>Second page content</>,
53
+ Footer: () => <>I have a footer but no subtitle</>
54
+ }))
44
55
  ```
45
56
 
46
- ## Learn more
57
+ ## Implementation
58
+
59
+ `with(components)` is a simple stage adding the provided record as a `components` argument. It has a restriction
60
+ on accepted values, to only accept a record of React components.
47
61
 
48
- Read the [`ModularComponent` ReadMe](https://github.com/jvdsande/modular-component/blob/master/README.md) for more information about the `ModularComponent` system.
62
+ ```tsx
63
+ import { ComponentType } from 'react'
64
+ import { ModularStage } from '@modular-component/core'
65
+
66
+ export function components<Components extends Record<string, ComponentType>>(
67
+ components: Components,
68
+ ): ModularStage<'components', () => Components> {
69
+ return { field: 'components', useStage: () => components }
70
+ }
71
+ ```
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "Sub-components",
10
10
  "Injection"
11
11
  ],
12
- "version": "0.2.0",
12
+ "version": "0.2.1",
13
13
  "type": "module",
14
14
  "license": "MIT",
15
15
  "publishConfig": {
@@ -27,7 +27,7 @@
27
27
  "license": "cp ../../LICENSE ./LICENSE"
28
28
  },
29
29
  "dependencies": {
30
- "@modular-component/core": "0.2.0"
30
+ "@modular-component/core": "0.2.1"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": ">=17 <19"