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

Entering choice value into other comment works incorrectly if storeOt… #9622

Merged
merged 1 commit into from
Mar 20, 2025
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
18 changes: 17 additions & 1 deletion packages/survey-core/src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,27 @@ export class QuestionSelectBase extends Question {
this.isSettingComment = true;
this.otherValueCore = newValue;
if (this.isOtherSelected && !this.isRenderedValueSetting) {
this.value = this.rendredValueToData(this.renderedValue);
this.value = this.getValueOnSettingOther(newValue);
}
this.isSettingComment = false;
}
}
private getValueOnSettingOther(otherValue: string): any {
const val = this.rendredValueToData(this.renderedValue);
if(this.showCommentArea || this.getStoreOthersAsComment()) return val;
const item = ItemValue.getItemByValue(this.visibleChoices, otherValue);
if(!item || item === this.otherItem) return val;
this.otherValueCore = "";
if(!Array.isArray(val)) return otherValue;
const index = val.indexOf(this.otherItem.value);
if(index > -1) {
val.splice(index, 1);
}
if(val.indexOf(otherValue) < 0) {
val.push(otherValue);
}
return val;
}
public clearValue(keepComment?: boolean) {
super.clearValue(keepComment);
this.prevOtherValue = undefined;
Expand Down
27 changes: 27 additions & 0 deletions packages/survey-core/tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,33 @@ QUnit.test("dropdown.clearValue(true) for showCommentArea & showOtherItem, bug#8
assert.equal(q4.value, undefined, "q4.value");
assert.notOk(q4.comment, "q4.comment");
});
QUnit.test("storeOthersAsComment & entering comment equals to the value in the choice, bug#9619", (assert) => {
const survey = new SurveyModel({
elements: [
{ type: "radiogroup", name: "q1", choices: ["red", "yellow", "green"], "showOtherItem": true },
{ type: "checkbox", name: "q2", choices: ["red", "yellow", "green"], "showOtherItem": true }
],
storeOthersAsComment: false
});
const q1 = <QuestionRadiogroupModel>survey.getQuestionByName("q1");
q1.value = "other";
assert.equal(q1.isOtherSelected, true, "q1, isOther is selected");
q1.comment = "red";
assert.equal(q1.value, "red", "q1, value is red");
assert.equal(q1.selectedItem.value, "red", "q1, Make the red item seleted");
assert.equal(q1.isOtherSelected, false, "q1, isOther is not selected");
const q2 = <QuestionCheckboxModel>survey.getQuestionByName("q2");
q2.value = ["green", "other"];
assert.equal(q2.isOtherSelected, true, "q2, isOther is selected, #1");
q2.comment = "red";
assert.deepEqual(q2.value, ["green", "red"], "q2, Make the red item seleted, #1");
assert.equal(q2.isOtherSelected, false, "q2, isOther is not selected, #1");
q2.value = ["green", "other"];
assert.equal(q2.isOtherSelected, true, "q2, isOther is selected, #2");
q2.comment = "green";
assert.deepEqual(q2.value, ["green"], "q2, Make the red item seleted, #2");
assert.equal(q2.isOtherSelected, false, "q2, isOther is not selected, #2");
});
QUnit.test("valuePropertyName & complete trigger, bug#8434", (assert) => {
const survey = new SurveyModel({
"pages": [
Expand Down