Skip to content

Commit

Permalink
Fix issue with has default value for localization strings
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 5, 2023
1 parent 5d9c81e commit 191aee7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ export class Base {
if(!prop || prop.isCustom && this.isCreating) return undefined;
const dValue = prop.defaultValue;
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);
if (prop.type == "boolean" || prop.type == "switch") return false;
if (prop.isCustom && !!prop.onGetValue) return prop.onGetValue(this);
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/localizablestring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class LocalizableString implements ILocalizableString {
var oldValue = this.onStrChanged && loc === curLoc ? this.pureText : undefined;
delete (<any>this).htmlValues[loc];
if (this.isValueEmpty(value)) {
if (this.getValue(loc)) this.deleteValue(loc);
this.deleteValue(loc);
} else {
if (typeof value === "string") {
if (this.canRemoveLocValue(loc, value)) {
Expand Down
16 changes: 14 additions & 2 deletions tests/basetests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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";
import { QuestionDropdownModel } from "../src/question_dropdown";

export default QUnit.module("Base");

Expand Down Expand Up @@ -736,7 +736,7 @@ QUnit.test("Subscribe localizable property", function (assert) {
assert.equal(updaterCallCount1, 2, "update called - localizable value");
});
QUnit.test("base.hasDefaultPropertyValue, base.getDefaultPropertyValue and base.resetPropertyValue()", function (assert) {
const question = new QuestionTextModel("q1");
const question = new QuestionDropdownModel("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";
Expand All @@ -753,6 +753,18 @@ QUnit.test("base.hasDefaultPropertyValue, base.getDefaultPropertyValue and base.
assert.strictEqual(question.minWidth, "", "minWidth property value is empty string");
question.resetPropertyValue("minWidth");
assert.equal(question.minWidth, "300px", "minWidth property value is reset, #2");

assert.equal(question.hasDefaultPropertyValue("placeholder"), true, "question.placeholder has default value");
assert.equal(question.getDefaultPropertyValue("placeholder"), "Select...", "question.placeholder default value");
assert.equal(question.placeholder, "Select...", "question.placeholder value");
question.placeholder = "abc";
assert.equal(question.placeholder, "abc", "placeholder property is set to 200px");
question.resetPropertyValue("placeholder");
assert.equal(question.placeholder, "Select...", "placeholder property value is reset, #1");
question.placeholder = "";
assert.strictEqual(question.placeholder, "", "placeholder property value is empty string");
question.resetPropertyValue("placeholder");
assert.equal(question.placeholder, "Select...", "placeholder property value is reset, #2");
});
QUnit.test("base.resetPropertyValue() for localization string", function (assert) {
const survey = new SurveyModel();
Expand Down
4 changes: 4 additions & 0 deletions tests/surveyserializationtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ QUnit.test("Allow to save empty string for localization strings", function (asse
assert.equal(q1.placeholder, "test", "set value for placeholder, #3");
q1.locPlaceholder.clearLocale();
assert.equal(q1.placeholder, "Select...", "ClearLocale for placeholder");
q1.placeholder = "";
assert.equal(q1.placeholder, "", "placeholder is empty");
q1.locPlaceholder.clearLocale();
assert.equal(q1.placeholder, "Select...", "ClearLocale for placeholder, #2");
});
QUnit.test("Allow to save empty string for trings with default value", function (assert) {
const q = new QuestionTextModel("q1");
Expand Down

0 comments on commit 191aee7

Please sign in to comment.