FIX: call onSelect plugin callbacks for noop rows (#6682)

FIX: call onSelect plugin callbacks for noop rows (#6682)

From 8da8f5d0f73fc4f4db2dac6609b900692efb304a Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Thu, 29 Nov 2018 15:56:19 +0100
Subject: [PATCH] FIX: call onSelect plugin callbacks for noop rows (#6682)


diff --git a/app/assets/javascripts/select-kit/components/multi-select.js.es6 b/app/assets/javascripts/select-kit/components/multi-select.js.es6
index 2dc6408..ed64094 100644
--- a/app/assets/javascripts/select-kit/components/multi-select.js.es6
+++ b/app/assets/javascripts/select-kit/components/multi-select.js.es6
@@ -265,6 +265,12 @@ export default SelectKitComponent.extend({
     }
 
     if (computedContentItem.__sk_row_type === "noopRow") {
+      applyOnSelectPluginApiCallbacks(
+        this.get("pluginApiIdentifiers"),
+        computedContentItem.value,
+        this
+      );
+
       this._boundaryActionHandler("onSelect", computedContentItem.value);
       return;
     }
diff --git a/app/assets/javascripts/select-kit/components/single-select.js.es6 b/app/assets/javascripts/select-kit/components/single-select.js.es6
index 4e18f73..66e13bf 100644
--- a/app/assets/javascripts/select-kit/components/single-select.js.es6
+++ b/app/assets/javascripts/select-kit/components/single-select.js.es6
@@ -211,6 +211,12 @@ export default SelectKitComponent.extend({
 
   select(computedContentItem) {
     if (computedContentItem.__sk_row_type === "noopRow") {
+      applyOnSelectPluginApiCallbacks(
+        this.get("pluginApiIdentifiers"),
+        computedContentItem.value,
+        this
+      );
+
       this._boundaryActionHandler("onSelect", computedContentItem.value);
       return;
     }
diff --git a/test/javascripts/components/multi-select-test.js.es6 b/test/javascripts/components/multi-select-test.js.es6
index ea13a61..81c07bd 100644
--- a/test/javascripts/components/multi-select-test.js.es6
+++ b/test/javascripts/components/multi-select-test.js.es6
@@ -1,4 +1,7 @@
 import componentTest from "helpers/component-test";
+import { withPluginApi } from "discourse/lib/plugin-api";
+import { clearCallbacks } from "select-kit/mixins/plugin-api";
+
 moduleForComponent("multi-select", {
   integration: true,
   beforeEach: function() {
@@ -317,3 +320,39 @@ componentTest("with forceEscape", {
     );
   }
 });
+
+componentTest("support modifying on select behavior through plugin api", {
+  template:
+    '<span class="on-select-test"></span>{{multi-select content=content}}',
+
+  beforeEach() {
+    withPluginApi("0.8.13", api => {
+      api.modifySelectKit("select-kit").onSelect((context, value) => {
+        find(".on-select-test").html(value);
+      });
+    });
+
+    this.set("content", [
+      { id: "1", name: "robin" },
+      { id: "2", name: "arpit", __sk_row_type: "noopRow" }
+    ]);
+  },
+
+  async test(assert) {
+    await this.get("subject").expand();
+    await this.get("subject").selectRowByValue(1);
+
+    assert.equal(find(".on-select-test").html(), "1");
+
+    await this.get("subject").expand();
+    await this.get("subject").selectRowByValue(2);
+
+    assert.equal(
+      find(".on-select-test").html(),
+      "2",
+      "it calls onSelect for noopRows"
+    );
+
+    clearCallbacks();
+  }
+});
diff --git a/test/javascripts/components/single-select-test.js.es6 b/test/javascripts/components/single-select-test.js.es6
index a56157b..2b65db8 100644
--- a/test/javascripts/components/single-select-test.js.es6
+++ b/test/javascripts/components/single-select-test.js.es6
@@ -488,7 +488,10 @@ componentTest("support modifying on select behavior through plugin api", {
       });
     });
 
-    this.set("content", [{ id: "1", name: "robin" }]);
+    this.set("content", [
+      { id: "1", name: "robin" },
+      { id: "2", name: "arpit", __sk_row_type: "noopRow" }
+    ]);
   },
 
   async test(assert) {
@@ -497,6 +500,15 @@ componentTest("support modifying on select behavior through plugin api", {
 
     assert.equal(find(".on-select-test").html(), "1");
 
+    await this.get("subject").expand();
+    await this.get("subject").selectRowByValue(2);
+
+    assert.equal(
+      find(".on-select-test").html(),
+      "2",
+      "it calls onSelect for noopRows"
+    );
+
     clearCallbacks();
   }
 });

GitHub

1 Like