Class: Avo::TabGroupComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/avo/tab_group_component.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from BaseComponent

#component_name, #has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Instance Method Details

#active_tabObject



65
66
67
68
69
70
71
# File 'app/components/avo/tab_group_component.rb', line 65

def active_tab
  return if group.visible_items.blank?

  group.visible_items.find do |tab|
    tab.name.to_s == active_tab_name.to_s
  end
end

#active_tab_nameObject



49
50
51
# File 'app/components/avo/tab_group_component.rb', line 49

def active_tab_name
  CGI.unescape(params[group_param] || group.visible_items&.first&.name)
end

#after_initializeObject



13
14
15
# File 'app/components/avo/tab_group_component.rb', line 13

def after_initialize
  group.index = index
end

#args(tab) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/components/avo/tab_group_component.rb', line 73

def args(tab)
  {
    # Hide the turbo frames that aren't in the current tab
    # This way we can lazy load the un-selected tabs on the show view
    class: "block #{'hidden' unless tab.name == active_tab_name}",
    data: {
      # Add a marker to know if we already loaded a turbo frame
      loaded: tab.name == active_tab_name,
      tabs_target: :tabPanel,
      tab_id: tab.name,
    }
  }
end

#frame_args(tab) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/components/avo/tab_group_component.rb', line 21

def frame_args(tab)
  args = {
    target: :_top,
    class: "block"
  }

  if is_not_loaded?(tab)
    args[:loading] = :lazy
    args[:src] = helpers.resource_path(
      resource: @resource,
      record: @resource.record,
      keep_query_params: true,
      active_tab_name: tab.name,
      tab_turbo_frame: tab.turbo_frame_id(parent: @group)
    )
  end

  args
end

#is_not_loaded?(tab) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/components/avo/tab_group_component.rb', line 41

def is_not_loaded?(tab)
  params[:tab_turbo_frame] != tab.turbo_frame_id(parent: @group)
end

#render?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/components/avo/tab_group_component.rb', line 17

def render?
  tabs_have_content? && visible_tabs.present?
end

#tabsObject



53
54
55
56
57
# File 'app/components/avo/tab_group_component.rb', line 53

def tabs
  @group.visible_items.map do |tab|
    tab.hydrate(view: view)
  end
end

#tabs_have_content?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/components/avo/tab_group_component.rb', line 45

def tabs_have_content?
  visible_tabs.present?
end

#visible_tabsObject



59
60
61
62
63
# File 'app/components/avo/tab_group_component.rb', line 59

def visible_tabs
  tabs.select do |tab|
    tab.visible?
  end
end