Skip to content

Conversation

@goosys
Copy link
Contributor

@goosys goosys commented Mar 17, 2025

Issue

Previously, Selectize was initialized every time the Stimulus controller reconnected, leading to multiple instances being created when navigating back in the browser. Additionally, since disconnect() was not implemented, Selectize instances were never properly cleaned up, which likely caused unexpected behavior and potential memory leaks.

image

Fix

  • Prevents Selectize from losing selected values and settings on reinitialization
  • Stores initial Selectize options (settings) and restores them on reconnection
  • Implements disconnect() to properly destroy Selectize and prevent multiple instances
  • Retains the selected values between disconnections and reconnections

This ensures that Selectize behaves correctly across page navigations and Stimulus lifecycle events.

@goosys goosys force-pushed the fix/stimulus-selectize-reinitialization branch from 1885acb to 5b426cb Compare March 17, 2025 16:56
Previously, Selectize was initialized every time the Stimulus controller
reconnected, leading to multiple instances being created when navigating
back in the browser. Additionally, since `disconnect()` was not
implemented, Selectize instances were never properly cleaned up, which
likely caused unexpected behavior and potential memory leaks.

This change:

* Prevents Selectize from losing selected values and settings on
  reinitialization
* Stores initial Selectize options (`settings`) and restores them on
  reconnection
* Implements `disconnect()` to properly destroy Selectize and prevent
  multiple instances
* Retains the selected values between disconnections and reconnections

This ensures that Selectize behaves correctly across page navigations
and Stimulus lifecycle events.
@nickcharlton nickcharlton force-pushed the fix/stimulus-selectize-reinitialization branch from 5b426cb to 24d08b0 Compare March 20, 2025 11:35
@nickcharlton nickcharlton merged commit d4ec8d7 into thoughtbot:main Mar 20, 2025
25 checks passed
@nickcharlton
Copy link
Member

Thank you!

nickcharlton pushed a commit to goosys/administrate that referenced this pull request Nov 27, 2025
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 pushed a commit that referenced this pull request Nov 27, 2025
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 #2545 (Selectize removes blank options from selects)
3. Closes #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 #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.

#2545
#2790
hotwired/stimulus#104 (comment)
#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
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.

2 participants