DEV: Add default results option to user-chooser (#19521)

defaultSearchResults search option can be used to show a list of results
by default, before any search ran, when the filter is empty.
This commit is contained in:
Bianca Nenciu 2022-12-21 14:47:47 +02:00 committed by GitHub
parent d2e24f9569
commit 8f9933d1dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -28,4 +28,18 @@ module("Integration | Component | select-kit/user-chooser", function (hooks) {
await this.subject.deselectItemByValue("bob");
assert.strictEqual(this.subject.header().name(), "martin");
});
test("can display default search results", async function (assert) {
this.set("options", {
customSearchOptions: {
defaultSearchResults: [{ username: "foo" }, { username: "bar" }],
},
});
await render(hbs`<UserChooser @options={{this.options}} />`);
await this.subject.expand();
assert.strictEqual(this.subject.rowByIndex(0).value(), "foo");
assert.strictEqual(this.subject.rowByIndex(1).value(), "bar");
});
});

View File

@ -57,6 +57,9 @@ export default MultiSelectComponent.extend({
filter = filter || "";
filter = filter.replace(/^@/, "");
const options = this.selectKit.options;
if (filter === "" && options?.customSearchOptions?.defaultSearchResults) {
return Promise.resolve(options.customSearchOptions.defaultSearchResults);
}
// prevents doing ajax request for nothing
const skippedSearch = skipSearch(filter, options.allowEmails);