Skip to content

Commit

Permalink
work for #8450 TagBox - Prevoiusly selected options disappear when se…
Browse files Browse the repository at this point in the history
…arching for a new value (#8494)

Co-authored-by: OlgaLarina <[email protected]>
  • Loading branch information
OlgaLarina and OlgaLarina authored Jul 2, 2024
1 parent d75a574 commit c7aa246
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/question_checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
this.updateSelectedItemValues();
}

return this.validateItemValues(itemValues);
const validValues = this.validateItemValues(itemValues);
return validValues;
}
public get selectedItems(): Array<ItemValue> { return this.selectedChoices; }
public get hasFilteredValue(): boolean { return !!this.valuePropertyName; }
Expand Down
2 changes: 1 addition & 1 deletion src/question_dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class QuestionDropdownModel extends QuestionSelectBase {
return super.hasUnknownValue(val, true, false);
}
protected getItemIfChoicesNotContainThisValue(value: any, text?: string): any {
if(this.choicesLazyLoadEnabled && !this.dropdownListModel.isAllDataLoaded) {
if (this.choicesLazyLoadEnabled) {
return this.createItemValue(value, text);
} else {
return super.getItemIfChoicesNotContainThisValue(value, text);
Expand Down
2 changes: 1 addition & 1 deletion src/question_tagbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class QuestionTagboxModel extends QuestionCheckboxModel {
}
}
protected getItemIfChoicesNotContainThisValue(value: any, text?: string): any {
if(this.choicesLazyLoadEnabled && !this.dropdownListModel?.isAllDataLoaded) {
if (this.choicesLazyLoadEnabled) {
return this.createItemValue(value, text);
} else {
return super.getItemIfChoicesNotContainThisValue(value, text);
Expand Down
64 changes: 63 additions & 1 deletion tests/question_tagbox_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ const onChoicesLazyLoadCallbackTimeOut = 5;
const callbackTimeOutDelta = 1;

const callback = (_, opt) => {
const total = 70;
const total = opt.filter == "888" ? 17 : 70;
setTimeout(() => {
if (opt.skip + opt.take < total) {
opt.setItems(getNumberArray(opt.skip + 1, opt.take, opt.filter), total);
Expand Down Expand Up @@ -1633,3 +1633,65 @@ QUnit.test("Create tag box from json, dropdownListModel instance", (assert) => {
const question = <QuestionTagboxModel>survey.getAllQuestions()[0];
assert.ok(question.dropdownListModel, "It is created");
});

QUnit.test("Prevoiusly selected options disappear", (assert) => {
const done1 = assert.async();
const done2 = assert.async();
const done3 = assert.async();
const done4 = assert.async();
const json = {
questions: [{
"type": "tagbox",
"name": "q1",
"defaultValue": [5],
"choicesLazyLoadEnabled": true,
}]
};
const survey = new SurveyModel(json);
survey.onChoicesLazyLoad.add(callback);
survey.onGetChoiceDisplayValue.add((sender, options) => {
if (options.question.name == "q1") {
options.setItems(options.values.map(item => ("DisplayText_" + item)));
}
});

const question = <QuestionTagboxModel>survey.getAllQuestions()[0];
const dropdownListModel = question.dropdownListModel;
const list: MultiSelectListModel = dropdownListModel.popupModel.contentComponentData.model as MultiSelectListModel;
assert.deepEqual(question.value, [5], "question value");
assert.equal(question.selectedItems.length, 1);
assert.equal(question.selectedChoices.length, 1);

question.dropdownListModel.popupModel.show();
setTimeout(() => {
dropdownListModel.inputStringRendered = "777";
assert.deepEqual(question.value, [5], "question value");
assert.equal(question.selectedItems.length, 1);
assert.equal(question.selectedChoices.length, 1);

setTimeout(() => {
list.onItemClick(list.actions[0]);
assert.deepEqual(question.value, [5, 777], "question value");
assert.equal(question.selectedItems.length, 2);
assert.equal(question.selectedChoices.length, 2);

setTimeout(() => {
dropdownListModel.inputStringRendered = "888";
assert.deepEqual(question.value, [5, 777], "question value");
assert.equal(question.selectedItems.length, 2);
assert.equal(question.selectedChoices.length, 2);

setTimeout(() => {
assert.deepEqual(question.value, [5, 777], "question value");
assert.equal(question.selectedItems.length, 2);
assert.equal(question.selectedChoices.length, 2);

done4();
}, onChoicesLazyLoadCallbackTimeOut + callbackTimeOutDelta);
done3();
}, onChoicesLazyLoadCallbackTimeOut + callbackTimeOutDelta);
done2();
}, onChoicesLazyLoadCallbackTimeOut + callbackTimeOutDelta);
done1();
}, onChoicesLazyLoadCallbackTimeOut + callbackTimeOutDelta);
});

0 comments on commit c7aa246

Please sign in to comment.