From a4f69c5c12e4bdf31c66994174d75fd82bc76674 Mon Sep 17 00:00:00 2001 From: Filipp Dernovoy Date: Wed, 16 Oct 2019 17:25:58 +0500 Subject: [PATCH] feat(di): fill registry with components via object literal --- packages/di/README.md | 18 ++++++++++++++++++ packages/di/di.tsx | 14 ++++++++++++++ packages/di/test/di.test.tsx | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) 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