Sha256: ed350409c7773e966a5bfc13e3d7ffdbd0770919c3fc5644596a7ee48ceedcdd

Contents?: true

Size: 1.05 KB

Versions: 62

Compression:

Stored size: 1.05 KB

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 aria_label_for_page_nav(label)
      @system_arguments[:tag] == :nav ? @system_arguments[:"aria-label"] = label : @body_arguments[:"aria-label"] = label
    end

    def tab_container_wrapper(with_panel:, **system_arguments)
      return yield unless with_panel

      render Primer::TabContainerComponent.new(**system_arguments) do
        yield if block_given?
      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

62 entries across 62 versions & 1 rubygems

Version Path
primer_view_components-0.0.116 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.115 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.114 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.113 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.112 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.111 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.110 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.109 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.108 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.107 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.106 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.105 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.104 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.103 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.102 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.101 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.100 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.99 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.98 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.0.97 app/lib/primer/tabbed_component_helper.rb