Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The setValueExpression: currentDate() doesn't work for Date and Time … #8484

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading