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

TagBox - Prevoiusly selected options disappear when searching for a new value #8494

Merged
merged 1 commit into from
Jul 2, 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
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);
});