codevise / activeadmin-searchable_select

Searchable select boxes for forms and filters
Other
96 stars 33 forks source link

Support for blank option #15

Open denisahearn opened 4 years ago

denisahearn commented 4 years ago

First off, thank you very much for creating activeadmin-searchable_select. We've been using ActiveAdmin in our Rails app for 5 years, and as the amount of data in our database has grown, we've struggled with all the problems that select controls with too many options in them brings. Integrating AJAX-based select2 controls into ActiveAdmin with your gem was easy, and vastly improves the UX within ActiveAdmin.

One thing we're missing from the old select controls in our ActiveRecord filters and forms is a blank option and the ability to clear the currently selected option. Does activeadmin-searchable_select support this? If not, would it be possible to add this feature?

Thanks, Denis

tf commented 4 years ago

Hi Denis,

happy to hear you like the gem. Support for a blank option is currently still missing. There is an open PR (#14) that enables Select2's allowClear option to solve a similar issue. But it goes a bit too far by always enabling it even when the underlying select does not include a blank option. I still think, allowClear is the correct way to handle blank options with Select2. I wrote down my suggestions in a comment on the PR, but it has since been abandoned.

I'm happy to accept a PR that does not change existing use cases and includes specs/docs for the new ones.

Best Tim

diei commented 8 months ago

Hi Denis,

you can pass the options to be able to clear the select to the select2 control by the input_html attribute:

form do |f|
  f.semantic_errors(*f.object.errors.attribute_names)
  f.inputs do
    f.input :article, as: :searchable_select, input_html: { data: { 'allow-clear': true, placeholder: '' } }
  end
  f.actions
end

Kind regards Dirk

denisahearn commented 8 months ago

Hi Dirk,

Wow, I had forgotten I even asked this question. :-) Thanks for responding.

Somewhere along the line we discovered the allow-clear option and have been using it successfully. Here's an example of how our implementation is using allow-clear:

# In the ActiveAdmin form
input :user, as: :searchable_select, ajax: { resource: User, params: { organization_id: organization.id } }, input_html: clearable_select_html('active_admin.prompts.choose_a_user_or_leave_blank')

# View helper method
def clearable_select_html(prompt_key)
  {
    data: {
      searchable_select: {
        placeholder: I18n.t(prompt_key),
        allowClear: true
      }
    }
  }
end