Skip to content

Commit

Permalink
The setValueExpression: currentDate() doesn't work for Date and Time …
Browse files Browse the repository at this point in the history
…input fields fix #8471 (#8484)
  • Loading branch information
andrewtelnov authored Jun 28, 2024
1 parent 49642c2 commit c2e1f8b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,7 @@ export class Question extends SurveyElement<Question>
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;
Expand Down Expand Up @@ -1929,7 +1927,7 @@ export class Question extends SurveyElement<Question>
properties: HashTable<any> = null
): void {
const func = (val: any) => {
this.runExpressionSetValue(val, setFunc);
this.runExpressionSetValueCore(val, setFunc);
};
if (!this.runDefaultValueExpression(runner, values, properties, func)) {
func(defaultValue);
Expand All @@ -1938,19 +1936,22 @@ export class Question extends SurveyElement<Question>
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<any> = null,
properties: HashTable<any> = 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() : {};
Expand Down
37 changes: 37 additions & 0 deletions tests/surveyquestiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down

0 comments on commit c2e1f8b

Please sign in to comment.