Skip to content

Commit 44cc677

Browse files
committed
Added dependency injection handling.
1 parent 734019b commit 44cc677

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
"dependencies": {
1616
"@paperbits/common": "0.1.529",
17+
"inversify-react": "^1.0.2",
1718
"react": "^18.2.0",
1819
"react-dom": "^18.2.0"
1920
},

src/bindings/reactComponentBinder.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@
99
import * as React from "react";
1010
import * as ReactDOM from "react-dom";
1111
import { ComponentBinder } from "@paperbits/common/editing/componentBinder";
12+
import { IInjector } from "@paperbits/common/injection";
13+
import { ReactComponentWrapper } from "./reactComponentWrapper";
1214

1315

1416
export class ReactComponentBinder implements ComponentBinder {
17+
constructor(private readonly injector: IInjector) { }
18+
1519
public async bind<TInstance>(element: Element, componentDefinition: any, componentParams: unknown): Promise<TInstance> {
16-
const reactElement = React.createElement(componentDefinition, componentParams || {});
17-
const reactComponentInstance = ReactDOM.render(reactElement, element);
20+
const params = componentParams || {};
21+
const componentReference = React.createRef<TInstance>();
22+
params["ref"] = componentReference
23+
24+
const reactElement = React.createElement(ReactComponentWrapper, {
25+
inversifyContainer: this.injector["container"], // hack to access private property
26+
childComponents: React.createElement(componentDefinition, params)
27+
});
28+
29+
ReactDOM.render(reactElement, element);
1830

31+
const reactComponentInstance = componentReference.current;
1932
return reactComponentInstance;
2033
}
2134

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "reflect-metadata";
2+
import * as React from "react";
3+
import { Provider } from "inversify-react";
4+
5+
export function ReactComponentWrapper({ inversifyContainer, childComponents }) {
6+
return (
7+
<Provider container={inversifyContainer}>
8+
{childComponents}
9+
</Provider>
10+
);
11+
}

src/decorators/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*/
88

99
export * from "./runtimeComponent.decorator";
10-
export * from "./reactRuntimeComponentConfig";
10+
export * from "./reactRuntimeComponentConfig";
11+
export * from "./resolve";

src/decorators/resolve.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { resolve } from "inversify-react";
2+
export const Resolve = resolve;

0 commit comments

Comments
 (0)