Sha256: c28802865966e5997bf76a086e30799324968fb9411ba30eb07e958d1b094c55

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Yattho
  # 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)
      if @system_arguments[:tag] == :nav
        @system_arguments[:'aria-label'] =
          label
      else
        @body_arguments[:'aria-label'] = label
      end
    end

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

      render Yattho::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

2 entries across 2 versions & 1 rubygems

Version Path
yattho_view_components-0.1.1 app/lib/yattho/tabbed_component_helper.rb
yattho_view_components-0.0.1 app/lib/yattho/tabbed_component_helper.rb