Sha256: d328c46aba104c345174e56d78729bc5759d68f57e91eedeef809547335547ee

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 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, :blank, :filterable_as, :autocompletable_as, keyword_init: true

    attr_reader :option

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

    def id
      @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

    def blank?
      option.try(:blank).present?
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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