Skip to content

Commit

Permalink
Add support for React 18
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Jan 7, 2024
1 parent 074398b commit 119dab6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"microbundle": "^0.15.0"
},
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react": "^17.0.2 || ^18",
"react-dom": "^17.0.2 || ^18",
"react-modal": "^3.15.1"
}
}
43 changes: 33 additions & 10 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,16 @@ export function FormInstance(config) {
this.onChange = this.onChange.bind(this);

this.render = function() {
if (ReactDOM.hasOwnProperty('createRoot'))
this._renderReact18();
else
this._renderReact17();
};

this._renderReact17 = function() {
try {
ReactDOM.render(
<FormContainer
schema={this.schema}
dataInputId={this.dataInputId}
data={this.data}
errorMap={this.errorMap}
fileHandler={this.fileHandler}
fileHandlerArgs={this.fileHandlerArgs}
onChange={this.onChange}
readonly={this.readonly}
/>,
this._getFormContainerComponent(),
document.getElementById(this.containerId)
);
} catch (error) {
Expand All @@ -72,6 +70,31 @@ export function FormInstance(config) {
}
};

this._renderReact18 = function() {
const root = ReactDOM.createRoot(document.getElementById(this.containerId));

try {
root.render(this._getFormContainerComponent());
} catch (error) {
root.render(<ErrorReporter error={error} />);
}
};

this._getFormContainerComponent = function() {
return (
<FormContainer
schema={this.schema}
dataInputId={this.dataInputId}
data={this.data}
errorMap={this.errorMap}
fileHandler={this.fileHandler}
fileHandlerArgs={this.fileHandlerArgs}
onChange={this.onChange}
readonly={this.readonly}
/>
);
};

this.update = function(config) {
this.schema = config.schema || this.schema;
this.data = config.data || this.data;
Expand Down

0 comments on commit 119dab6

Please sign in to comment.