From 8937019f2b397b1c26c750a2c5f60a3fd807e20f Mon Sep 17 00:00:00 2001 From: Robin Tissot Date: Fri, 9 Jul 2021 21:53:30 +0200 Subject: [PATCH] Fixes wrapped widgets not being created on editor instanciation. --- src/editor/widgets/WrappedWidget.jsx | 40 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/editor/widgets/WrappedWidget.jsx b/src/editor/widgets/WrappedWidget.jsx index 4c1a184..9b9ac4e 100644 --- a/src/editor/widgets/WrappedWidget.jsx +++ b/src/editor/widgets/WrappedWidget.jsx @@ -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); } } }