Sha256: 126f7436cd812e466bf84d252b2b0a7b734b458d102aeeb17aa73703497cc9a3

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "securerandom"

class HotwireCombobox::Listbox::Option
  def initialize(option)
    @option = option.is_a?(Hash) ? Data.new(**option) : option
  end

  def render_in(view)
    view.tag.li content, **options
  end

  def value
    option.try(:value) || option.id
  end

  def autocompletable_as
    option.try(:autocompletable_as) || option.try(:display)
  end

  private
    Data = Struct.new :id, :value, :display, :content, :filterable_as, :autocompletable_as, keyword_init: true

    attr_reader :option

    def options
      {
        id: id,
        role: :option,
        class: "hw-combobox__option",
        data: data
      }
    end

    def id
      option.try(:id) || SecureRandom.uuid
    end

    def data
      {
        action: "click->hw-combobox#selectOption",
        filterable_as: filterable_as,
        autocompletable_as: autocompletable_as,
        value: value
      }
    end

    def content
      option.try(:content) || option.try(:display)
    end

    def filterable_as
      option.try(:filterable_as) || option.try(:display)
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hotwire_combobox-0.1.37 app/presenters/hotwire_combobox/listbox/option.rb