Fixes wrapped widgets not being created on editor instanciation.

This commit is contained in:
Robin Tissot 2021-07-09 21:53:30 +02:00
parent a335f51b17
commit 8937019f2b
1 changed files with 23 additions and 17 deletions

View File

@ -1,25 +1,21 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
export default class WrappedWidget extends Component { export default class WrappedWidget extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.element = React.createRef(); this.element = React.createRef();
} }
componentWillReceiveProps(next) { updateWidget(props) {
if (this.element.current) {
if (this.props.annotation !== next.annotation) {
const widgetEl = this.props.widget({ const widgetEl = this.props.widget({
annotation: next.annotation, annotation: props.annotation,
readOnly: next.readOnly, readOnly: props.readOnly,
...this.props.config, ...this.props.config,
onAppendBody: body => next.onAppendBody(body), onAppendBody: body => props.onAppendBody(body),
onUpdateBody: (previous, updated) => next.onUpdateBody(previous, updated), onUpdateBody: (previous, updated) => props.onUpdateBody(previous, updated),
onRemoveBody: body => next.onRemoveBody(body), onRemoveBody: body => props.onRemoveBody(body),
onSaveAndClose: () => next.onSaveAndClose() onSaveAndClose: () => props.onSaveAndClose()
}); });
// Delete previous rendered state // Delete previous rendered state
@ -28,6 +24,16 @@ export default class WrappedWidget extends Component {
this.element.current.appendChild(widgetEl); this.element.current.appendChild(widgetEl);
} }
componentDidMount() {
this.updateWidget(this.props);
}
componentWillReceiveProps(next) {
if (this.element.current) {
if (this.props.annotation !== next.annotation) {
this.updateWidget(next);
}
} }
} }