Skip to content

Commit

Permalink
obj parameter in property.defaultFunc(obj) could be undefined fix #9108
Browse files Browse the repository at this point in the history
… (#9109)
  • Loading branch information
andrewtelnov authored Nov 27, 2024
1 parent e88c14a commit 8c2adad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/survey-core/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ export class Base {
public getDefaultPropertyValue(name: string): any {
const prop = this.getPropertyByName(name);
if (!prop || prop.isCustom && this.isCreating) return undefined;
const dValue = prop.defaultValue;
if (!!prop.defaultValueFunc) return dValue;
if (!!prop.defaultValueFunc) return prop.defaultValueFunc(this);
const dValue = prop.getDefaultValue(this);
if (!this.isPropertyEmpty(dValue) && !Array.isArray(dValue)) return dValue;
const locStr = this.localizableStrings ? this.localizableStrings[name] : undefined;
if (locStr && locStr.localizationName) return this.getLocalizationString(locStr.localizationName);
Expand Down
7 changes: 3 additions & 4 deletions packages/survey-core/src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ export class CustomPropertiesCollection {
obj.createCustomLocalizableObj
) {
const locStr = obj.createCustomLocalizableObj(prop.name);
locStr.defaultValue = prop.defaultValue;
locStr.defaultValue = prop.getDefaultValue(obj);
var locDesc = {
get: function () {
return obj.getLocalizableString(prop.name);
Expand All @@ -699,7 +699,6 @@ export class CustomPropertiesCollection {
};
Object.defineProperty(obj, prop.name, desc);
} else {
var defaultValue = prop.defaultValue;
var isArrayProp = prop.isArray || prop.type === "multiplevalues";
if (typeof obj.createNewArray === "function") {
if (Serializer.isDescendantOf(prop.className, "itemvalue")) {
Expand All @@ -715,10 +714,10 @@ export class CustomPropertiesCollection {
}
}
if (isArrayProp) {
const defaultValue = prop.getDefaultValue(obj);
if (Array.isArray(defaultValue)) {
obj.setPropertyValue(prop.name, defaultValue);
}
defaultValue = null;
}
}
if (!!obj.getPropertyValue && !!obj.setPropertyValue) {
Expand All @@ -727,7 +726,7 @@ export class CustomPropertiesCollection {
if (!!prop.onGetValue) {
return prop.onGetValue(obj);
}
return obj.getPropertyValue(prop.name, defaultValue);
return obj.getPropertyValue(prop.name, undefined);
},
set: function (v: any) {
if (!!prop.onSetValue) {
Expand Down
2 changes: 1 addition & 1 deletion packages/survey-core/src/mask/mask_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class InputMaskBase extends Base implements IInputMask {
const properties = Serializer.getProperties(this.getType());
properties.forEach(property => {
const currentValue = json[property.name];
(this as any)[property.name] = currentValue !== undefined ? currentValue : property.defaultValue;
(this as any)[property.name] = currentValue !== undefined ? currentValue : property.getDefaultValue(this);
});
}
public getData(): any {
Expand Down
20 changes: 19 additions & 1 deletion packages/survey-core/tests/jsonobjecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3442,4 +3442,22 @@ QUnit.test("Add availableInMatrixColumn attribute", function (assert) {
Serializer.removeProperty("question", "prop1");
Serializer.removeProperty("question", "prop2");
Serializer.removeProperty("question", "prop3");
});
});
QUnit.test("Add defaultFunc attribute based on another property & obj parameter, Bug#9108", function (assert) {
Serializer.addProperty("question", { name: "secondName", defaultFunc: (obj: any) => { return obj.name + "_second"; } });
const obj: any = new QuestionTextModel("q1");
assert.equal(obj.secondName, "q1_second", "secondName #1");
obj.name = "q2";
assert.equal(obj.secondName, "q2_second", "secondName #2");
assert.deepEqual(obj.toJSON(), { name: "q2" }, "toJSON #1");

obj.secondName = "q3_s";
assert.equal(obj.secondName, "q3_s", "secondName #3");
assert.deepEqual(obj.toJSON(), { name: "q2", secondName: "q3_s" }, "toJSON #2");

obj.resetPropertyValue("secondName");
assert.equal(obj.secondName, "q2_second", "secondName #4");
assert.deepEqual(obj.toJSON(), { name: "q2" }, "toJSON #3");

Serializer.removeProperty("question", "secondName");
});

0 comments on commit 8c2adad

Please sign in to comment.