diff --git a/app/assets/javascripts/select-kit/addon/components/category-drop.js b/app/assets/javascripts/select-kit/addon/components/category-drop.js index d39f8181254..4a9f3c5d073 100644 --- a/app/assets/javascripts/select-kit/addon/components/category-drop.js +++ b/app/assets/javascripts/select-kit/addon/components/category-drop.js @@ -139,22 +139,26 @@ export default ComboBoxComponent.extend({ async search(filter) { if (this.site.lazy_load_categories) { - const results = await Category.asyncSearch(filter, { - parentCategoryId: this.options.parentCategory?.id || -1, - includeUncategorized: this.siteSettings.allow_uncategorized_topics, - limit: 15, - }); - return this.shortcuts.concat( - results.sort((a, b) => { - if (a.parent_category_id && !b.parent_category_id) { - return 1; - } else if (!a.parent_category_id && b.parent_category_id) { - return -1; - } else { - return 0; - } + let parentCategoryId; + if (this.options.parentCategory?.id) { + parentCategoryId = this.options.parentCategory.id; + } else if (!filter) { + // Only top-level categories should be displayed by default. + // If there is a search term, the term can match any category, + // including subcategories. + parentCategoryId = -1; + } + + const results = ( + await Category.asyncSearch(filter, { + parentCategoryId, + includeUncategorized: this.siteSettings.allow_uncategorized_topics, + includeAncestors: true, + limit: 15, }) - ); + ).categories; + + return this.shortcuts.concat(results); } const opts = {