@modular-component/with-components 0.2.0 → 0.2.2

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 +16 -0
  2. package/README.md +55 -34
  3. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @modular-component/with-components
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 77c46f4: Fix an issue in core rendering system
8
+ - Updated dependencies [77c46f4]
9
+ - @modular-component/core@0.2.2
10
+
11
+ ## 0.2.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 09b9673: Update readmes
16
+ - Updated dependencies [09b9673]
17
+ - @modular-component/core@0.2.1
18
+
3
19
  ## 0.2.0
4
20
 
5
21
  ### Minor Changes
package/README.md CHANGED
@@ -1,48 +1,69 @@
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 }) => <components.SomeComponent />))
31
19
  ```
32
20
 
33
- ```tsx
34
- // Usage in tests
21
+ ## Replacing sub-components
35
22
 
36
- const SubComponentMock = someMock()
37
- const TestMyModularComponent = MyModularComponent.withComponents({
38
- SubComponent: SubComponentMock,
39
- })
23
+ Replacing sub-components can be done either by updating or mocking the stage.
24
+ It allows creating a clone of the component with a different sub-component implementation,
25
+ either for tests or for content.
26
+ For instance, one could imagine a `Layout` base component taking advantage of this functionality:
40
27
 
41
- render(<TestMyModularComponent />)
28
+ ```tsx
29
+ const PageLayout = ModularComponent()
30
+ .with(components({
31
+ Title: React.Fragment,
32
+ Subtitle: React.Fragment,
33
+ Content: React.Fragment,
34
+ Footer: React.Fragment
35
+ }))
36
+ .with(render(({ components }) => (
37
+ // Build a layout using <components.Title />, <components.Subtitle />...
38
+ )))
42
39
 
43
- expect(someMock).toHaveBeenCalledOnce()
40
+ const PageOne = PageLayout.with(components({
41
+ Title: () => <>First page</>,
42
+ Subtitle: () => <>I have a subtitle but no footer</>,
43
+ Content: () => <>First page content</>,
44
+ Footer: React.Fragment
45
+ }))
46
+
47
+ const PageTwo = PageLayout.with(components({
48
+ Title: () => <>Second page</>,
49
+ Subtitle: React.Fragment,
50
+ Content: () => <>Second page content</>,
51
+ Footer: () => <>I have a footer but no subtitle</>
52
+ }))
44
53
  ```
45
54
 
46
- ## Learn more
55
+ ## Implementation
56
+
57
+ `with(components)` is a simple stage adding the provided record as a `components` argument. It has a restriction
58
+ on accepted values, to only accept a record of React components.
47
59
 
48
- Read the [`ModularComponent` ReadMe](https://github.com/jvdsande/modular-component/blob/master/README.md) for more information about the `ModularComponent` system.
60
+ ```tsx
61
+ import { ComponentType } from 'react'
62
+ import { ModularStage } from '@modular-component/core'
63
+
64
+ export function components<Components extends Record<string, ComponentType>>(
65
+ components: Components,
66
+ ): ModularStage<'components', () => Components> {
67
+ return { field: 'components', useStage: () => components }
68
+ }
69
+ ```
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.2",
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.2"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": ">=17 <19"