Sha256: 9ed7200e56ef68374834830a5d3d004f31161efcd86e4f27f1c22f808beda0d6

Contents?: true

Size: 1.03 KB

Versions: 30

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Ariadne
  # 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)
      @attributes[:tag] == :nav ? @attributes[:"aria-label"] = label : @body_arguments[:"aria-label"] = label
    end

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

      render(Ariadne::TabContainerComponent.new(**attributes)) 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

30 entries across 30 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.12 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-x86_64-linux app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-x86_64-darwin app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-x64-mingw32 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-x64-mingw-ucrt app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-arm64-darwin app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.12-aarch64-linux app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-x86_64-linux app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-x86_64-darwin app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-x64-mingw32 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-x64-mingw-ucrt app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-arm64-darwin app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.11-aarch64-linux app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10-x86_64-linux app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10-x86_64-darwin app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10-x64-mingw32 app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10-x64-mingw-ucrt app/lib/ariadne/tabbed_component_helper.rb
ariadne_view_components-0.0.10-arm64-darwin app/lib/ariadne/tabbed_component_helper.rb