Skip to content

Commit 91cdf54

Browse files
committed
feat(cloud-templates): conditions on dropdown choices
Related to camunda/camunda-modeler#3682
1 parent 4d0b34e commit 91cdf54

File tree

3 files changed

+223
-2
lines changed

3 files changed

+223
-2
lines changed

src/cloud-element-templates/Condition.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import { getPropertyValue } from './util/propertyUtil';
66
export function applyConditions(element, elementTemplate) {
77
const { properties } = elementTemplate;
88

9-
const filteredProperties = properties.filter(property => {
10-
return isConditionMet(element, properties, property);
9+
let filteredProperties = [];
10+
11+
properties.forEach(property => {
12+
if (isConditionMet(element, properties, property)) {
13+
filteredProperties.push(
14+
applyDropdownConditions(element, properties, property)
15+
);
16+
}
1117
});
1218

1319
return {
@@ -16,6 +22,20 @@ export function applyConditions(element, elementTemplate) {
1622
};
1723
}
1824

25+
export function applyDropdownConditions(element, properties, property) {
26+
const { type, choices } = property;
27+
28+
if (type != 'Dropdown')
29+
return property;
30+
31+
else {
32+
return {
33+
...property,
34+
choices: choices.filter(choice => isConditionMet(element, properties, choice))
35+
};
36+
}
37+
}
38+
1939
export function isConditionMet(element, properties, property) {
2040
const { condition } = property;
2141

test/spec/cloud-element-templates/ElementTemplateConditionChecker.spec.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TestContainer from 'mocha-test-container-support';
22

33
import {
44
bootstrapModeler,
5+
bootstrapPropertiesPanel,
56
inject
67
} from 'test/TestHelper';
78

@@ -17,12 +18,18 @@ import messageDiagramXML from './fixtures/condition-message.bpmn';
1718

1819
import template from './fixtures/condition.json';
1920
import messageTemplates from './fixtures/condition-message.json';
21+
import dropdownConditions from './fixtures/condition-dropdown.json';
2022
import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';
2123
import { findExtension, findMessage, findZeebeSubscription } from 'src/cloud-element-templates/Helper';
2224
import ElementTemplatesConditionChecker from 'src/cloud-element-templates/ElementTemplatesConditionChecker';
2325
import { getBpmnJS } from 'bpmn-js/test/helper';
2426
import { isString } from 'min-dash';
2527

28+
import {
29+
query as domQuery
30+
} from 'min-dom';
31+
32+
import { act } from '@testing-library/preact';
2633

2734
describe('provider/cloud-element-templates - ElementTemplatesConditionChecker', function() {
2835

@@ -1116,6 +1123,147 @@ describe('provider/cloud-element-templates - ElementTemplatesConditionChecker',
11161123
})
11171124
);
11181125
});
1126+
1127+
1128+
describe('conditional dropdown choices', function() {
1129+
1130+
beforeEach(bootstrapPropertiesPanel(diagramXML, {
1131+
container: container,
1132+
modules: [
1133+
coreModule,
1134+
elementTemplatesModule,
1135+
modelingModule,
1136+
ElementTemplatesConditionChecker,
1137+
BpmnPropertiesPanelModule
1138+
],
1139+
moddleExtensions: {
1140+
zeebe: zeebeModdlePackage
1141+
}
1142+
}));
1143+
1144+
beforeEach(inject(function(elementTemplates) {
1145+
elementTemplates.set([ dropdownConditions ]);
1146+
}));
1147+
1148+
1149+
it('should add conditional entries', inject(
1150+
async function(elementRegistry, modeling, selection) {
1151+
1152+
// given
1153+
const element = elementRegistry.get('Task_1');
1154+
1155+
changeTemplate(element, dropdownConditions);
1156+
1157+
// when
1158+
await act(() => {
1159+
selection.select(element);
1160+
modeling.updateProperties(element, { name: 'foo' });
1161+
});
1162+
1163+
// then
1164+
expectDropdownOptions(container, 2, 'foo');
1165+
})
1166+
);
1167+
1168+
1169+
it('should switch between conditional properties', inject(
1170+
async function(elementRegistry, modeling, selection) {
1171+
1172+
// given
1173+
const element = elementRegistry.get('Task_1');
1174+
1175+
changeTemplate(element, dropdownConditions);
1176+
1177+
// when
1178+
await act(() => {
1179+
selection.select(element);
1180+
modeling.updateProperties(element, { name: 'foo' });
1181+
});
1182+
1183+
// then
1184+
expectDropdownOptions(container, 2, 'foo');
1185+
1186+
// when
1187+
await act(() =>
1188+
modeling.updateProperties(element, { name: 'bar' })
1189+
);
1190+
1191+
// then
1192+
expectDropdownOptions(container, 2, 'bar');
1193+
})
1194+
);
1195+
1196+
1197+
it('undo', inject(async function(commandStack, elementRegistry, modeling, selection) {
1198+
1199+
// given
1200+
const element = elementRegistry.get('Task_1');
1201+
1202+
changeTemplate(element, dropdownConditions);
1203+
1204+
// when
1205+
await act(() => {
1206+
selection.select(element);
1207+
modeling.updateProperties(element, { name: 'foo' });
1208+
});
1209+
1210+
// assume
1211+
expectDropdownOptions(container, 2, 'foo');
1212+
1213+
// when
1214+
await act(() => commandStack.undo());
1215+
1216+
// then
1217+
expectDropdownOptions(container, 1, 'foobar');
1218+
}));
1219+
1220+
1221+
it('redo', inject(async function(commandStack, elementRegistry, modeling, selection) {
1222+
1223+
// given
1224+
const element = elementRegistry.get('Task_1');
1225+
1226+
changeTemplate(element, dropdownConditions);
1227+
1228+
// when
1229+
await act(() => {
1230+
selection.select(element);
1231+
modeling.updateProperties(element, { name: 'foo' });
1232+
});
1233+
1234+
// when
1235+
await act(() => commandStack.undo());
1236+
1237+
// then
1238+
expectDropdownOptions(container, 1, 'foobar');
1239+
1240+
// when
1241+
await act(() => commandStack.redo());
1242+
1243+
// then
1244+
expectDropdownOptions(container, 2, 'foo');
1245+
}));
1246+
1247+
1248+
it('should remove conditional entries', inject(
1249+
async function(elementRegistry, modeling, selection) {
1250+
1251+
// given
1252+
const element = elementRegistry.get('Task_1');
1253+
1254+
changeTemplate(element, dropdownConditions);
1255+
1256+
// when
1257+
await act(() => {
1258+
selection.select(element);
1259+
modeling.updateProperties(element, { name: '' });
1260+
});
1261+
1262+
// then
1263+
expectDropdownOptions(container, 1, 'foobar');
1264+
})
1265+
);
1266+
});
11191267
});
11201268

