Skip to content

Commit 4999bef

Browse files
authored
Add allow empty option for @selectize/selectize (#2817)
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
1 parent a517c32 commit 4999bef

File tree

17 files changed

+1679
-1449
lines changed

17 files changed

+1679
-1449
lines changed

app/assets/builds/administrate/application.css

Lines changed: 596 additions & 202 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/builds/administrate/application.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/builds/administrate/application.js

Lines changed: 825 additions & 1139 deletions
Large diffs are not rendered by default.

app/assets/builds/administrate/application.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/javascripts/administrate/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import "trix";
33
import "@rails/actiontext";
44

55
import "@hotwired/turbo-rails";
6-
import "selectize/dist/js/selectize.min.js";
6+
import "@selectize/selectize/dist/js/selectize.min.js";
77

88
import "./controllers";
99
import "./vendor/css-anchor-positioning";

app/assets/javascripts/administrate/controllers/application.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ const application = Application.start();
66
application.debug = false;
77
window.Stimulus = application;
88

9+
// Workaround for Stimulus controllers not being properly disconnected
10+
// https://github.com/hotwired/stimulus/issues/104#issuecomment-365393601
11+
document.addEventListener('turbo:before-cache', function() {
12+
application.controllers.forEach(function(controller){
13+
if(typeof controller.teardown === 'function') {
14+
controller.teardown();
15+
}
16+
});
17+
});
18+
919
export { application };

app/assets/javascripts/administrate/controllers/select_controller.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
import { Controller } from "@hotwired/stimulus";
22
import $ from "jquery";
33

4+
const default_options = {
5+
allowEmptyOption: true,
6+
deselectBehavior: 'previous'
7+
}
8+
49
export default class extends Controller {
510
connect() {
611
if (!this.selectize) {
7-
const options = this.selectizeOptions || {};
12+
const options = this.selectizeOptions || default_options;
813
const selectedValues = $(this.element).val();
914
this.selectize = $(this.element).selectize(options)[0].selectize;
1015
this.selectize.setValue(selectedValues);
16+
if (this.element.getAttribute('data-selectize-required') === 'true') {
17+
this.selectize.on('change', (value) => {
18+
if (value.length === 0) {
19+
this.selectize.setValue(selectedValues);
20+
}
21+
});
22+
}
1123
}
1224
}
1325

14-
disconnect() {
26+
teardown() {
1527
if (this.selectize) {
1628
const selectedValues = this.selectize.getValue();
1729
if (!this.selectizeOptions) {

app/assets/stylesheets/administrate/application.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@import "reset/normalize";
44

55
@import "trix/dist/trix";
6-
@import "selectize/dist/css/selectize";
7-
@import "selectize/dist/css/selectize.default";
6+
@import "@selectize/selectize/dist/css/selectize";
7+
@import "@selectize/selectize/dist/css/selectize.default";
88

99
@import "library/clearfix";
1010
@import "library/data-label";

app/views/fields/belongs_to/_form.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ that displays all possible records to associate with.
2222
<div class="field-unit__field">
2323
<%= f.select(field.permitted_attribute,
2424
options_for_select(field.associated_resource_options, field.selected_option),
25-
{include_blank: field.include_blank_option},
26-
data: {controller: field.html_controller}) %>
25+
field.tag_options,
26+
field.html_options) %>
2727
</div>

app/views/fields/select/_form.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ to be displayed on a resource's edit form page.
2626
field.selectable_options,
2727
field.data,
2828
),
29-
{include_blank: field.include_blank_option},
30-
data: {controller: field.html_controller}
29+
field.tag_options,
30+
field.html_options
3131
)
3232
%>
3333
</div>

0 commit comments

Comments
 (0)