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

rapidly changing search filter #8498

Merged
merged 2 commits into from
Jul 4, 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
5 changes: 0 additions & 5 deletions src/dropdownListModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class DropdownListModel extends Base {
}
}
private itemsSettings: { skip: number, take: number, totalCount: number, items: any[] } = { skip: 0, take: 0, totalCount: 0, items: [] };
private isRunningLoadQuestionChoices = false;
protected listModel: ListModel<ItemValue>;
protected popupCssClasses = "sv-single-select-list";
protected listModelFilterStringChanged = (newValue: string) => {
Expand All @@ -58,14 +57,12 @@ export class DropdownListModel extends Base {
this.question.choices = this.itemsSettings.items;
}
private loadQuestionChoices(callbackAfterItemsLoaded?: () => void) {
this.isRunningLoadQuestionChoices = true;
this.question.survey.loadQuestionChoices({
question: this.question,
filter: this.filterString,
skip: this.itemsSettings.skip,
take: this.itemsSettings.take,
setItems: (items: Array<any>, totalCount: number) => {
this.isRunningLoadQuestionChoices = false;
this.setItems(items || [], totalCount || 0);
this.popupRecalculatePosition(this.itemsSettings.skip === this.itemsSettings.take);
if (!!callbackAfterItemsLoaded) {
Expand All @@ -76,8 +73,6 @@ export class DropdownListModel extends Base {
this.itemsSettings.skip += this.itemsSettings.take;
}
private updateQuestionChoices(callbackAfterItemsLoaded?: () => void): void {
if (this.isRunningLoadQuestionChoices) return;

const isUpdate = (this.itemsSettings.skip + 1) < this.itemsSettings.totalCount;
if (!this.itemsSettings.skip || isUpdate) {

Expand Down
60 changes: 60 additions & 0 deletions tests/questionDropdownTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2058,4 +2058,64 @@ QUnit.test("Dropdown choicesLazyLoadEnabled into matrixdynamic", function (asser

done1();
}, onChoicesLazyLoadCallbackTimeOut + callbackTimeOutDelta);
});

QUnit.test("Rapidly Changing Search Filter", (assert) => {
const newValueDebouncedInputValue = 2 * onChoicesLazyLoadCallbackTimeOut;
const oldValueDebouncedInputValue = settings.dropdownSearchDelay;
settings.dropdownSearchDelay = newValueDebouncedInputValue;

const done1 = assert.async();
const done2 = assert.async();
const done3 = assert.async();
const done4 = assert.async();
let filterValue = "";
let filterValueLog = "";
const json = {
questions: [{
"type": "dropdown",
"name": "q1",
"choicesLazyLoadEnabled": true,
}]
};
const survey = new SurveyModel(json);
survey.onChoicesLazyLoad.add((sender, option) => {
filterValueLog += (option.filter + "->");
callback(sender, option);
});

const question = <QuestionDropdownModel>survey.getAllQuestions()[0];
const dropdownListModel = question.dropdownListModel;

filterValue = "1";
dropdownListModel.inputStringRendered = filterValue;
setTimeout(() => {
assert.equal(question.choices.length, 0);
assert.equal(filterValueLog, "", "filter value 1");

filterValue += "2";
dropdownListModel.inputStringRendered = filterValue;
setTimeout(() => {
assert.equal(question.choices.length, 0);
assert.equal(filterValueLog, "", "filter value 12");

filterValue += "3";
dropdownListModel.inputStringRendered = filterValue;
setTimeout(() => {
assert.equal(filterValueLog, "123->", "filter value 123 #1");
assert.equal(question.choices.length, 0);

setTimeout(() => {
assert.equal(filterValueLog, "123->", "filter value 123 #2");
assert.equal(question.choices.length, 25);

settings.dropdownSearchDelay = oldValueDebouncedInputValue;
done4();
}, callbackTimeOutDelta + onChoicesLazyLoadCallbackTimeOut);
done3();
}, callbackTimeOutDelta + newValueDebouncedInputValue);
done2();
}, callbackTimeOutDelta);
done1();
}, callbackTimeOutDelta);
});