Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: allows to set style/icon on chat approval buttons (#42630)

Joffrey JAFFEUXc3e143daf18a
diff --git a/frontend/discourse/app/form-kit/components/fk/control/icon.gjs b/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
index d26e5ed3182..cd6fbfb00a8 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
@@ -15,6 +15,7 @@ export default class FKControlIcon extends FKBaseControl {
@value={{@field.value}}
@onChange={{this.handleInput}}
@disabled={{@field.disabled}}
+ @allowClear={{@allowClear}}
@showCaret={{true}}
@showSelectedName={{true}}
class="form-kit__control-icon"
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/icon-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/icon-test.gjs
index 637376efb05..9a814175b8e 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/icon-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/icon-test.gjs
@@ -37,6 +37,31 @@ module("Integration | Component | FormKit | Controls | Icon", function (hooks) {
assert.form().field("foo").hasValue("pencil");
});
+ test("forwards allowClear and clears the field value", async function (assert) {
+ let data = { foo: "pencil" };
+ const mutateData = (value) => (data = value);
+
+ await render(
+ <template>
+ <Form @onSubmit={{mutateData}} @data={{data}} as |form|>
+ <form.Field @type="icon" @name="foo" @title="Foo" as |field|>
+ <field.Control @allowClear={{true}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom(".d-icon-grid-picker__clear")
+ .exists("the clear action is forwarded to the icon picker");
+
+ await click(".d-icon-grid-picker__clear");
+ await formKit().submit();
+
+ assert.strictEqual(data.foo, null, "the icon value is cleared");
+ assert.form().field("foo").hasNoValue("the cleared field has no value");
+ });
+
test("when disabled", async function (assert) {
await render(
<template>
diff --git a/plugins/chat/app/serializers/chat/blocks/elements/button_serializer.rb b/plugins/chat/app/serializers/chat/blocks/elements/button_serializer.rb
index 6731330b39a..0c7fa7babb3 100644
--- a/plugins/chat/app/serializers/chat/blocks/elements/button_serializer.rb
+++ b/plugins/chat/app/serializers/chat/blocks/elements/button_serializer.rb
@@ -4,7 +4,7 @@ module Chat
module Blocks
module Elements
class ButtonSerializer < ApplicationSerializer
- attributes :action_id, :type, :text, :style
+ attributes :action_id, :type, :text, :style, :icon
def action_id
object["action_id"]
@@ -18,6 +18,10 @@ module Chat
object["style"]
end
+ def icon
+ object["icon"]
+ end
+
def text
Chat::Blocks::Elements::TextSerializer.new(object["text"], root: false).as_json
end
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/button.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/button.gjs
index f3776834ae9..c9a93de8ecd 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/button.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/button.gjs
@@ -22,6 +22,7 @@ export default class Button extends Component {
<template>
<DButton
@isLoading={{this.interacting}}
+ @icon={{@definition.icon}}
@translatedLabel={{dReplaceEmoji @definition.text.text}}
@action={{this.createInteraction}}
id={{@definition.action_id}}
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index d3b2e4430af..818843b73ba 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -916,6 +916,17 @@ en:
value: "Value"
value_description: "The value returned by the workflow when this button is clicked."
value_placeholder: "Returned value"
+ style: "Style"
+ style_description: "Choose a button style or leave blank to preserve the standard chat button appearance."
+ style_none: "No style"
+ style_default: "Default"
+ style_primary: "Primary"
+ style_danger: "Danger"
+ style_success: "Success"
+ style_flat: "Flat"
+ style_transparent: "Transparent"
+ icon: "Icon"
+ icon_description: "Choose an icon to show before the button label."
approve_label: "Approve button label"
approve_label_description: "Defaults to \"Approve\" if left blank."
deny_label: "Deny button label"
diff --git a/plugins/chat/config/locales/server.en.yml b/plugins/chat/config/locales/server.en.yml
index dfabf42f4c3..fbfa3edfaad 100644
--- a/plugins/chat/config/locales/server.en.yml
+++ b/plugins/chat/config/locales/server.en.yml
@@ -269,6 +269,8 @@ en:
button_label_too_long: "Chat approval button %{position} label must be %{max} characters or fewer."
button_value_required: "Chat approval button %{position} requires a value."
button_value_too_long: "Chat approval button %{position} value must be %{max} characters or fewer."
+ button_style_invalid: "Chat approval button %{position} style must be one of: %{styles}."
+ button_icon_too_long: "Chat approval button %{position} icon must be %{max} characters or fewer."
reviewable_score_types:
needs_review:
diff --git a/plugins/chat/lib/chat/schemas/message_blocks.rb b/plugins/chat/lib/chat/schemas/message_blocks.rb
index 672b37f9452..ad6b3466499 100644
--- a/plugins/chat/lib/chat/schemas/message_blocks.rb
+++ b/plugins/chat/lib/chat/schemas/message_blocks.rb
@@ -2,6 +2,9 @@
module Chat
module Schemas
+ BUTTON_STYLES = %w[default primary danger success flat transparent].freeze
+ BUTTON_ICON_MAX_LENGTH = 255
+
Text = {
type: "object",
properties: {
@@ -40,7 +43,11 @@ module Chat
},
style: {
type: "string",
- enum: %w[default primary danger],
+ enum: BUTTON_STYLES,
+ },
+ icon: {
+ type: "string",
+ maxLength: BUTTON_ICON_MAX_LENGTH,
},
},
required: %w[schema_version type text],
diff --git a/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v2.rb b/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v2.rb
index 46a6c1fd9ff..38241140f43 100644
--- a/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v2.rb
+++ b/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v2.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require_relative "../../../chat/schemas/message_blocks"
+
if defined?(DiscourseWorkflows)
module DiscourseWorkflows
module Nodes
@@ -8,6 +10,8 @@ if defined?(DiscourseWorkflows)
MAX_BUTTONS = 10
MAX_LABEL_LENGTH = 75
MAX_VALUE_LENGTH = 2000
+ MAX_ICON_LENGTH = Chat::Schemas::BUTTON_ICON_MAX_LENGTH
+ BUTTON_STYLES = Chat::Schemas::BUTTON_STYLES
TIMEOUT_ACTIONS = %w[continue fail].freeze
ACTION_IDS = Array.new(MAX_BUTTONS) { |index| "button_#{index}" }.freeze
@@ -121,6 +125,20 @@ if defined?(DiscourseWorkflows)
required: true,
no_data_expression: true,
},
+ style: {
+ type: :options,
+ required: false,
+ options: BUTTON_STYLES,
+ no_data_expression: true,
+ control_options: {
+ none: "discourse_workflows.chat_approval.style_none",
+ },
+ },
+ icon: {
+ type: :icon,
+ required: false,
+ no_data_expression: true,
+ },
},
},
],
@@ -251,6 +269,8 @@ if defined?(DiscourseWorkflows)
rows.each_with_index do |row, index|
label = row.is_a?(Hash) ? row["label"].to_s : ""
value = row.is_a?(Hash) ? row["value"].to_s : ""
+ style = row.is_a?(Hash) ? row["style"].to_s : ""
+ icon = row.is_a?(Hash) ? row["icon"].to_s : ""
position = index + 1
if label.blank?
@@ -278,6 +298,22 @@ if defined?(DiscourseWorkflows)
max: MAX_VALUE_LENGTH,
)
end
+
+ if style.present? && BUTTON_STYLES.exclude?(style)
+ errors << I18n.t(
+ "discourse_workflows.errors.chat_approval.button_style_invalid",
+ position: position,
+ styles: BUTTON_STYLES.join(", "),
+ )
+ end
+
+ if icon.length > MAX_ICON_LENGTH
+ errors << I18n.t(
+ "discourse_workflows.errors.chat_approval.button_icon_too_long",
+ position: position,
+ max: MAX_ICON_LENGTH,
+ )
+ end
end
errors
@@ -313,11 +349,15 @@ if defined?(DiscourseWorkflows)
"type" => "actions",
"elements" =>
buttons.map.with_index do |button, index|
- button_block(
- button["label"].to_s,
- exec_ctx.resume_action_id(ACTION_IDS[index]),
- button["value"].to_s,
- )
+ block =
+ button_block(
+ button["label"].to_s,
+ exec_ctx.resume_action_id(ACTION_IDS[index]),
+ button["value"].to_s,
+ )
+ block["style"] = button["style"].to_s if button["style"].present?
+ block["icon"] = button["icon"].to_s if button["icon"].present?
+ block
end,
},
]
diff --git a/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v2_spec.rb b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v2_spec.rb
index d162bb52cf1..feadb250921 100644
--- a/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v2_spec.rb
+++ b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v2_spec.rb
@@ -19,8 +19,9 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
).to eq(described_class)
end
- it "declares editable label and value rows with Chat's collection limits" do
+ it "declares editable button rows with Chat's collection limits" do
buttons = described_class.property_schema.fetch(:buttons)
+ values = buttons.dig(:options, 0, :values)
expect(buttons).to include(
type: :fixed_collection,
@@ -29,16 +30,26 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
default: described_class.default_buttons,
)
expect(buttons.dig(:type_options, :sortable)).to eq(true)
- expect(buttons.dig(:options, 0, :values, :label)).to include(
+ expect(values.fetch(:label)).to include(
type: :string,
required: true,
no_data_expression: true,
)
- expect(buttons.dig(:options, 0, :values, :value)).to include(
+ expect(values.fetch(:value)).to include(
type: :string,
required: true,
no_data_expression: true,
)
+ expect(values.fetch(:style)).to eq(
+ type: :options,
+ required: false,
+ options: Chat::Schemas::BUTTON_STYLES,
+ no_data_expression: true,
+ control_options: {
+ none: "discourse_workflows.chat_approval.style_none",
+ },
+ )
+ expect(values.fetch(:icon)).to eq(type: :icon, required: false, no_data_expression: true)
expect(described_class.property_schema.fetch(:timeout_action)).to include(
options: %w[continue fail],
default: "continue",
@@ -102,8 +113,15 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
errors
end
- it "accepts one through ten buttons at the label and value length boundaries" do
- one_button = [{ "label" => "L" * 75, "value" => "v" * 2000 }]
+ it "accepts one through ten buttons at all length boundaries" do
+ one_button = [
+ {
+ "label" => "L" * 75,
+ "value" => "v" * 2000,
+ "style" => "transparent",
+ "icon" => "i" * 255,
+ },
+ ]
ten_buttons =
Array.new(10) { |index| { "label" => "Button #{index}", "value" => index.to_s } }
@@ -120,6 +138,15 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
expect(errors_for(rows)).to be_empty
end
+ it "accepts every supported style" do
+ rows =
+ Chat::Schemas::BUTTON_STYLES.map do |style|
+ { "label" => style, "value" => style, "style" => style }
+ end
+
+ expect(errors_for(rows)).to be_empty
+ end
+
it "rejects empty, excessive, and oversized button collections", :aggregate_failures do
valid_row = { "label" => "Choose", "value" => "choice" }
@@ -149,6 +176,24 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
expect(errors_for([{ "label" => "Choose", "value" => "" }])[:base]).to include(
I18n.t("discourse_workflows.errors.chat_approval.button_value_required", position: 1),
)
+ expect(
+ errors_for([{ "label" => "Choose", "value" => "choice", "style" => "unknown" }])[:base],
+ ).to include(
+ I18n.t(
+ "discourse_workflows.errors.chat_approval.button_style_invalid",
+ position: 1,
+ styles: Chat::Schemas::BUTTON_STYLES.join(", "),
+ ),
+ )
+ expect(
+ errors_for([{ "label" => "Choose", "value" => "choice", "icon" => "i" * 256 }])[:base],
+ ).to include(
+ I18n.t(
+ "discourse_workflows.errors.chat_approval.button_icon_too_long",
+ position: 1,
+ max: 255,
+ ),
+ )
end
end
@@ -203,14 +248,25 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
expect(buttons.map { |button| [button.dig("text", "text"), button["value"]] }).to eq(
[%w[Approve approve], %w[Deny deny]],
)
+ expect(buttons.map { |button| button.slice("style", "icon") }).to eq([{}, {}])
end
- it "creates indexed signed actions for custom, duplicate, punctuation, and long values" do
+ it "creates indexed signed actions and emits only nonblank presentation attributes" do
long_value = "v" * 2000
rows = [
- { "label" => "Review later", "value" => "review:later" },
- { "label" => "Same outcome", "value" => "review:later" },
- { "label" => "Long outcome", "value" => long_value },
+ {
+ "label" => "Review later",
+ "value" => "review:later",
+ "style" => "success",
+ "icon" => "check",
+ },
+ {
+ "label" => "Same outcome",
+ "value" => "review:later",
+ "style" => "default",
+ "icon" => "",
+ },
+ { "label" => "Long outcome", "value" => long_value, "style" => "", "icon" => nil },
]
config = {
"message" => "Choose",
@@ -231,12 +287,36 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V2 do
expect(buttons.map { |button| button["value"] }).to eq(
["review:later", "review:later", long_value],
)
+ expect(buttons.map { |button| button.slice("style", "icon") }).to eq(
+ [{ "style" => "success", "icon" => "check" }, { "style" => "default" }, {}],
+ )
expect(payloads.map { |payload| payload["action"] }).to eq(%w[button_0 button_1 button_2])
expect(buttons.map { |button| button["action_id"] }).to all(
satisfy { |action_id| action_id.length <= 255 && !action_id.include?(long_value) },
)
end
+ it "rejects unsupported presentation attributes before sending a message" do
+ config = {
+ "message" => "Choose",
+ "buttons" => {
+ "values" => [{ "label" => "Choose", "value" => "choice", "style" => "unknown" }],
+ },
+ "channel_id" => channel.id.to_s,
+ }
+ instance = described_class.new(parameters: config)
+
+ expect { instance.execute(build_v2_exec_ctx(config)) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t(
+ "discourse_workflows.errors.chat_approval.button_style_invalid",
+ position: 1,
+ styles: Chat::Schemas::BUTTON_STYLES.join(", "),
+ ),
+ )
+ expect(channel.chat_messages).to be_empty
+ end
+
it "treats an explicitly empty button collection as invalid" do
config = {
"message" => "Choose",
diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb
index 69acec14923..659e88ca492 100644
--- a/plugins/chat/spec/models/chat/message_spec.rb
+++ b/plugins/chat/spec/models/chat/message_spec.rb
@@ -75,8 +75,36 @@ describe Chat::Message do
let(:blocks) { nil }
+ def button_blocks(style: nil, icon: nil)
+ button = { type: "button", text: { text: "Foo", type: "plain_text" } }
+ button[:style] = style if style
+ button[:icon] = icon if icon
+
+ [{ type: "actions", elements: [button] }]
+ end
+
it { is_expected.to validate_length_of(:cooked).is_at_most(20_000) }
+ context "when button presentation is provided" do
+ it "allows every supported style and an icon at the maximum length" do
+ aggregate_failures do
+ Chat::Schemas::BUTTON_STYLES.each do |style|
+ expect(message).to allow_value(
+ button_blocks(style:, icon: "a" * Chat::Schemas::BUTTON_ICON_MAX_LENGTH),
+ ).for(:blocks)
+ end
+ end
+ end
+
+ it "rejects an unsupported style" do
+ expect(message).not_to allow_value(button_blocks(style: "warning")).for(:blocks)
+ end
+
+ it "rejects an icon longer than 255 characters" do
+ expect(message).not_to allow_value(button_blocks(icon: "a" * 256)).for(:blocks)
+ end
+ end
+
context "when blocks format is invalid" do
let(:blocks) { [{ type: "actions", elements: [{ type: "buttoxn" }] }] }
diff --git a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
index 5480ec1e933..b31669700e7 100644
--- a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
+++ b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
@@ -19,6 +19,43 @@ describe Chat::MessageSerializer do
end
end
+ describe "#blocks" do
+ it "serializes button presentation without its private value" do
+ message_1.update!(
+ blocks: [
+ {
+ type: "actions",
+ elements: [
+ {
+ type: "button",
+ text: {
+ type: "plain_text",
+ text: "Continue",
+ },
+ style: "success",
+ icon: "check",
+ value: "private-value",
+ },
+ ],
+ },
+ ],
+ )
+
+ serialized_button = serializer.as_json.dig(:blocks, 0, :elements, 0)
+
+ expect(serialized_button).to eq(
+ action_id: message_1.blocks.dig(0, "elements", 0, "action_id"),
+ type: "button",
+ text: {
+ text: "Continue",
+ type: "plain_text",
+ },
+ style: "success",
+ icon: "check",
+ )
+ end
+ end
+
describe "#reactions" do
fab!(:custom_emoji) { CustomEmoji.create!(name: "trout", upload: Fabricate(:upload)) }
fab!(:reaction_1) do
diff --git a/plugins/chat/spec/serializer/chat/message_interaction_serializer_spec.rb b/plugins/chat/spec/serializer/chat/message_interaction_serializer_spec.rb
index baf0bee3f49..80559a17a8c 100644
--- a/plugins/chat/spec/serializer/chat/message_interaction_serializer_spec.rb
+++ b/plugins/chat/spec/serializer/chat/message_interaction_serializer_spec.rb
@@ -22,7 +22,17 @@ RSpec.describe Chat::MessageInteractionSerializer do
{
type: "actions",
elements: [
- { type: "button", text: { type: "plain_text", text: "Like" }, action_id: "like" },
+ {
+ type: "button",
+ text: {
+ type: "plain_text",
+ text: "Like",
+ },
+ action_id: "like",
+ style: "success",
+ icon: "check",
+ value: "private-value",
+ },
],
},
],
@@ -48,7 +58,11 @@ RSpec.describe Chat::MessageInteractionSerializer do
let(:channel) { Fabricate(:chat_channel) }
it "serializes the interaction" do
- expect(serializer.as_json).to match_response_schema("message_interaction")
+ json = serializer.as_json
+
+ expect(json).to match_response_schema("message_interaction")
+ expect(json[:action]).to include("style" => "success", "icon" => "check")
+ expect(json[:action]).not_to have_key("value")
end
end
end
diff --git a/plugins/chat/spec/support/api/schemas/message_interaction.json b/plugins/chat/spec/support/api/schemas/message_interaction.json
index f43525265ca..2a9f87332b3 100644
--- a/plugins/chat/spec/support/api/schemas/message_interaction.json
+++ b/plugins/chat/spec/support/api/schemas/message_interaction.json
@@ -19,9 +19,24 @@
"action_id": {
"type": "string"
},
+ "icon": {
+ "type": "string",
+ "maxLength": 255
+ },
"schema_version": {
"type": "integer"
},
+ "style": {
+ "type": "string",
+ "enum": [
+ "default",
+ "primary",
+ "danger",
+ "success",
+ "flat",
+ "transparent"
+ ]
+ },
"text": {
"type": "object",
"required": [
diff --git a/plugins/chat/test/javascripts/components/chat-message/blocks/elements/button-test.gjs b/plugins/chat/test/javascripts/components/chat-message/blocks/elements/button-test.gjs
new file mode 100644
index 00000000000..218a7efd657
--- /dev/null
+++ b/plugins/chat/test/javascripts/components/chat-message/blocks/elements/button-test.gjs
@@ -0,0 +1,79 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import Button from "discourse/plugins/chat/discourse/components/chat-message/blocks/elements/button";
+
+module(
+ "Component | ChatMessage | Blocks | Elements | Button",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("renders each supported button style", async function (assert) {
+ const styles = [
+ "default",
+ "primary",
+ "danger",
+ "success",
+ "flat",
+ "transparent",
+ ];
+ this.definitions = styles.map((style) => ({
+ action_id: style,
+ style,
+ text: { text: style },
+ }));
+
+ await render(
+ <template>
+ {{#each this.definitions as |definition|}}
+ <Button @definition={{definition}} />
+ {{/each}}
+ </template>
+ );
+
+ for (const style of styles) {
+ assert
+ .dom(`#${style}`)
+ .hasClass(`btn-${style}`, `${style} uses the matching button class`);
+ }
+ });
+
+ test("renders an icon with the button label", async function (assert) {
+ this.definition = {
+ action_id: "approve",
+ icon: "check",
+ text: { text: "Approve" },
+ };
+
+ await render(
+ <template><Button @definition={{this.definition}} /></template>
+ );
+
+ assert.dom("#approve").hasText("Approve", "the button renders its label");
+ assert
+ .dom("#approve .d-icon-check")
+ .exists("the button renders its configured icon");
+ });
+
+ test("preserves the legacy button without a style or icon", async function (assert) {
+ this.definition = {
+ action_id: "approve",
+ text: { text: "Approve" },
+ };
+
+ await render(
+ <template><Button @definition={{this.definition}} /></template>
+ );
+
+ assert
+ .dom("#approve")
+ .hasClass("btn", "the base button class is present");
+ assert
+ .dom("#approve")
+ .doesNotHaveClass(/btn-.+/, "no button style class is added");
+ assert
+ .dom("#approve .d-icon")
+ .doesNotExist("no icon is rendered by default");
+ });
+ }
+);
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/icon-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/icon-control.gjs
index 0f929ffa9b4..5e0ad8cf186 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/icon-control.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/icon-control.gjs
@@ -1,3 +1,4 @@
+import { not } from "discourse/truth-helpers";
import ExpressionWrapper from "./expression-wrapper";
export default <template>
@@ -9,6 +10,6 @@ export default <template>
@dynamicValueHint={{@dynamicValueHint}}
@session={{@session}}
>
- <@field.Control />
+ <@field.Control @allowClear={{not @schema.required}} />
</ExpressionWrapper>
</template>
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/select-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/select-control.gjs
index 54e7cd13a95..5bf2df4f1a1 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/select-control.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/select-control.gjs
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";
+import { i18n } from "discourse-i18n";
import {
normalizeOptions,
propertyOptionLabel,
@@ -6,6 +7,18 @@ import {
import ExpressionWrapper from "./expression-wrapper";
export default class SelectControl extends Component {
+ get noneKey() {
+ return this.args.schema?.control_options?.none;
+ }
+
+ get includeNone() {
+ return Boolean(this.noneKey);
+ }
+
+ get nonePlaceholder() {
+ return this.noneKey ? i18n(this.noneKey) : undefined;
+ }
+
get options() {
return normalizeOptions(this.args.schema.options).map((option) => ({
...option,
@@ -26,7 +39,11 @@ export default class SelectControl extends Component {
@dynamicValueHint={{@dynamicValueHint}}
@session={{@session}}
>
- <@field.Control @includeNone={{false}} as |c|>
+ <@field.Control
+ @includeNone={{this.includeNone}}
+ @nonePlaceholder={{this.nonePlaceholder}}
+ as |c|
+ >
{{#each this.options as |choice|}}
<c.Option @value={{choice.value}}>{{choice.label}}</c.Option>
{{/each}}
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
index 9d4fa2f3670..f2e89f7f8ba 100644
--- a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
@@ -13,6 +13,7 @@ import Form from "discourse/components/form";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import selectKit from "discourse/tests/helpers/select-kit-helper";
+import { NO_VALUE_OPTION } from "discourse/ui-kit/d-select";
import I18n, { i18n } from "discourse-i18n";
import PropertyEngineConfigurator from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/property-engine";
import WorkflowEditorSession from "discourse/plugins/discourse-workflows/admin/lib/workflows/editor-session";
@@ -1023,6 +1024,137 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert.strictEqual(this.formApi.get("entries.values").length, 1);
});
+ test("edits optional fixed collection styles and icons without expressions", async function (assert) {
+ this.setProperties({
+ configuration: {
+ buttons: {
+ values: [{ label: "Approve", value: "approve" }],
+ },
+ },
+ formApi: null,
+ nodeType: "action:chat_approval",
+ schema: {
+ buttons: {
+ type: "fixed_collection",
+ options: [
+ {
+ name: "values",
+ values: {
+ label: {
+ type: "string",
+ required: true,
+ no_data_expression: true,
+ },
+ value: {
+ type: "string",
+ required: true,
+ no_data_expression: true,
+ },
+ style: {
+ type: "options",
+ required: false,
+ options: [
+ "default",
+ "primary",
+ "danger",
+ "success",
+ "flat",
+ "transparent",
+ ],
+ no_data_expression: true,
+ control_options: {
+ none: "none_placeholder",
+ },
+ },
+ icon: {
+ type: "icon",
+ required: false,
+ no_data_expression: true,
+ },
+ },
+ },
+ ],
+ type_options: {
+ multiple_values: true,
+ },
+ },
+ },
+ registerApi: (api) => {
+ this.set("formApi", api);
+ },
+ });
+
+ await render(
+ <template>
+ <Form
+ @data={{this.configuration}}
+ @onRegisterApi={{this.registerApi}}
+ as |form transientData|
+ >
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @formApi={{this.formApi}}
+ @configuration={{transientData}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const row = ".workflows-property-engine__collection-row";
+ const styleField = '[data-name="buttons.values.0.style"]';
+ const iconField = '[data-name="buttons.values.0.icon"]';
+
+ assert.dom(styleField).exists("the optional style is always shown");
+ assert.dom(iconField).exists("the optional icon is always shown");
+ assert
+ .dom(`${row} .workflows-property-engine__mode-control`)
+ .doesNotExist("the button fields do not offer expression controls");
+
+ await select(`${styleField} select`, "primary");
+
+ assert.strictEqual(
+ this.formApi.get("buttons.values.0.style"),
+ "primary",
+ "the nested style is selected"
+ );
+ assert
+ .dom(`${styleField} option[value="${NO_VALUE_OPTION}"]`)
+ .hasText(
+ i18n("none_placeholder"),
+ "the opt-in no-value option remains available after selection"
+ );
+
+ await click(`${iconField} .d-icon-grid-picker-trigger`);
+ await waitFor("[data-icon-id='gear']");
+ await click("[data-icon-id='gear']");
+
+ assert.strictEqual(
+ this.formApi.get("buttons.values.0.icon"),
+ "gear",
+ "the nested icon is selected"
+ );
+ assert
+ .dom(`${iconField} .d-icon-grid-picker__clear`)
+ .exists("the optional icon can be cleared");
+
+ await select(`${styleField} select`, NO_VALUE_OPTION);
+ await click(`${iconField} .d-icon-grid-picker__clear`);
+
+ assert.strictEqual(
+ this.formApi.get("buttons.values.0.style"),
+ null,
+ "the nested style is cleared"
+ );
+ assert.strictEqual(
+ this.formApi.get("buttons.values.0.icon"),
+ null,
+ "the nested icon is cleared"
+ );
+ });
+
test("reorders sortable fixed collection items", async function (assert) {
this.setProperties({
configuration: {