Class: Primer::ConditionalWrapper

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/conditional_wrapper.rb

Overview

Conditionally renders a component 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.

Constant Summary

Constants inherited from Component

Primer::Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Methods included from AttributesHelper

#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Methods included from ExperimentalSlotHelpers

included

Methods included from ExperimentalRenderHelpers

included

Constructor Details

#initialize(condition:, component: Primer::BaseComponent, **base_component_arguments) ⇒ ConditionalWrapper

Returns a new instance of ConditionalWrapper.

Parameters:

  • condition (Boolean)

    Whether or not to wrap the content in a component.

  • component (Class) (defaults to: Primer::BaseComponent)

    The component class to use as a wrapper, defaults to ‘Primer::BaseComponent`

  • base_component_arguments (Hash)

    The arguments to pass to the component.



11
12
13
14
15
16
# File 'app/components/primer/conditional_wrapper.rb', line 11

def initialize(condition:, component: Primer::BaseComponent, **base_component_arguments)
  @condition = condition
  @component = component
  @base_component_arguments = base_component_arguments
  @trim = !!@base_component_arguments.delete(:trim)
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
# File 'app/components/primer/conditional_wrapper.rb', line 18

def call
  unless @condition
    return @trim ? trimmed_content : content
  end

  @component.new(trim: @trim, **@base_component_arguments).render_in(self) { content }
end