diff --git a/packages/di/README.md b/packages/di/README.md index 5083b9ee..48d83146 100644 --- a/packages/di/README.md +++ b/packages/di/README.md @@ -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 diff --git a/packages/di/di.tsx b/packages/di/di.tsx index 30ab060d..3748f25f 100644 --- a/packages/di/di.tsx +++ b/packages/di/di.tsx @@ -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. * diff --git a/packages/di/test/di.test.tsx b/packages/di/test/di.test.tsx index 43e97600..0bf750de 100644 --- a/packages/di/test/di.test.tsx +++ b/packages/di/test/di.test.tsx @@ -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 = () => + + 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