Class: Bootstrap4Helper::Tab

Inherits:
Component show all
Defined in:
lib/bootstrap4_helper/tab.rb,
lib/bootstrap4_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, #uuid

Constructor Details

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

Note:

The support types are: ‘:tabs` and `:pills`

Class constructor

Parameters:

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

Options Hash (opts):

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


17
18
19
20
21
22
23
24
25
# File 'lib/bootstrap4_helper/tab.rb', line 17

def initialize(template, opts = {}, &block)
  super(template)

  @type    = opts.fetch(:type,  :tabs)
  @id      = opts.fetch(:id,    uuid)
  @class   = opts.fetch(:class, '')
  @data    = opts.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:



54
55
56
# File 'lib/bootstrap4_helper/tab.rb', line 54

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

Builds a custom Nav component for the tabs.

Parameters:

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

Options Hash (opts):

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

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bootstrap4_helper/tab.rb', line 34

def nav(opts = {}, &block)
  opts[:class] = (opts[:class] || '') << " nav-#{@type}"
  opts[:data]  = (opts[:data]  || {}).merge(toggle: 'tab')
  opts[:child] = {
    data: {
      toggle: 'tab'
    }
  }

  Nav.new(@template, opts, &block)
end

#to_sObject

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. If we return the return value of the block, we will get the last element added to the input buffer as an unescaped string.



64
65
66
67
68
# File 'lib/bootstrap4_helper/tab.rb', line 64

def to_s
  @content.call(self)

  nil
end