Skip to content

Commit

Permalink
feat(di): fill registry with components via object literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipp Dernovoy authored and yarastqt committed Oct 23, 2019
1 parent b83ef61 commit a4f69c5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/di/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ registry.set('Header', Header)
registry.set('Footer', Footer)
```

or

```ts
registry.fill({
Header,
Footer,
})
```

or

```ts
registry.fill({
'id-1': Header,
'id-2': Footer,
})
```

3. Export the App version with its registry of components:

```ts
Expand Down
14 changes: 14 additions & 0 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ export class Registry {
return this
}

/**
* Set react components in registry via object literal.
*
* @param componentsSet set of valid react components
*/
fill(componentsSet: IRegistryComponents) {
this.components = {
...this.components,
...componentsSet,
}

return this
}

/**
* Get react component from registry by id.
*
Expand Down
17 changes: 17 additions & 0 deletions packages/di/test/di.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ describe('@bem-react/di', () => {
expect(registry.get('id-2')).to.eq(Component2)
})

it('should fill components via object literal', () => {
const registry = new Registry({ id: 'registry' })
const Component1 = () => null
const Component2 = () => <span />

registry.fill({
Component1,
Component2,
})

const snapshot: any = {}
snapshot['Component1'] = Component1
snapshot['Component2'] = Component2

expect(registry.snapshot()).to.eql(snapshot)
})

it('should return list of components', () => {
const registry = new Registry({ id: 'registry' })
const Component1 = () => null
Expand Down

0 comments on commit a4f69c5

Please sign in to comment.