diff --git a/packages/survey-core/src/base.ts b/packages/survey-core/src/base.ts index 20a018b6f5..cc54b1c5fa 100644 --- a/packages/survey-core/src/base.ts +++ b/packages/survey-core/src/base.ts @@ -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); diff --git a/packages/survey-core/src/jsonobject.ts b/packages/survey-core/src/jsonobject.ts index f3bec76c7a..f7244a1419 100644 --- a/packages/survey-core/src/jsonobject.ts +++ b/packages/survey-core/src/jsonobject.ts @@ -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); @@ -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")) { @@ -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) { @@ -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) { diff --git a/packages/survey-core/src/mask/mask_base.ts b/packages/survey-core/src/mask/mask_base.ts index fa53b920ff..16ee4f5816 100644 --- a/packages/survey-core/src/mask/mask_base.ts +++ b/packages/survey-core/src/mask/mask_base.ts @@ -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 { diff --git a/packages/survey-core/tests/jsonobjecttests.ts b/packages/survey-core/tests/jsonobjecttests.ts index db98ab63ed..e17591a759 100644 --- a/packages/survey-core/tests/jsonobjecttests.ts +++ b/packages/survey-core/tests/jsonobjecttests.ts @@ -3442,4 +3442,22 @@ QUnit.test("Add availableInMatrixColumn attribute", function (assert) { Serializer.removeProperty("question", "prop1"); Serializer.removeProperty("question", "prop2"); Serializer.removeProperty("question", "prop3"); -}); \ No newline at end of file +}); +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"); +});