A platform for community discussion. Free, open, simple.
FIX: Limit displayed groups in `<GroupChooser />` to 100 groups (#31288)
Osama Sayeghd0498c9e1d68
diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/group-chooser-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/select-kit/group-chooser-test.gjsnew file mode 100644index 00000000000..5cea5454cab--- /dev/null+++ b/app/assets/javascripts/discourse/tests/integration/components/select-kit/group-chooser-test.gjs@@ -0,0 +1,88 @@+import { hash } from "@ember/helper";+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import selectKit from "discourse/tests/helpers/select-kit-helper";+import GroupChooser from "select-kit/components/group-chooser";++module("Integration | Component | select-kit/group-chooser", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.subject = selectKit();+ });++ test("limiting the displayed groups", async function (assert) {+ const content = [+ {+ id: 1,+ name: "A",+ },+ {+ id: 2,+ name: "AB",+ },+ {+ id: 3,+ name: "ABC",+ },+ ];+ await render(<template>+ <GroupChooser+ @content={{content}}+ @options={{hash displayedGroupsLimit=1}}+ />+ </template>);++ await this.subject.expand();++ assert.strictEqual(+ this.subject.rows().length,+ 1,+ "only 1 group is displayed"+ );+ assert.strictEqual(+ this.subject.rowByIndex(0).name(),+ "A",+ "the first group in the list is displayed"+ );++ assert+ .dom(this.subject.el().querySelector(".filter-for-more"))+ .exists("has indicator that there are more groups");++ await this.subject.fillInFilter("AB");++ assert.strictEqual(+ this.subject.rows().length,+ 1,+ "only 1 group is displayed"+ );+ assert.strictEqual(+ this.subject.rowByIndex(0).name(),+ "AB",+ "the first group that matches the filter in the list is displayed"+ );+ assert+ .dom(this.subject.el().querySelector(".filter-for-more"))+ .exists("has indicator that there are more groups matching the filter");++ await this.subject.fillInFilter("C");++ assert.strictEqual(+ this.subject.rows().length,+ 1,+ "only 1 group is displayed"+ );+ assert.strictEqual(+ this.subject.rowByIndex(0).name(),+ "ABC",+ "the first group that matches the filter in the list is displayed"+ );+ assert+ .dom(this.subject.el().querySelector(".filter-for-more"))+ .doesNotExist(+ "doesn't have an indicator when there are no more matching elements"+ );+ });+});diff --git a/app/assets/javascripts/select-kit/addon/components/group-chooser.js b/app/assets/javascripts/select-kit/addon/components/group-chooser.jsindex 904ce932700..975abbad871 100644--- a/app/assets/javascripts/select-kit/addon/components/group-chooser.js+++ b/app/assets/javascripts/select-kit/addon/components/group-chooser.js@@ -1,13 +1,52 @@import { classNames } from "@ember-decorators/component";+import FilterForMore from "select-kit/components/filter-for-more";import MultiSelectComponent from "select-kit/components/multi-select";import {+ MAIN_COLLECTION,pluginApiIdentifiers,selectKitOptions,} from "select-kit/components/select-kit";+const FILTER_FOR_MORE_GROUPS_COLLECTION = "MORE_GROUPS_COLLECTION";+@classNames("group-chooser")@selectKitOptions({allowAny: false,+ displayedGroupsLimit: 100,})@pluginApiIdentifiers("group-chooser")-export default class GroupChooser extends MultiSelectComponent {}+export default class GroupChooser extends MultiSelectComponent {+ init() {+ super.init(...arguments);++ this.insertAfterCollection(+ MAIN_COLLECTION,+ FILTER_FOR_MORE_GROUPS_COLLECTION+ );+ }++ modifyComponentForCollection(identifier) {+ if (identifier === FILTER_FOR_MORE_GROUPS_COLLECTION) {+ return FilterForMore;+ }+ }++ modifyContent(content) {+ const limit = this.selectKit.options.displayedGroupsLimit;+ if (content.length > limit) {+ this.showFilterForMore = true;+ content = content.slice(0, limit);+ } else {+ this.showFilterForMore = false;+ }+ return content;+ }++ modifyContentForCollection(identifier) {+ if (identifier === FILTER_FOR_MORE_GROUPS_COLLECTION) {+ return {+ shouldShowMoreTip: this.showFilterForMore,+ };+ }+ }+}