Skip to content

Commit

Permalink
A custom trigger's property dependent choices are not updated in a Lo…
Browse files Browse the repository at this point in the history
…gic tab fix #5937
  • Loading branch information
andrewtelnov committed Oct 7, 2024
1 parent a47906d commit 9810342
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PropertyJSONGenerator } from "../../property-grid";
import { ISurveyCreatorOptions } from "../../creator-settings";
import { SurveyLogicAction } from "./logic-items";
import { SurveyLogicType } from "./logic-types";
import { PropertyGridEditorCollection } from "../../property-grid/index";

export class LogicActionModelBase {
public isTrigger: boolean;
Expand Down Expand Up @@ -181,6 +182,21 @@ export class LogicActionTriggerModel extends LogicActionModelBase {
panel.visible = false;
}
}
public onPanelQuestionValueChanged(panel: PanelModel, qName: string): void {
if(this.panelObj) {
const prop = Serializer.findProperty(this.panelObj.getType(), qName);
const depProps = prop?.getDependedProperties();
if(Array.isArray(depProps) && depProps.length > 0) {
depProps.forEach(dp => {
const dQ = panel.getQuestionByName(dp);
const dProp = Serializer.findProperty(this.panelObj.getType(), dp);
if(!!dQ && !!dProp) {
PropertyGridEditorCollection.onMasterValueChanged(this.panelObj, dProp, dQ);
}
});
}
}
}
private updatePanelQuestionsValue(panel: PanelModel) {
panel.onSurveyLoad();
panel.questions.forEach(q => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ export class LogicItemEditor extends PropertyEditorSetupValue {
this.editSurvey.onValueChanged.add((sender, options) => {
this.onValueChanged(options);
});
this.editSurvey.onDynamicPanelItemValueChanged.add((sender, options) => {
const q = options.panel.getQuestionByName(options.name);
if(!!q && q.parent?.name === "triggerEditorPanel") {
const action = <LogicActionTriggerModel>this.getActionModelByPanel(options.panel);
if(action) {
action.onPanelQuestionValueChanged(<PanelModel>q.parent, options.name);
}
}
});
this.setEditableItem(editableItem);
}
public get editableItem(): SurveyLogicItem {
Expand Down
114 changes: 114 additions & 0 deletions packages/survey-creator-core/tests/tabs/logic.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,59 @@ test("Custom trigger in logic", () => {
delete SurveyLogic.types["increment_counter"];
Serializer.removeClass("incrementcountertrigger");
});
test("Custom trigger vs depends on in logic, Bug#5937", () => {
Serializer.addClass(
LocationTrigger.triggerName, LocationTrigger.properties,
function () {
return new LocationTrigger();
},
"surveytrigger"
);
SurveyLogic.types.push({
name: LocationTrigger.triggerName,
baseClass: LocationTrigger.triggerName,
propertyName: "expression"
});

const survey = new SurveyModel({
elements: [
{ type: "text", name: "q1" },
{ type: "text", name: "q2" }
],
triggers: [
{
type: LocationTrigger.triggerName,
expression: "{q1} = 1",
country: "germany",
city: "berlin"
}
]
});
const logic = new SurveyLogicUI(survey);
expect(logic.matrixItems.visibleRows).toHaveLength(1);
const row = logic.matrixItems.visibleRows[0];
row.showDetailPanel();
const panel = logic.itemEditor.panels[0];
expect(panel.getQuestionByName("logicTypeName").value).toBe(LocationTrigger.triggerName);
const triggerEditorPanel = <PanelModel>panel.getElementByName("triggerEditorPanel");
const countryQuestion = <QuestionDropdownModel>triggerEditorPanel.getQuestionByName("country");
const cityQuestion = <QuestionDropdownModel>triggerEditorPanel.getQuestionByName("city");
expect(countryQuestion.value).toBe("germany");
expect(countryQuestion.choices).toHaveLength(2);
expect(cityQuestion.value).toBe("berlin");
expect(cityQuestion.choices).toHaveLength(2);
expect(cityQuestion.choices[0].value).toBe("berlin");
expect(cityQuestion.choices[1].value).toBe("frankfurt");

countryQuestion.value = "usa";
expect(cityQuestion.value).toBeFalsy();
expect(cityQuestion.choices).toHaveLength(2);
expect(cityQuestion.choices[0].value).toBe("new-york");
expect(cityQuestion.choices[1].value).toBe("los-angeles");

delete SurveyLogic.types[LocationTrigger.triggerName];
Serializer.removeClass(LocationTrigger.triggerName);
});
test("SurveyLogicItem, setValue for paneldynamic, Bug#4824", () => {
const survey = new SurveyModel({
elements: [
Expand Down Expand Up @@ -3419,3 +3472,64 @@ class IncrementCounterTrigger extends SurveyTrigger {
this.setPropertyValue("initialNumber", val);
}
}

class LocationTrigger extends SurveyTrigger {
static countries = [
{
value: "usa",
text: "USA",
cities: [
{ value: "new-york", text: "New York" },
{ value: "los-angeles", text: "Los Angeles" },
],
},
{
value: "germany",
text: "Germany",
cities: [
{ value: "berlin", text: "Berlin" },
{ value: "frankfurt", text: "Frankfurt" },
],
},
];
static triggerName = "locationtrigger";
static properties = [
{
type: "dropdown",
name: "country",
displayName: "Country",
choices: LocationTrigger.countries
},
{
type: "dropdown",
name: "city",
dependsOn: "country",
visibleIf: (obj: any) => {
return !!obj.country;
},
choices: (obj, choicesCallback) => {
if (!obj.country) choicesCallback([]);
const currentCountry = LocationTrigger.countries.find(
({ value }) => value === obj.country
);
choicesCallback(currentCountry?.cities);
},
},
];

getType(): string {
return LocationTrigger.triggerName;
}
get country(): string {
return this.getPropertyValue("country", "");
}
set country(val: string) {
this.setPropertyValue("country", val);
}
get city(): string {
return this.getPropertyValue("city", "");
}
set city(val: string) {
this.setPropertyValue("city", val);
}
}

0 comments on commit 9810342

Please sign in to comment.