Skip to content

Commit c43590b

Browse files
committed
fix: update element state on element prop changed
Closes #1131
1 parent d1a0be2 commit c43590b

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

src/render/BpmnPropertiesPanel.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ export default function BpmnPropertiesPanel(props) {
8787
});
8888
};
8989

90+
// TODO: this implements getDerivedStateFromProps and should be replaced with
91+
// a better solution
92+
// https://legacy.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#when-to-use-derived-state
93+
if (element !== state.selectedElement) {
94+
_update(element);
95+
}
96+
9097
// (2) react on element changes
9198

9299
// (2a) selection changed

test/spec/BpmnPropertiesPanel.spec.js

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('<BpmnPropertiesPanel>', function() {
5959
it('should render', function() {
6060

6161
// given
62-
const result = createBpmnPropertiesPanel({ container });
62+
const result = renderBpmnPropertiesPanel({ container });
6363

6464
// then
6565
expect(domQuery('.bio-properties-panel', result.container)).to.exist;
@@ -107,7 +107,7 @@ describe('<BpmnPropertiesPanel>', function() {
107107
};
108108

109109
// when
110-
const result = createBpmnPropertiesPanel({ container, getProviders });
110+
const result = renderBpmnPropertiesPanel({ container, getProviders });
111111

112112
// then
113113
expect(domQueryAll('.bio-properties-panel-group', result.container)).to.have.length(4);
@@ -122,7 +122,7 @@ describe('<BpmnPropertiesPanel>', function() {
122122
const eventBus = new eventBusMock();
123123

124124
// when
125-
const result = createBpmnPropertiesPanel({ container, eventBus, element });
125+
const result = renderBpmnPropertiesPanel({ container, eventBus, element });
126126

127127
// then
128128
expect(domQuery('.bio-properties-panel-placeholder', result.container)).to.exist;
@@ -141,7 +141,7 @@ describe('<BpmnPropertiesPanel>', function() {
141141

142142
const eventBus = new eventBusMock();
143143

144-
const result = createBpmnPropertiesPanel({ container, eventBus });
144+
const result = renderBpmnPropertiesPanel({ container, eventBus });
145145

146146
// when
147147
await act(() => {
@@ -155,6 +155,29 @@ describe('<BpmnPropertiesPanel>', function() {
155155
});
156156

157157

158+
it('should update if element prop different from state', function() {
159+
160+
// given
161+
const updateSpy = sinon.spy();
162+
163+
const eventBus = new eventBusMock();
164+
165+
const element = noopElement;
166+
167+
const { rerender } = renderBpmnPropertiesPanel({ container, eventBus, element });
168+
169+
// when
170+
const newElement = { ...noopElement, id: 'bar' };
171+
172+
eventBus.on('propertiesPanel.updated', updateSpy);
173+
174+
renderBpmnPropertiesPanel({ container, eventBus, element: newElement, rerender });
175+
176+
// then
177+
expect(updateSpy).to.have.been.calledWith({ element: newElement });
178+
});
179+
180+
158181
describe('event emitting', function() {
159182

160183
it('should update on selection changed', function() {
@@ -166,7 +189,7 @@ describe('<BpmnPropertiesPanel>', function() {
166189

167190
eventBus.on('propertiesPanel.updated', updateSpy);
168191

169-
createBpmnPropertiesPanel({ container, eventBus });
192+
renderBpmnPropertiesPanel({ container, eventBus });
170193

171194
// when
172195
eventBus.fire('selection.changed', { newSelection: [ noopElement ] });
@@ -192,7 +215,7 @@ describe('<BpmnPropertiesPanel>', function() {
192215

193216
eventBus.on('propertiesPanel.updated', updateSpy);
194217

195-
createBpmnPropertiesPanel({ container, eventBus });
218+
renderBpmnPropertiesPanel({ container, eventBus });
196219

197220
// when
198221
eventBus.fire('selection.changed', { newSelection: elements });
@@ -213,7 +236,7 @@ describe('<BpmnPropertiesPanel>', function() {
213236

214237
eventBus.on('propertiesPanel.updated', updateSpy);
215238

216-
createBpmnPropertiesPanel({ container, eventBus });
239+
renderBpmnPropertiesPanel({ container, eventBus });
217240

218241
// when
219242
eventBus.fire('elements.changed', { elements: [ noopElement ] });
@@ -234,7 +257,7 @@ describe('<BpmnPropertiesPanel>', function() {
234257

235258
eventBus.on('propertiesPanel.updated', updateSpy);
236259

237-
createBpmnPropertiesPanel({ container, eventBus });
260+
renderBpmnPropertiesPanel({ container, eventBus });
238261

239262
// when
240263
eventBus.fire('root.added', { element: noopElement });
@@ -255,7 +278,7 @@ describe('<BpmnPropertiesPanel>', function() {
255278

256279
eventBus.on('propertiesPanel.updated', updateSpy);
257280

258-
createBpmnPropertiesPanel({ container, eventBus });
281+
renderBpmnPropertiesPanel({ container, eventBus });
259282

260283
// when
261284
eventBus.fire('propertiesPanel.providersChanged');
@@ -274,7 +297,7 @@ describe('<BpmnPropertiesPanel>', function() {
274297

275298
eventBus.on('propertiesPanel.updated', updateSpy);
276299

277-
createBpmnPropertiesPanel({ container, eventBus });
300+
renderBpmnPropertiesPanel({ container, eventBus });
278301

279302
// when
280303
eventBus.fire('elementTemplates.changed');
@@ -296,7 +319,7 @@ describe('<BpmnPropertiesPanel>', function() {
296319
eventBus.on('propertiesPanel.layoutChanged', updateSpy);
297320

298321
// when
299-
createBpmnPropertiesPanel({ container, eventBus });
322+
renderBpmnPropertiesPanel({ container, eventBus });
300323

301324
// then
302325
expect(updateSpy).to.have.been.calledOnce;
@@ -317,7 +340,7 @@ describe('<BpmnPropertiesPanel>', function() {
317340
const eventBus = new eventBusMock();
318341
eventBus.on('propertiesPanel.layoutChanged', updateSpy);
319342

320-
createBpmnPropertiesPanel({ container, eventBus, layoutConfig: originalLayout });
343+
renderBpmnPropertiesPanel({ container, eventBus, layoutConfig: originalLayout });
321344

322345
// assume
323346
expect(updateSpy).to.have.been.calledWith(sinon.match({ layout: originalLayout }));
@@ -347,7 +370,7 @@ describe('<BpmnPropertiesPanel>', function() {
347370
eventBus.on('propertiesPanel.descriptionLoaded', loadedSpy);
348371

349372
// when
350-
createBpmnPropertiesPanel({ container, eventBus });
373+
renderBpmnPropertiesPanel({ container, eventBus });
351374

352375
// then
353376
expect(loadedSpy).to.have.been.called;
@@ -364,7 +387,7 @@ describe('<BpmnPropertiesPanel>', function() {
364387
eventBus.on('propertiesPanel.tooltipLoaded', loadedSpy);
365388

366389
// when
367-
createBpmnPropertiesPanel({ container, eventBus });
390+
renderBpmnPropertiesPanel({ container, eventBus });
368391

369392
// then
370393
expect(loadedSpy).to.have.been.called;
@@ -380,7 +403,7 @@ describe('<BpmnPropertiesPanel>', function() {
380403

381404
eventBus.on('propertiesPanel.updated', updateSpy);
382405

383-
createBpmnPropertiesPanel({ container, eventBus });
406+
renderBpmnPropertiesPanel({ container, eventBus });
384407

385408
// when
386409
eventBus.fire('propertiesPanel.providersChanged');
@@ -412,7 +435,7 @@ describe('<BpmnPropertiesPanel>', function() {
412435
const eventBus = new eventBusMock();
413436
eventBus.on('propertiesPanel.updated', updateSpy);
414437

415-
createBpmnPropertiesPanel({
438+
renderBpmnPropertiesPanel({
416439
container,
417440
element,
418441
elementRegistry,
@@ -436,7 +459,7 @@ describe('<BpmnPropertiesPanel>', function() {
436459

437460
// helpers /////////////////////////
438461

439-
function createBpmnPropertiesPanel(options = {}) {
462+
function renderBpmnPropertiesPanel(options = {}) {
440463

441464
const {
442465
element = noopElement,
@@ -446,6 +469,7 @@ function createBpmnPropertiesPanel(options = {}) {
446469
descriptionLoaded,
447470
tooltipConfig,
448471
tooltipLoaded,
472+
rerender,
449473
container
450474
} = options;
451475

@@ -463,6 +487,21 @@ function createBpmnPropertiesPanel(options = {}) {
463487
elementRegistry
464488
});
465489

490+
if (rerender) {
491+
rerender(
492+
<BpmnPropertiesPanel
493+
element={ element }
494+
injector={ injector }
495+
getProviders={ getProviders }
496+
layoutConfig={ layoutConfig }
497+
descriptionConfig={ descriptionConfig }
498+
descriptionLoaded={ descriptionLoaded }
499+
tooltipConfig={ tooltipConfig }
500+
tooltipLoaded={ tooltipLoaded }
501+
/>
502+
);
503+
}
504+
466505
return render(
467506
<BpmnPropertiesPanel
468507
element={ element }

0 commit comments

Comments
 (0)