Skip to content

Commit 97ced9d

Browse files
authored
Merge pull request #3824 from dbwinger/feature/select-editor-allow-clear
Add allow_clear support and required attribute to SelectEditor
2 parents d3e44f1 + 3170879 commit 97ced9d

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

app/components/alchemy/ingredients/select_editor.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@ def input_field
2020
options_for_select(select_values, value)
2121
end
2222
select_tag form_field_name, options_tags, {
23+
include_blank: allow_clear?,
2324
id: form_field_id,
2425
class: ["ingredient-editor-select"],
2526
is: "alchemy-select",
27+
required: presence_validation?,
2628
multiple: settings[:multiple],
27-
disabled: !editable?
29+
disabled: !editable?,
30+
data: allow_clear? ? {"allow-clear": true} : {}
2831
}
2932
end
3033
end
3134

3235
private
3336

3437
def select_values = settings[:select_values]
38+
39+
def allow_clear? = settings[:allow_clear]
3540
end
3641
end
3742
end

app/models/alchemy/ingredients/select.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module Ingredients
55
# A text value from a select box
66
#
77
class Select < Alchemy::Ingredient
8-
allow_settings %i[display_inline select_values]
9-
allow_settings %i[display_inline select_values multiple]
8+
allow_settings %i[allow_clear display_inline select_values multiple]
109

1110
serialize :value, coder: JSON
1211

spec/components/alchemy/ingredients/select_editor_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,53 @@
9898
is_expected.not_to have_css("select[multiple]")
9999
end
100100
end
101+
102+
context "with allow_clear enabled" do
103+
before do
104+
expect(ingredient).to receive(:settings).at_least(:once) do
105+
{
106+
allow_clear: true,
107+
select_values: ["handhelds", "fridges", "watches"]
108+
}
109+
end
110+
end
111+
112+
it "renders a select box with a blank option" do
113+
is_expected.to have_css("select option[value='']", visible: :all)
114+
end
115+
116+
it "renders a select box with data-allow-clear attribute" do
117+
is_expected.to have_css("select[data-allow-clear]")
118+
end
119+
end
120+
121+
context "without allow_clear" do
122+
before do
123+
expect(ingredient).to receive(:settings).at_least(:once) do
124+
{
125+
select_values: ["handhelds", "fridges", "watches"]
126+
}
127+
end
128+
end
129+
130+
it "renders a select box without a blank option" do
131+
is_expected.not_to have_css("select option[value='']", visible: :all)
132+
end
133+
134+
it "renders a select box without data-allow-clear attribute" do
135+
is_expected.not_to have_css("select[data-allow-clear]")
136+
end
137+
end
138+
139+
context "with presence validation" do
140+
before do
141+
allow(ingredient).to receive(:definition) do
142+
Alchemy::IngredientDefinition.new(role: "select", type: "Select", validate: [:presence], settings: {select_values: ["a", "b"]})
143+
end
144+
end
145+
146+
it "renders a select box with required attribute" do
147+
is_expected.to have_css("select[required]")
148+
end
149+
end
101150
end

0 commit comments

Comments
 (0)