FEATURE: Improve "+ subcategories" option (#26086)

This commit improves the "+ X subcategories" option that shows sometimes
in the category selector. It used to show when there was a single match,
but now it also shows up on exact matches even though there are multiple
results.

It also makes it work when lazy_load_categories is enabled by searching
the subcategories before rendering them.
This commit is contained in:
Bianca Nenciu 2024-03-07 20:46:45 +02:00 committed by GitHub
parent 4b813427e7
commit 2a6e4a7ba0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 15 deletions

View File

@ -49,8 +49,8 @@ module(
"Parent Category × 95" "Parent Category × 95"
); );
assert.equal( assert.equal(
this.subject.rowByIndex(1).el().innerText.replace("\n", " "), this.subject.rowByIndex(1).el().innerText.replaceAll("\n", " "),
"Parent Category + 2 subcategories" "Parent Category × 95 + 2 subcategories"
); );
}); });
} }

View File

@ -111,6 +111,9 @@ export default class CategoryRow extends Component {
this.allowUncategorizedTopics || this.allowUncategorized, this.allowUncategorizedTopics || this.allowUncategorized,
hideParent: !!this.parentCategory, hideParent: !!this.parentCategory,
topicCount: this.topicCount, topicCount: this.topicCount,
subcategoryCount: this.args.item?.categories
? this.args.item.categories.length - 1
: 0,
}) })
); );
} }

View File

@ -1,6 +1,5 @@
import EmberObject, { computed } from "@ember/object"; import EmberObject, { computed } from "@ember/object";
import { mapBy } from "@ember/object/computed"; import { mapBy } from "@ember/object/computed";
import { categoryBadgeHTML } from "discourse/helpers/category-link";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import { makeArray } from "discourse-common/lib/helpers"; import { makeArray } from "discourse-common/lib/helpers";
import CategoryRow from "select-kit/components/category-row"; import CategoryRow from "select-kit/components/category-row";
@ -70,19 +69,30 @@ export default MultiSelectComponent.extend({
categories = this._super(filter); categories = this._super(filter);
} }
// If there is a single match and it has subcategories, add a row for // If there is a single match or an exact match and it has subcategories,
// selecting all // add a row for selecting all subcategories
if (categories.length === 1) { if (
const descendants = categories[0].descendants; categories.length === 1 ||
if (descendants.length > 1) { (categories.length > 0 && categories[0].name.localeCompare(filter) === 0)
categories.push( ) {
// Descendants may not be loaded if lazy loading is enabled. Search for
// subcategories will make sure these are loaded
if (this.site.lazy_load_categories) {
await Category.asyncSearch("", {
parentCategoryId: categories[0].id,
});
}
if (categories[0].descendants.length > 1) {
categories.splice(
1,
0,
EmberObject.create({ EmberObject.create({
label: categoryBadgeHTML(descendants[0], { // This is just a hack to ensure the IDs are unique, but ensure
link: false, // that parseInt still returns a valid ID in order to generate the
recursive: true, // label
subcategoryCount: descendants.length - 1, id: `${categories[0].id}+subcategories`,
}), categories: categories[0].descendants,
categories: [...descendants],
}) })
); );
} }