Class: Bootstrap5Helper::Tab

Inherits:
Component show all
Defined in:
lib/bootstrap5_helper/tab.rb,
lib/bootstrap5_helper/tab/content.rb

Overview

Builds a Tab component.

Defined Under Namespace

Classes: Content

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid

Constructor Details

#initialize(template, type_or_options = nil, opts = {}, &block) ⇒ Tab

Class constructor

Parameters:

  • template (ActionView)
  • type_or_options (Symbol|String|Hash) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)


15
16
17
18
19
20
21
22
23
# File 'lib/bootstrap5_helper/tab.rb', line 15

def initialize(template, type_or_options = nil, opts = {}, &block)
  super(template)
  @type, args = type_or_options(type_or_options, opts)

  @id      = args.fetch(:id,    uuid)
  @class   = args.fetch(:class, '')
  @data    = args.fetch(:data,  {})
  @content = block || proc { '' }
end

Instance Method Details

#content(opts = {}, &block) ⇒ Tab::Content

Builds the Content object for the Tab.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:



63
64
65
# File 'lib/bootstrap5_helper/tab.rb', line 63

def content(opts = {}, &block)
  Content.new(@template, opts, &block)
end

Builds a custom Nav component for the tabs.

Overloads:

  • #nav(tag, opts) ⇒ Nav

    Parameters:

    • tag (Symbol|String)
      • :nav, :ul

    • opts (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
    • :child (Hash)
      • data attributes for child, NOT wrapper

  • #nav(opts) ⇒ Nav

    Parameters:

    • opts (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
    • :child (Hash)
      • data attributes for child, NOT wrapper

Yields:

Returns:



45
46
47
48
49
50
51
52
53
# File 'lib/bootstrap5_helper/tab.rb', line 45

def nav(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})

  args[:class] = (args[:class] || '') << " nav-#{@type}"
  args[:data]  = (args[:data]  || {}).merge('bs-toggle' => 'tab')
  args[:child] = { data: { 'bs-toggle' => 'tab' } }

  Nav.new(@template, tag, args, &block)
end

#to_sObject

Note:

This has a weird interaction. Because this object doesn’t actually return any wrapping string or DOM element, we want to return nil, so that only the output buffer on the sub components are returned.

Note:

Was updated to return an empty string opposed to nil.

See Also:

  • Bootstrap5Helper::Tab.changelogchangelog.md


74
75
76
77
78
# File 'lib/bootstrap5_helper/tab.rb', line 74

def to_s
  @content.call(self)

  ''
end

#type_or_options(*args) ⇒ Object



80
81
82
# File 'lib/bootstrap5_helper/tab.rb', line 80

def type_or_options(*args)
  parse_arguments(*args, :tabs)
end