summaryrefslogtreecommitdiff
path: root/app/helpers/dropdowns_helper.rb
blob: a5728968ae10b389c11119255b0c592115ce9c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module DropdownsHelper
  def dropdown_tag(toggle_text, id: nil, toggle_class: nil, title: false, filter: false, placeholder: "", &block)
    content_tag :div, class: "dropdown" do
      dropdown_output = ""
      dropdown_output += content_tag :button, class: "dropdown-menu-toggle #{toggle_class}", id: id, type: "button", data: {toggle: "dropdown"} do
        output = toggle_text
        output << icon('chevron-down')
        output.html_safe
      end

      dropdown_output += content_tag :div, class: "dropdown-menu dropdown-select dropdown-menu-selectable" do
        output = ""

        if title
          output += content_tag :div, class: "dropdown-title" do
            title_output = content_tag(:span, title)

            title_output += content_tag :button, class: "dropdown-title-button dropdown-menu-close", aria: {label: "close"} do
              icon('times')
            end.html_safe
          end
        end

        if filter
          output += content_tag :div, class: "dropdown-input" do
            filter_output = search_field_tag nil, nil, class: "dropdown-input-field", placeholder: placeholder
            filter_output += icon('search')

            filter_output.html_safe
          end
        end

        output += content_tag :div, class: "dropdown-content" do
          capture(&block) if block
        end

        output += content_tag :div, class: "dropdown-loading" do
          icon('spinner spin')
        end

        output.html_safe
      end

      dropdown_output.html_safe
    end
  end
end