Skip to content

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

FIX: Form Templates using the tag name instead of the formated version on the composer (#38345)

Juan David Martínez Cubillosed8a62e2729f
diff --git a/frontend/discourse/app/components/form-template-field/tag-chooser.gjs b/frontend/discourse/app/components/form-template-field/tag-chooser.gjs
index 8b9dcf82cf5..c5059d6940c 100644
--- a/frontend/discourse/app/components/form-template-field/tag-chooser.gjs
+++ b/frontend/discourse/app/components/form-template-field/tag-chooser.gjs
@@ -16,12 +16,18 @@ export default class TagChooserField extends Component {
@service dialog;
get formattedChoices() {
+ if (!this.args.choices) {
+ return [];
+ }
+
+ const tagChoices = this.args.attributes?.tag_choices || {};
+
return this.args.choices.map((tag) => ({
id: tag.id,
name: tag.name,
- display: this.args.attributes.tag_choices[tag.name]
- ? this.args.attributes.tag_choices[tag.name]
- : tag.name.replace(/-/g, " ").toUpperCase(),
+ display: tagChoices[tag.name]
+ ? tagChoices[tag.name]
+ : (tag.name || "").replace(/-/g, " ").toUpperCase(),
}));
}
@@ -89,15 +95,31 @@ export default class TagChooserField extends Component {
}
@action
- handleSelectedTagNames(event) {
- return Array.from(event.target.selectedOptions)
- .map((option) => option.value)
- .filter((name) => name !== "");
+ handleSelectedValues(event) {
+ const getFallbackValue = (optionValue) =>
+ optionValue.toLowerCase().replace(/\s+/g, "-");
+ let choiceMap = null;
+ const tagChoices = this.args.attributes?.tag_choices;
+
+ if (tagChoices) {
+ choiceMap = new Map(
+ Object.entries(tagChoices).map(([key, value]) => [value, key])
+ );
+ }
+
+ const selectedValues = Array.from(event.target.selectedOptions).map(
+ (option) => {
+ const mappedValue = choiceMap?.get(option.textContent.trim());
+ return mappedValue ?? getFallbackValue(option.value);
+ }
+ );
+
+ return selectedValues;
}
@action
handleInput(event) {
- const selectedTagNames = this.handleSelectedTagNames(event);
+ const selectedTagNames = this.handleSelectedValues(event);
const validTagNames = this.formattedChoices.map((choice) => choice.name);
const filteredTagNames = selectedTagNames.filter((name) =>
validTagNames.includes(name)
@@ -167,7 +189,7 @@ export default class TagChooserField extends Component {
{{/if}}
{{#each this.formattedChoices as |choice|}}
<option
- value={{choice.name}}
+ value={{choice.display}}
selected={{this.isSelected choice.id}}
>{{choice.display}}</option>
{{/each}}
diff --git a/spec/system/composer/form_template_tag_chooser_spec.rb b/spec/system/composer/form_template_tag_chooser_spec.rb
index 0bd69230567..31cb29183c0 100644
--- a/spec/system/composer/form_template_tag_chooser_spec.rb
+++ b/spec/system/composer/form_template_tag_chooser_spec.rb
@@ -43,7 +43,7 @@ describe "Composer Form Template", type: :system do
find(".form-template-field__multi-select[name='cat-breed']").select("PERSIAN")
preview = find(".d-editor-preview")
- expect(preview).to have_content("persian")
+ expect(preview).to have_content("PERSIAN")
expect(preview).to have_no_content(tag1.id.to_s)
end
end
diff --git a/spec/system/page_objects/pages/form_template.rb b/spec/system/page_objects/pages/form_template.rb
index 248f2b7eb5c..9b34aaf245b 100644
--- a/spec/system/page_objects/pages/form_template.rb
+++ b/spec/system/page_objects/pages/form_template.rb
@@ -84,7 +84,8 @@ module PageObjects
end
def has_tag_chooser_tag?(tag)
- find("option[value='#{tag.name}']", visible: :all).present?
+ display_value = tag.name.tr("-", " ").upcase
+ find("option[value='#{display_value}']", visible: :all).present?
end
def has_preview_modal?