diff --git a/src/question.ts b/src/question.ts index 4d04d59aa0..c863028272 100644 --- a/src/question.ts +++ b/src/question.ts @@ -532,9 +532,7 @@ export class Question extends SurveyElement if(!this.setValueExpressionRunner) { this.setValueExpressionRunner = new ExpressionRunner(this.setValueExpression); this.setValueExpressionRunner.onRunComplete = (res: any): void => { - if(!this.isTwoValueEquals(this.value, res)) { - this.value = res; - } + this.runExpressionSetValue(res); }; } else { this.setValueExpressionRunner.expression = this.setValueExpression; @@ -1929,7 +1927,7 @@ export class Question extends SurveyElement properties: HashTable = null ): void { const func = (val: any) => { - this.runExpressionSetValue(val, setFunc); + this.runExpressionSetValueCore(val, setFunc); }; if (!this.runDefaultValueExpression(runner, values, properties, func)) { func(defaultValue); @@ -1938,19 +1936,22 @@ export class Question extends SurveyElement protected convertFuncValuetoQuestionValue(val: any): any { return Helpers.convertValToQuestionVal(val); } - private runExpressionSetValue(val: any, setFunc?: (val: any) => void): void { + private runExpressionSetValueCore(val: any, setFunc?: (val: any) => void): void { setFunc(this.convertFuncValuetoQuestionValue(val)); } + private runExpressionSetValue(val: any): void { + this.runExpressionSetValueCore(val, (val: any): void => { + if (!this.isTwoValueEquals(this.value, val)) { + this.value = val; + } + }); + } private runDefaultValueExpression(runner: ExpressionRunner, values: HashTable = null, properties: HashTable = null, setFunc?: (val: any) => void): boolean { if (!runner || !this.data) return false; if (!setFunc) { setFunc = (val: any): void => { - this.runExpressionSetValue(val, (val: any): void => { - if (!this.isTwoValueEquals(this.value, val)) { - this.value = val; - } - }); + this.runExpressionSetValue(val); }; } if (!values) values = this.defaultValueExpression ? this.data.getFilteredValues() : {}; diff --git a/tests/surveyquestiontests.ts b/tests/surveyquestiontests.ts index a7fbc769f9..90625501bb 100644 --- a/tests/surveyquestiontests.ts +++ b/tests/surveyquestiontests.ts @@ -6938,6 +6938,43 @@ QUnit.test("defaultValueExpressions, currentDate() and 'date'+'datetime-local' i assert.equal(q2.displayValue.indexOf(prefix), 0, "datetime-local has year"); assert.equal(q2.displayValue.indexOf(":") < 0, true, "date has no time"); }); +QUnit.test("setValueExpression, currentDate() and 'date'+'datetime-local' inputtype, Bug#8471", function ( + assert +) { + const survey = new SurveyModel({ + elements: [{ + "name": "q1", + "type": "text", + "inputType": "datetime-local", + "setValueIf": "{q3} > 10", + "setValueExpression": "currentDate()" + }, + { + "name": "q2", + "type": "text", + "inputType": "date", + "setValueIf": "{q3} > 10", + "setValueExpression": "currentDate()" + }, + { "name": "q3", "type": "text" } + ] }); + const d = new Date(); + let prefix = d.getFullYear() + "-"; + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + assert.equal(q1.isEmpty(), true, "q1 is Empty"); + assert.equal(q2.isEmpty(), true, "q2 is Empty"); + survey.setValue("q3", 11); + assert.equal(q1.isEmpty(), false, "q1 is not Empty"); + assert.equal(q2.isEmpty(), false, "q2 is not Empty"); + assert.ok(q1.displayValue, "q1 has displayValue"); + assert.ok(q2.displayValue, "q2 has displayValue"); + assert.equal(q1.inputType, "datetime-local", "inputType is correct"); + assert.equal(q1.displayValue.indexOf(prefix), 0, "datetime-local has year"); + assert.equal(q1.displayValue.indexOf(":") > 0, true, "datetime-local has time"); + assert.equal(q2.displayValue.indexOf(prefix), 0, "datetime-local has year"); + assert.equal(q2.displayValue.indexOf(":") < 0, true, "date has no time"); +}); QUnit.test("Supporting showCommentArea property, Bug#5479", function ( assert ) {