Merge pull request #70 from lauxley/fix/editor-creation-wrapped-widget
Fixes wrapped widgets not being created on editor instanciation.
This commit is contained in:
commit
b29bb21d34
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue