Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for blank option #15

Open
denisahearn opened this issue Dec 7, 2019 · 3 comments
Open

Support for blank option #15

denisahearn opened this issue Dec 7, 2019 · 3 comments

Comments

@denisahearn
Copy link

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
Copy link
Member

tf commented Dec 9, 2019

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

@tf tf added the help wanted label Mar 25, 2020
@diei
Copy link

diei commented Jan 5, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants