Skip to content

feat(di): set components via object literal #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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