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

Add allowSpaceAsAnswer (false by default) non-serializable property i… #6679

Merged
merged 1 commit into from
Aug 9, 2023
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
11 changes: 6 additions & 5 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,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 @@ -1574,7 +1574,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 @@ -1686,7 +1686,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 @@ -1902,11 +1902,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 @@ -1922,7 +1923,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");
});