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

Make ConditionalLayoutComponent render fallback

Alexander Brandon Colescdeadda82b85
diff --git a/app/components/op_primer/conditional_layout_component.rb b/app/components/op_primer/conditional_layout_component.rb
index b00613384e5..767d9d7b212 100644
--- a/app/components/op_primer/conditional_layout_component.rb
+++ b/app/components/op_primer/conditional_layout_component.rb
@@ -31,21 +31,34 @@
module OpPrimer
# Conditionally renders a `Primer::Alpha::Layout` around the given content. If
# the given condition is true, the component will render around the content.
- # If the condition is false, only the content is rendered.
+ # If the condition is false, the content is rendered in a fallback component.
class ConditionalLayoutComponent < Primer::Component # rubocop:disable OpenProject/AddPreviewForViewComponent
delegate :with_sidebar, to: :@layout
# @param condition [Boolean] Whether or not to wrap the content in a Layout component.
# @param layout_component_args [Hash] The arguments to pass to the Layout component.
- def initialize(condition:, **layout_component_args)
+ # @param fallback_component [Class] The component class to use as a fallback, defaults to `Primer::BaseComponent
+ # @param fallback_component_args [Hash] The arguments to pass to the fallback component.
+ # @param system_arguments [Hash] The arguments to pass to either Layout or fallback component.
+ def initialize(
+ condition:,
+ layout_component_args: {},
+ fallback_component: Primer::BaseComponent,
+ fallback_component_args: {},
+ **system_arguments
+ )
super
@condition = condition
- @layout = Primer::Alpha::Layout.new(**layout_component_args)
+ @fallback_component = fallback_component
+ @fallback_component_args = fallback_component_args
+ @system_arguments = system_arguments
+
+ @layout = Primer::Alpha::Layout.new(**@system_arguments, **layout_component_args)
end
def call
- return content unless @condition
+ return render_fallback unless @condition
render @layout
end
@@ -61,5 +74,11 @@ module OpPrimer
@layout.with_main { content }
end
+
+ def render_fallback
+ render @fallback_component.new(tag: :div, **@system_arguments, **@fallback_component_args) do
+ content
+ end
+ end
end
end
diff --git a/spec/components/op_primer/conditional_layout_component_spec.rb b/spec/components/op_primer/conditional_layout_component_spec.rb
index 5e3e91282cd..4acc926c465 100644
--- a/spec/components/op_primer/conditional_layout_component_spec.rb
+++ b/spec/components/op_primer/conditional_layout_component_spec.rb
@@ -39,7 +39,7 @@ RSpec.describe OpPrimer::ConditionalLayoutComponent, type: :component do
let(:main_content) { "content" }
subject(:rendered_component) do
- render_component(condition:) do |component|
+ render_component(condition:, fallback_component_args: { classes: "op-fallback" }) do |component|
component.with_sidebar do
sidebar_content
end
@@ -52,8 +52,8 @@ RSpec.describe OpPrimer::ConditionalLayoutComponent, type: :component do
let(:condition) { false }
let(:main_content) { "" }
- it "renders nothing" do
- expect(rendered_component.to_s).to be_blank
+ it "renders content in fallback" do
+ expect(rendered_component).to have_css ".op-fallback", text: ""
end
end
@@ -64,8 +64,8 @@ RSpec.describe OpPrimer::ConditionalLayoutComponent, type: :component do
expect(rendered_component).to have_no_css ".Layout"
end
- it "renders content" do
- expect(rendered_component).to have_text "content"
+ it "renders content in fallback" do
+ expect(rendered_component).to have_css ".op-fallback", text: "content"
end
it "does not render sidebar" do
@@ -80,7 +80,7 @@ RSpec.describe OpPrimer::ConditionalLayoutComponent, type: :component do
expect(rendered_component).to have_css ".Layout"
end
- it "renders content" do
+ it "renders content in main" do
expect(rendered_component).to have_css ".Layout-main", text: "content"
end