josefarias / hotwire_combobox

An accessible autocomplete for Ruby on Rails.
https://hotwirecombobox.com
MIT License
436 stars 26 forks source link

`include_blank: true` renders `true` into the combobox #125

Closed searls closed 4 months ago

searls commented 4 months ago

Describe the bug

The include_blank: option wants a hash or text to fill the "blank" option, but an actually blank string option won't be present? and thus fail to register. And setting it to true (as one would for a select box) renders the literal true

To Reproduce

<!-- Renders "true" -->
<%= f.combobox :thing_id, Thing.all, include_blank: true %>

<!-- No blank, because not present? -->
<%= f.combobox :thing_id, Thing.all, include_blank: "" %>

<!-- Renders an invisible non-breaking space character successfully -->
<%= f.combobox :thing_id, Thing.all, include_blank: "&nbsp;".html_safe %>

Screenshots

screenshot-2024-03-29-08h10m22s screenshot-2024-03-29-08h10m31s

Additional context

My advice would be to add a final else to this that just renders a string. Something like this maybe?

    def extract_blank_display_and_content
      if include_blank.is_a? Hash
        text = include_blank.delete(:text)

        [ text, render_content(render_opts: include_blank, object: text, attrs: { display: text, value: "" }) ]
      elsif include_blank.is_a?(String)
        [ include_blank, include_blank ]
      else
        [ "", nil ]
      end
    end
josefarias commented 4 months ago

Thanks Justin! Fixed in https://github.com/josefarias/hotwire_combobox/pull/130