11211269

@@ -1178,4 +1326,11 @@ function expectZeebePropertyValue(businessObject, value) {
11781326
expect(zeebeProperties).to.exist;
11791327
expect(properties).to.have.lengthOf(1);
11801328
expect(properties[0].value).to.eql(value);
1329+
}
1330+
1331+
function expectDropdownOptions(container, length, value) {
1332+
const selectOptions = domQuery('select', container).options;
1333+
1334+
expect(selectOptions).to.have.lengthOf(length);
1335+
expect(selectOptions[0].value).to.eql(value);
11811336
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
3+
"name": "Conditional dropdown",
4+
"id": "example.com.dropdown-condition",
5+
"description": "A conditional template.",
6+
"appliesTo": ["bpmn:Task"],
7+
"properties": [
8+
{
9+
"id": "nameProp",
10+
"label": "name",
11+
"type": "String",
12+
"binding": {
13+
"type": "property",
14+
"name": "name"
15+
}
16+
},
17+
{
18+
"label": "dropdown",
19+
"type": "Dropdown",
20+
"value": "foo",
21+
"choices": [
22+
{
23+
"name": "FOO",
24+
"value": "foo",
25+
"condition": {
26+
"property": "nameProp",
27+
"equals": "foo"
28+
}
29+
},
30+
{
31+
"name": "BAR",
32+
"value": "bar",
33+
"condition": {
34+
"property": "nameProp",
35+
"equals": "bar"
36+
}
37+
},
38+
{ "name": "FOOBAR", "value": "foobar" }
39+
],
40+
"binding": {
41+
"type": "property",
42+
"name": "customDropdownValue"
43+
}
44+
}
45+
]
46+
}

0 commit comments

Comments
 (0)