({})
+const RegistriesConsumer = registryContext.Consumer
const RegistryProvider = registryContext.Provider
-export const RegistryConsumer = registryContext.Consumer
-
export function withRegistry(...registries: Registry[]): (Component: ComponentType
) => FC
export function withRegistry() {
// Use arguments instead of rest-arguments to get faster and more compact code.
@@ -25,7 +24,7 @@ export function withRegistry() {
const providedRegistriesRef = useRef(null)
return (
-
+
{(contextRegistries) => {
if (providedRegistriesRef.current === null) {
const providedRegistries = { ...contextRegistries }
@@ -53,7 +52,7 @@ export function withRegistry() {
)
}}
-
+
)
}
@@ -67,13 +66,13 @@ export function withRegistry() {
}
}
-export interface IComponentRegistryConsumerProps {
+export interface IRegistryConsumerProps {
id: string
children: (registry: any) => ReactNode
}
-export const ComponentRegistryConsumer: FC = (props) => (
-
+export const RegistryConsumer: FC = (props) => (
+
{(registries) => {
if (__DEV__) {
if (!registries[props.id]) {
@@ -83,9 +82,14 @@ export const ComponentRegistryConsumer: FC = (p
return props.children(registries[props.id].snapshot())
}}
-
+
)
+/**
+ * @deprecated consider using 'RegistryConsumer' instead
+ */
+export const ComponentRegistryConsumer = RegistryConsumer
+
export const useRegistries = () => {
return useContext(registryContext)
}
diff --git a/packages/di/test/di.test.tsx b/packages/di/test/di.test.tsx
index f81097fc..0d80ba74 100644
--- a/packages/di/test/di.test.tsx
+++ b/packages/di/test/di.test.tsx
@@ -1,14 +1,7 @@
import React from 'react'
import { render } from 'enzyme'
-import {
- Registry,
- withRegistry,
- RegistryConsumer,
- ComponentRegistryConsumer,
- useRegistries,
- useRegistry,
-} from '../di'
+import { Registry, withRegistry, RegistryConsumer, useRegistries, useRegistry } from '../di'
import { compose } from '../../core/core'
interface ICommonProps {
@@ -155,7 +148,7 @@ describe('@bem-react/di', () => {
})
describe('consumer', () => {
- test('should provide registry to context', () => {
+ test('should provide registry with component', () => {
const compositorRegistry = new Registry({ id: 'Compositor' })
const Element: React.FC = () => content
@@ -166,35 +159,9 @@ describe('@bem-react/di', () => {
compositorRegistry.set('Element', Element)
const CompositorPresenter: React.FC = () => (
-
- {(registries) => {
- const registry = registries.Compositor
- const { Element } = registry.snapshot()
-
- return
- }}
-
- )
-
- const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
-
- expect(render().text()).toEqual('content')
- })
-
- test('should provide assign registry with component', () => {
- const compositorRegistry = new Registry({ id: 'Compositor' })
- const Element: React.FC = () => content
-
- interface ICompositorRegistry {
- Element: React.ComponentType
- }
-
- compositorRegistry.set('Element', Element)
-
- const CompositorPresenter: React.FC = () => (
-
+
{({ Element }: ICompositorRegistry) => }
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -218,9 +185,9 @@ describe('@bem-react/di', () => {
const CompositorPresenter: React.FC = () => {
const Content: React.FC = withRegistry(overridedCompositorRegistry)(() => (
-
+
{({ Element }: ICompositorRegistry) => }
-
+
))
return
@@ -246,9 +213,9 @@ describe('@bem-react/di', () => {
overridedCompositorRegistry.set('Element', OverridedElement)
const CompositorPresenter: React.FC = () => (
-
+
{({ Element }: ICompositorRegistry) => }
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -276,14 +243,14 @@ describe('@bem-react/di', () => {
overridedCompositorRegistry.set('Element1', OverridedElement)
const CompositorPresenter: React.FC = () => (
-
+
{({ Element1, Element2 }: ICompositorRegistry) => (
<>
>
)}
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -325,14 +292,14 @@ describe('@bem-react/di', () => {
})
const CompositorPresenter: React.FC = () => (
-
+
{({ Element1, Element2 }: ICompositorRegistry) => (
<>
>
)}
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -364,13 +331,13 @@ describe('@bem-react/di', () => {
)
const CompositorPresenter: React.FC = () => (
-
+
{({ prop, functionProp }: ICompositorRegistry) => (
{prop} / {functionProp()}
)}
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -402,14 +369,14 @@ describe('@bem-react/di', () => {
}
const CompositorPresenter: React.FC = () => (
-
+
{({ Element1, Element2 }: ICompositorRegistry) => (
<>
>
)}
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -425,14 +392,14 @@ describe('@bem-react/di', () => {
const element2Registry = new Registry({ id: 'Element2' })
const Element1: React.FC = () => content
const Element2Presenter: React.FC = () => (
-
+
{({ Element }: ICompositorRegistry) => (
<>
extra
>
)}
-
+
)
const Element2 = withRegistry(element2Registry)(Element2Presenter)
@@ -445,9 +412,9 @@ describe('@bem-react/di', () => {
compositorRegistry.set('Element2', Element2)
const CompositorPresenter: React.FC = () => (
-
+
{({ Element2 }: ICompositorRegistry) => }
-
+
)
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter)
@@ -466,9 +433,7 @@ describe('@bem-react/di', () => {
registryB.set('Element', elementB)
const ElementPresenter: React.FC = () => (
-
- {({ Element }) => }
-
+ {({ Element }) => }
)
const BranchA = withRegistry(registryA)(ElementPresenter)
@@ -495,9 +460,7 @@ describe('@bem-react/di', () => {
registryA.set('ElementA', ElementA)
const ElementAPresenter: React.FC = () => (
-
- {({ ElementA }) => }
-
+ {({ ElementA }) => }
)
const ElementB = (_props: ICommonProps) => content of elementB
@@ -506,9 +469,7 @@ describe('@bem-react/di', () => {
registryB.set('ElementB', ElementB)
const ElementBPresenter: React.FC = () => (
-
- {({ ElementB }) => }
-
+ {({ ElementB }) => }
)
const AppA = withRegistry(registryA, registryB)(ElementAPresenter)