Skip to content

Commit a4f69c5

Browse files
Filipp Dernovoyyarastqt
authored andcommitted
feat(di): fill registry with components via object literal
1 parent b83ef61 commit a4f69c5

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

packages/di/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ registry.set('Header', Header)
4848
registry.set('Footer', Footer)
4949
```
5050

51+
or
52+
53+
```ts
54+
registry.fill({
55+
Header,
56+
Footer,
57+
})
58+
```
59+
60+
or
61+
62+
```ts
63+
registry.fill({
64+
'id-1': Header,
65+
'id-2': Footer,
66+
})
67+
```
68+
5169
3. Export the App version with its registry of components:
5270

5371
```ts

packages/di/di.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ export class Registry {
103103
return this
104104
}
105105

106+
/**
107+
* Set react components in registry via object literal.
108+
*
109+
* @param componentsSet set of valid react components
110+
*/
111+
fill(componentsSet: IRegistryComponents) {
112+
this.components = {
113+
...this.components,
114+
...componentsSet,
115+
}
116+
117+
return this
118+
}
119+
106120
/**
107121
* Get react component from registry by id.
108122
*

packages/di/test/di.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ describe('@bem-react/di', () => {
3131
expect(registry.get('id-2')).to.eq(Component2)
3232
})
3333

34+
it('should fill components via object literal', () => {
35+
const registry = new Registry({ id: 'registry' })
36+
const Component1 = () => null
37+
const Component2 = () => <span />
38+
39+
registry.fill({
40+
Component1,
41+
Component2,
42+
})
43+
44+
const snapshot: any = {}
45+
snapshot['Component1'] = Component1
46+
snapshot['Component2'] = Component2
47+
48+
expect(registry.snapshot()).to.eql(snapshot)
49+
})
50+
3451
it('should return list of components', () => {
3552
const registry = new Registry({ id: 'registry' })
3653
const Component1 = () => null

0 commit comments

Comments
 (0)