Skip to content

Add allow empty option for @selectize/selectize#2817

Merged
nickcharlton merged 1 commit intothoughtbot:mainfrom
goosys:add-allow-empty-option-2
Nov 27, 2025
Merged

Add allow empty option for @selectize/selectize#2817
nickcharlton merged 1 commit intothoughtbot:mainfrom
goosys:add-allow-empty-option-2

Conversation

@goosys
Copy link
Contributor

@goosys goosys commented Mar 22, 2025

Updating @selectize/selectize (v0.12 -> 0.15) and revisiting #2790

Updating Selectize improved the visibility of empty options without requiring custom CSS. However, due to compatibility issues between Selectize, Rails, and Stimulus/Turbo, this ended up being a larger change than expected.

Preventing form hangs after updating Selectize

Right after upgrading to @selectize/selectize, forms started hanging. To address this, I applied a workaround in the second commit:
hotwired/stimulus#104 (comment)

This issue is likely not limited to Selectize. It seems that every time we use a JS library, we’ll need to implement a teardown() function to avoid similar problems.

Switching from disconnect() to teardown() also prevents a regression of #2811.

Reintroducing allowEmptyOption

Adding allowEmptyOption again worked perfectly as expected.

I also tested the new showEmptyOptionInDropdown option, but it didn’t quite fit our use case, so I decided not to use it.

Preventing unintended removal when include_blank: false

I encountered an issue where pressing Backspace would remove the selection, even when include_blank: false was set.

Since Selectize doesn’t provide a built-in option for this, I added a (somewhat quirky) workaround in select_controller to prevent unintended deletions.

Additionally, since Selectize doesn’t properly recognize the required attribute on select_tag, I had to manually add the constraint via data attributes.

Workaround for placeholder rendering issue

After resolving most of the issues, I ran into one last problem:

When updating a form with include_blank and navigating back via Turbo, the placeholder would display only its first character. A full page reload would render it correctly.
selectize/selectize.js#1498

I couldn't find a proper fix for this, so we’ll have to wait for an update from Selectize.

As a workaround, I set the placeholder to --- instead of a meaningful string. This way, even if only one character is displayed, it won’t look out of place. 😄

@radwo
Copy link

radwo commented Nov 21, 2025

Hey! Thanks for the great work on Administrate so far!
Now that we have 1.0 versions, what's the plan here to bring selectize up to date?

@goosys
Copy link
Contributor Author

goosys commented Nov 24, 2025

I’m thinking that, in the future, it might be nice to make this a plugin so users can choose whether they want to use Selectize or not—there are probably other good libraries out there too.

But I don’t think we can go that far right now, so for the moment I think it’s best to just stay up to date with the latest Selectize.

What do you think? @nickcharlton @pablobm

@nickcharlton
Copy link
Member

I think we should try and get this in as soon as we can. @goosys, could you rebase and update the branch to resolve the conflicts?

I’m thinking that, in the future, it might be nice to make this a plugin so users can choose whether they want to use Selectize or not—there are probably other good libraries out there too.

Yeah, I've thought the same. I'd quite like to replace it with something that's primarily web components based. Even if we wrote our own for it.

@goosys goosys force-pushed the add-allow-empty-option-2 branch 2 times, most recently from d061e8d to 4f9de88 Compare November 27, 2025 13:37
@goosys
Copy link
Contributor Author

goosys commented Nov 27, 2025

@nickcharlton nickcharlton temporarily deployed to administrate-add-allow--edofk4 November 27, 2025 14:49 Inactive
@nickcharlton
Copy link
Member

Thanks!

I can't access that link because it's invalid, but I'll spin up a review app and test it out.

This does several things in one go, because they can't be done
indepentantly:

1. Upgrades `@selectize/selectize` from v0.12 to v0.15,
2. Fixes thoughtbot#2545 (Selectize removes blank options from selects)
3. Closes thoughtbot#2790 (Add allow empty option)

Updating Selectize improved the visibility of empty options without
requiring custom CSS. However, due to compatibility issues between
Selectize, Rails, and Stimulus/Turbo, this ended up being a larger
change than expected.

Right after upgrading to @selectize/selectize, forms started hanging. To
address this, workaround is used. This issue is likely not limited to
Selectize.

It seems that every time we use a JS library, we’ll need to implement a
teardown() function to avoid similar problems. Switching from
disconnect() to teardown() also prevents a regression of thoughtbot#2811.

To prevent unintended removal when using `include_blank: false`, we also
introduce a workaround to prevent unintended deletions.

Finally, when updating a form with include_blank and navigating back via
Turbo, the placeholder would display only its first character. A full
page reload would render it correctly. To workaround this problem, we
use a placeholder of `---` instead of a meaningful string so that it
doesn't look wrong.

thoughtbot#2545
thoughtbot#2790
hotwired/stimulus#104 (comment)
thoughtbot#2811
selectize/selectize.js#1498

Updated selectize from 0.12 to @selectize/selectize 0.15

Used teardown instead of disconnect to address reinitialization issues

Refactor select for @selectize/selectize
@nickcharlton nickcharlton force-pushed the add-allow-empty-option-2 branch from 4f9de88 to 4411f61 Compare November 27, 2025 15:12
@nickcharlton nickcharlton merged commit 4999bef into thoughtbot:main Nov 27, 2025
25 checks passed
@radwo
Copy link

radwo commented Nov 29, 2025

Thanks!

@radwo
Copy link

radwo commented Jan 16, 2026

Preventing unintended removal when include_blank: false

I encountered an issue where pressing Backspace would remove the selection, even when include_blank: false was set.

Since Selectize doesn’t provide a built-in option for this, I added a (somewhat quirky) workaround in select_controller to prevent unintended deletions.

Additionally, since Selectize doesn’t properly recognize the required attribute on select_tag, I had to manually add the constraint via data attributes.

This change brake the ability to type and filter the select list when include_blank is set to false.

@nickcharlton
Copy link
Member

@radwo, if that's a new problem since this, can you open a new issue and mention this one?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Selectize removes the blank options from selects

3 participants