Sha256: b06482588904ca715038af2c5bca98bcf57902e14bbe435499ca2f7aedb59554

Contents?: true

Size: 827 Bytes

Versions: 2

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Primer
  # Helper to share tab validation logic between components.
  # The component will raise an error if there are 0 or 2+ selected tabs.
  module TabbedComponentHelper
    extend ActiveSupport::Concern

    class MultipleSelectedTabsError < StandardError; end

    def before_render
      validate_single_selected_tab unless Rails.env.production?
    end

    private

    def wrapper
      return yield unless @with_panel

      render Primer::TabContainerComponent.new do
        yield
      end
    end

    def validate_single_selected_tab
      raise MultipleSelectedTabsError, "only one tab can be selected" if selected_tabs_count > 1
    end

    def selected_tabs_count
      @selected_tabs_count ||= tabs.count(&:selected)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
primer_view_components-0.0.34 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.33 app/lib/primer/tabbed_component_helper.rb