Sha256: 9cdab347748c78a7b7b4c6a6c5c7433ec6687e268e5b66a4dd0d7bf7e2dddf12

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Decidim
  # Helper that provides a single method to create datalist select.
  module DatalistSelectHelper
    # Public: Creates HTML for datalist select so that you can use separate ids and labels.
    #
    # items - Items in datalist, item should have id and name.
    # options - a Hash with options
    #           :id - id of wrapper
    #           :class - wrapper css classes
    #           :data - wrapper data attributes
    #           :label - label for input
    #           :name - name of input
    #           :autocomplete - enable or disable autocomplete provided by browser.
    #           :list - id of datalist
    # yield - additional html (e.g. hidden input)
    #
    # Returns a HTML String div containing following children: label, input and datalist elements.
    def datalist_select(items, options = {})
      default_options = {
        list: "datalist-list",
        autocomplete: "off"
      }
      options = default_options.merge(options)
      tag.div(id: options[:id], class: options[:class], data: options[:data]) do
        html = ""
        html += yield if block_given?
        html += tag.label(options[:label], for: options[:name])
        html += tag.input(type: "text", name: options[:name], list: options[:list], autocomplete: options[:autocomplete], placeholder: options[:placeholder])
        html += tag.datalist(id: options[:list]) do
          items.map do |item|
            tag.option(translated_attribute(item.name), data: { value: item.id })
          end.join.html_safe
        end
        html.html_safe
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/helpers/decidim/datalist_select_helper.rb
decidim-core-0.29.0 app/helpers/decidim/datalist_select_helper.rb
decidim-core-0.29.0.rc4 app/helpers/decidim/datalist_select_helper.rb
decidim-core-0.29.0.rc3 app/helpers/decidim/datalist_select_helper.rb
decidim-core-0.29.0.rc2 app/helpers/decidim/datalist_select_helper.rb
decidim-core-0.29.0.rc1 app/helpers/decidim/datalist_select_helper.rb