Skip to content

Commit f1f780a

Browse files
committed
Refactor select for @selectize/selectize
1 parent b46b23c commit f1f780a

File tree

10 files changed

+233
-12
lines changed

10 files changed

+233
-12
lines changed

app/assets/builds/administrate/application.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21387,13 +21387,24 @@
2138721387

2138821388
// app/assets/javascripts/administrate/controllers/select_controller.js
2138921389
var import_jquery2 = __toESM(require_jquery());
21390+
var default_options = {
21391+
allowEmptyOption: true,
21392+
deselectBehavior: "previous"
21393+
};
2139021394
var select_controller_default = class extends Controller {
2139121395
connect() {
2139221396
if (!this.selectize) {
21393-
const options = this.selectizeOptions || {};
21397+
const options = this.selectizeOptions || default_options;
2139421398
const selectedValues = (0, import_jquery2.default)(this.element).val();
2139521399
this.selectize = (0, import_jquery2.default)(this.element).selectize(options)[0].selectize;
2139621400
this.selectize.setValue(selectedValues);
21401+
if (this.element.getAttribute("data-selectize-required") === "true") {
21402+
this.selectize.on("change", (value) => {
21403+
if (value.length === 0) {
21404+
this.selectize.setValue(selectedValues);
21405+
}
21406+
});
21407+
}
2139721408
}
2139821409
}
2139921410
teardown() {

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

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

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
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

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>

lib/administrate/field/belongs_to.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,45 @@ def selected_option
3434
data&.send(association_primary_key)
3535
end
3636

37+
def tag_options
38+
{include_blank: selectize_include_blank}
39+
end
40+
41+
def html_options
42+
{
43+
placeholder: selectize_placeholder,
44+
data: {
45+
controller: html_controller,
46+
**selectize_required_options
47+
}
48+
}
49+
end
50+
3751
def include_blank_option
3852
options.fetch(:include_blank, true)
3953
end
4054

55+
def selectize_include_blank
56+
if include_blank_option === true
57+
# I18n.t(:"helpers.select.prompt")
58+
"---" # Workaround for https://github.com/selectize/selectize.js/issues/1498
59+
elsif include_blank_option.is_a?(String)
60+
include_blank_option
61+
end
62+
end
63+
64+
def selectize_placeholder
65+
selectize_include_blank
66+
end
67+
68+
def selectize_required_options
69+
if include_blank_option === false
70+
{"selectize-required": true}
71+
else
72+
{}
73+
end
74+
end
75+
4176
private
4277

4378
def candidate_resources

lib/administrate/field/select.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,45 @@ def selectable_options
2424
values
2525
end
2626

27+
def tag_options
28+
{include_blank: selectize_include_blank}
29+
end
30+
31+
def html_options
32+
{
33+
placeholder: selectize_placeholder,
34+
data: {
35+
controller: html_controller,
36+
**selectize_required_options
37+
}
38+
}
39+
end
40+
2741
def include_blank_option
2842
options.fetch(:include_blank, false)
2943
end
3044

45+
def selectize_include_blank
46+
if include_blank_option === true
47+
# I18n.t(:"helpers.select.prompt")
48+
"---" # Workaround for https://github.com/selectize/selectize.js/issues/1498
49+
elsif include_blank_option.is_a?(String)
50+
include_blank_option
51+
end
52+
end
53+
54+
def selectize_placeholder
55+
selectize_include_blank
56+
end
57+
58+
def selectize_required_options
59+
if include_blank_option === false
60+
{"selectize-required": true}
61+
else
62+
{}
63+
end
64+
end
65+
3166
def active_record_enum?
3267
resource.class.defined_enums.key?(attribute.to_s)
3368
end

spec/administrate/views/fields/select/_edit_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
attribute: :email_subscriber,
1010
data: false,
1111
selectable_options: [true, false, nil],
12-
include_blank_option: false,
13-
html_controller: "select"
12+
tag_options: {include_blank: false},
13+
html_options: {data: {controller: "select"}}
1414
)
1515

1616
fields model: customer do |f|
@@ -33,8 +33,8 @@
3333
attribute: :email_subscriber,
3434
data: "Yes",
3535
selectable_options: ["Yes", "No"],
36-
include_blank_option: "Unknown",
37-
html_controller: "select"
36+
tag_options: {include_blank: "Unknown"},
37+
html_options: {data: {controller: "select"}}
3838
)
3939

