Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic properties in custom component are not fired survey property … #7796

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,27 +675,21 @@ export class Base {
arrayChanges?: ArrayChanges,
target?: Base
) {
if (this.isInternal) return;
const fireCallback = (obj: Base): void => {
if (!!obj && !!obj.onPropertyValueChangedCallback) {
obj.onPropertyValueChangedCallback(name, oldValue, newValue, target, arrayChanges);
}
};
if (this.isInternal) {
fireCallback(this);
return;
}
if (!target) target = this;
var notifier: any = this.getSurvey();
if (!notifier) notifier = this;
if (!!notifier.onPropertyValueChangedCallback) {
notifier.onPropertyValueChangedCallback(
name,
oldValue,
newValue,
target,
arrayChanges
);
}
if (notifier !== this && !!this.onPropertyValueChangedCallback) {
this.onPropertyValueChangedCallback(
name,
oldValue,
newValue,
target,
arrayChanges
);
fireCallback(notifier);
if (notifier !== this) {
fireCallback(this);
}
}
public addExpressionProperty(name: string, onExecute: (obj: Base, res: any) => void, canRun?: (obj: Base) => boolean): void {
Expand Down
24 changes: 20 additions & 4 deletions src/question_custom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Question, IConditionObject } from "./question";
import { Serializer, CustomPropertiesCollection, JsonObjectProperty } from "./jsonobject";
import { Base } from "./base";
import { Base, ArrayChanges } from "./base";
import {
ISurveyImpl,
ISurveyData,
Expand Down Expand Up @@ -345,9 +345,8 @@ export class ComponentQuestionJSON {
}
public getDynamicProperties(): Array<JsonObjectProperty> {
if(!Array.isArray(this.dynamicProperties)) {

this.dynamicProperties = this.calcDynamicProperties();
}
this.dynamicProperties = this.calcDynamicProperties();
return this.dynamicProperties;
}
private calcDynamicProperties(): Array<JsonObjectProperty> {
Expand Down Expand Up @@ -733,6 +732,21 @@ export class QuestionCustomModel extends QuestionCustomModelBase {
protected createWrapper(): void {
this.questionWrapper = this.createQuestion();
this.createDynamicProperties(this.questionWrapper);
if(this.getDynamicProperties().length > 0) {
this.questionWrapper.onPropertyValueChangedCallback = (name: string, oldValue: any, newValue: any, sender: Base, arrayChanges: ArrayChanges): void => {
const prop = this.getDynamicProperty(name);
if(prop) {
this.propertyValueChanged(name, oldValue, newValue, arrayChanges);
}
};
}
}
private getDynamicProperty(name: string): JsonObjectProperty {
const props = this.getDynamicProperties();
for(let i = 0; i < props.length; i ++) {
if(props[i].name === name) return props[i];
}
return null;
}
protected getElement(): SurveyElement {
return this.contentQuestion;
Expand Down Expand Up @@ -1110,7 +1124,9 @@ export class QuestionCompositeModel extends QuestionCustomModelBase {
getFilteredValues(): any {
const values = !!this.data ? this.data.getFilteredValues() : {};
if (!!this.contentPanel) {
values[QuestionCompositeModel.ItemVariableName] = this.contentPanel.getValue();
values[
QuestionCompositeModel.ItemVariableName
] = this.contentPanel.getValue();
}
return values;
}
Expand Down
11 changes: 10 additions & 1 deletion tests/question_customtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ItemValue } from "../src/itemvalue";
import { LocalizableString } from "../src/localizablestring";
import { PanelModel } from "../src/panel";
import { StylesManager } from "../src/stylesmanager";
import { ArrayChanges, Base } from "../src/base";

export default QUnit.module("custom questions");

Expand Down Expand Up @@ -2770,7 +2771,7 @@ QUnit.test("single component: inheritBaseProps: array<string>", function (assert

ComponentCollection.Instance.clear();
});
QUnit.test("single component: inheritBaseProps: array<string> #2", function (assert) {
QUnit.test("single component: inheritBaseProps: array<string> #2 + check property change notification #", function (assert) {
ComponentCollection.Instance.add({
name: "customtext",
inheritBaseProps: ["placeholder"],
Expand All @@ -2785,11 +2786,19 @@ QUnit.test("single component: inheritBaseProps: array<string> #2", function (ass
{ type: "customtext", name: "q1" }
]
});
let propertyName = "";
let counter = 0;
const q1 = <QuestionCustomModel>survey.getQuestionByName("q1");
const content = <QuestionTextModel>q1.contentQuestion;
assert.equal(q1.placeholder, "abc", "q1.placeholder #1");
assert.equal(content.placeholder, "abc", "content.placeholder #1");
survey.onPropertyValueChangedCallback = (name: string, oldValue: any, newValue: any, sender: Base, arrayChanges: ArrayChanges): void => {
propertyName = name;
counter ++;
};
q1.placeholder = "bcd";
assert.equal(propertyName, "placeholder", "send notification, propertyname");
assert.equal(counter, 1, "send notification, counter");
assert.equal(q1.placeholder, "bcd", "q1.placeholder #2");
assert.equal(content.placeholder, "bcd", "content.placeholder #2");
content.placeholder = "cde";
Expand Down
Loading