Skip to content

Commit

Permalink
Add allowSpaceAsAnswer (false by default) non-serializable property i…
Browse files Browse the repository at this point in the history
…nto question fix #6678 (#6679)
  • Loading branch information
andrewtelnov authored Aug 9, 2023
1 parent 4e12a66 commit d8c60ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ export class Question extends SurveyElement<Question>
if (res) return res;
}
value = value == undefined ? this.createValueCopy() : value;
if (this.isValueEmpty(value)) return this.getDisplayValueEmpty();
if (this.isValueEmpty(value, !this.allowSpaceAsAnswer)) return this.getDisplayValueEmpty();
return this.getDisplayValueCore(keysAsText, value);
}
protected getDisplayValueCore(keyAsText: boolean, value: any): any {
Expand Down Expand Up @@ -1578,7 +1578,7 @@ export class Question extends SurveyElement<Question>
return this.defaultValue;
}
protected isDefaultValueEmpty(): boolean {
return !this.defaultValueExpression && this.isValueEmpty(this.defaultValue);
return !this.defaultValueExpression && this.isValueEmpty(this.defaultValue, !this.allowSpaceAsAnswer);
}
protected getDefaultRunner(runner: ExpressionRunner, expression: string): ExpressionRunner {
if (!runner && !!expression) {
Expand Down Expand Up @@ -1690,7 +1690,7 @@ export class Question extends SurveyElement<Question>
* Returns `true` if the question value is an empty string, array, or object or if it equals `undefined` or `null`.
*/
public isEmpty(): boolean {
return this.isValueEmpty(this.value);
return this.isValueEmpty(this.value, !this.allowSpaceAsAnswer);
}
public get isAnswered(): boolean {
return this.getPropertyValue("isAnswered");
Expand Down Expand Up @@ -1906,11 +1906,12 @@ export class Question extends SurveyElement<Question>
this.onCompletedAsyncValidators = null;
}
}
public allowSpaceAsAnswer: boolean;
private isValueChangedInSurvey = false;
protected allowNotifyValueChanged = true;
protected setNewValue(newValue: any): void {
if(this.isNewValueEqualsToValue(newValue)) return;
if(!this.isValueEmpty(newValue) && !this.isNewValueCorrect(newValue)) {
if(!this.isValueEmpty(newValue, !this.allowSpaceAsAnswer) && !this.isNewValueCorrect(newValue)) {
ConsoleWarnings.inCorrectQuestionValue(this.name, newValue);
return;
}
Expand All @@ -1926,7 +1927,7 @@ export class Question extends SurveyElement<Question>
}
protected isNewValueEqualsToValue(newValue: any): boolean {
const val = this.value;
if(!this.isTwoValueEquals(newValue, val)) return false;
if(!this.isTwoValueEquals(newValue, val, false, false)) return false;
const isObj = newValue === val && !!val && (Array.isArray(val) || typeof val === "object");
return !isObj;
}
Expand Down
2 changes: 1 addition & 1 deletion src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6078,7 +6078,7 @@ export class SurveyModel extends SurveyElementCore
)
return;
var oldValue = this.getValue(name);
if (this.isValueEmpty(newValue)) {
if (this.isValueEmpty(newValue, false)) {
this.deleteDataValueCore(this.valuesHash, name);
} else {
newValue = this.getUnbindValue(newValue);
Expand Down
32 changes: 32 additions & 0 deletions tests/question_texttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,35 @@ QUnit.test("CharacterCounter + settings.showMaxLengthIndicator", function(assert
ch.updateRemainingCharacterCounter("abcd", 7);
assert.equal(ch.remainingCharacterCounter, "4/7", "#4");
});

QUnit.test("Set empty text", function(assert) {
const survey = new SurveyModel({
elements: [{ type: "text", name: "q1" }]
});
const q = survey.getQuestionByName("q1");
q.value = " ";
assert.equal(q.isEmpty(), true, "question.isEmpty() #1");
assert.equal(q.value, " ", "question.value #1");
assert.deepEqual(survey.data, { q1: " " }, "survey.data #1");
q.value = "a";
assert.equal(q.isEmpty(), false, "question.isEmpty() #2");
assert.equal(q.value, "a", "question.value #2");
assert.deepEqual(survey.data, { q1: "a" }, "survey.data #2");
q.value = " a ";
assert.equal(q.isEmpty(), false, "question.isEmpty() #3");
assert.equal(q.value, " a ", "question.value #3");
assert.deepEqual(survey.data, { q1: " a " }, "survey.data #3");
q.allowSpaceAsAnswer = true;
q.value = " ";
assert.equal(q.isEmpty(), false, "question.isEmpty() #4");
assert.equal(q.value, " ", "question.value #4");
assert.deepEqual(survey.data, { q1: " " }, "survey.data #4");
q.value = "a";
assert.equal(q.isEmpty(), false, "question.isEmpty() #5");
assert.equal(q.value, "a", "question.value #5");
assert.deepEqual(survey.data, { q1: "a" }, "survey.data #5");
q.value = " a ";
assert.equal(q.isEmpty(), false, "question.isEmpty() #6");
assert.equal(q.value, " a ", "question.value #6");
assert.deepEqual(survey.data, { q1: " a " }, "survey.data #6");
});

0 comments on commit d8c60ca

Please sign in to comment.