4040
fields model: customer do |f|

spec/lib/fields/belongs_to_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,72 @@
171171
expect(candidates).to eq([])
172172
end
173173
end
174+
175+
context "when given an include_blank option is true" do
176+
it "returns include_blank and placeholder options with '---'" do
177+
customer = create(:customer, territory: nil)
178+
association = Administrate::Field::BelongsTo.with_options(
179+
include_blank: true
180+
)
181+
field = association.new(
182+
:territory,
183+
[],
184+
:edit,
185+
resource: customer
186+
)
187+
188+
tag_options = field.tag_options
189+
html_options = field.html_options
190+
191+
expect(tag_options[:include_blank]).to eq("---")
192+
expect(html_options[:placeholder]).to eq("---")
193+
expect(html_options.dig(:data, :"selectize-required")).to be_nil
194+
end
195+
end
196+
197+
context "when given an include_blank option is a string" do
198+
it "returns include_blank and placeholder options with the given string" do
199+
customer = create(:customer, territory: nil)
200+
association = Administrate::Field::BelongsTo.with_options(
201+
include_blank: "Select an option"
202+
)
203+
field = association.new(
204+
:territory,
205+
[],
206+
:edit,
207+
resource: customer
208+
)
209+
210+
tag_options = field.tag_options
211+
html_options = field.html_options
212+
213+
expect(tag_options[:include_blank]).to eq("Select an option")
214+
expect(html_options[:placeholder]).to eq("Select an option")
215+
expect(html_options.dig(:data, :"selectize-required")).to be_nil
216+
end
217+
end
218+
219+
context "when given an include_blank option is false" do
220+
it "returns include_blank and placeholder options with nil" do
221+
customer = create(:customer, territory: nil)
222+
association = Administrate::Field::BelongsTo.with_options(
223+
include_blank: false
224+
)
225+
field = association.new(
226+
:territory,
227+
[],
228+
:edit,
229+
resource: customer
230+
)
231+
232+
tag_options = field.tag_options
233+
html_options = field.html_options
234+
235+
expect(tag_options[:include_blank]).to eq(nil)
236+
expect(html_options[:placeholder]).to eq(nil)
237+
expect(html_options.dig(:data, :"selectize-required")).to eq(true)
238+
end
239+
end
174240
end
175241

176242
describe "primary_key option" do

spec/lib/fields/select_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,66 @@
140140
expect(field.selectable_options).to eq([])
141141
end
142142
end
143+
144+
describe ":include_blank option" do
145+
context "when given an include_blank option is true" do
146+
it "returns include_blank and placeholder options with '---'" do
147+
customer = create(:customer)
148+
field = described_class.new(
149+
:email_subscriber,
150+
"yes",
151+
:_page_,
152+
resource: customer,
153+
include_blank: true
154+
)
155+
156+
tag_options = field.tag_options
157+
html_options = field.html_options
158+
159+
expect(tag_options[:include_blank]).to eq("---")
160+
expect(html_options[:placeholder]).to eq("---")
161+
expect(html_options.dig(:data, :"selectize-required")).to be_nil
162+
end
163+
end
164+
165+
context "when given an include_blank option is a string" do
166+
it "returns include_blank and placeholder options with the given string" do
167+
customer = create(:customer)
168+
field = described_class.new(
169+
:email_subscriber,
170+
"yes",
171+
:_page_,
172+
resource: customer,
173+
include_blank: "Select an option"
174+
)
175+
176+
tag_options = field.tag_options
177+
html_options = field.html_options
178+
179+
expect(tag_options[:include_blank]).to eq("Select an option")
180+
expect(html_options[:placeholder]).to eq("Select an option")
181+
expect(html_options.dig(:data, :"selectize-required")).to be_nil
182+
end
183+
end
184+
185+
context "when given an include_blank option is false" do
186+
it "returns include_blank and placeholder options with nil" do
187+
customer = create(:customer)
188+
field = described_class.new(
189+
:email_subscriber,
190+
"yes",
191+
:_page_,
192+
resource: customer,
193+
include_blank: false
194+
)
195+
196+
tag_options = field.tag_options
197+
html_options = field.html_options
198+
199+
expect(tag_options[:include_blank]).to be_nil
200+
expect(html_options[:placeholder]).to be_nil
201+
expect(html_options.dig(:data, :"selectize-required")).to eq(true)
202+
end
203+
end
204+
end
143205
end

0 commit comments

Comments
 (0)