Sha256: 8d058aef56005630499821b1a5a1b8e2c6fbe07a371ba3e1a6f137c2aae90eb8

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Yattho
  # Conditionally renders a `Yattho::BaseComponent` around the given content. If the given condition
  # is true, a `Yattho::BaseComponent` will render around the content. If the condition is false, only
  # the content is rendered.
  #
  # @example True conditional
  #   <%# condition is true, so content will be wrapped in a <span> tag
  #   <%= render Yattho::ConditionalWrapper.new(condition: true, tag: :span, class: "foobar")) do %>
  #     <%# also rendered %>
  #     <p class="bazboo">Some text</p>
  #   <% end %>
  #
  # @example False conditional
  #   <%# condition is false so no <span> tag will render around the content (i.e. the <p> tag)
  #   <%= render(Yattho::ConditionalWrapper.new(condition: false, tag: :span, class: "foobar")) do %>
  #     <%# this content will be rendered %>
  #     <p class="bazboo">Some text</p>
  #   <% end %>
  #
  # @param condition [Boolean] Whether or not to wrap the content in a `Yattho::BaseComponent`.
  # @param base_component_arguments [Hash] The arguments to pass to `Yattho::BaseComponent`.
  class ConditionalWrapper < Yattho::Component
    def initialize(condition:, **base_component_arguments)
      @condition = condition
      @base_component_arguments = base_component_arguments
    end

    def call
      return content unless @condition

      BaseComponent.new(**@base_component_arguments).render_in(self) { content }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yattho_view_components-0.1.1 app/components/yattho/conditional_wrapper.rb
yattho_view_components-0.0.1 app/components/yattho/conditional_wrapper.rb