A platform for community discussion. Free, open, simple.
DEV: Support action rows in select-kit (#40836)
chapoi2f9f2c006623
diff --git a/frontend/discourse/select-kit/components/category-chooser.js b/frontend/discourse/select-kit/components/category-chooser.jsindex 555c4080a47..6905d61d7d5 100644--- a/frontend/discourse/select-kit/components/category-chooser.js+++ b/frontend/discourse/select-kit/components/category-chooser.js@@ -6,6 +6,7 @@ import Category from "discourse/models/category";import PermissionType from "discourse/models/permission-type";import CategoryRow from "discourse/select-kit/components/category-row";import ComboBoxComponent from "discourse/select-kit/components/combo-box";+import SelectKitRow from "discourse/select-kit/components/select-kit/select-kit-row";import { categoryBadgeHTML } from "discourse/ui-kit/helpers/d-category-link";import { i18n } from "discourse-i18n";import { pluginApiIdentifiers, selectKitOptions } from "./select-kit";@@ -51,7 +52,11 @@ export default class CategoryChooser extends ComboBoxComponent {return this.siteSettings.fixed_category_positions_on_create;}- modifyComponentForRow() {+ modifyComponentForRow(collection, item) {+ if (typeof item?.onSelect === "function") {+ return SelectKitRow;+ }+return CategoryRow;}diff --git a/frontend/discourse/select-kit/components/mini-tag-chooser.js b/frontend/discourse/select-kit/components/mini-tag-chooser.jsindex 3d2c2797f40..c6f9dbf282c 100644--- a/frontend/discourse/select-kit/components/mini-tag-chooser.js+++ b/frontend/discourse/select-kit/components/mini-tag-chooser.js@@ -69,6 +69,10 @@ export default class MiniTagChooser extends MultiSelectComponent {}modifyComponentForRow(collection, item) {+ if (typeof item?.onSelect === "function") {+ return SelectKitRow;+ }+if (this.getValue(item) === this.selectKit.filter && !item.count) {return SelectKitRow;}diff --git a/frontend/discourse/select-kit/components/multi-select.gjs b/frontend/discourse/select-kit/components/multi-select.gjsindex 3e0b15e7e59..0651ebd3f33 100644--- a/frontend/discourse/select-kit/components/multi-select.gjs+++ b/frontend/discourse/select-kit/components/multi-select.gjs@@ -91,6 +91,11 @@ export default class MultiSelect extends SelectKitComponent {}select(value, item) {+ if (typeof item?.onSelect === "function") {+ item.onSelect(this.selectKit, item);+ return;+ }+if (this.selectKit.hasSelection && this.selectKit.options.maximum === 1) {const newItem = item || this.defaultItem(value, value);this.selectKit.change(makeArray(value), makeArray(newItem));diff --git a/frontend/discourse/select-kit/components/select-kit.js b/frontend/discourse/select-kit/components/select-kit.jsindex 4d77fce7262..2985aee0c8b 100644--- a/frontend/discourse/select-kit/components/select-kit.js+++ b/frontend/discourse/select-kit/components/select-kit.js@@ -916,6 +916,11 @@ export default class SelectKit extends Component {}select(value, item) {+ if (typeof item?.onSelect === "function") {+ item.onSelect(this.selectKit, item);+ return;+ }+if (!isPresent(value)) {this._onClearSelection();} else {diff --git a/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs b/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjsindex e5911ac7aaa..39f4abf9ae1 100644--- a/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs+++ b/frontend/discourse/select-kit/components/select-kit/select-kit-row.gjs@@ -42,10 +42,18 @@ import { i18n } from "discourse-i18n";export default class SelectKitRow extends Component {tabIndex = 0;index = 0;- role = "menuitemradio";@tracked _langOverride;+ get isActionRow() {+ return typeof this.item?.onSelect === "function";+ }++ @computed("item.onSelect")+ get role() {+ return this.isActionRow ? "menuitem" : "menuitemradio";+ }+@computed("item.lang")get lang() {if (this._langOverride !== undefined) {@@ -86,8 +94,11 @@ export default class SelectKitRow extends Component {return guidFor(this.item);}- @computed("isSelected")+ @computed("isSelected", "item.onSelect")get ariaChecked() {+ if (this.isActionRow) {+ return undefined;+ }return this.isSelected ? "true" : "false";}diff --git a/frontend/discourse/tests/integration/components/select-kit/api-test.gjs b/frontend/discourse/tests/integration/components/select-kit/api-test.gjsindex 3ccd757ba10..0c380654922 100644--- a/frontend/discourse/tests/integration/components/select-kit/api-test.gjs+++ b/frontend/discourse/tests/integration/components/select-kit/api-test.gjs@@ -129,6 +129,42 @@ module("Integration | Component | SelectKit | Api", function (hooks) {assert.dom("#test").hasText("foo");});+ test("an item with onSelect acts as an action row instead of being selected", async function (assert) {+ setDefaultState(this, "foo", { content: DEFAULT_CONTENT });++ let actioned = false;+ withPluginApi((api) => {+ api.modifySelectKit("combo-box").prependContent(() => {+ return {+ id: "action",+ name: "Do something",+ onSelect: () => {+ actioned = true;+ },+ };+ });+ });++ await render(+ <template>+ <ComboBox+ @value={{this.value}}+ @content={{this.content}}+ @onChange={{this.onChange}}+ />+ </template>+ );+ await this.comboBox.expand();+ await this.comboBox.selectRowByValue("action");++ assert.true(actioned, "the row's onSelect callback runs");+ assert.strictEqual(this.value, "foo", "the selected value is unchanged");+ assert.true(this.comboBox.isExpanded(), "the dropdown stays open");+ assert+ .dom(".select-kit-row[data-value='action']")+ .hasAttribute("role", "menuitem");+ });+test("modifySelectKit(identifier).replaceContent", async function (assert) {setDefaultState(this, null, { content: DEFAULT_CONTENT });