Sha256: b7bb59a01f7b797ade1286308df16ab92157e5d48637dfa67e41846629e766b6

Contents?: true

Size: 1.05 KB

Versions: 145

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::Alpha::TabContainer.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

145 entries across 145 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.15.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.14.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.13.1 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.13.2 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.13.1 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.13.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.13.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.12.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.11.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.12.1 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.10.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.11.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.10.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.9.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.9.1 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.9.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.8.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.8.0 app/lib/primer/tabbed_component_helper.rb
primer_view_components-0.7.0 app/lib/primer/tabbed_component_helper.rb
openproject-primer_view_components-0.7.0 app/lib/primer/tabbed_component_helper.rb