Skip to content

Commit

Permalink
Merge pull request #6765 from surveyjs/features/add-defaultvalueprop-…
Browse files Browse the repository at this point in the history
…functions-base

Add base.getPropertyDefaultValue and base.resetPropertyValue func
  • Loading branch information
OlgaLarina authored Aug 22, 2023
2 parents 8e6b4eb + a7e8124 commit bc62dcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,12 @@ export class Base {
const locStr = this.localizableStrings ? this.localizableStrings[name] : undefined;
if(locStr) return locStr.text;
if (defaultValue != null) return defaultValue;
const propDefaultValue = this.getDefaultValueFromProperty(name);
const propDefaultValue = this.getDefaultPropertyValue(name);
if(propDefaultValue !== undefined) return propDefaultValue;
}
return res;
}
private getDefaultValueFromProperty(name: string): any {
public getDefaultPropertyValue(name: string): any {
const prop = this.getPropertyByName(name);
if(!prop || prop.isCustom && this.isCreating) return undefined;
const dValue = prop.defaultValue;
Expand All @@ -486,6 +486,12 @@ export class Base {
if (prop.isCustom && !!prop.onGetValue) return prop.onGetValue(this);
return undefined;
}
public hasDefaultPropertyValue(name: string): boolean {
return this.getDefaultPropertyValue(name) !== undefined;
}
public resetPropertyValue(name: string): void {
this.setPropertyValue(name, undefined);
}
protected getPropertyValueWithoutDefault(name: string): any {
return this.getPropertyValueCore(this.propertyHash, name);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/basetests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { property, Serializer } from "../src/jsonobject";
import { SurveyModel } from "../src/survey";
import { Action } from "../src/actions/action";
import { findParentByClassNames } from "../src/utils/utils";
import { QuestionTextModel } from "../src/question_text";

export default QUnit.module("Base");

Expand Down Expand Up @@ -694,4 +695,23 @@ QUnit.test("Subscribe localizable property", function (assert) {
base4.propL = "localizable value";
assert.equal(base4.propC, "localizable value");
assert.equal(updaterCallCount1, 2, "update called - localizable value");
});
QUnit.test("base.hasDefaultPropertyValue, base.getDefaultPropertyValue and base.resetPropertyValue()", function (assert) {
const question = new QuestionTextModel("q1");
assert.equal(question.hasDefaultPropertyValue("width"), false, "question.width has no default value");
assert.notOk(question.getDefaultPropertyValue("width"), "question.width default value is undefined");
question.width = "200px";
question.resetPropertyValue("width");
assert.notOk(question.width, "width property value is empty");

assert.equal(question.hasDefaultPropertyValue("minWidth"), true, "question.minWidth has default value");
assert.ok(question.getDefaultPropertyValue("minWidth"), "question.minWidth default value is 300px");
question.minWidth = "200px";
assert.equal(question.minWidth, "200px", "minWidth property is set to 200px");
question.resetPropertyValue("minWidth");
assert.equal(question.minWidth, "300px", "minWidth property value is reset, #1");
question.minWidth = "";
assert.strictEqual(question.minWidth, "", "minWidth property value is empty string");
question.resetPropertyValue("minWidth");
assert.equal(question.minWidth, "300px", "minWidth property value is reset, #2");
});

0 comments on commit bc62dcd

Please sign in to comment.