Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix RichTextArea label ids, form namespace support

Alexander Brandon Coles6a799e39cb99
diff --git a/app/forms/settings/multi_lang_form.rb b/app/forms/settings/multi_lang_form.rb
index eed7b6caf64..b13a92afc77 100644
--- a/app/forms/settings/multi_lang_form.rb
+++ b/app/forms/settings/multi_lang_form.rb
@@ -59,7 +59,6 @@ module Settings
disabled: setting_disabled?(name),
visually_hide_label: true,
rich_text_options: {
- text_area_id: "settings-#{name}",
turboMode: true,
showAttachments: false
}
diff --git a/lib/primer/open_project/forms/rich_text_area.html.erb b/lib/primer/open_project/forms/rich_text_area.html.erb
index 0379d716982..a25af691581 100644
--- a/lib/primer/open_project/forms/rich_text_area.html.erb
+++ b/lib/primer/open_project/forms/rich_text_area.html.erb
@@ -1,8 +1,6 @@
<%= render(FormControl.new(input: @input)) do %>
<%= content_tag(:div, hidden: true) do %>
- <%= builder.text_area(@input.name,
- id: @text_area_id,
- **@input.input_arguments) %>
+ <%= builder.text_area(@input.name, **@input.input_arguments) %>
<% end %>
<%= angular_component_tag "opce-ckeditor-augmented-textarea",
inputs: @rich_text_options.reverse_merge(
diff --git a/lib/primer/open_project/forms/rich_text_area.rb b/lib/primer/open_project/forms/rich_text_area.rb
index c0276720a62..a40d6dcd033 100644
--- a/lib/primer/open_project/forms/rich_text_area.rb
+++ b/lib/primer/open_project/forms/rich_text_area.rb
@@ -15,7 +15,7 @@ module Primer
@rich_text_data = rich_text_options.delete(:data) { {} }
@rich_text_data[:"test-selector"] ||= "augmented-text-area-#{@input.name}"
@rich_text_options = rich_text_options
- @text_area_id = rich_text_options.delete(:text_area_id) || builder.field_id(@input.name)
+ @text_area_id = @input.id || builder.field_id(@input.name)
end
end
end
diff --git a/spec/lib/primer/open_project/forms/rich_text_area_spec.rb b/spec/lib/primer/open_project/forms/rich_text_area_spec.rb
new file mode 100644
index 00000000000..95042379a95
--- /dev/null
+++ b/spec/lib/primer/open_project/forms/rich_text_area_spec.rb
@@ -0,0 +1,109 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+#
+require "spec_helper"
+
+RSpec.describe Primer::OpenProject::Forms::RichTextArea, type: :forms do
+ include ViewComponent::TestHelpers
+
+ let(:params) { { rich_text_options: {} } }
+ let(:model) { build_stubbed(:comment) }
+
+ def render_form
+ render_in_view_context(model, namespace, params) do |model, namespace, params|
+ primer_form_with(url: "/foo", model:, namespace:) do |f|
+ render_inline_form(f) do |test_form|
+ test_form.rich_text_area(name: :ultimate_answer, label: "Ultimate answer", **params)
+ end
+ end
+ end
+ end
+
+ subject(:rendered_form) do
+ render_form
+ page
+ end
+
+ shared_examples_for "successful render" do |text_area_id:|
+ it "renders the label" do
+ expect(rendered_form).to have_element :label, for: text_area_id
+ end
+
+ it "renders the hidden textarea" do
+ expect(rendered_form).to have_field text_area_id, type: "textarea", visible: :hidden
+ end
+
+ it "renders the rich text area" do
+ expect(rendered_form).to have_element "opce-ckeditor-augmented-textarea",
+ "data-text-area-id": text_area_id.to_json
+ end
+ end
+
+ context "without form namespace" do
+ let(:namespace) { nil }
+
+ context "with default field id" do
+ it_behaves_like "successful render", text_area_id: "comment_ultimate_answer"
+ end
+
+ context "with explicit field id" do
+ let(:params) { { id: "explicit_id", rich_text_options: {} } }
+
+ it_behaves_like "successful render", text_area_id: "explicit_id"
+ end
+ end
+
+ context "with form namespace" do
+ let(:namespace) { "super_form" }
+
+ context "with default field id" do
+ it_behaves_like "successful render", text_area_id: "super_form_comment_ultimate_answer"
+ end
+
+ context "with explicit field id" do
+ let(:params) { { id: "explicit_id", rich_text_options: {} } }
+
+ it "renders the label" do
+ expect(rendered_form).to have_element :label, for: "explicit_id"
+ end
+
+ it "renders the hidden textarea" do
+ pending "Primer Forms does not handle ids consistently when a form namespace is set."
+
+ expect(rendered_form).to have_field "explicit_id", type: "textarea", visible: :hidden
+ end
+
+ it "renders the rich text area" do
+ expect(rendered_form).to have_element "opce-ckeditor-augmented-textarea",
+ "data-text-area-id": "explicit_id".to_json
+ end
+ end
+ end
+end