From ce5ff2d081c422cf69a46a8faf427f14d15d0b7d Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Mon, 1 Jul 2024 18:06:31 +0300 Subject: [PATCH] Allow to have undefined values #8470 --- src/jsonobject.ts | 2 +- src/question_comment.ts | 33 ++++++++++------- tests/surveytests.ts | 80 ++++++++++++++++++++++++++++------------- 3 files changed, 78 insertions(+), 37 deletions(-) diff --git a/src/jsonobject.ts b/src/jsonobject.ts index e86e589c9c..13bbc190ea 100644 --- a/src/jsonobject.ts +++ b/src/jsonobject.ts @@ -410,7 +410,7 @@ export class JsonObjectProperty implements IObject, IJsonPropertyInfo { } if(this.isLocalizable) return value === null || value === undefined; return ( - (value === false && (this.type == "boolean" || this.type == "switch")) || + (value === false && (this.type == "boolean" || this.type == "switch") && !this.defaultValueFunc) || value === "" || Helpers.isValueEmpty(value) ); } diff --git a/src/question_comment.ts b/src/question_comment.ts index 16e8d8e0d3..330255ada1 100644 --- a/src/question_comment.ts +++ b/src/question_comment.ts @@ -47,11 +47,17 @@ export class QuestionCommentModel extends QuestionTextBase { * Default value: `false` (inherited from `SurveyModel`'s [`autoGrowComment`](https://surveyjs.io/form-library/documentation/surveymodel#autoGrowComment) property) * @see allowResize */ - public get autoGrow(): boolean { - return this.getPropertyValue("autoGrow") || (this.survey && this.survey.autoGrowComment); + public get autoGrow(): boolean | undefined { + return this.getPropertyValueWithoutDefault("autoGrow"); } - public set autoGrow(val: boolean) { - this.setPropertyValue("autoGrow", val); + public set autoGrow(val: boolean | undefined) { + if(val !== this.autoGrow) { + this.setPropertyValueDirectly("autoGrow", val); + } + } + public get renderedAutoGrow(): boolean { + const autoGrow = this.autoGrow; + return autoGrow === undefined && this.survey ? this.survey.autoGrowComment : !!autoGrow; } /** * Specifies whether to display a resize handle for the comment area. @@ -59,15 +65,18 @@ export class QuestionCommentModel extends QuestionTextBase { * Default value: `true` (inherited from `SurveyModel`'s [`allowResizeComment`](https://surveyjs.io/form-library/documentation/surveymodel#allowResizeComment) property) * @see autoGrow */ - public get allowResize(): boolean { - const value = this.getPropertyValue("allowResize"); - return value === null ? (this.survey && this.survey.allowResizeComment) : value; + public get allowResize(): boolean | undefined { + return this.getPropertyValueWithoutDefault("allowResize"); } - public set allowResize(val: boolean) { - this.setPropertyValue("allowResize", val); + public set allowResize(val: boolean | undefined) { + if(val !== this.allowResize) { + this.setPropertyValueDirectly("allowResize", val); + } } public get renderedAllowResize(): boolean { - return this.allowResize && !this.isPreviewStyle && !this.isReadOnlyStyle; + const res = this.allowResize; + const allowResize = res === undefined && this.survey ? this.survey.allowResizeComment : !!res; + return allowResize && !this.isPreviewStyle && !this.isReadOnlyStyle; } public get resizeStyle() { return this.renderedAllowResize ? "both" : "none"; @@ -139,8 +148,8 @@ Serializer.addClass( default: "default", choices: ["default", "onBlur", "onTyping"], }, - { name: "autoGrow:boolean", default: null }, - { name: "allowResize:boolean", default: null }, + { name: "autoGrow:boolean", defaultFunc: () => undefined }, + { name: "allowResize:boolean", defaultFunc: () => undefined }, { name: "acceptCarriageReturn:boolean", default: true, visible: false } ], function () { diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 6f05c77959..12f42a5c93 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -15025,7 +15025,7 @@ QUnit.test("onElementWrapperComponentName event", function (assert) { QUnit.test("onElementWrapperComponentName event", function (assert) { const survey = new SurveyModel({ elements: [{ type: "text", name: "q1" }, { type: "checkbox", name: "q2", choices: [1, 2] }, - { type: "matrixdynamic", name: "q3", rowCount: 1, columns: [{ name: "col1" }] } + { type: "matrixdynamic", name: "q3", rowCount: 1, columns: [{ name: "col1" }] } ] }); const q1 = survey.getQuestionByName("q1"); @@ -15476,18 +15476,42 @@ QUnit.test("survey.autoGrowComment", function (assert) { ] }; let survey = new SurveyModel(json); - let comment1 = survey.getQuestionByName("comment1"); - let comment2 = survey.getQuestionByName("comment2"); + let comment1 = survey.getQuestionByName("comment1") as QuestionCommentModel; + let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel; - assert.equal(survey.autoGrowComment, true); - assert.equal(comment1.autoGrow, true); - assert.equal(comment2.autoGrow, true); + assert.equal(survey.autoGrowComment, true, "#1"); + assert.equal(comment1.renderedAutoGrow, true, "#2"); + assert.equal(comment2.renderedAutoGrow, true, "#3"); survey.autoGrowComment = false; - assert.equal(comment1.autoGrow, false); - assert.equal(comment2.autoGrow, true); + assert.equal(comment1.renderedAutoGrow, false, "#4"); + assert.equal(comment2.renderedAutoGrow, true, "#5"); }); -QUnit.only("survey.allowResizeComment", function (assert) { +QUnit.test("save comment autoGrow && autoResize", function (assert) { + let json = { + autoGrowComment: true, + pages: [ + { + elements: [ + { + type: "comment", + name: "q1", + } + ] + } + ] + }; + let survey = new SurveyModel(json); + let question = survey.getQuestionByName("q1") as QuestionCommentModel; + assert.deepEqual(question.toJSON(), { name: "q1" }, "#1"); + question.allowResize = false; + question.autoGrow = false; + assert.deepEqual(question.toJSON(), { name: "q1", allowResize: false, autoGrow: false }, "#2"); + question.allowResize = true; + question.autoGrow = true; + assert.deepEqual(question.toJSON(), { name: "q1", allowResize: true, autoGrow: true }, "#3"); +}); +QUnit.test("survey.allowResizeComment", function (assert) { let json = { allowResizeComment: false, pages: [ @@ -15511,23 +15535,23 @@ QUnit.only("survey.allowResizeComment", function (assert) { let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel; assert.equal(survey.allowResizeComment, false); - assert.equal(comment1.renderedAllowResize, false, "comment1 survey.allowResizeComment = false"); - assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false"); + assert.equal(comment1.renderedAllowResize, false, "comment1 survey.allowResizeComment = false, #1"); + assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false, #2"); survey.allowResizeComment = true; - assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true"); - assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = true"); + assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true, #3"); + assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = true, #4"); comment1.readOnly = true; - assert.equal(comment1.renderedAllowResize, false); + assert.equal(comment1.renderedAllowResize, false, "#5"); comment1.readOnly = false; survey.showPreview(); let comment1Preview = survey.getQuestionByName("comment1"); - assert.equal(comment1Preview.renderedAllowResize, false); + assert.equal(comment1Preview.renderedAllowResize, false, "#6"); }); -QUnit.only("survey.allowResizeComment & survey.autoGrowComment override this properties for individual properties", function (assert) { +QUnit.test("survey.allowResizeComment & survey.autoGrowComment override this properties for individual properties", function (assert) { let json = { allowResizeComment: false, autoGrowComment: false, @@ -15543,6 +15567,11 @@ QUnit.only("survey.allowResizeComment & survey.autoGrowComment override this pro { type: "comment", name: "comment2", + }, + { + type: "comment", + name: "comment3", + autoGrow: false } ] } @@ -15551,18 +15580,21 @@ QUnit.only("survey.allowResizeComment & survey.autoGrowComment override this pro let survey = new SurveyModel(json); let comment1 = survey.getQuestionByName("comment1") as QuestionCommentModel; let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel; + let comment3 = survey.getQuestionByName("comment3") as QuestionCommentModel; - assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = false"); - assert.equal(comment1.autoGrow, true, "comment1 survey.autoGrowComment = false"); - assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false"); - assert.equal(comment2.autoGrow, false, "comment2 survey.autoGrowComment = false"); + assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = false, #1"); + assert.equal(comment1.renderedAutoGrow, true, "comment1 survey.autoGrowComment = false, #2"); + assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false, #3"); + assert.equal(comment2.renderedAutoGrow, false, "comment2 survey.autoGrowComment = false, #4"); + assert.equal(comment3.renderedAutoGrow, false, "comment2 survey.autoGrowComment = false, #5"); survey.allowResizeComment = true; survey.autoGrowComment = true; - assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true"); - assert.equal(comment1.autoGrow, true, "comment1 survey.autoGrowComment = true"); - assert.equal(comment2.renderedAllowResize, true, "comment2 survey.allowResizeComment = true"); - assert.equal(comment2.autoGrow, true, "comment2 survey.autoGrowComment = true"); + assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true, #6"); + assert.equal(comment1.renderedAutoGrow, true, "comment1 survey.autoGrowComment = true, #7"); + assert.equal(comment2.renderedAllowResize, true, "comment2 survey.allowResizeComment = true, #8"); + assert.equal(comment2.renderedAutoGrow, true, "comment2 survey.autoGrowComment = true, #9"); + assert.equal(comment3.renderedAutoGrow, false, "comment2 survey.autoGrowComment = true, #10"); }); QUnit.test("utils.increaseHeightByContent", assert => {