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,32 +1,38 @@
import React, { Component } from 'react';
export default class WrappedWidget extends Component {
constructor(props) {
super(props);
this.element = React.createRef();
}
updateWidget(props) {
const widgetEl = this.props.widget({
annotation: props.annotation,
readOnly: props.readOnly,
...this.props.config,
onAppendBody: body => props.onAppendBody(body),
onUpdateBody: (previous, updated) => props.onUpdateBody(previous, updated),
onRemoveBody: body => props.onRemoveBody(body),
onSaveAndClose: () => props.onSaveAndClose()
});
// Delete previous rendered state
while (this.element.current.firstChild)
this.element.current.removeChild(this.element.current.lastChild);
this.element.current.appendChild(widgetEl);
}
componentDidMount() {
this.updateWidget(this.props);
}
componentWillReceiveProps(next) {
if (this.element.current) {
if (this.props.annotation !== next.annotation) {
const widgetEl = this.props.widget({
annotation: next.annotation,
readOnly: next.readOnly,
...this.props.config,
onAppendBody: body => next.onAppendBody(body),
onUpdateBody: (previous, updated) => next.onUpdateBody(previous, updated),
onRemoveBody: body => next.onRemoveBody(body),
onSaveAndClose: () => next.onSaveAndClose()
});
// Delete previous rendered state
while (this.element.current.firstChild)
this.element.current.removeChild(this.element.current.lastChild);
this.element.current.appendChild(widgetEl);
this.updateWidget(next);
}
}
}